@compiled/parcel-transformer 0.13.3 → 0.14.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.js +24 -17
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +13 -1
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +25 -0
- package/dist/utils.js.map +1 -0
- package/package.json +4 -4
- package/src/__tests__/transformer.parceltest.ts +45 -23
- package/src/index.ts +27 -20
- package/src/types.ts +12 -1
- package/src/utils.ts +26 -0
package/dist/index.js
CHANGED
|
@@ -3,14 +3,13 @@ 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"));
|
|
7
6
|
const path_1 = require("path");
|
|
8
7
|
const core_1 = require("@babel/core");
|
|
9
8
|
const generator_1 = __importDefault(require("@babel/generator"));
|
|
10
9
|
const utils_1 = require("@compiled/utils");
|
|
11
10
|
const plugin_1 = require("@parcel/plugin");
|
|
12
11
|
const source_map_1 = __importDefault(require("@parcel/source-map"));
|
|
13
|
-
const
|
|
12
|
+
const utils_2 = require("./utils");
|
|
14
13
|
const configFiles = [
|
|
15
14
|
'.compiledcssrc',
|
|
16
15
|
'.compiledcssrc.json',
|
|
@@ -87,7 +86,24 @@ exports.default = new plugin_1.Transformer({
|
|
|
87
86
|
};
|
|
88
87
|
},
|
|
89
88
|
async transform({ asset, config, options }) {
|
|
90
|
-
var _a, _b, _c;
|
|
89
|
+
var _a, _b, _c, _d, _e;
|
|
90
|
+
const distStyleRules = [];
|
|
91
|
+
let someCode = await asset.getCode();
|
|
92
|
+
for (const match of someCode.matchAll(/(import ['"](?<importSpec>.+\.compiled\.css)['"];)|(require\(['"](?<requireSpec>.+\.compiled\.css)['"]\);)/g)) {
|
|
93
|
+
const specifierPath = ((_a = match.groups) === null || _a === void 0 ? void 0 : _a.importSpec) || ((_b = match.groups) === null || _b === void 0 ? void 0 : _b.requireSpec);
|
|
94
|
+
if (!specifierPath)
|
|
95
|
+
continue;
|
|
96
|
+
someCode = someCode.replace(match[0], '');
|
|
97
|
+
asset.setCode(someCode);
|
|
98
|
+
const cssFilePath = (0, path_1.isAbsolute)(specifierPath)
|
|
99
|
+
? specifierPath
|
|
100
|
+
: (0, path_1.join)((0, path_1.dirname)(asset.filePath), specifierPath);
|
|
101
|
+
const cssContent = (await asset.fs.readFile(cssFilePath)).toString().split('\n');
|
|
102
|
+
if (!asset.meta.styleRules) {
|
|
103
|
+
asset.meta.styleRules = [];
|
|
104
|
+
}
|
|
105
|
+
asset.meta.styleRules.push(...cssContent);
|
|
106
|
+
}
|
|
91
107
|
const ast = await asset.getAST();
|
|
92
108
|
if (!ast) {
|
|
93
109
|
// We will only receive ASTs for assets we're interested in.
|
|
@@ -98,11 +114,6 @@ exports.default = new plugin_1.Transformer({
|
|
|
98
114
|
const extract = config.extract && options.mode !== 'development';
|
|
99
115
|
const includedFiles = [];
|
|
100
116
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
101
|
-
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 && {
|
|
102
|
-
extensions: config.extensions,
|
|
103
|
-
})), ((_a = config.resolve) !== null && _a !== void 0 ? _a : {})), {
|
|
104
|
-
// This makes the resolver invoke the callback synchronously
|
|
105
|
-
useSyncFileSystemCalls: true }));
|
|
106
117
|
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
107
118
|
code: true,
|
|
108
119
|
ast: false,
|
|
@@ -111,23 +122,19 @@ exports.default = new plugin_1.Transformer({
|
|
|
111
122
|
configFile: false,
|
|
112
123
|
sourceMaps: true,
|
|
113
124
|
parserOpts: {
|
|
114
|
-
plugins: (
|
|
125
|
+
plugins: (_c = config.parserBabelPlugins) !== null && _c !== void 0 ? _c : undefined,
|
|
115
126
|
},
|
|
116
127
|
plugins: [
|
|
117
|
-
...((
|
|
128
|
+
...((_d = config.transformerBabelPlugins) !== null && _d !== void 0 ? _d : []),
|
|
118
129
|
asset.isSource && [
|
|
119
130
|
'@compiled/babel-plugin',
|
|
120
|
-
Object.assign(Object.assign({}, config), { classNameCompressionMap: config.extract && config.classNameCompressionMap, onIncludedFiles: (files) => includedFiles.push(...files), resolver:
|
|
121
|
-
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
122
|
-
resolveSync: (context, request) => {
|
|
123
|
-
return resolver.resolveSync({}, (0, path_1.dirname)(context), request);
|
|
124
|
-
},
|
|
125
|
-
}, cache: false }),
|
|
131
|
+
Object.assign(Object.assign({}, config), { classNameCompressionMap: config.extract && config.classNameCompressionMap, onIncludedFiles: (files) => includedFiles.push(...files), resolver: config.resolver ? config.resolver : (0, utils_2.createDefaultResolver)(config), cache: false }),
|
|
126
132
|
],
|
|
127
133
|
extract && [
|
|
128
134
|
'@compiled/babel-plugin-strip-runtime',
|
|
129
135
|
{
|
|
130
136
|
compiledRequireExclude: true,
|
|
137
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
131
138
|
},
|
|
132
139
|
],
|
|
133
140
|
].filter(utils_1.toBoolean),
|
|
@@ -146,7 +153,7 @@ exports.default = new plugin_1.Transformer({
|
|
|
146
153
|
if (extract) {
|
|
147
154
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
148
155
|
const metadata = result === null || result === void 0 ? void 0 : result.metadata;
|
|
149
|
-
asset.meta.styleRules = metadata.styleRules;
|
|
156
|
+
asset.meta.styleRules = [...((_e = metadata.styleRules) !== null && _e !== void 0 ? _e : []), ...distStyleRules];
|
|
150
157
|
}
|
|
151
158
|
return [asset];
|
|
152
159
|
},
|
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,+BAAiD;AAEjD,sCAAgE;AAChE,iEAAwC;AAMxC,2CAA4C;AAC5C,2CAA6C;AAC7C,oEAA2C;AAG3C,mCAAgD;AAEhD,MAAM,WAAW,GAAG;IAClB,gBAAgB;IAChB,qBAAqB;IACrB,gBAAgB;IAChB,uBAAuB;CACxB,CAAC;AAEF,MAAM,UAAU,GAAG,8BAA8B,CAAC;AAElD;;GAEG;AACH,kBAAe,IAAI,oBAAW,CAAwB;IACpD,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE;QAClC,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,aAAa,CACrC,IAAA,WAAI,EAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,EAClC,WAAW,EACX;YACE,UAAU;SACX,CACF,CAAC;QAEF,MAAM,QAAQ,GAA0B;YACtC,OAAO,EAAE,KAAK;YACd,WAAW,EAAE,IAAI;YACjB,GAAG,EAAE,KAAK;SACX,CAAC;QAEF,IAAI,IAAI,EAAE;YACR,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;gBACjC,MAAM,CAAC,mBAAmB,EAAE,CAAC;aAC9B;YAED,uEAAuE;YACvE,+FAA+F;YAC/F,kEAAkE;YAClE,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,uBAAuB,IAAI,IAAI,CAAC,QAAQ,CAAC,+BAA+B,EAAE;gBAC3F,gFAAgF;gBAChF,MAAM,6BAA6B,GAAG,MAAM,MAAM,CAAC,aAAa,CAC9D,IAAA,WAAI,EAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,EAClC,CAAC,IAAI,CAAC,QAAQ,CAAC,+BAA+B,CAAC,EAC/C;oBACE,UAAU;iBACX,CACF,CAAC;gB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AAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { ResolveOptions } from 'enhanced-resolve';
|
|
|
4
4
|
type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles' | 'resolver'>;
|
|
5
5
|
export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
6
6
|
extract?: boolean;
|
|
7
|
+
extractFromDistributedCode?: boolean;
|
|
7
8
|
stylesheetPath?: string;
|
|
8
9
|
/**
|
|
9
10
|
* List of transformer babel plugins to be applied to evaluated files
|
|
@@ -23,9 +24,13 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
23
24
|
*/
|
|
24
25
|
optimizeCss?: boolean;
|
|
25
26
|
/**
|
|
26
|
-
* Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
27
|
+
* @deprecated Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
27
28
|
*/
|
|
28
29
|
resolve?: ResolveOptions;
|
|
30
|
+
/**
|
|
31
|
+
* Override the default @compiled/babel-plugin resolver, by providing a module path that exports a custom resolver
|
|
32
|
+
*/
|
|
33
|
+
resolver?: string;
|
|
29
34
|
/**
|
|
30
35
|
* Add the component name as class name to DOM in non-production environment if styled is used
|
|
31
36
|
*
|
|
@@ -49,5 +54,12 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
49
54
|
* Default to `undefined`
|
|
50
55
|
*/
|
|
51
56
|
classNameCompressionMapFilePath?: string;
|
|
57
|
+
/**
|
|
58
|
+
* When set, extract styles to an external CSS file
|
|
59
|
+
*/
|
|
60
|
+
extractStylesToDirectory?: {
|
|
61
|
+
source: string;
|
|
62
|
+
dest: string;
|
|
63
|
+
};
|
|
52
64
|
}
|
|
53
65
|
export {};
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDefaultResolver = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const enhanced_resolve_1 = require("enhanced-resolve");
|
|
10
|
+
function createDefaultResolver(config) {
|
|
11
|
+
var _a;
|
|
12
|
+
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 && {
|
|
13
|
+
extensions: config.extensions,
|
|
14
|
+
})), ((_a = config.resolve) !== null && _a !== void 0 ? _a : {})), {
|
|
15
|
+
// This makes the resolver invoke the callback synchronously
|
|
16
|
+
useSyncFileSystemCalls: true }));
|
|
17
|
+
return {
|
|
18
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
19
|
+
resolveSync(context, request) {
|
|
20
|
+
return resolver.resolveSync({}, (0, path_1.dirname)(context), request);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
exports.createDefaultResolver = createDefaultResolver;
|
|
25
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,+BAA+B;AAG/B,uDAA0E;AAI1E,SAAgB,qBAAqB,CAAC,MAA6B;;IACjE,MAAM,QAAQ,GAAG,kCAAe,CAAC,cAAc,6CAC7C,UAAU,EAAE,IAAI,wCAAqB,CAAC,YAAE,EAAE,IAAI,CAAC,IAC5C,CAAC,MAAM,CAAC,UAAU,IAAI;QACvB,UAAU,EAAE,MAAM,CAAC,UAAU;KAC9B,CAAC,GACC,CAAC,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC;QACzB,4DAA4D;QAC5D,sBAAsB,EAAE,IAAI,IAC5B,CAAC;IAEH,OAAO;QACL,6EAA6E;QAC7E,WAAW,CAAC,OAAe,EAAE,OAAe;YAC1C,OAAO,QAAQ,CAAC,WAAW,CAAC,EAAE,EAAE,IAAA,cAAO,EAAC,OAAO,CAAC,EAAE,OAAO,CAAW,CAAC;QACvE,CAAC;KACF,CAAC;AACJ,CAAC;AAjBD,sDAiBC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compiled/parcel-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.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,12 +22,12 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@babel/core": "^7.21.8",
|
|
24
24
|
"@babel/generator": "^7.21.5",
|
|
25
|
-
"@compiled/babel-plugin": "^0.
|
|
26
|
-
"@compiled/babel-plugin-strip-runtime": "^0.
|
|
25
|
+
"@compiled/babel-plugin": "^0.24.0",
|
|
26
|
+
"@compiled/babel-plugin-strip-runtime": "^0.23.0",
|
|
27
27
|
"@compiled/utils": "^0.8.0",
|
|
28
28
|
"@parcel/plugin": "^2.8.3",
|
|
29
29
|
"@parcel/source-map": "^2.1.1",
|
|
30
|
-
"enhanced-resolve": "^5.
|
|
30
|
+
"enhanced-resolve": "^5.15.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@parcel/core": "^2.8.3",
|
|
@@ -6,15 +6,6 @@ import { format } from 'prettier';
|
|
|
6
6
|
|
|
7
7
|
const rootPath = join(__dirname, '..', '..', '..', '..');
|
|
8
8
|
const fixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-app');
|
|
9
|
-
const extractionFixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-extract-app');
|
|
10
|
-
const compressingClassNameFixtureRoot = join(
|
|
11
|
-
rootPath,
|
|
12
|
-
'fixtures/parcel-transformer-test-compress-class-name-app'
|
|
13
|
-
);
|
|
14
|
-
const customResolverFixtureRoot = join(
|
|
15
|
-
rootPath,
|
|
16
|
-
'fixtures/parcel-transformer-test-custom-resolver-app'
|
|
17
|
-
);
|
|
18
9
|
const babelComponentFixture = join(rootPath, 'fixtures/babel-component');
|
|
19
10
|
|
|
20
11
|
const workerFarm = createWorkerFarm();
|
|
@@ -79,7 +70,28 @@ it('transforms assets with babel plugin', async () => {
|
|
|
79
70
|
`);
|
|
80
71
|
}, 50000);
|
|
81
72
|
|
|
73
|
+
it('transforms assets with custom resolve and statically evaluates imports', async () => {
|
|
74
|
+
const customResolveFixtureRoot = join(
|
|
75
|
+
rootPath,
|
|
76
|
+
'fixtures/parcel-transformer-test-custom-resolve-app'
|
|
77
|
+
);
|
|
78
|
+
const parcel = getParcelInstance(customResolveFixtureRoot);
|
|
79
|
+
const { changedAssets } = await parcel.run();
|
|
80
|
+
|
|
81
|
+
const asset = Array.from(changedAssets.values()).find(
|
|
82
|
+
(asset) => asset.filePath === join(customResolveFixtureRoot, 'src/index.jsx')
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const code = await asset?.getCode();
|
|
86
|
+
|
|
87
|
+
expect(code).toInclude('color:red');
|
|
88
|
+
}, 50000);
|
|
89
|
+
|
|
82
90
|
it('transforms assets with custom resolver and statically evaluates imports', async () => {
|
|
91
|
+
const customResolverFixtureRoot = join(
|
|
92
|
+
rootPath,
|
|
93
|
+
'fixtures/parcel-transformer-test-custom-resolver-app'
|
|
94
|
+
);
|
|
83
95
|
const parcel = getParcelInstance(customResolverFixtureRoot);
|
|
84
96
|
const { changedAssets } = await parcel.run();
|
|
85
97
|
|
|
@@ -93,6 +105,7 @@ it('transforms assets with custom resolver and statically evaluates imports', as
|
|
|
93
105
|
}, 50000);
|
|
94
106
|
|
|
95
107
|
it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
108
|
+
const extractionFixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-extract-app');
|
|
96
109
|
const parcel = getParcelInstance(extractionFixtureRoot);
|
|
97
110
|
const { changedAssets, bundleGraph } = await parcel.run();
|
|
98
111
|
const assets = Array.from(changedAssets.values());
|
|
@@ -104,6 +117,8 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
104
117
|
"/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
|
105
118
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
106
119
|
var _runtime = require("@compiled/react/runtime");
|
|
120
|
+
var _index = require("@compiled/babel-component-extracted-fixture/dist/index");
|
|
121
|
+
var _indexDefault = parcelHelpers.interopDefault(_index);
|
|
107
122
|
var _babelComponentFixture = require("@compiled/babel-component-fixture");
|
|
108
123
|
var _babelComponentFixtureDefault = parcelHelpers.interopDefault(_babelComponentFixture);
|
|
109
124
|
var App = function() {
|
|
@@ -111,12 +126,15 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
111
126
|
children: [
|
|
112
127
|
/*#__PURE__*/ (0, _jsxRuntime.jsx)("div", {
|
|
113
128
|
className: (0, _runtime.ax)([
|
|
114
|
-
"_1wyb12am
|
|
129
|
+
"_1wyb12am _syaz13q2"
|
|
115
130
|
]),
|
|
116
131
|
children: "CSS prop"
|
|
117
132
|
}),
|
|
118
133
|
/*#__PURE__*/ (0, _jsxRuntime.jsx)((0, _babelComponentFixtureDefault.default), {
|
|
119
134
|
children: "Babel component"
|
|
135
|
+
}),
|
|
136
|
+
/*#__PURE__*/ (0, _jsxRuntime.jsx)((0, _indexDefault.default), {
|
|
137
|
+
children: "Component from NPM"
|
|
120
138
|
})
|
|
121
139
|
]
|
|
122
140
|
});
|
|
@@ -146,32 +164,32 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
146
164
|
"._1wyb12am {
|
|
147
165
|
font-size: 50px;
|
|
148
166
|
}
|
|
149
|
-
._syaz5scu {
|
|
150
|
-
color: red;
|
|
151
|
-
}
|
|
152
167
|
._syaz13q2 {
|
|
153
168
|
color: blue;
|
|
154
169
|
}
|
|
155
|
-
.
|
|
156
|
-
|
|
170
|
+
._19bv1vi7 {
|
|
171
|
+
padding-left: 32px;
|
|
157
172
|
}
|
|
158
173
|
._19itlf8h {
|
|
159
174
|
border: 2px solid blue;
|
|
160
175
|
}
|
|
176
|
+
._19pk1ul9 {
|
|
177
|
+
margin-top: 30px;
|
|
178
|
+
}
|
|
179
|
+
._1wyb1ul9 {
|
|
180
|
+
font-size: 30px;
|
|
181
|
+
}
|
|
182
|
+
._bfhk1gy6 {
|
|
183
|
+
background-color: yellow;
|
|
184
|
+
}
|
|
161
185
|
._ca0q1vi7 {
|
|
162
186
|
padding-top: 32px;
|
|
163
187
|
}
|
|
164
|
-
._u5f31vi7 {
|
|
165
|
-
padding-right: 32px;
|
|
166
|
-
}
|
|
167
188
|
._n3td1vi7 {
|
|
168
189
|
padding-bottom: 32px;
|
|
169
190
|
}
|
|
170
|
-
.
|
|
171
|
-
padding-
|
|
172
|
-
}
|
|
173
|
-
._19pk1ul9 {
|
|
174
|
-
margin-top: 30px;
|
|
191
|
+
._u5f31vi7 {
|
|
192
|
+
padding-right: 32px;
|
|
175
193
|
}
|
|
176
194
|
"
|
|
177
195
|
`);
|
|
@@ -211,6 +229,10 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
211
229
|
}, 50000);
|
|
212
230
|
|
|
213
231
|
it('transforms assets with class name compression enabled', async () => {
|
|
232
|
+
const compressingClassNameFixtureRoot = join(
|
|
233
|
+
rootPath,
|
|
234
|
+
'fixtures/parcel-transformer-test-compress-class-name-app'
|
|
235
|
+
);
|
|
214
236
|
const parcel = getParcelInstance(compressingClassNameFixtureRoot);
|
|
215
237
|
const { changedAssets, bundleGraph } = await parcel.run();
|
|
216
238
|
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { join, dirname } from 'path';
|
|
1
|
+
import { join, dirname, isAbsolute } from 'path';
|
|
3
2
|
|
|
4
3
|
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
5
4
|
import generate from '@babel/generator';
|
|
@@ -11,9 +10,9 @@ import type {
|
|
|
11
10
|
import { toBoolean } from '@compiled/utils';
|
|
12
11
|
import { Transformer } from '@parcel/plugin';
|
|
13
12
|
import SourceMap from '@parcel/source-map';
|
|
14
|
-
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve';
|
|
15
13
|
|
|
16
14
|
import type { ParcelTransformerOpts } from './types';
|
|
15
|
+
import { createDefaultResolver } from './utils';
|
|
17
16
|
|
|
18
17
|
const configFiles = [
|
|
19
18
|
'.compiledcssrc',
|
|
@@ -112,7 +111,29 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
112
111
|
},
|
|
113
112
|
|
|
114
113
|
async transform({ asset, config, options }) {
|
|
114
|
+
const distStyleRules: string[] = [];
|
|
115
|
+
let someCode = await asset.getCode();
|
|
116
|
+
for (const match of someCode.matchAll(
|
|
117
|
+
/(import ['"](?<importSpec>.+\.compiled\.css)['"];)|(require\(['"](?<requireSpec>.+\.compiled\.css)['"]\);)/g
|
|
118
|
+
)) {
|
|
119
|
+
const specifierPath = match.groups?.importSpec || match.groups?.requireSpec;
|
|
120
|
+
if (!specifierPath) continue;
|
|
121
|
+
someCode = someCode.replace(match[0], '');
|
|
122
|
+
asset.setCode(someCode);
|
|
123
|
+
|
|
124
|
+
const cssFilePath = isAbsolute(specifierPath)
|
|
125
|
+
? specifierPath
|
|
126
|
+
: join(dirname(asset.filePath), specifierPath);
|
|
127
|
+
|
|
128
|
+
const cssContent = (await asset.fs.readFile(cssFilePath)).toString().split('\n');
|
|
129
|
+
if (!asset.meta.styleRules) {
|
|
130
|
+
asset.meta.styleRules = [];
|
|
131
|
+
}
|
|
132
|
+
(asset.meta.styleRules as string[]).push(...cssContent);
|
|
133
|
+
}
|
|
134
|
+
|
|
115
135
|
const ast = await asset.getAST();
|
|
136
|
+
|
|
116
137
|
if (!ast) {
|
|
117
138
|
// We will only receive ASTs for assets we're interested in.
|
|
118
139
|
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
@@ -124,16 +145,6 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
124
145
|
const includedFiles: string[] = [];
|
|
125
146
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
126
147
|
|
|
127
|
-
const resolver = ResolverFactory.createResolver({
|
|
128
|
-
fileSystem: new CachedInputFileSystem(fs, 4000),
|
|
129
|
-
...(config.extensions && {
|
|
130
|
-
extensions: config.extensions,
|
|
131
|
-
}),
|
|
132
|
-
...(config.resolve ?? {}),
|
|
133
|
-
// This makes the resolver invoke the callback synchronously
|
|
134
|
-
useSyncFileSystemCalls: true,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
148
|
const result = await transformFromAstAsync(ast.program, code, {
|
|
138
149
|
code: true,
|
|
139
150
|
ast: false,
|
|
@@ -152,12 +163,7 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
152
163
|
...config,
|
|
153
164
|
classNameCompressionMap: config.extract && config.classNameCompressionMap,
|
|
154
165
|
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
155
|
-
resolver:
|
|
156
|
-
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
157
|
-
resolveSync: (context: string, request: string) => {
|
|
158
|
-
return resolver.resolveSync({}, dirname(context), request);
|
|
159
|
-
},
|
|
160
|
-
},
|
|
166
|
+
resolver: config.resolver ? config.resolver : createDefaultResolver(config),
|
|
161
167
|
cache: false,
|
|
162
168
|
} as BabelPluginOptions,
|
|
163
169
|
],
|
|
@@ -165,6 +171,7 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
165
171
|
'@compiled/babel-plugin-strip-runtime',
|
|
166
172
|
{
|
|
167
173
|
compiledRequireExclude: true,
|
|
174
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
168
175
|
} as BabelStripRuntimePluginOptions,
|
|
169
176
|
],
|
|
170
177
|
].filter(toBoolean),
|
|
@@ -187,7 +194,7 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
187
194
|
if (extract) {
|
|
188
195
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
189
196
|
const metadata = result?.metadata as BabelFileMetadata;
|
|
190
|
-
asset.meta.styleRules = metadata.styleRules;
|
|
197
|
+
asset.meta.styleRules = [...(metadata.styleRules ?? []), ...distStyleRules];
|
|
191
198
|
}
|
|
192
199
|
|
|
193
200
|
return [asset];
|
package/src/types.ts
CHANGED
|
@@ -6,6 +6,7 @@ type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles' | 'resolv
|
|
|
6
6
|
|
|
7
7
|
export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
8
8
|
extract?: boolean;
|
|
9
|
+
extractFromDistributedCode?: boolean;
|
|
9
10
|
stylesheetPath?: string;
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -29,10 +30,15 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
29
30
|
optimizeCss?: boolean;
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
|
-
* Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
33
|
+
* @deprecated Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
33
34
|
*/
|
|
34
35
|
resolve?: ResolveOptions;
|
|
35
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Override the default @compiled/babel-plugin resolver, by providing a module path that exports a custom resolver
|
|
39
|
+
*/
|
|
40
|
+
resolver?: string;
|
|
41
|
+
|
|
36
42
|
/**
|
|
37
43
|
* Add the component name as class name to DOM in non-production environment if styled is used
|
|
38
44
|
*
|
|
@@ -56,4 +62,9 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
56
62
|
* Default to `undefined`
|
|
57
63
|
*/
|
|
58
64
|
classNameCompressionMapFilePath?: string;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* When set, extract styles to an external CSS file
|
|
68
|
+
*/
|
|
69
|
+
extractStylesToDirectory?: { source: string; dest: string };
|
|
59
70
|
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { dirname } from 'path';
|
|
3
|
+
|
|
4
|
+
import type { Resolver } from '@compiled/babel-plugin';
|
|
5
|
+
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve';
|
|
6
|
+
|
|
7
|
+
import type { ParcelTransformerOpts } from './types';
|
|
8
|
+
|
|
9
|
+
export function createDefaultResolver(config: ParcelTransformerOpts): Resolver {
|
|
10
|
+
const resolver = ResolverFactory.createResolver({
|
|
11
|
+
fileSystem: new CachedInputFileSystem(fs, 4000),
|
|
12
|
+
...(config.extensions && {
|
|
13
|
+
extensions: config.extensions,
|
|
14
|
+
}),
|
|
15
|
+
...(config.resolve ?? {}),
|
|
16
|
+
// This makes the resolver invoke the callback synchronously
|
|
17
|
+
useSyncFileSystemCalls: true,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
22
|
+
resolveSync(context: string, request: string) {
|
|
23
|
+
return resolver.resolveSync({}, dirname(context), request) as string;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|