@compiled/parcel-transformer 0.9.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +30 -10
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +12 -1
- package/package.json +7 -5
- package/src/__tests__/transformer.parceltest.ts +89 -46
- package/src/index.ts +38 -8
- package/src/types.ts +14 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Transformer } from '@parcel/plugin';
|
|
2
2
|
import type { ParcelTransformerOpts } from './types';
|
|
3
|
-
declare const _default: Transformer<ParcelTransformerOpts>;
|
|
4
3
|
/**
|
|
5
4
|
* Compiled parcel transformer.
|
|
6
5
|
*/
|
|
6
|
+
declare const _default: Transformer<ParcelTransformerOpts>;
|
|
7
7
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -3,12 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
6
7
|
const path_1 = require("path");
|
|
7
8
|
const core_1 = require("@babel/core");
|
|
8
9
|
const generator_1 = __importDefault(require("@babel/generator"));
|
|
9
10
|
const utils_1 = require("@compiled/utils");
|
|
10
11
|
const plugin_1 = require("@parcel/plugin");
|
|
11
12
|
const source_map_1 = __importDefault(require("@parcel/source-map"));
|
|
13
|
+
const enhanced_resolve_1 = require("enhanced-resolve");
|
|
12
14
|
const configFiles = [
|
|
13
15
|
'.compiledcssrc',
|
|
14
16
|
'.compiledcssrc.json',
|
|
@@ -38,9 +40,11 @@ exports.default = new plugin_1.Transformer({
|
|
|
38
40
|
// Compiled should run before any other JS transformer.
|
|
39
41
|
return false;
|
|
40
42
|
},
|
|
41
|
-
async parse({ asset, config }) {
|
|
43
|
+
async parse({ asset, config, options }) {
|
|
42
44
|
var _a, _b;
|
|
43
|
-
|
|
45
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
46
|
+
const extract = config.extract && options.mode !== 'development';
|
|
47
|
+
if (!asset.isSource && !extract) {
|
|
44
48
|
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
45
49
|
return undefined;
|
|
46
50
|
}
|
|
@@ -65,16 +69,23 @@ exports.default = new plugin_1.Transformer({
|
|
|
65
69
|
program,
|
|
66
70
|
};
|
|
67
71
|
},
|
|
68
|
-
async transform({ asset, config }) {
|
|
69
|
-
var _a, _b;
|
|
72
|
+
async transform({ asset, config, options }) {
|
|
73
|
+
var _a, _b, _c;
|
|
70
74
|
const ast = await asset.getAST();
|
|
71
75
|
if (!ast) {
|
|
72
76
|
// We will only receive ASTs for assets we're interested in.
|
|
73
77
|
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
74
78
|
return [asset];
|
|
75
79
|
}
|
|
80
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
81
|
+
const extract = config.extract && options.mode !== 'development';
|
|
76
82
|
const includedFiles = [];
|
|
77
83
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
84
|
+
const resolver = enhanced_resolve_1.ResolverFactory.createResolver(Object.assign(Object.assign(Object.assign({ fileSystem: new enhanced_resolve_1.CachedInputFileSystem(fs_1.default, 4000) }, (config.extensions && {
|
|
85
|
+
extensions: config.extensions,
|
|
86
|
+
})), ((_a = config.resolve) !== null && _a !== void 0 ? _a : {})), {
|
|
87
|
+
// This makes the resolver invoke the callback synchronously
|
|
88
|
+
useSyncFileSystemCalls: true }));
|
|
78
89
|
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
79
90
|
code: true,
|
|
80
91
|
ast: false,
|
|
@@ -83,19 +94,23 @@ exports.default = new plugin_1.Transformer({
|
|
|
83
94
|
configFile: false,
|
|
84
95
|
sourceMaps: true,
|
|
85
96
|
parserOpts: {
|
|
86
|
-
plugins: (
|
|
97
|
+
plugins: (_b = config.parserBabelPlugins) !== null && _b !== void 0 ? _b : undefined,
|
|
87
98
|
},
|
|
88
99
|
plugins: [
|
|
89
|
-
...((
|
|
100
|
+
...((_c = config.transformerBabelPlugins) !== null && _c !== void 0 ? _c : []),
|
|
90
101
|
asset.isSource && [
|
|
91
102
|
'@compiled/babel-plugin',
|
|
92
|
-
Object.assign(Object.assign({}, config), { onIncludedFiles: (files) => includedFiles.push(...files),
|
|
103
|
+
Object.assign(Object.assign({}, config), { onIncludedFiles: (files) => includedFiles.push(...files), resolver: {
|
|
104
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
105
|
+
resolveSync: (context, request) => {
|
|
106
|
+
return resolver.resolveSync({}, (0, path_1.dirname)(context), request);
|
|
107
|
+
},
|
|
108
|
+
}, cache: false }),
|
|
93
109
|
],
|
|
94
|
-
|
|
110
|
+
extract && [
|
|
95
111
|
'@compiled/babel-plugin-strip-runtime',
|
|
96
112
|
{
|
|
97
|
-
|
|
98
|
-
compiledRequireExclude: config.ssr,
|
|
113
|
+
compiledRequireExclude: true,
|
|
99
114
|
},
|
|
100
115
|
],
|
|
101
116
|
].filter(utils_1.toBoolean),
|
|
@@ -111,6 +126,11 @@ exports.default = new plugin_1.Transformer({
|
|
|
111
126
|
asset.invalidateOnFileChange(file);
|
|
112
127
|
});
|
|
113
128
|
asset.setCode(output);
|
|
129
|
+
if (extract) {
|
|
130
|
+
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
131
|
+
const metadata = result === null || result === void 0 ? void 0 : result.metadata;
|
|
132
|
+
asset.meta.styleRules = metadata.styleRules;
|
|
133
|
+
}
|
|
114
134
|
return [asset];
|
|
115
135
|
},
|
|
116
136
|
async generate({ asset, ast }) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,+
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,4CAAoB;AACpB,+BAAqC;AAErC,sCAAgE;AAChE,iEAAwC;AAMxC,2CAA4C;AAC5C,2CAA6C;AAC7C,oEAA2C;AAC3C,uDAA0E;AAI1E,MAAM,WAAW,GAAG;IAClB,gBAAgB;IAChB,qBAAqB;IACrB,gBAAgB;IAChB,uBAAuB;CACxB,CAAC;AAEF;;GAEG;AACH,kBAAe,IAAI,oBAAW,CAAwB;IACpD,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE;QAClC,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,IAAA,WAAI,EAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE;YACvF,UAAU,EAAE,8BAA8B;SAC3C,CAAC,CAAC;QAEH,MAAM,QAAQ,GAAG;YACf,OAAO,EAAE,KAAK;YACd,WAAW,EAAE,IAAI;YACjB,GAAG,EAAE,KAAK;SACX,CAAC;QAEF,IAAI,IAAI,EAAE;YACR,MAAM,CAAC,mBAAmB,EAAE,CAAC;YAC7B,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;SACxC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,WAAW;QACT,uDAAuD;QACvD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE;;QACpC,sGAAsG;QACtG,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,aAAa,CAAC;QACjE,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,OAAO,EAAE;YAC/B,2GAA2G;YAC3G,OAAO,SAAS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AAN,MAAM,uBAAN,MAAM,CAAE,IAAI,KAAI,EAAE,CAAC;QAElC,aAAa,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC7B,iFAAiF;YACjF,uFAAuF;YACvF,SAAS;YACT,KAAK,CAAC,sBAAsB,CAAC,IAAI,CAAC,CAAC;QACrC,CAAC,CAAC,CAAC;QAEH,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAEtB,IAAI,OAAO,EAAE;YACX,0EAA0E;YAC1E,MAAM,QAAQ,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,QAA6B,CAAC;YACvD,KAAK,CAAC,IAAI,CAAC,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAC;SAC7C;QAED,OAAO,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE;QAC3B,oFAAoF;QACpF,4EAA4E;QAC5E,uCAAuC;QACvC,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,IAAA,mBAAQ,EAAC,GAAG,CAAC,OAAO,EAAE;YACpD,QAAQ,EAAE,KAAK,CAAC,QAAQ;YACxB,UAAU,EAAE,IAAI;YAChB,cAAc,EAAE,KAAK,CAAC,QAAQ;SAC/B,CAAC,CAAC;QAEH,OAAO;YACL,OAAO,EAAE,IAAI;YACb,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,IAAI,oBAAS,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI;SAC3D,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PluginItem } from '@babel/core';
|
|
2
2
|
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
3
|
-
|
|
3
|
+
import type { ResolveOptions } from 'enhanced-resolve';
|
|
4
|
+
declare type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles' | 'resolver'>;
|
|
4
5
|
export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
5
6
|
extract?: boolean;
|
|
6
7
|
stylesheetPath?: string;
|
|
@@ -15,5 +16,15 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
15
16
|
* Defaults to `false`.
|
|
16
17
|
*/
|
|
17
18
|
ssr?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Will run additional cssnano plugins to normalize CSS during build.
|
|
21
|
+
*
|
|
22
|
+
* Default to `true`.
|
|
23
|
+
*/
|
|
24
|
+
optimizeCss?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
27
|
+
*/
|
|
28
|
+
resolve?: ResolveOptions;
|
|
18
29
|
}
|
|
19
30
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compiled/parcel-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "A familiar and performant compile time CSS-in-JS library for React.",
|
|
5
5
|
"homepage": "https://compiledcssinjs.com/docs/pkg-parcel-transformer",
|
|
6
6
|
"bugs": "https://github.com/atlassian-labs/compiled/issues/new?assignees=&labels=bug&template=bug_report.md",
|
|
@@ -22,17 +22,19 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@babel/core": "^7.17.10",
|
|
24
24
|
"@babel/generator": "^7.17.10",
|
|
25
|
-
"@compiled/babel-plugin": "^0.16.
|
|
26
|
-
"@compiled/babel-plugin-strip-runtime": "^0.
|
|
25
|
+
"@compiled/babel-plugin": "^0.16.5",
|
|
26
|
+
"@compiled/babel-plugin-strip-runtime": "^0.17.0",
|
|
27
27
|
"@compiled/utils": "^0.6.16",
|
|
28
28
|
"@parcel/plugin": "^2.3.2",
|
|
29
|
-
"@parcel/source-map": "^2.0.2"
|
|
29
|
+
"@parcel/source-map": "^2.0.2",
|
|
30
|
+
"enhanced-resolve": "^5.10.0"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@parcel/core": "^2.3.1",
|
|
33
34
|
"@parcel/fs": "^2.3.1",
|
|
34
35
|
"@parcel/types": "^2.3.1",
|
|
35
|
-
"@types/babel__core": "^7.1.19"
|
|
36
|
+
"@types/babel__core": "^7.1.19",
|
|
37
|
+
"prettier": "^2.7.1"
|
|
36
38
|
},
|
|
37
39
|
"engines": {
|
|
38
40
|
"parcel": "^2.0.0"
|
|
@@ -2,10 +2,15 @@ import { join } from 'path';
|
|
|
2
2
|
|
|
3
3
|
import Parcel, { createWorkerFarm } from '@parcel/core';
|
|
4
4
|
import { MemoryFS } from '@parcel/fs';
|
|
5
|
+
import { format } from 'prettier';
|
|
5
6
|
|
|
6
7
|
const rootPath = join(__dirname, '..', '..', '..', '..');
|
|
7
8
|
const fixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-app');
|
|
8
9
|
const extractionFixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-extract-app');
|
|
10
|
+
const customResolverFixtureRoot = join(
|
|
11
|
+
rootPath,
|
|
12
|
+
'fixtures/parcel-transformer-test-custom-resolver-app'
|
|
13
|
+
);
|
|
9
14
|
const babelComponentFixture = join(rootPath, 'fixtures/babel-component');
|
|
10
15
|
|
|
11
16
|
const workerFarm = createWorkerFarm();
|
|
@@ -14,8 +19,9 @@ afterAll(() => {
|
|
|
14
19
|
workerFarm.end();
|
|
15
20
|
});
|
|
16
21
|
|
|
22
|
+
const outputFS = new MemoryFS(workerFarm);
|
|
23
|
+
|
|
17
24
|
const getParcelInstance = (workingDir: string) => {
|
|
18
|
-
const outputFS = new MemoryFS(workerFarm);
|
|
19
25
|
return new Parcel({
|
|
20
26
|
config: join(workingDir, '.parcelrc'),
|
|
21
27
|
entries: [join(workingDir, 'src', 'index.html')],
|
|
@@ -26,6 +32,7 @@ const getParcelInstance = (workingDir: string) => {
|
|
|
26
32
|
},
|
|
27
33
|
},
|
|
28
34
|
workerFarm,
|
|
35
|
+
mode: 'production',
|
|
29
36
|
});
|
|
30
37
|
};
|
|
31
38
|
|
|
@@ -68,9 +75,22 @@ it('transforms assets with babel plugin', async () => {
|
|
|
68
75
|
`);
|
|
69
76
|
}, 50000);
|
|
70
77
|
|
|
78
|
+
it('transforms assets with custom resolver and statically evaluates imports', async () => {
|
|
79
|
+
const parcel = getParcelInstance(customResolverFixtureRoot);
|
|
80
|
+
const { changedAssets } = await parcel.run();
|
|
81
|
+
|
|
82
|
+
const asset = Array.from(changedAssets.values()).find(
|
|
83
|
+
(asset) => asset.filePath === join(customResolverFixtureRoot, 'src/index.jsx')
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const code = await asset?.getCode();
|
|
87
|
+
|
|
88
|
+
expect(code).toInclude('color:red');
|
|
89
|
+
}, 50000);
|
|
90
|
+
|
|
71
91
|
it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
72
92
|
const parcel = getParcelInstance(extractionFixtureRoot);
|
|
73
|
-
const { changedAssets } = await parcel.run();
|
|
93
|
+
const { changedAssets, bundleGraph } = await parcel.run();
|
|
74
94
|
const assets = Array.from(changedAssets.values());
|
|
75
95
|
|
|
76
96
|
const indexJsCode = await assets
|
|
@@ -78,75 +98,98 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
78
98
|
?.getCode();
|
|
79
99
|
expect(indexJsCode).toMatchInlineSnapshot(`
|
|
80
100
|
"var parcelHelpers = require(\\"@parcel/transformer-js/src/esmodule-helpers.js\\");
|
|
81
|
-
var
|
|
82
|
-
var _runtime = require(\\"@compiled/react/runtime\\");
|
|
101
|
+
var _jsxRuntime = require(\\"react/jsx-runtime\\");
|
|
102
|
+
/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ var _runtime = require(\\"@compiled/react/runtime\\");
|
|
83
103
|
var _babelComponentFixture = require(\\"@compiled/babel-component-fixture\\");
|
|
84
104
|
var _babelComponentFixtureDefault = parcelHelpers.interopDefault(_babelComponentFixture);
|
|
85
|
-
var _this = undefined;
|
|
86
|
-
/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ require(\\"compiled-css!?style=._syaz5scu%7Bcolor%3Ared%7D\\");
|
|
87
|
-
require(\\"compiled-css!?style=._1wyb12am%7Bfont-size%3A50px%7D\\");
|
|
88
105
|
var App = function() {
|
|
89
|
-
/*#__PURE__*/ return
|
|
106
|
+
/*#__PURE__*/ return _jsxRuntime.jsxs(_jsxRuntime.Fragment, {
|
|
90
107
|
children: [
|
|
91
|
-
/*#__PURE__*/
|
|
108
|
+
/*#__PURE__*/ _jsxRuntime.jsx(\\"div\\", {
|
|
92
109
|
className: _runtime.ax([
|
|
93
110
|
\\"_1wyb12am _syaz5scu\\"
|
|
94
111
|
]),
|
|
95
112
|
children: \\"CSS prop\\"
|
|
96
|
-
},
|
|
97
|
-
|
|
98
|
-
lineNumber: 11,
|
|
99
|
-
columnNumber: 5
|
|
100
|
-
}, _this),
|
|
101
|
-
/*#__PURE__*/ _jsxDevRuntime.jsxDEV(_babelComponentFixtureDefault.default, {
|
|
113
|
+
}),
|
|
114
|
+
/*#__PURE__*/ _jsxRuntime.jsx(_babelComponentFixtureDefault.default, {
|
|
102
115
|
children: \\"Babel component\\"
|
|
103
|
-
}
|
|
104
|
-
fileName: \\"src/index.jsx\\",
|
|
105
|
-
lineNumber: 12,
|
|
106
|
-
columnNumber: 5
|
|
107
|
-
}, _this)
|
|
116
|
+
})
|
|
108
117
|
]
|
|
109
|
-
}
|
|
118
|
+
});
|
|
110
119
|
};
|
|
111
120
|
"
|
|
112
121
|
`);
|
|
113
122
|
|
|
123
|
+
const htmlAsset = Array.from(changedAssets.values()).find(
|
|
124
|
+
(asset) => asset.filePath === join(extractionFixtureRoot, '/src/index.html')
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const outputHtml = await outputFS.readFile(
|
|
128
|
+
bundleGraph.getBundlesWithAsset(htmlAsset!)[0].filePath,
|
|
129
|
+
'utf8'
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
const css = /<style>(.*?)<\/style>/.exec(outputHtml)?.pop();
|
|
133
|
+
|
|
134
|
+
if (!css) throw new Error('No CSS is found.');
|
|
135
|
+
|
|
136
|
+
expect(
|
|
137
|
+
format(css, {
|
|
138
|
+
parser: 'css',
|
|
139
|
+
singleQuote: true,
|
|
140
|
+
})
|
|
141
|
+
).toMatchInlineSnapshot(`
|
|
142
|
+
"._1wyb12am {
|
|
143
|
+
font-size: 50px;
|
|
144
|
+
}
|
|
145
|
+
._syaz5scu {
|
|
146
|
+
color: red;
|
|
147
|
+
}
|
|
148
|
+
._syaz13q2 {
|
|
149
|
+
color: blue;
|
|
150
|
+
}
|
|
151
|
+
._1wyb1ul9 {
|
|
152
|
+
font-size: 30px;
|
|
153
|
+
}
|
|
154
|
+
._19itlf8h {
|
|
155
|
+
border: 2px solid blue;
|
|
156
|
+
}
|
|
157
|
+
._ca0q1vi7 {
|
|
158
|
+
padding-top: 32px;
|
|
159
|
+
}
|
|
160
|
+
._u5f31vi7 {
|
|
161
|
+
padding-right: 32px;
|
|
162
|
+
}
|
|
163
|
+
._n3td1vi7 {
|
|
164
|
+
padding-bottom: 32px;
|
|
165
|
+
}
|
|
166
|
+
._19bv1vi7 {
|
|
167
|
+
padding-left: 32px;
|
|
168
|
+
}
|
|
169
|
+
._19pk1ul9 {
|
|
170
|
+
margin-top: 30px;
|
|
171
|
+
}
|
|
172
|
+
"
|
|
173
|
+
`);
|
|
174
|
+
|
|
114
175
|
const babelComponentCode = await assets
|
|
115
176
|
.find((asset) => asset.filePath === join(babelComponentFixture, 'dist/index.js'))
|
|
116
177
|
?.getCode();
|
|
117
178
|
|
|
118
|
-
const extractedCss = babelComponentCode
|
|
119
|
-
?.slice(0, babelComponentCode.indexOf('exports.default = BabelComponent;'))
|
|
120
|
-
.trim();
|
|
121
|
-
expect(extractedCss).toMatchInlineSnapshot(`
|
|
122
|
-
"/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ \\"use strict\\";
|
|
123
|
-
require(\\"compiled-css!?style=._19pk1ul9%7Bmargin-top%3A30px%7D\\");
|
|
124
|
-
require(\\"compiled-css!?style=._19bvftgi%7Bpadding-left%3A8px%7D\\");
|
|
125
|
-
require(\\"compiled-css!?style=._n3tdftgi%7Bpadding-bottom%3A8px%7D\\");
|
|
126
|
-
require(\\"compiled-css!?style=._u5f3ftgi%7Bpadding-right%3A8px%7D\\");
|
|
127
|
-
require(\\"compiled-css!?style=._ca0qftgi%7Bpadding-top%3A8px%7D\\");
|
|
128
|
-
require(\\"compiled-css!?style=._19itlf8h%7Bborder%3A2px%20solid%20blue%7D\\");
|
|
129
|
-
require(\\"compiled-css!?style=._1wyb1ul9%7Bfont-size%3A30px%7D\\");
|
|
130
|
-
require(\\"compiled-css!?style=._syaz13q2%7Bcolor%3Ablue%7D\\");
|
|
131
|
-
Object.defineProperty(exports, \\"__esModule\\", {
|
|
132
|
-
value: true
|
|
133
|
-
});"
|
|
134
|
-
`);
|
|
135
|
-
|
|
136
179
|
const extractedComponent = babelComponentCode
|
|
137
180
|
?.slice(babelComponentCode.indexOf('var Button'))
|
|
138
181
|
.trim();
|
|
139
182
|
expect(extractedComponent).toMatchInlineSnapshot(`
|
|
140
|
-
"var Button = (0, _react.forwardRef)(function(_ref,
|
|
141
|
-
var _ref$as = _ref.as, C = _ref$as === void 0 ? \\"button\\" : _ref$as,
|
|
183
|
+
"var Button = (0, _react.forwardRef)(function(_ref, __cmplr) {
|
|
184
|
+
var _ref$as = _ref.as, C = _ref$as === void 0 ? \\"button\\" : _ref$as, __cmpls = _ref.style, __cmplp = _objectWithoutProperties(_ref, _excluded);
|
|
142
185
|
return (0, _jsxRuntime.jsx)(C, _objectSpread(_objectSpread({
|
|
143
|
-
},
|
|
186
|
+
}, __cmplp), {
|
|
144
187
|
}, {
|
|
145
|
-
style:
|
|
146
|
-
ref:
|
|
188
|
+
style: __cmpls,
|
|
189
|
+
ref: __cmplr,
|
|
147
190
|
className: (0, _runtime.ax)([
|
|
148
|
-
\\"_syaz13q2 _1wyb1ul9 _19itlf8h
|
|
149
|
-
|
|
191
|
+
\\"_syaz13q2 _1wyb1ul9 _19itlf8h _ca0q1vi7 _u5f31vi7 _n3td1vi7 _19bv1vi7\\",
|
|
192
|
+
__cmplp.className
|
|
150
193
|
])
|
|
151
194
|
}));
|
|
152
195
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { join, dirname } from 'path';
|
|
2
3
|
|
|
3
4
|
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
4
5
|
import generate from '@babel/generator';
|
|
5
6
|
import type { PluginOptions as BabelPluginOptions } from '@compiled/babel-plugin';
|
|
6
|
-
import type {
|
|
7
|
+
import type {
|
|
8
|
+
PluginOptions as BabelStripRuntimePluginOptions,
|
|
9
|
+
BabelFileMetadata,
|
|
10
|
+
} from '@compiled/babel-plugin-strip-runtime';
|
|
7
11
|
import { toBoolean } from '@compiled/utils';
|
|
8
12
|
import { Transformer } from '@parcel/plugin';
|
|
9
13
|
import SourceMap from '@parcel/source-map';
|
|
14
|
+
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve';
|
|
10
15
|
|
|
11
16
|
import type { ParcelTransformerOpts } from './types';
|
|
12
17
|
|
|
@@ -45,8 +50,10 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
45
50
|
return false;
|
|
46
51
|
},
|
|
47
52
|
|
|
48
|
-
async parse({ asset, config }) {
|
|
49
|
-
|
|
53
|
+
async parse({ asset, config, options }) {
|
|
54
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
55
|
+
const extract = config.extract && options.mode !== 'development';
|
|
56
|
+
if (!asset.isSource && !extract) {
|
|
50
57
|
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
51
58
|
return undefined;
|
|
52
59
|
}
|
|
@@ -75,7 +82,7 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
75
82
|
};
|
|
76
83
|
},
|
|
77
84
|
|
|
78
|
-
async transform({ asset, config }) {
|
|
85
|
+
async transform({ asset, config, options }) {
|
|
79
86
|
const ast = await asset.getAST();
|
|
80
87
|
if (!ast) {
|
|
81
88
|
// We will only receive ASTs for assets we're interested in.
|
|
@@ -83,9 +90,21 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
83
90
|
return [asset];
|
|
84
91
|
}
|
|
85
92
|
|
|
93
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
94
|
+
const extract = config.extract && options.mode !== 'development';
|
|
86
95
|
const includedFiles: string[] = [];
|
|
87
96
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
88
97
|
|
|
98
|
+
const resolver = ResolverFactory.createResolver({
|
|
99
|
+
fileSystem: new CachedInputFileSystem(fs, 4000),
|
|
100
|
+
...(config.extensions && {
|
|
101
|
+
extensions: config.extensions,
|
|
102
|
+
}),
|
|
103
|
+
...(config.resolve ?? {}),
|
|
104
|
+
// This makes the resolver invoke the callback synchronously
|
|
105
|
+
useSyncFileSystemCalls: true,
|
|
106
|
+
});
|
|
107
|
+
|
|
89
108
|
const result = await transformFromAstAsync(ast.program, code, {
|
|
90
109
|
code: true,
|
|
91
110
|
ast: false,
|
|
@@ -103,14 +122,19 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
103
122
|
{
|
|
104
123
|
...config,
|
|
105
124
|
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
125
|
+
resolver: {
|
|
126
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
127
|
+
resolveSync: (context: string, request: string) => {
|
|
128
|
+
return resolver.resolveSync({}, dirname(context), request);
|
|
129
|
+
},
|
|
130
|
+
},
|
|
106
131
|
cache: false,
|
|
107
132
|
} as BabelPluginOptions,
|
|
108
133
|
],
|
|
109
|
-
|
|
134
|
+
extract && [
|
|
110
135
|
'@compiled/babel-plugin-strip-runtime',
|
|
111
136
|
{
|
|
112
|
-
|
|
113
|
-
compiledRequireExclude: config.ssr,
|
|
137
|
+
compiledRequireExclude: true,
|
|
114
138
|
} as BabelStripRuntimePluginOptions,
|
|
115
139
|
],
|
|
116
140
|
].filter(toBoolean),
|
|
@@ -130,6 +154,12 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
130
154
|
|
|
131
155
|
asset.setCode(output);
|
|
132
156
|
|
|
157
|
+
if (extract) {
|
|
158
|
+
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
159
|
+
const metadata = result?.metadata as BabelFileMetadata;
|
|
160
|
+
asset.meta.styleRules = metadata.styleRules;
|
|
161
|
+
}
|
|
162
|
+
|
|
133
163
|
return [asset];
|
|
134
164
|
},
|
|
135
165
|
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { PluginItem } from '@babel/core';
|
|
2
2
|
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
3
|
+
import type { ResolveOptions } from 'enhanced-resolve';
|
|
3
4
|
|
|
4
|
-
type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles'>;
|
|
5
|
+
type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles' | 'resolver'>;
|
|
5
6
|
|
|
6
7
|
export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
7
8
|
extract?: boolean;
|
|
@@ -19,4 +20,16 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
19
20
|
* Defaults to `false`.
|
|
20
21
|
*/
|
|
21
22
|
ssr?: boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Will run additional cssnano plugins to normalize CSS during build.
|
|
26
|
+
*
|
|
27
|
+
* Default to `true`.
|
|
28
|
+
*/
|
|
29
|
+
optimizeCss?: boolean;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
33
|
+
*/
|
|
34
|
+
resolve?: ResolveOptions;
|
|
22
35
|
}
|