@atlaspack/transformer-css 2.14.5-canary.34 → 2.14.5-canary.340
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/CHANGELOG.md +378 -0
- package/dist/CSSTransformer.js +405 -0
- package/lib/CSSTransformer.js +60 -7
- package/lib/types/CSSTransformer.d.ts +3 -0
- package/package.json +15 -9
- package/src/{CSSTransformer.js → CSSTransformer.ts} +70 -18
- package/tsconfig.json +27 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type {SourceLocation} from '@atlaspack/types';
|
|
1
|
+
import type {MutableAsset, SourceLocation} from '@atlaspack/types';
|
|
4
2
|
|
|
5
3
|
import path from 'path';
|
|
6
|
-
import SourceMap from '@
|
|
4
|
+
import SourceMap from '@atlaspack/source-map';
|
|
7
5
|
import {Transformer} from '@atlaspack/plugin';
|
|
8
6
|
import {
|
|
9
7
|
remapSourceLocation,
|
|
@@ -11,37 +9,57 @@ import {
|
|
|
11
9
|
globToRegex,
|
|
12
10
|
normalizeSeparators,
|
|
13
11
|
} from '@atlaspack/utils';
|
|
14
|
-
import {
|
|
12
|
+
import {SourceLocation as LightningSourceLocation} from 'lightningcss';
|
|
15
13
|
import * as native from 'lightningcss';
|
|
16
14
|
import browserslist from 'browserslist';
|
|
17
15
|
import nullthrows from 'nullthrows';
|
|
18
16
|
import ThrowableDiagnostic, {errorToDiagnostic} from '@atlaspack/diagnostic';
|
|
17
|
+
import {getFeatureFlag} from '@atlaspack/feature-flags';
|
|
19
18
|
|
|
20
19
|
const {transform, transformStyleAttribute, browserslistToTargets} = native;
|
|
21
20
|
|
|
22
|
-
export default
|
|
21
|
+
export default new Transformer({
|
|
23
22
|
async loadConfig({config, options}) {
|
|
24
23
|
let conf = await config.getConfigFrom(options.projectRoot + '/index', [], {
|
|
25
24
|
packageKey: '@atlaspack/transformer-css',
|
|
26
25
|
});
|
|
27
26
|
let contents = conf?.contents;
|
|
27
|
+
// @ts-expect-error TS2339
|
|
28
28
|
if (typeof contents?.cssModules?.include === 'string') {
|
|
29
|
+
// @ts-expect-error TS2339
|
|
29
30
|
contents.cssModules.include = [globToRegex(contents.cssModules.include)];
|
|
31
|
+
// @ts-expect-error TS2339
|
|
30
32
|
} else if (Array.isArray(contents?.cssModules?.include)) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
// @ts-expect-error TS2339
|
|
34
|
+
contents.cssModules.include = contents.cssModules.include.map(
|
|
35
|
+
// @ts-expect-error TS7006
|
|
36
|
+
(include) =>
|
|
37
|
+
typeof include === 'string' ? globToRegex(include) : include,
|
|
33
38
|
);
|
|
34
39
|
}
|
|
40
|
+
// @ts-expect-error TS2339
|
|
35
41
|
if (typeof contents?.cssModules?.exclude === 'string') {
|
|
42
|
+
// @ts-expect-error TS2339
|
|
36
43
|
contents.cssModules.exclude = [globToRegex(contents.cssModules.exclude)];
|
|
44
|
+
// @ts-expect-error TS2339
|
|
37
45
|
} else if (Array.isArray(contents?.cssModules?.exclude)) {
|
|
38
|
-
|
|
39
|
-
|
|
46
|
+
// @ts-expect-error TS2339
|
|
47
|
+
contents.cssModules.exclude = contents.cssModules.exclude.map(
|
|
48
|
+
// @ts-expect-error TS7006
|
|
49
|
+
(exclude) =>
|
|
50
|
+
typeof exclude === 'string' ? globToRegex(exclude) : exclude,
|
|
40
51
|
);
|
|
41
52
|
}
|
|
42
53
|
return contents;
|
|
43
54
|
},
|
|
44
55
|
async transform({asset, config, options, logger}) {
|
|
56
|
+
if (
|
|
57
|
+
getFeatureFlag('compiledCssInJsTransformer') &&
|
|
58
|
+
asset.filePath.endsWith('.compiled.css')
|
|
59
|
+
) {
|
|
60
|
+
return handleCompiledCssAsset(asset);
|
|
61
|
+
}
|
|
62
|
+
|
|
45
63
|
// Normalize the asset's environment so that properties that only affect JS don't cause CSS to be duplicated.
|
|
46
64
|
// For example, with ESModule and CommonJS targets, only a single shared CSS bundle should be produced.
|
|
47
65
|
let env = asset.env;
|
|
@@ -53,12 +71,17 @@ export default (new Transformer({
|
|
|
53
71
|
shouldOptimize: asset.env.shouldOptimize,
|
|
54
72
|
shouldScopeHoist: asset.env.shouldScopeHoist,
|
|
55
73
|
sourceMap: asset.env.sourceMap,
|
|
74
|
+
unstableSingleFileOutput: getFeatureFlag(
|
|
75
|
+
'preserveUnstableSingleFileOutputInCss',
|
|
76
|
+
)
|
|
77
|
+
? asset.env.unstableSingleFileOutput
|
|
78
|
+
: undefined,
|
|
56
79
|
});
|
|
57
80
|
|
|
58
81
|
let [code, originalMap] = await Promise.all([
|
|
59
82
|
asset.getBuffer(),
|
|
60
83
|
asset.getMap(),
|
|
61
|
-
//
|
|
84
|
+
// @ts-expect-error TS2339
|
|
62
85
|
process.browser && native.default(),
|
|
63
86
|
]);
|
|
64
87
|
|
|
@@ -69,6 +92,7 @@ export default (new Transformer({
|
|
|
69
92
|
res = transformStyleAttribute({
|
|
70
93
|
code,
|
|
71
94
|
analyzeDependencies: true,
|
|
95
|
+
// @ts-expect-error TS2339
|
|
72
96
|
errorRecovery: config?.errorRecovery || false,
|
|
73
97
|
targets,
|
|
74
98
|
});
|
|
@@ -78,6 +102,7 @@ export default (new Transformer({
|
|
|
78
102
|
asset.meta.type !== 'tag' &&
|
|
79
103
|
asset.meta.cssModulesCompiled == null
|
|
80
104
|
) {
|
|
105
|
+
// @ts-expect-error TS2339
|
|
81
106
|
let cssModulesConfig = config?.cssModules;
|
|
82
107
|
let isCSSModule = /\.module\./.test(asset.filePath);
|
|
83
108
|
if (asset.isSource) {
|
|
@@ -88,6 +113,7 @@ export default (new Transformer({
|
|
|
88
113
|
if (typeof cssModulesConfig === 'boolean') {
|
|
89
114
|
isCSSModule = true;
|
|
90
115
|
} else if (cssModulesConfig?.include) {
|
|
116
|
+
// @ts-expect-error TS7006
|
|
91
117
|
isCSSModule = cssModulesConfig.include.some((include) =>
|
|
92
118
|
include.test(projectRootPath),
|
|
93
119
|
);
|
|
@@ -96,7 +122,7 @@ export default (new Transformer({
|
|
|
96
122
|
}
|
|
97
123
|
|
|
98
124
|
if (
|
|
99
|
-
cssModulesConfig?.exclude?.some((exclude) =>
|
|
125
|
+
cssModulesConfig?.exclude?.some((exclude: any) =>
|
|
100
126
|
exclude.test(projectRootPath),
|
|
101
127
|
)
|
|
102
128
|
) {
|
|
@@ -126,13 +152,16 @@ export default (new Transformer({
|
|
|
126
152
|
}
|
|
127
153
|
: false,
|
|
128
154
|
sourceMap: !!asset.env.sourceMap,
|
|
155
|
+
// @ts-expect-error TS2339
|
|
129
156
|
drafts: config?.drafts,
|
|
157
|
+
// @ts-expect-error TS2339
|
|
130
158
|
pseudoClasses: config?.pseudoClasses,
|
|
159
|
+
// @ts-expect-error TS2339
|
|
131
160
|
errorRecovery: config?.errorRecovery || false,
|
|
132
161
|
targets,
|
|
133
162
|
});
|
|
134
163
|
}
|
|
135
|
-
} catch (err) {
|
|
164
|
+
} catch (err: any) {
|
|
136
165
|
err.filePath = asset.filePath;
|
|
137
166
|
let diagnostic = errorToDiagnostic(err, {
|
|
138
167
|
origin: '@atlaspack/transformer-css',
|
|
@@ -180,7 +209,9 @@ export default (new Transformer({
|
|
|
180
209
|
}
|
|
181
210
|
}
|
|
182
211
|
|
|
212
|
+
// @ts-expect-error TS2339
|
|
183
213
|
if (res.map != null) {
|
|
214
|
+
// @ts-expect-error TS2339
|
|
184
215
|
let vlqMap = JSON.parse(Buffer.from(res.map).toString());
|
|
185
216
|
let map = new SourceMap(options.projectRoot);
|
|
186
217
|
map.addVLQMap(vlqMap);
|
|
@@ -196,9 +227,10 @@ export default (new Transformer({
|
|
|
196
227
|
for (let dep of res.dependencies) {
|
|
197
228
|
let loc = convertLoc(dep.loc);
|
|
198
229
|
if (originalMap) {
|
|
199
|
-
loc = remapSourceLocation(loc, originalMap);
|
|
230
|
+
loc = remapSourceLocation(loc, originalMap, options.projectRoot);
|
|
200
231
|
}
|
|
201
232
|
|
|
233
|
+
// @ts-expect-error TS2339
|
|
202
234
|
if (dep.type === 'import' && !res.exports) {
|
|
203
235
|
asset.addDependency({
|
|
204
236
|
specifier: dep.url,
|
|
@@ -226,7 +258,9 @@ export default (new Transformer({
|
|
|
226
258
|
let assets = [asset];
|
|
227
259
|
let buffer = Buffer.from(res.code);
|
|
228
260
|
|
|
261
|
+
// @ts-expect-error TS2339
|
|
229
262
|
if (res.exports != null) {
|
|
263
|
+
// @ts-expect-error TS2339
|
|
230
264
|
let exports = res.exports;
|
|
231
265
|
asset.symbols.ensure();
|
|
232
266
|
asset.symbols.set('default', 'default');
|
|
@@ -238,7 +272,7 @@ export default (new Transformer({
|
|
|
238
272
|
let js = '';
|
|
239
273
|
let cssImports = '';
|
|
240
274
|
|
|
241
|
-
let jsDeps = [];
|
|
275
|
+
let jsDeps: Array<never> = [];
|
|
242
276
|
|
|
243
277
|
for (let key in exports) {
|
|
244
278
|
locals.set(exports[key].name, key);
|
|
@@ -247,7 +281,7 @@ export default (new Transformer({
|
|
|
247
281
|
asset.uniqueKey ??= asset.id;
|
|
248
282
|
|
|
249
283
|
let seen = new Set();
|
|
250
|
-
let add = (key) => {
|
|
284
|
+
let add = (key: string) => {
|
|
251
285
|
if (seen.has(key)) {
|
|
252
286
|
return;
|
|
253
287
|
}
|
|
@@ -299,6 +333,7 @@ export default (new Transformer({
|
|
|
299
333
|
asset.addDependency({
|
|
300
334
|
specifier: nullthrows(asset.uniqueKey),
|
|
301
335
|
specifierType: 'esm',
|
|
336
|
+
|
|
302
337
|
symbols: new Map([
|
|
303
338
|
[key, {local: exports[key].name, isWeak: false, loc: null}],
|
|
304
339
|
]),
|
|
@@ -322,12 +357,15 @@ export default (new Transformer({
|
|
|
322
357
|
let d = `dep_$${c++}`;
|
|
323
358
|
depjs += `import * as ${d} from ${JSON.stringify(dep.url)};\n`;
|
|
324
359
|
js += `for (let key in ${d}) { if (key in module.exports) module.exports[key] += ' ' + ${d}[key]; else module.exports[key] = ${d}[key]; }\n`;
|
|
360
|
+
|
|
325
361
|
asset.symbols.set('*', '*');
|
|
326
362
|
}
|
|
327
363
|
}
|
|
328
364
|
}
|
|
329
365
|
|
|
366
|
+
// @ts-expect-error TS2339
|
|
330
367
|
if (res.references != null) {
|
|
368
|
+
// @ts-expect-error TS2339
|
|
331
369
|
let references = res.references;
|
|
332
370
|
for (let symbol in references) {
|
|
333
371
|
let reference = references[symbol];
|
|
@@ -347,6 +385,7 @@ export default (new Transformer({
|
|
|
347
385
|
|
|
348
386
|
assets.push({
|
|
349
387
|
type: 'js',
|
|
388
|
+
// @ts-expect-error TS2353
|
|
350
389
|
content: depjs + js,
|
|
351
390
|
dependencies: jsDeps,
|
|
352
391
|
env,
|
|
@@ -361,11 +400,11 @@ export default (new Transformer({
|
|
|
361
400
|
asset.setBuffer(buffer);
|
|
362
401
|
return assets;
|
|
363
402
|
},
|
|
364
|
-
})
|
|
403
|
+
}) as Transformer<unknown>;
|
|
365
404
|
|
|
366
405
|
let cache = new Map();
|
|
367
406
|
|
|
368
|
-
function getTargets(browsers) {
|
|
407
|
+
function getTargets(browsers: undefined | string | Array<string>) {
|
|
369
408
|
if (browsers == null) {
|
|
370
409
|
return undefined;
|
|
371
410
|
}
|
|
@@ -388,3 +427,16 @@ function convertLoc(loc: LightningSourceLocation): SourceLocation {
|
|
|
388
427
|
end: {line: loc.end.line, column: loc.end.column + 1},
|
|
389
428
|
};
|
|
390
429
|
}
|
|
430
|
+
|
|
431
|
+
async function handleCompiledCssAsset(asset: MutableAsset) {
|
|
432
|
+
const code = await asset.getCode();
|
|
433
|
+
|
|
434
|
+
const rules: Array<string> = code.split('\n').map((line) => line.trim());
|
|
435
|
+
// styleRules will be consumed by the optimiser in @compiled/parcel-optimizer
|
|
436
|
+
asset.meta.styleRules = rules;
|
|
437
|
+
|
|
438
|
+
// Empty the code because we only use compiled CSS assets for their metadata
|
|
439
|
+
asset.setCode('');
|
|
440
|
+
|
|
441
|
+
return [asset];
|
|
442
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"include": ["src"],
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"composite": true
|
|
6
|
+
},
|
|
7
|
+
"references": [
|
|
8
|
+
{
|
|
9
|
+
"path": "../../core/diagnostic/tsconfig.json"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"path": "../../core/feature-flags/tsconfig.json"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"path": "../../core/plugin/tsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "../../core/types/tsconfig.json"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"path": "../../core/utils/tsconfig.json"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"path": "../../core/source-map/tsconfig.json"
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|