@parcel/transformer-css 2.4.0 → 2.6.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/lib/CSSTransformer.js +64 -50
- package/package.json +7 -7
- package/src/CSSTransformer.js +68 -50
package/lib/CSSTransformer.js
CHANGED
|
@@ -107,6 +107,18 @@ var _default = new (_plugin().Transformer)({
|
|
|
107
107
|
config,
|
|
108
108
|
options
|
|
109
109
|
}) {
|
|
110
|
+
// Normalize the asset's environment so that properties that only affect JS don't cause CSS to be duplicated.
|
|
111
|
+
// For example, with ESModule and CommonJS targets, only a single shared CSS bundle should be produced.
|
|
112
|
+
let env = asset.env;
|
|
113
|
+
asset.setEnvironment({
|
|
114
|
+
context: 'browser',
|
|
115
|
+
engines: {
|
|
116
|
+
browsers: asset.env.engines.browsers
|
|
117
|
+
},
|
|
118
|
+
shouldOptimize: asset.env.shouldOptimize,
|
|
119
|
+
shouldScopeHoist: asset.env.shouldScopeHoist,
|
|
120
|
+
sourceMap: asset.env.sourceMap
|
|
121
|
+
});
|
|
110
122
|
let [code, originalMap] = await Promise.all([asset.getBuffer(), asset.getMap()]);
|
|
111
123
|
let targets = getTargets(asset.env.engines.browsers);
|
|
112
124
|
let res;
|
|
@@ -119,12 +131,24 @@ var _default = new (_plugin().Transformer)({
|
|
|
119
131
|
targets
|
|
120
132
|
});
|
|
121
133
|
} else {
|
|
122
|
-
|
|
134
|
+
let cssModules = false;
|
|
135
|
+
|
|
136
|
+
if (asset.meta.type !== 'tag' && asset.meta.cssModulesCompiled == null) {
|
|
137
|
+
let cssModulesConfig = config === null || config === void 0 ? void 0 : config.cssModules;
|
|
138
|
+
|
|
139
|
+
if (asset.isSource && (typeof cssModulesConfig === 'boolean' || cssModulesConfig !== null && cssModulesConfig !== void 0 && cssModulesConfig.global) || /\.module\./.test(asset.filePath)) {
|
|
140
|
+
if (cssModulesConfig !== null && cssModulesConfig !== void 0 && cssModulesConfig.dashedIdents && !asset.isSource) {
|
|
141
|
+
cssModulesConfig.dashedIdents = false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
cssModules = cssModulesConfig !== null && cssModulesConfig !== void 0 ? cssModulesConfig : true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
123
147
|
|
|
124
148
|
res = (0, _css().transform)({
|
|
125
149
|
filename: _path().default.relative(options.projectRoot, asset.filePath),
|
|
126
150
|
code,
|
|
127
|
-
cssModules
|
|
151
|
+
cssModules,
|
|
128
152
|
analyzeDependencies: asset.meta.hasDependencies !== false,
|
|
129
153
|
sourceMap: !!asset.env.sourceMap,
|
|
130
154
|
drafts: config === null || config === void 0 ? void 0 : config.drafts,
|
|
@@ -173,7 +197,7 @@ var _default = new (_plugin().Transformer)({
|
|
|
173
197
|
loc = (0, _utils().remapSourceLocation)(loc, originalMap);
|
|
174
198
|
}
|
|
175
199
|
|
|
176
|
-
if (dep.type === 'import') {
|
|
200
|
+
if (dep.type === 'import' && !res.exports) {
|
|
177
201
|
asset.addDependency({
|
|
178
202
|
specifier: dep.url,
|
|
179
203
|
specifierType: 'url',
|
|
@@ -182,12 +206,7 @@ var _default = new (_plugin().Transformer)({
|
|
|
182
206
|
// For the glob resolver to distinguish between `@import` and other URL dependencies.
|
|
183
207
|
isCSSImport: true,
|
|
184
208
|
media: dep.media
|
|
185
|
-
}
|
|
186
|
-
symbols: new Map([['*', {
|
|
187
|
-
local: '*',
|
|
188
|
-
isWeak: true,
|
|
189
|
-
loc
|
|
190
|
-
}]])
|
|
209
|
+
}
|
|
191
210
|
});
|
|
192
211
|
} else if (dep.type === 'url') {
|
|
193
212
|
asset.addURLDependency(dep.url, {
|
|
@@ -207,7 +226,6 @@ var _default = new (_plugin().Transformer)({
|
|
|
207
226
|
asset.symbols.ensure();
|
|
208
227
|
asset.symbols.set('default', 'default');
|
|
209
228
|
let dependencies = new Map();
|
|
210
|
-
let selfReferences = new Set();
|
|
211
229
|
let locals = new Map();
|
|
212
230
|
let c = 0;
|
|
213
231
|
let depjs = '';
|
|
@@ -228,10 +246,6 @@ var _default = new (_plugin().Transformer)({
|
|
|
228
246
|
let e = exports[key];
|
|
229
247
|
let s = `module.exports[${JSON.stringify(key)}] = \`${e.name}`;
|
|
230
248
|
|
|
231
|
-
if (e.isReferenced) {
|
|
232
|
-
selfReferences.add(e.name);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
249
|
for (let ref of e.composes) {
|
|
236
250
|
s += ' ';
|
|
237
251
|
|
|
@@ -247,17 +261,19 @@ var _default = new (_plugin().Transformer)({
|
|
|
247
261
|
d = `dep_${c++}`;
|
|
248
262
|
depjs += `import * as ${d} from ${JSON.stringify(ref.specifier)};\n`;
|
|
249
263
|
dependencies.set(ref.specifier, d);
|
|
250
|
-
asset.addDependency({
|
|
251
|
-
specifier: ref.specifier,
|
|
252
|
-
specifierType: 'url'
|
|
253
|
-
});
|
|
254
264
|
}
|
|
255
265
|
|
|
256
266
|
s += '${' + `${d}[${JSON.stringify(ref.name)}]` + '}';
|
|
257
267
|
}
|
|
258
268
|
}
|
|
259
269
|
|
|
260
|
-
s += '`;\n';
|
|
270
|
+
s += '`;\n'; // If the export is referenced internally (e.g. used @keyframes), add a self-reference
|
|
271
|
+
// to the JS so the symbol is retained during tree-shaking.
|
|
272
|
+
|
|
273
|
+
if (e.isReferenced) {
|
|
274
|
+
s += `module.exports[${JSON.stringify(key)}];\n`;
|
|
275
|
+
}
|
|
276
|
+
|
|
261
277
|
js += s;
|
|
262
278
|
};
|
|
263
279
|
|
|
@@ -266,12 +282,33 @@ var _default = new (_plugin().Transformer)({
|
|
|
266
282
|
add(key);
|
|
267
283
|
}
|
|
268
284
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
285
|
+
if (res.dependencies) {
|
|
286
|
+
for (let dep of res.dependencies) {
|
|
287
|
+
if (dep.type === 'import') {
|
|
288
|
+
// TODO: Figure out how to treeshake this
|
|
289
|
+
let d = `dep_$${c++}`;
|
|
290
|
+
depjs += `import * as ${d} from ${JSON.stringify(dep.url)};\n`;
|
|
291
|
+
js += `for (let key in ${d}) { if (key in module.exports) module.exports[key] += ' ' + ${d}[key]; else module.exports[key] = ${d}[key]; }\n`;
|
|
292
|
+
asset.symbols.set('*', '*');
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (res.references != null) {
|
|
298
|
+
let references = res.references;
|
|
299
|
+
|
|
300
|
+
for (let symbol in references) {
|
|
301
|
+
let reference = references[symbol];
|
|
302
|
+
asset.addDependency({
|
|
303
|
+
specifier: reference.specifier,
|
|
304
|
+
specifierType: 'esm',
|
|
305
|
+
symbols: new Map([[reference.name, {
|
|
306
|
+
local: symbol,
|
|
307
|
+
isWeak: false,
|
|
308
|
+
loc: null
|
|
309
|
+
}]])
|
|
310
|
+
});
|
|
311
|
+
asset.meta.hasReferences = true;
|
|
275
312
|
}
|
|
276
313
|
}
|
|
277
314
|
|
|
@@ -279,33 +316,10 @@ var _default = new (_plugin().Transformer)({
|
|
|
279
316
|
type: 'js',
|
|
280
317
|
content: depjs + js,
|
|
281
318
|
dependencies: [],
|
|
282
|
-
env
|
|
319
|
+
env
|
|
283
320
|
});
|
|
321
|
+
}
|
|
284
322
|
|
|
285
|
-
if (selfReferences.size > 0) {
|
|
286
|
-
asset.addDependency({
|
|
287
|
-
specifier: `./${_path().default.basename(asset.filePath)}`,
|
|
288
|
-
specifierType: 'url',
|
|
289
|
-
symbols: new Map([...locals].filter(([local]) => selfReferences.has(local)).map(([local, exported]) => [exported, {
|
|
290
|
-
local,
|
|
291
|
-
isWeak: false,
|
|
292
|
-
loc: null
|
|
293
|
-
}]))
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
} // Normalize the asset's environment so that properties that only affect JS don't cause CSS to be duplicated.
|
|
297
|
-
// For example, with ESModule and CommonJS targets, only a single shared CSS bundle should be produced.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
asset.setEnvironment({
|
|
301
|
-
context: 'browser',
|
|
302
|
-
engines: {
|
|
303
|
-
browsers: asset.env.engines.browsers
|
|
304
|
-
},
|
|
305
|
-
shouldOptimize: asset.env.shouldOptimize,
|
|
306
|
-
shouldScopeHoist: asset.env.shouldScopeHoist,
|
|
307
|
-
sourceMap: asset.env.sourceMap
|
|
308
|
-
});
|
|
309
323
|
return assets;
|
|
310
324
|
}
|
|
311
325
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/transformer-css",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,16 +17,16 @@
|
|
|
17
17
|
"source": "src/CSSTransformer.js",
|
|
18
18
|
"engines": {
|
|
19
19
|
"node": ">= 12.0.0",
|
|
20
|
-
"parcel": "^2.
|
|
20
|
+
"parcel": "^2.6.0"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@parcel/css": "^1.
|
|
24
|
-
"@parcel/diagnostic": "2.
|
|
25
|
-
"@parcel/plugin": "2.
|
|
23
|
+
"@parcel/css": "^1.9.0",
|
|
24
|
+
"@parcel/diagnostic": "2.6.0",
|
|
25
|
+
"@parcel/plugin": "2.6.0",
|
|
26
26
|
"@parcel/source-map": "^2.0.0",
|
|
27
|
-
"@parcel/utils": "2.
|
|
27
|
+
"@parcel/utils": "2.6.0",
|
|
28
28
|
"browserslist": "^4.6.6",
|
|
29
29
|
"nullthrows": "^1.1.1"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "f2d0a3a27d6e493b23ddc2edbc8a4c0053ff34ab"
|
|
32
32
|
}
|
package/src/CSSTransformer.js
CHANGED
|
@@ -21,6 +21,19 @@ export default (new Transformer({
|
|
|
21
21
|
return conf?.contents;
|
|
22
22
|
},
|
|
23
23
|
async transform({asset, config, options}) {
|
|
24
|
+
// Normalize the asset's environment so that properties that only affect JS don't cause CSS to be duplicated.
|
|
25
|
+
// For example, with ESModule and CommonJS targets, only a single shared CSS bundle should be produced.
|
|
26
|
+
let env = asset.env;
|
|
27
|
+
asset.setEnvironment({
|
|
28
|
+
context: 'browser',
|
|
29
|
+
engines: {
|
|
30
|
+
browsers: asset.env.engines.browsers,
|
|
31
|
+
},
|
|
32
|
+
shouldOptimize: asset.env.shouldOptimize,
|
|
33
|
+
shouldScopeHoist: asset.env.shouldScopeHoist,
|
|
34
|
+
sourceMap: asset.env.sourceMap,
|
|
35
|
+
});
|
|
36
|
+
|
|
24
37
|
let [code, originalMap] = await Promise.all([
|
|
25
38
|
asset.getBuffer(),
|
|
26
39
|
asset.getMap(),
|
|
@@ -36,13 +49,30 @@ export default (new Transformer({
|
|
|
36
49
|
targets,
|
|
37
50
|
});
|
|
38
51
|
} else {
|
|
52
|
+
let cssModules = false;
|
|
53
|
+
if (
|
|
54
|
+
asset.meta.type !== 'tag' &&
|
|
55
|
+
asset.meta.cssModulesCompiled == null
|
|
56
|
+
) {
|
|
57
|
+
let cssModulesConfig = config?.cssModules;
|
|
58
|
+
if (
|
|
59
|
+
(asset.isSource &&
|
|
60
|
+
(typeof cssModulesConfig === 'boolean' ||
|
|
61
|
+
cssModulesConfig?.global)) ||
|
|
62
|
+
/\.module\./.test(asset.filePath)
|
|
63
|
+
) {
|
|
64
|
+
if (cssModulesConfig?.dashedIdents && !asset.isSource) {
|
|
65
|
+
cssModulesConfig.dashedIdents = false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
cssModules = cssModulesConfig ?? true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
39
72
|
res = transform({
|
|
40
73
|
filename: path.relative(options.projectRoot, asset.filePath),
|
|
41
74
|
code,
|
|
42
|
-
cssModules
|
|
43
|
-
config?.cssModules ??
|
|
44
|
-
(asset.meta.cssModulesCompiled !== true &&
|
|
45
|
-
/\.module\./.test(asset.filePath)),
|
|
75
|
+
cssModules,
|
|
46
76
|
analyzeDependencies: asset.meta.hasDependencies !== false,
|
|
47
77
|
sourceMap: !!asset.env.sourceMap,
|
|
48
78
|
drafts: config?.drafts,
|
|
@@ -94,7 +124,7 @@ export default (new Transformer({
|
|
|
94
124
|
loc = remapSourceLocation(loc, originalMap);
|
|
95
125
|
}
|
|
96
126
|
|
|
97
|
-
if (dep.type === 'import') {
|
|
127
|
+
if (dep.type === 'import' && !res.exports) {
|
|
98
128
|
asset.addDependency({
|
|
99
129
|
specifier: dep.url,
|
|
100
130
|
specifierType: 'url',
|
|
@@ -104,7 +134,6 @@ export default (new Transformer({
|
|
|
104
134
|
isCSSImport: true,
|
|
105
135
|
media: dep.media,
|
|
106
136
|
},
|
|
107
|
-
symbols: new Map([['*', {local: '*', isWeak: true, loc}]]),
|
|
108
137
|
});
|
|
109
138
|
} else if (dep.type === 'url') {
|
|
110
139
|
asset.addURLDependency(dep.url, {
|
|
@@ -125,7 +154,6 @@ export default (new Transformer({
|
|
|
125
154
|
asset.symbols.set('default', 'default');
|
|
126
155
|
|
|
127
156
|
let dependencies = new Map();
|
|
128
|
-
let selfReferences = new Set();
|
|
129
157
|
let locals = new Map();
|
|
130
158
|
let c = 0;
|
|
131
159
|
let depjs = '';
|
|
@@ -147,10 +175,6 @@ export default (new Transformer({
|
|
|
147
175
|
let e = exports[key];
|
|
148
176
|
let s = `module.exports[${JSON.stringify(key)}] = \`${e.name}`;
|
|
149
177
|
|
|
150
|
-
if (e.isReferenced) {
|
|
151
|
-
selfReferences.add(e.name);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
178
|
for (let ref of e.composes) {
|
|
155
179
|
s += ' ';
|
|
156
180
|
if (ref.type === 'local') {
|
|
@@ -171,17 +195,19 @@ export default (new Transformer({
|
|
|
171
195
|
ref.specifier,
|
|
172
196
|
)};\n`;
|
|
173
197
|
dependencies.set(ref.specifier, d);
|
|
174
|
-
|
|
175
|
-
asset.addDependency({
|
|
176
|
-
specifier: ref.specifier,
|
|
177
|
-
specifierType: 'url',
|
|
178
|
-
});
|
|
179
198
|
}
|
|
180
199
|
s += '${' + `${d}[${JSON.stringify(ref.name)}]` + '}';
|
|
181
200
|
}
|
|
182
201
|
}
|
|
183
202
|
|
|
184
203
|
s += '`;\n';
|
|
204
|
+
|
|
205
|
+
// If the export is referenced internally (e.g. used @keyframes), add a self-reference
|
|
206
|
+
// to the JS so the symbol is retained during tree-shaking.
|
|
207
|
+
if (e.isReferenced) {
|
|
208
|
+
s += `module.exports[${JSON.stringify(key)}];\n`;
|
|
209
|
+
}
|
|
210
|
+
|
|
185
211
|
js += s;
|
|
186
212
|
};
|
|
187
213
|
|
|
@@ -190,12 +216,31 @@ export default (new Transformer({
|
|
|
190
216
|
add(key);
|
|
191
217
|
}
|
|
192
218
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
219
|
+
if (res.dependencies) {
|
|
220
|
+
for (let dep of res.dependencies) {
|
|
221
|
+
if (dep.type === 'import') {
|
|
222
|
+
// TODO: Figure out how to treeshake this
|
|
223
|
+
let d = `dep_$${c++}`;
|
|
224
|
+
depjs += `import * as ${d} from ${JSON.stringify(dep.url)};\n`;
|
|
225
|
+
js += `for (let key in ${d}) { if (key in module.exports) module.exports[key] += ' ' + ${d}[key]; else module.exports[key] = ${d}[key]; }\n`;
|
|
226
|
+
asset.symbols.set('*', '*');
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (res.references != null) {
|
|
232
|
+
let references = res.references;
|
|
233
|
+
for (let symbol in references) {
|
|
234
|
+
let reference = references[symbol];
|
|
235
|
+
asset.addDependency({
|
|
236
|
+
specifier: reference.specifier,
|
|
237
|
+
specifierType: 'esm',
|
|
238
|
+
symbols: new Map([
|
|
239
|
+
[reference.name, {local: symbol, isWeak: false, loc: null}],
|
|
240
|
+
]),
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
asset.meta.hasReferences = true;
|
|
199
244
|
}
|
|
200
245
|
}
|
|
201
246
|
|
|
@@ -203,37 +248,10 @@ export default (new Transformer({
|
|
|
203
248
|
type: 'js',
|
|
204
249
|
content: depjs + js,
|
|
205
250
|
dependencies: jsDeps,
|
|
206
|
-
env
|
|
251
|
+
env,
|
|
207
252
|
});
|
|
208
|
-
|
|
209
|
-
if (selfReferences.size > 0) {
|
|
210
|
-
asset.addDependency({
|
|
211
|
-
specifier: `./${path.basename(asset.filePath)}`,
|
|
212
|
-
specifierType: 'url',
|
|
213
|
-
symbols: new Map(
|
|
214
|
-
[...locals]
|
|
215
|
-
.filter(([local]) => selfReferences.has(local))
|
|
216
|
-
.map(([local, exported]) => [
|
|
217
|
-
exported,
|
|
218
|
-
{local, isWeak: false, loc: null},
|
|
219
|
-
]),
|
|
220
|
-
),
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
253
|
}
|
|
224
254
|
|
|
225
|
-
// Normalize the asset's environment so that properties that only affect JS don't cause CSS to be duplicated.
|
|
226
|
-
// For example, with ESModule and CommonJS targets, only a single shared CSS bundle should be produced.
|
|
227
|
-
asset.setEnvironment({
|
|
228
|
-
context: 'browser',
|
|
229
|
-
engines: {
|
|
230
|
-
browsers: asset.env.engines.browsers,
|
|
231
|
-
},
|
|
232
|
-
shouldOptimize: asset.env.shouldOptimize,
|
|
233
|
-
shouldScopeHoist: asset.env.shouldScopeHoist,
|
|
234
|
-
sourceMap: asset.env.sourceMap,
|
|
235
|
-
});
|
|
236
|
-
|
|
237
255
|
return assets;
|
|
238
256
|
},
|
|
239
257
|
}): Transformer);
|