Skip to content

Commit 9304fa3

Browse files
liamqmaJakeLane
andauthored
Feature/handle extracted compiled files (#1513)
* Extract styles to file in babel plugin * Add changeset * Add test for extracting to css files * WIP * Remove parcel-transformer-css reference * Fix type errors * Add .synk * Update .synk * Update .synk * Rename .synk to snyk (silly me) * Update webpack plugin * Update Parcel integration test * Remove duplicates * Update snapshot test * mute ts error in the fixture * Specify file name * Refactor tests * Add .css to prettierignore * remove .only * PR feedbacks * Add extractStylesToDirectory option to Webpack and Parcel * escape chars * Use AST contruction instead of template.ast --------- Co-authored-by: Jake Lane <jlane2@atlassian.com>
1 parent a49e950 commit 9304fa3

32 files changed

Lines changed: 469 additions & 179 deletions

File tree

.changeset/fuzzy-walls-fly.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@compiled/babel-plugin-strip-runtime': minor
3+
'@compiled/parcel-transformer': patch
4+
'@compiled/webpack-loader': patch
5+
---
6+
7+
Add extractStylesToDirectory config to support extraction to CSS files

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ CHANGELOG.md
22
coverage
33
dist
44
storybook-static
5+
**/__fixtures__/*.css

examples/parcel/.compiledcssrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"importReact": false,
2+
"importReact": true,
33
"extensions": [".js", ".jsx", ".ts", ".tsx", ".customjsx"],
44
"parserBabelPlugins": ["typescript", "jsx"],
55
"transformerBabelPlugins": [

examples/parcel/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
"private": true,
55
"scripts": {
66
"build": "parcel build ./src/index.html",
7-
"start": "parcel serve ./src/index.html --open --no-cache"
7+
"start": "parcel serve ./src/index.html --open --no-cache",
8+
"start:debug": "node --inspect-brk node_modules/.bin/parcel serve ./src/index.html --open --no-cache"
89
},
910
"dependencies": {
11+
"@compiled/babel-component-extracted-fixture": "*",
1012
"@compiled/react": "*",
1113
"react": "17.0.2",
1214
"react-dom": "17.0.2"
1315
},
1416
"devDependencies": {
17+
"@babel/plugin-proposal-decorators": "^7.22.15",
1518
"@compiled/parcel-config": "*",
1619
"@compiled/parcel-transformer": "*",
1720
"@parcel/config-default": "^2.8.3",

examples/parcel/src/app.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import * as React from 'react';
2-
import '@compiled/react';
1+
import BabelComponentExtracted from '@compiled/babel-component-extracted-fixture/dist/index';
2+
import { css } from '@compiled/react';
3+
import React, { lazy } from 'react';
34
// These are unused placeholder examples, as including them will break the application. The static evaluation in the
45
// @compiled/babel-plugin must be synchronous, whereas parcel offers promise-based APIs, making them incompatible.
56
// Eventually, the static evaluation (i.e. resolveBindingNode) should be replaced or removed so that these aliases
@@ -16,14 +17,14 @@ import {
1617
} from './ui/custom-file-extension.customjsx';
1718
import { TypeScript } from './ui/typescript';
1819

19-
const AsyncComponent = React.lazy(() => import('./async'));
20-
20+
const AsyncComponent = lazy(() => import('./async'));
2121
export const App = () => (
2222
<>
23-
<div css={{ fontSize: 50, color: primary }}>hello from parcel 2</div>
23+
<div css={css({ fontSize: 50, color: primary })}>hello from parcel 2</div>
2424
<TypeScript color="blue" />
2525
{/*<div css={parcelAliasStyles}>custom alias</div>*/}
2626
{/*<div css={parcelResolverAliasStyles}>custom resolver</div>*/}
27+
<BabelComponentExtracted>Component from NPM</BabelComponentExtracted>
2728
<CustomFileExtensionStyled>Custom File Extension Styled</CustomFileExtensionStyled>
2829
<div css={customFileExtensionCss}>Custom File Extension CSS</div>
2930
<Annotated />

examples/webpack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"start": "webpack serve --hot"
88
},
99
"dependencies": {
10+
"@compiled/babel-component-extracted-fixture": "*",
1011
"@compiled/babel-component-fixture": "*",
1112
"@compiled/react": "*",
1213
"@compiled/webpack-loader": "*",

examples/webpack/src/app.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import BabelComponentExtracted from '@compiled/babel-component-extracted-fixture/dist/index';
12
import BabelComponent from '@compiled/babel-component-fixture';
2-
import '@compiled/react';
3+
import { css } from '@compiled/react';
34
import { Suspense, lazy } from 'react';
45

56
import { primary } from './common/constants';
@@ -15,12 +16,13 @@ const AsyncComponent = lazy(() => import('./ui/async'));
1516

1617
export const App = () => (
1718
<>
18-
<div css={{ fontSize: 50, color: primary }}>hello from webpack</div>
19+
<div css={css({ fontSize: 50, color: primary })}>hello from webpack</div>
1920
<TypeScript>TypeScript component</TypeScript>
2021
<BabelComponent>Component from NPM</BabelComponent>
2122
<Suspense fallback="Loading...">
2223
<AsyncComponent>I was loaded async</AsyncComponent>
2324
</Suspense>
25+
<BabelComponentExtracted>Component from NPM</BabelComponentExtracted>
2426
<CustomFileExtensionStyled>Custom File Extension Styled</CustomFileExtensionStyled>
2527
<div css={customFileExtensionCss}>Custom File Extension CSS</div>
2628
<Annotated />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
["@babel/preset-env", { "targets": { "browsers": "last 1 version" } }],
4+
["@babel/preset-react", { "runtime": "automatic" }]
5+
],
6+
"plugins": [
7+
["@compiled/babel-plugin", { "importReact": false, "optimizeCss": false }],
8+
[
9+
"@compiled/babel-plugin-strip-runtime",
10+
{ "extractStylesToDirectory": { "source": "src", "dest": "dist" } }
11+
]
12+
]
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@compiled/babel-component-extracted-fixture",
3+
"version": "0.1.0",
4+
"private": true,
5+
"main": "./dist/index.js",
6+
"scripts": {
7+
"build": "TEST_PKG_VERSION='0.0.0' babel ./src --out-dir=./dist"
8+
},
9+
"dependencies": {
10+
"@compiled/react": "*"
11+
},
12+
"devDependencies": {
13+
"@babel/cli": "^7.21.5",
14+
"@babel/core": "^7.21.8",
15+
"@babel/preset-env": "^7.21.5",
16+
"@babel/preset-react": "^7.18.6",
17+
"@compiled/babel-plugin": "*",
18+
"@compiled/babel-plugin-strip-runtime": "*"
19+
},
20+
"peerDependencies": {
21+
"react": "^17.0.1"
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { styled, css } from '@compiled/react';
2+
3+
const Button = styled.button({
4+
color: 'blue',
5+
fontSize: '30px',
6+
border: '2px solid blue',
7+
padding: '32px',
8+
backgroundColor: 'yellow',
9+
});
10+
11+
export default function BabelComponent({ children }) {
12+
return (
13+
<div css={css({ marginTop: 30 })}>
14+
<Button>{children}</Button>
15+
</div>
16+
);
17+
}

0 commit comments

Comments
 (0)