@compiled/parcel-transformer 0.13.3 → 0.13.4
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 +23 -5
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +8 -0
- package/package.json +3 -3
- package/src/__tests__/transformer.parceltest.ts +19 -14
- package/src/index.ts +25 -2
- package/src/types.ts +6 -0
package/dist/index.js
CHANGED
|
@@ -87,7 +87,24 @@ exports.default = new plugin_1.Transformer({
|
|
|
87
87
|
};
|
|
88
88
|
},
|
|
89
89
|
async transform({ asset, config, options }) {
|
|
90
|
-
var _a, _b, _c;
|
|
90
|
+
var _a, _b, _c, _d, _e, _f;
|
|
91
|
+
const distStyleRules = [];
|
|
92
|
+
let someCode = await asset.getCode();
|
|
93
|
+
for (const match of someCode.matchAll(/(import ['"](?<importSpec>.+\.compiled\.css)['"];)|(require\(['"](?<requireSpec>.+\.compiled\.css)['"]\);)/g)) {
|
|
94
|
+
const specifierPath = ((_a = match.groups) === null || _a === void 0 ? void 0 : _a.importSpec) || ((_b = match.groups) === null || _b === void 0 ? void 0 : _b.requireSpec);
|
|
95
|
+
if (!specifierPath)
|
|
96
|
+
continue;
|
|
97
|
+
someCode = someCode.replace(match[0], '');
|
|
98
|
+
asset.setCode(someCode);
|
|
99
|
+
const cssFilePath = (0, path_1.isAbsolute)(specifierPath)
|
|
100
|
+
? specifierPath
|
|
101
|
+
: (0, path_1.join)((0, path_1.dirname)(asset.filePath), specifierPath);
|
|
102
|
+
const cssContent = (await asset.fs.readFile(cssFilePath)).toString().split('\n');
|
|
103
|
+
if (!asset.meta.styleRules) {
|
|
104
|
+
asset.meta.styleRules = [];
|
|
105
|
+
}
|
|
106
|
+
asset.meta.styleRules.push(...cssContent);
|
|
107
|
+
}
|
|
91
108
|
const ast = await asset.getAST();
|
|
92
109
|
if (!ast) {
|
|
93
110
|
// We will only receive ASTs for assets we're interested in.
|
|
@@ -100,7 +117,7 @@ exports.default = new plugin_1.Transformer({
|
|
|
100
117
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
101
118
|
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
119
|
extensions: config.extensions,
|
|
103
|
-
})), ((
|
|
120
|
+
})), ((_c = config.resolve) !== null && _c !== void 0 ? _c : {})), {
|
|
104
121
|
// This makes the resolver invoke the callback synchronously
|
|
105
122
|
useSyncFileSystemCalls: true }));
|
|
106
123
|
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
@@ -111,10 +128,10 @@ exports.default = new plugin_1.Transformer({
|
|
|
111
128
|
configFile: false,
|
|
112
129
|
sourceMaps: true,
|
|
113
130
|
parserOpts: {
|
|
114
|
-
plugins: (
|
|
131
|
+
plugins: (_d = config.parserBabelPlugins) !== null && _d !== void 0 ? _d : undefined,
|
|
115
132
|
},
|
|
116
133
|
plugins: [
|
|
117
|
-
...((
|
|
134
|
+
...((_e = config.transformerBabelPlugins) !== null && _e !== void 0 ? _e : []),
|
|
118
135
|
asset.isSource && [
|
|
119
136
|
'@compiled/babel-plugin',
|
|
120
137
|
Object.assign(Object.assign({}, config), { classNameCompressionMap: config.extract && config.classNameCompressionMap, onIncludedFiles: (files) => includedFiles.push(...files), resolver: {
|
|
@@ -128,6 +145,7 @@ exports.default = new plugin_1.Transformer({
|
|
|
128
145
|
'@compiled/babel-plugin-strip-runtime',
|
|
129
146
|
{
|
|
130
147
|
compiledRequireExclude: true,
|
|
148
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
131
149
|
},
|
|
132
150
|
],
|
|
133
151
|
].filter(utils_1.toBoolean),
|
|
@@ -146,7 +164,7 @@ exports.default = new plugin_1.Transformer({
|
|
|
146
164
|
if (extract) {
|
|
147
165
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
148
166
|
const metadata = result === null || result === void 0 ? void 0 : result.metadata;
|
|
149
|
-
asset.meta.styleRules = metadata.styleRules;
|
|
167
|
+
asset.meta.styleRules = [...((_f = metadata.styleRules) !== null && _f !== void 0 ? _f : []), ...distStyleRules];
|
|
150
168
|
}
|
|
151
169
|
return [asset];
|
|
152
170
|
},
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,4CAAoB;AACpB,+
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,4CAAoB;AACpB,+BAAiD;AAEjD,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,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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,CAAC;YACvD,KAAK,CAAC,IAAI,CAAC,UAAU,GAAG,CAAC,GAAG,CAAC,MAAA,QAAQ,CAAC,UAAU,mCAAI,EAAE,CAAC,EAAE,GAAG,cAAc,CAAC,CAAC;SAC7E;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
|
@@ -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
|
|
@@ -49,5 +50,12 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
49
50
|
* Default to `undefined`
|
|
50
51
|
*/
|
|
51
52
|
classNameCompressionMapFilePath?: string;
|
|
53
|
+
/**
|
|
54
|
+
* When set, extract styles to an external CSS file
|
|
55
|
+
*/
|
|
56
|
+
extractStylesToDirectory?: {
|
|
57
|
+
source: string;
|
|
58
|
+
dest: string;
|
|
59
|
+
};
|
|
52
60
|
}
|
|
53
61
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compiled/parcel-transformer",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.4",
|
|
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",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"@babel/core": "^7.21.8",
|
|
24
24
|
"@babel/generator": "^7.21.5",
|
|
25
25
|
"@compiled/babel-plugin": "^0.22.0",
|
|
26
|
-
"@compiled/babel-plugin-strip-runtime": "^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",
|
|
@@ -104,6 +104,8 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
104
104
|
"/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
|
105
105
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
106
106
|
var _runtime = require("@compiled/react/runtime");
|
|
107
|
+
var _index = require("@compiled/babel-component-extracted-fixture/dist/index");
|
|
108
|
+
var _indexDefault = parcelHelpers.interopDefault(_index);
|
|
107
109
|
var _babelComponentFixture = require("@compiled/babel-component-fixture");
|
|
108
110
|
var _babelComponentFixtureDefault = parcelHelpers.interopDefault(_babelComponentFixture);
|
|
109
111
|
var App = function() {
|
|
@@ -111,12 +113,15 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
111
113
|
children: [
|
|
112
114
|
/*#__PURE__*/ (0, _jsxRuntime.jsx)("div", {
|
|
113
115
|
className: (0, _runtime.ax)([
|
|
114
|
-
"_1wyb12am
|
|
116
|
+
"_1wyb12am _syaz13q2"
|
|
115
117
|
]),
|
|
116
118
|
children: "CSS prop"
|
|
117
119
|
}),
|
|
118
120
|
/*#__PURE__*/ (0, _jsxRuntime.jsx)((0, _babelComponentFixtureDefault.default), {
|
|
119
121
|
children: "Babel component"
|
|
122
|
+
}),
|
|
123
|
+
/*#__PURE__*/ (0, _jsxRuntime.jsx)((0, _indexDefault.default), {
|
|
124
|
+
children: "Component from NPM"
|
|
120
125
|
})
|
|
121
126
|
]
|
|
122
127
|
});
|
|
@@ -146,32 +151,32 @@ it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
|
146
151
|
"._1wyb12am {
|
|
147
152
|
font-size: 50px;
|
|
148
153
|
}
|
|
149
|
-
._syaz5scu {
|
|
150
|
-
color: red;
|
|
151
|
-
}
|
|
152
154
|
._syaz13q2 {
|
|
153
155
|
color: blue;
|
|
154
156
|
}
|
|
155
|
-
.
|
|
156
|
-
|
|
157
|
+
._19bv1vi7 {
|
|
158
|
+
padding-left: 32px;
|
|
157
159
|
}
|
|
158
160
|
._19itlf8h {
|
|
159
161
|
border: 2px solid blue;
|
|
160
162
|
}
|
|
163
|
+
._19pk1ul9 {
|
|
164
|
+
margin-top: 30px;
|
|
165
|
+
}
|
|
166
|
+
._1wyb1ul9 {
|
|
167
|
+
font-size: 30px;
|
|
168
|
+
}
|
|
169
|
+
._bfhk1gy6 {
|
|
170
|
+
background-color: yellow;
|
|
171
|
+
}
|
|
161
172
|
._ca0q1vi7 {
|
|
162
173
|
padding-top: 32px;
|
|
163
174
|
}
|
|
164
|
-
._u5f31vi7 {
|
|
165
|
-
padding-right: 32px;
|
|
166
|
-
}
|
|
167
175
|
._n3td1vi7 {
|
|
168
176
|
padding-bottom: 32px;
|
|
169
177
|
}
|
|
170
|
-
.
|
|
171
|
-
padding-
|
|
172
|
-
}
|
|
173
|
-
._19pk1ul9 {
|
|
174
|
-
margin-top: 30px;
|
|
178
|
+
._u5f31vi7 {
|
|
179
|
+
padding-right: 32px;
|
|
175
180
|
}
|
|
176
181
|
"
|
|
177
182
|
`);
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
-
import { join, dirname } from 'path';
|
|
2
|
+
import { join, dirname, isAbsolute } from 'path';
|
|
3
3
|
|
|
4
4
|
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
5
5
|
import generate from '@babel/generator';
|
|
@@ -112,7 +112,29 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
112
112
|
},
|
|
113
113
|
|
|
114
114
|
async transform({ asset, config, options }) {
|
|
115
|
+
const distStyleRules: string[] = [];
|
|
116
|
+
let someCode = await asset.getCode();
|
|
117
|
+
for (const match of someCode.matchAll(
|
|
118
|
+
/(import ['"](?<importSpec>.+\.compiled\.css)['"];)|(require\(['"](?<requireSpec>.+\.compiled\.css)['"]\);)/g
|
|
119
|
+
)) {
|
|
120
|
+
const specifierPath = match.groups?.importSpec || match.groups?.requireSpec;
|
|
121
|
+
if (!specifierPath) continue;
|
|
122
|
+
someCode = someCode.replace(match[0], '');
|
|
123
|
+
asset.setCode(someCode);
|
|
124
|
+
|
|
125
|
+
const cssFilePath = isAbsolute(specifierPath)
|
|
126
|
+
? specifierPath
|
|
127
|
+
: join(dirname(asset.filePath), specifierPath);
|
|
128
|
+
|
|
129
|
+
const cssContent = (await asset.fs.readFile(cssFilePath)).toString().split('\n');
|
|
130
|
+
if (!asset.meta.styleRules) {
|
|
131
|
+
asset.meta.styleRules = [];
|
|
132
|
+
}
|
|
133
|
+
(asset.meta.styleRules as string[]).push(...cssContent);
|
|
134
|
+
}
|
|
135
|
+
|
|
115
136
|
const ast = await asset.getAST();
|
|
137
|
+
|
|
116
138
|
if (!ast) {
|
|
117
139
|
// We will only receive ASTs for assets we're interested in.
|
|
118
140
|
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
@@ -165,6 +187,7 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
165
187
|
'@compiled/babel-plugin-strip-runtime',
|
|
166
188
|
{
|
|
167
189
|
compiledRequireExclude: true,
|
|
190
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
168
191
|
} as BabelStripRuntimePluginOptions,
|
|
169
192
|
],
|
|
170
193
|
].filter(toBoolean),
|
|
@@ -187,7 +210,7 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
187
210
|
if (extract) {
|
|
188
211
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
189
212
|
const metadata = result?.metadata as BabelFileMetadata;
|
|
190
|
-
asset.meta.styleRules = metadata.styleRules;
|
|
213
|
+
asset.meta.styleRules = [...(metadata.styleRules ?? []), ...distStyleRules];
|
|
191
214
|
}
|
|
192
215
|
|
|
193
216
|
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
|
/**
|
|
@@ -56,4 +57,9 @@ export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
|
56
57
|
* Default to `undefined`
|
|
57
58
|
*/
|
|
58
59
|
classNameCompressionMapFilePath?: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* When set, extract styles to an external CSS file
|
|
63
|
+
*/
|
|
64
|
+
extractStylesToDirectory?: { source: string; dest: string };
|
|
59
65
|
}
|