@griffel/transform 2.0.2 → 3.0.1
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/LICENSE.md +21 -0
- package/package.json +9 -6
- package/src/constants.mjs +3 -0
- package/src/constants.mjs.map +1 -0
- package/{evaluation → src/evaluation}/astEvaluator.d.mts +2 -2
- package/src/evaluation/astEvaluator.mjs +90 -0
- package/src/evaluation/astEvaluator.mjs.map +1 -0
- package/{evaluation → src/evaluation}/batchEvaluator.d.mts +4 -4
- package/src/evaluation/batchEvaluator.mjs +54 -0
- package/src/evaluation/batchEvaluator.mjs.map +1 -0
- package/src/evaluation/evalCache.mjs +65 -0
- package/src/evaluation/evalCache.mjs.map +1 -0
- package/{evaluation → src/evaluation}/fluentTokensPlugin.d.mts +1 -1
- package/src/evaluation/fluentTokensPlugin.mjs +70 -0
- package/src/evaluation/fluentTokensPlugin.mjs.map +1 -0
- package/{evaluation → src/evaluation}/module.d.mts +13 -1
- package/src/evaluation/module.mjs +207 -0
- package/src/evaluation/module.mjs.map +1 -0
- package/src/evaluation/process.mjs +28 -0
- package/src/evaluation/process.mjs.map +1 -0
- package/{evaluation → src/evaluation}/types.d.mts +1 -1
- package/src/evaluation/types.mjs +2 -0
- package/src/evaluation/types.mjs.map +1 -0
- package/{evaluation → src/evaluation}/vmEvaluator.d.mts +2 -2
- package/src/evaluation/vmEvaluator.mjs +33 -0
- package/src/evaluation/vmEvaluator.mjs.map +1 -0
- package/src/index.mjs +9 -0
- package/src/index.mjs.map +1 -0
- package/{transformSync.d.mts → src/transformSync.d.mts} +9 -4
- package/src/transformSync.mjs +252 -0
- package/src/transformSync.mjs.map +1 -0
- package/{types.d.mts → src/types.d.mts} +1 -1
- package/src/types.mjs +2 -0
- package/src/types.mjs.map +1 -0
- package/src/utils/convertESMtoCJS.mjs +203 -0
- package/src/utils/convertESMtoCJS.mjs.map +1 -0
- package/{utils → src/utils}/dedupeCSSRules.d.mts +1 -1
- package/src/utils/dedupeCSSRules.mjs +19 -0
- package/src/utils/dedupeCSSRules.mjs.map +1 -0
- package/CHANGELOG.md +0 -67
- package/transform.js +0 -916
- /package/{constants.d.mts → src/constants.d.mts} +0 -0
- /package/{evaluation → src/evaluation}/evalCache.d.mts +0 -0
- /package/{evaluation → src/evaluation}/process.d.mts +0 -0
- /package/{index.d.mts → src/index.d.mts} +0 -0
- /package/{utils → src/utils}/convertESMtoCJS.d.mts +0 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocked `process` variable for the VM sandbox used in module evaluation.
|
|
3
|
+
* Copied from @linaria/babel-preset v3.0.0-beta.19.
|
|
4
|
+
*/
|
|
5
|
+
export const nextTick = (fn) => setTimeout(fn, 0);
|
|
6
|
+
export const platform = 'browser';
|
|
7
|
+
export const arch = 'browser';
|
|
8
|
+
export const execPath = 'browser';
|
|
9
|
+
export const title = 'browser';
|
|
10
|
+
export const pid = 1;
|
|
11
|
+
export const browser = true;
|
|
12
|
+
export const argv = [];
|
|
13
|
+
export const binding = function binding() {
|
|
14
|
+
throw new Error('No such module. (Possibly not yet loaded)');
|
|
15
|
+
};
|
|
16
|
+
export const cwd = () => '/';
|
|
17
|
+
const noop = () => { };
|
|
18
|
+
export const exit = noop;
|
|
19
|
+
export const kill = noop;
|
|
20
|
+
export const chdir = noop;
|
|
21
|
+
export const umask = noop;
|
|
22
|
+
export const dlopen = noop;
|
|
23
|
+
export const uptime = noop;
|
|
24
|
+
export const memoryUsage = noop;
|
|
25
|
+
export const uvCounters = noop;
|
|
26
|
+
export const features = {};
|
|
27
|
+
export const env = process.env;
|
|
28
|
+
//# sourceMappingURL=process.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.mjs","sourceRoot":"","sources":["../../../../../packages/transform/src/evaluation/process.mts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,EAAgC,EAAiC,EAA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types.mts"],"names":[],"mappings":""}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { TransformResolver } from './module.mjs';
|
|
2
|
-
import { EvalRule, EvaluationResult } from './types.mjs';
|
|
1
|
+
import type { TransformResolver } from './module.mjs';
|
|
2
|
+
import type { EvalRule, EvaluationResult } from './types.mjs';
|
|
3
3
|
export declare function vmEvaluator(sourceCode: string, filename: string, expressionCode: string, evaluationRules: EvalRule[], resolveFilename: TransformResolver): EvaluationResult;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Module } from './module.mjs';
|
|
2
|
+
function isError(e) {
|
|
3
|
+
return Object.prototype.toString.call(e) === '[object Error]';
|
|
4
|
+
}
|
|
5
|
+
export function vmEvaluator(sourceCode, filename, expressionCode, evaluationRules, resolveFilename) {
|
|
6
|
+
// Create a simple wrapper program for evaluation
|
|
7
|
+
const codeForEvaluation = `
|
|
8
|
+
${sourceCode}
|
|
9
|
+
|
|
10
|
+
export const __mkPreval = (() => {
|
|
11
|
+
try {
|
|
12
|
+
return ([${expressionCode}]);
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return e;
|
|
15
|
+
}
|
|
16
|
+
})();
|
|
17
|
+
`;
|
|
18
|
+
try {
|
|
19
|
+
const mod = new Module(filename, evaluationRules, resolveFilename);
|
|
20
|
+
// useEvalCache=false: each source file produces unique codeForEvaluation (different sourceCode +
|
|
21
|
+
// expressionCode), so the eval cache would never hit — skip the unnecessary hashing and map storage.
|
|
22
|
+
mod.evaluate(codeForEvaluation, ['__mkPreval'], /* useEvalCache */ false);
|
|
23
|
+
const result = mod.exports.__mkPreval;
|
|
24
|
+
if (isError(result)) {
|
|
25
|
+
return { confident: false, error: result };
|
|
26
|
+
}
|
|
27
|
+
return { confident: true, value: result };
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
return { confident: false, error: err };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=vmEvaluator.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vmEvaluator.mjs","sourceRoot":"","sources":["../../../../../packages/transform/src/evaluation/vmEvaluator.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAItC,SAAS,OAAO,CAAC,CAAU;IACzB,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,gBAAgB,CAAC;AAChE,CAAC;AAED,MAAM,UAAU,WAAW,CACzB,UAAkB,EAClB,QAAgB,EAChB,cAAsB,EACtB,eAA2B,EAC3B,eAAkC;IAElC,iDAAiD;IACjD,MAAM,iBAAiB,GAAG;EAC1B,UAAU;;;;eAIG,cAAc;;;;;CAK5B,CAAC;IAEA,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,QAAQ,EAAE,eAAe,EAAE,eAAe,CAAC,CAAC;QACnE,iGAAiG;QACjG,qGAAqG;QACrG,GAAG,CAAC,QAAQ,CAAC,iBAAiB,EAAE,CAAC,YAAY,CAAC,EAAE,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAE1E,MAAM,MAAM,GAAI,GAAG,CAAC,OAAmC,CAAC,UAAU,CAAC;QAEnE,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YACpB,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAC7C,CAAC;QAED,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAC5C,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,GAAY,EAAE,CAAC;IACnD,CAAC;AACH,CAAC"}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as shakerEvaluator } from '@griffel/transform-shaker';
|
|
2
|
+
export { Module } from './evaluation/module.mjs';
|
|
3
|
+
export * as EvalCache from './evaluation/evalCache.mjs';
|
|
4
|
+
export { ASSET_TAG_OPEN, ASSET_TAG_CLOSE } from './constants.mjs';
|
|
5
|
+
// Our APIs
|
|
6
|
+
export { transformSync } from './transformSync.mjs';
|
|
7
|
+
export { DEOPT } from './evaluation/astEvaluator.mjs';
|
|
8
|
+
export { fluentTokensPlugin } from './evaluation/fluentTokensPlugin.mjs';
|
|
9
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../../packages/transform/src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,EAAE,MAAM,EAA0B,MAAM,yBAAyB,CAAC;AACzE,OAAO,KAAK,SAAS,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAIlE,WAAW;AAEX,OAAO,EAAE,aAAa,EAA+C,MAAM,qBAAqB,CAAC;AACjG,OAAO,EAAE,KAAK,EAAc,MAAM,+BAA+B,CAAC;AAElE,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { TransformResolver } from './evaluation/module.mjs';
|
|
2
|
-
import { EvalRule, TransformPerfIssue
|
|
3
|
-
import { CSSRulesByBucket } from '@griffel/core';
|
|
1
|
+
import type { TransformResolver } from './evaluation/module.mjs';
|
|
2
|
+
import type { EvalRule, TransformPerfIssue } from './evaluation/types.mjs';
|
|
3
|
+
import { type CSSRulesByBucket } from '@griffel/core';
|
|
4
|
+
import type { AstEvaluatorPlugin } from './evaluation/types.mjs';
|
|
4
5
|
export type TransformOptions = {
|
|
5
6
|
filename: string;
|
|
6
7
|
/** Custom module resolver used to resolve imports inside evaluated modules. */
|
|
@@ -12,7 +13,9 @@ export type TransformOptions = {
|
|
|
12
13
|
*/
|
|
13
14
|
generateMetadata?: boolean;
|
|
14
15
|
/** Defines set of modules and imports handled by a transformPlugin. */
|
|
15
|
-
|
|
16
|
+
importsToTransform?: string[];
|
|
17
|
+
/** Defines the set of function names that should be treated as Griffel style calls. */
|
|
18
|
+
functionsToTransform?: FunctionKinds[];
|
|
16
19
|
/** The set of rules that defines how the matched files will be transformed during the evaluation. */
|
|
17
20
|
evaluationRules?: EvalRule[];
|
|
18
21
|
/** Plugins for extending AST evaluation with custom node handling. */
|
|
@@ -30,7 +33,9 @@ export type TransformResult = {
|
|
|
30
33
|
usedVMForEvaluation: boolean;
|
|
31
34
|
perfIssues?: TransformPerfIssue[];
|
|
32
35
|
};
|
|
36
|
+
type FunctionKinds = 'makeStyles' | 'makeResetStyles' | 'makeStaticStyles';
|
|
33
37
|
/**
|
|
34
38
|
* Transforms passed source code with oxc-parser and oxc-walker instead of Babel.
|
|
35
39
|
*/
|
|
36
40
|
export declare function transformSync(sourceCode: string, options: TransformOptions): TransformResult;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { parseSync } from 'oxc-parser';
|
|
2
|
+
import { walk, ScopeTracker } from 'oxc-walker';
|
|
3
|
+
import MagicString from 'magic-string';
|
|
4
|
+
import shakerEvaluator from '@griffel/transform-shaker';
|
|
5
|
+
import { resolveStyleRulesForSlots, resolveResetStyleRules, resolveStaticStyleRules, normalizeCSSBucketEntry, } from '@griffel/core';
|
|
6
|
+
import { batchEvaluator } from './evaluation/batchEvaluator.mjs';
|
|
7
|
+
import { fluentTokensPlugin } from './evaluation/fluentTokensPlugin.mjs';
|
|
8
|
+
import { dedupeCSSRules } from './utils/dedupeCSSRules.mjs';
|
|
9
|
+
const EXPORT_STAR_RE = /export\s+\*\s+from\s/;
|
|
10
|
+
function wrapWithPerfIssues(evaluator, perfIssues) {
|
|
11
|
+
return (filename, text, only) => {
|
|
12
|
+
const result = evaluator(filename, text, only);
|
|
13
|
+
if (result.moduleKind === 'cjs') {
|
|
14
|
+
perfIssues.push({ type: 'cjs-module', dependencyFilename: filename });
|
|
15
|
+
}
|
|
16
|
+
else if (EXPORT_STAR_RE.test(result.code)) {
|
|
17
|
+
perfIssues.push({ type: 'barrel-export-star', dependencyFilename: filename });
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const RUNTIME_IDENTIFIERS = new Map([
|
|
23
|
+
['makeStyles', '__css'],
|
|
24
|
+
['makeResetStyles', '__resetCSS'],
|
|
25
|
+
['makeStaticStyles', '__staticCSS'],
|
|
26
|
+
]);
|
|
27
|
+
/**
|
|
28
|
+
* Extracts CSS rules from evaluated reset styles to metadata
|
|
29
|
+
*/
|
|
30
|
+
function buildCSSResetEntriesMetadata(cssResetEntries, cssRules, declaratorId) {
|
|
31
|
+
const cssRulesByBucket = Array.isArray(cssRules) ? { d: cssRules } : cssRules;
|
|
32
|
+
cssResetEntries[declaratorId] ??= [];
|
|
33
|
+
cssResetEntries[declaratorId] = Object.values(cssRulesByBucket).flatMap(bucketEntries => {
|
|
34
|
+
return bucketEntries.map(bucketEntry => {
|
|
35
|
+
if (Array.isArray(bucketEntry)) {
|
|
36
|
+
throw new Error(`CSS rules in buckets for "makeResetStyles()" should not contain arrays, got: ${JSON.stringify(bucketEntry)})}`);
|
|
37
|
+
}
|
|
38
|
+
return bucketEntry;
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Extracts CSS rules from evaluated styles to metadata
|
|
44
|
+
*/
|
|
45
|
+
function buildCSSEntriesMetadata(cssEntries, classnamesMapping, cssRulesByBucket, declaratorId) {
|
|
46
|
+
const classesBySlot = Object.fromEntries(Object.entries(classnamesMapping).map(([slot, cssClassesMap]) => {
|
|
47
|
+
const uniqueClasses = new Set();
|
|
48
|
+
Object.values(cssClassesMap).forEach(cssClasses => {
|
|
49
|
+
if (typeof cssClasses === 'string') {
|
|
50
|
+
uniqueClasses.add(cssClasses);
|
|
51
|
+
}
|
|
52
|
+
else if (Array.isArray(cssClasses)) {
|
|
53
|
+
cssClasses.forEach(cssClass => uniqueClasses.add(cssClass));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return [slot, Array.from(uniqueClasses)];
|
|
57
|
+
}));
|
|
58
|
+
const cssRules = Object.values(cssRulesByBucket).flatMap(cssRulesByBucket => {
|
|
59
|
+
return cssRulesByBucket.map(bucketEntry => {
|
|
60
|
+
const [cssRule] = normalizeCSSBucketEntry(bucketEntry);
|
|
61
|
+
return cssRule;
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
cssEntries[declaratorId] = Object.fromEntries(Object.entries(classesBySlot).map(([slot, cssClasses]) => {
|
|
65
|
+
return [
|
|
66
|
+
slot,
|
|
67
|
+
cssClasses.map(cssClass => {
|
|
68
|
+
return cssRules.find(rule => rule.includes(cssClass));
|
|
69
|
+
}),
|
|
70
|
+
];
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
function concatCSSRulesByBucket(bucketA = {}, bucketB) {
|
|
74
|
+
for (const cssBucketName in bucketB) {
|
|
75
|
+
const bucketName = cssBucketName;
|
|
76
|
+
const bucketBEntries = bucketB[bucketName] ?? [];
|
|
77
|
+
if (bucketA[bucketName]) {
|
|
78
|
+
bucketA[bucketName].push(...bucketBEntries);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
bucketA[bucketName] = bucketBEntries;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return bucketA;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Transforms passed source code with oxc-parser and oxc-walker instead of Babel.
|
|
88
|
+
*/
|
|
89
|
+
export function transformSync(sourceCode, options) {
|
|
90
|
+
const perfIssues = options.collectPerfIssues ? [] : undefined;
|
|
91
|
+
const { filename, resolveModule, classNameHashSalt = '', generateMetadata = false, importsToTransform = ['@griffel/core', '@griffel/react', '@fluentui/react-components'], functionsToTransform = ['makeStyles', 'makeResetStyles', 'makeStaticStyles'], evaluationRules = [{ action: perfIssues ? wrapWithPerfIssues(shakerEvaluator, perfIssues) : shakerEvaluator }], astEvaluationPlugins = [fluentTokensPlugin], } = options;
|
|
92
|
+
const importsToTransformSet = new Set(importsToTransform);
|
|
93
|
+
const functionsToTransformSet = new Set(functionsToTransform);
|
|
94
|
+
if (!filename) {
|
|
95
|
+
throw new Error('Transform error: "filename" option is required');
|
|
96
|
+
}
|
|
97
|
+
const parseResult = parseSync(filename, sourceCode);
|
|
98
|
+
if (parseResult.errors.length > 0) {
|
|
99
|
+
throw new Error(`Failed to parse "${filename}": ${parseResult.errors.map(e => e.message).join(', ')}`);
|
|
100
|
+
}
|
|
101
|
+
if (parseResult.program.body.length > 0 && !parseResult.module.hasModuleSyntax) {
|
|
102
|
+
throw new Error(`Transform error: "${filename}" is not an ES module. ` +
|
|
103
|
+
`@griffel/transform only supports ES modules (files using import/export syntax).`);
|
|
104
|
+
}
|
|
105
|
+
const magicString = new MagicString(sourceCode);
|
|
106
|
+
const programAst = parseResult.program;
|
|
107
|
+
// Quick bail-out: if no Griffel imports exist, skip the AST walk entirely
|
|
108
|
+
const hasGriffelImports = parseResult.module.staticImports.some(si => importsToTransformSet.has(si.moduleRequest.value) &&
|
|
109
|
+
si.entries.some(e => e.importName.kind === 'Name' && functionsToTransformSet.has(e.importName.name)));
|
|
110
|
+
if (!hasGriffelImports) {
|
|
111
|
+
return { code: sourceCode, usedProcessing: false, usedVMForEvaluation: false };
|
|
112
|
+
}
|
|
113
|
+
const styleCalls = [];
|
|
114
|
+
const cssEntries = {};
|
|
115
|
+
const cssResetEntries = {};
|
|
116
|
+
let cssRulesByBucket = {};
|
|
117
|
+
// -----
|
|
118
|
+
// Walk AST to collect style function calls using ScopeTracker for scope-aware import resolution
|
|
119
|
+
const scopeTracker = new ScopeTracker();
|
|
120
|
+
const matchedSpecifiers = new Map();
|
|
121
|
+
walk(programAst, {
|
|
122
|
+
scopeTracker,
|
|
123
|
+
enter(node, parent) {
|
|
124
|
+
if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
|
|
125
|
+
const declaration = scopeTracker.getDeclaration(node.callee.name);
|
|
126
|
+
if (declaration?.type !== 'Import' || declaration.node.type !== 'ImportSpecifier') {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const importSource = declaration.importNode.source.value;
|
|
130
|
+
if (!importsToTransformSet.has(importSource)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const imported = declaration.node.imported;
|
|
134
|
+
if (imported.type !== 'Identifier') {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const importedName = imported.name;
|
|
138
|
+
if (!functionsToTransformSet.has(importedName)) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const functionKind = importedName;
|
|
142
|
+
if (node.arguments.length !== 1) {
|
|
143
|
+
throw new Error(`${functionKind}() function accepts only a single param, got ${node.arguments.length} in ${filename}`);
|
|
144
|
+
}
|
|
145
|
+
// Track the import specifier for rewriting (deduped by node start position)
|
|
146
|
+
matchedSpecifiers.set(declaration.node.start, {
|
|
147
|
+
start: declaration.node.start,
|
|
148
|
+
end: declaration.node.end,
|
|
149
|
+
functionKind,
|
|
150
|
+
});
|
|
151
|
+
// Find the variable declarator to get the hook name
|
|
152
|
+
let declaratorId = 'unknownHook';
|
|
153
|
+
let current = parent;
|
|
154
|
+
while (current) {
|
|
155
|
+
if (!current) {
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
if (current.type === 'VariableDeclarator' && current.id.type === 'Identifier') {
|
|
159
|
+
declaratorId = current.id.name;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
if ('parent' in current) {
|
|
163
|
+
current = current.parent;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
const argument = node.arguments[0];
|
|
169
|
+
styleCalls.push({
|
|
170
|
+
declaratorId,
|
|
171
|
+
functionKind,
|
|
172
|
+
argumentStart: argument.start,
|
|
173
|
+
argumentEnd: argument.end,
|
|
174
|
+
argumentCode: sourceCode.slice(argument.start, argument.end),
|
|
175
|
+
argumentNode: argument,
|
|
176
|
+
callStart: node.start,
|
|
177
|
+
callEnd: node.end,
|
|
178
|
+
importId: node.callee.name,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
// If no style calls found, return original code
|
|
184
|
+
if (styleCalls.length === 0) {
|
|
185
|
+
return {
|
|
186
|
+
code: sourceCode,
|
|
187
|
+
usedProcessing: false,
|
|
188
|
+
usedVMForEvaluation: false,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
// Process style calls - evaluate and transform
|
|
192
|
+
const { evaluationResults, usedVMForEvaluation } = batchEvaluator(sourceCode, filename, styleCalls, evaluationRules, resolveModule, programAst, astEvaluationPlugins);
|
|
193
|
+
for (let i = styleCalls.length - 1; i >= 0; i--) {
|
|
194
|
+
const styleCall = styleCalls[i];
|
|
195
|
+
const evaluationResult = evaluationResults[i];
|
|
196
|
+
switch (styleCall.functionKind) {
|
|
197
|
+
case 'makeStyles':
|
|
198
|
+
{
|
|
199
|
+
const stylesBySlots = evaluationResult;
|
|
200
|
+
const [classnamesMapping, resolvedCSSRules] = resolveStyleRulesForSlots(stylesBySlots, classNameHashSalt);
|
|
201
|
+
const uniqueCSSRules = dedupeCSSRules(cssRulesByBucket);
|
|
202
|
+
if (generateMetadata) {
|
|
203
|
+
buildCSSEntriesMetadata(cssEntries, classnamesMapping, uniqueCSSRules, styleCall.declaratorId);
|
|
204
|
+
}
|
|
205
|
+
// Replace the function call arguments
|
|
206
|
+
magicString.overwrite(styleCall.argumentStart, styleCall.argumentEnd, `${JSON.stringify(classnamesMapping)}`);
|
|
207
|
+
cssRulesByBucket = concatCSSRulesByBucket(cssRulesByBucket, resolvedCSSRules);
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
case 'makeResetStyles':
|
|
211
|
+
{
|
|
212
|
+
const styles = evaluationResult;
|
|
213
|
+
const [ltrClassName, rtlClassName, cssRules] = resolveResetStyleRules(styles, classNameHashSalt);
|
|
214
|
+
if (generateMetadata) {
|
|
215
|
+
buildCSSResetEntriesMetadata(cssResetEntries, cssRules, styleCall.declaratorId);
|
|
216
|
+
}
|
|
217
|
+
// Replace the function call arguments
|
|
218
|
+
magicString.overwrite(styleCall.argumentStart, styleCall.argumentEnd, `${JSON.stringify(ltrClassName)}, ${JSON.stringify(rtlClassName)}`);
|
|
219
|
+
cssRulesByBucket = concatCSSRulesByBucket(cssRulesByBucket, Array.isArray(cssRules) ? { r: cssRules } : cssRules);
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
case 'makeStaticStyles':
|
|
223
|
+
{
|
|
224
|
+
const styles = evaluationResult;
|
|
225
|
+
const stylesSet = Array.isArray(styles) ? styles : [styles];
|
|
226
|
+
const cssRules = resolveStaticStyleRules(stylesSet);
|
|
227
|
+
// Replace the function call arguments with the resolved CSS rules bucket
|
|
228
|
+
magicString.overwrite(styleCall.argumentStart, styleCall.argumentEnd, JSON.stringify({ d: cssRules }));
|
|
229
|
+
cssRulesByBucket = concatCSSRulesByBucket(cssRulesByBucket, { d: cssRules });
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// ---
|
|
235
|
+
// Transform imports and function names
|
|
236
|
+
for (const specifier of matchedSpecifiers.values()) {
|
|
237
|
+
magicString.overwrite(specifier.start, specifier.end, RUNTIME_IDENTIFIERS.get(specifier.functionKind));
|
|
238
|
+
}
|
|
239
|
+
// ---
|
|
240
|
+
// Transform function call names
|
|
241
|
+
for (const styleCall of styleCalls) {
|
|
242
|
+
magicString.overwrite(styleCall.callStart, styleCall.callStart + styleCall.importId.length, RUNTIME_IDENTIFIERS.get(styleCall.functionKind));
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
code: magicString.toString(),
|
|
246
|
+
cssRulesByBucket,
|
|
247
|
+
usedProcessing: true,
|
|
248
|
+
usedVMForEvaluation,
|
|
249
|
+
perfIssues,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
//# sourceMappingURL=transformSync.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformSync.mjs","sourceRoot":"","sources":["../../../../packages/transform/src/transformSync.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,YAAY,EAA2B,MAAM,YAAY,CAAC;AACzE,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,eAAe,MAAM,2BAA2B,CAAC;AAIxD,OAAO,EACL,yBAAyB,EACzB,sBAAsB,EACtB,uBAAuB,EACvB,uBAAuB,GAMxB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAEzE,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AA8C5D,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAE9C,SAAS,kBAAkB,CAAC,SAAoB,EAAE,UAAgC;IAChF,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;QAC9B,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAE/C,IAAI,MAAM,CAAC,UAAU,KAAK,KAAK,EAAE,CAAC;YAChC,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,kBAAkB,EAAE,QAAQ,EAAE,CAAC,CAAC;QACxE,CAAC;aAAM,IAAI,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5C,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AC,aAAa,EAAE,SAAS,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC;oBACvG,gBAAgB,GAAG,sBAAsB,CAAC,gBAAgB,EAAE,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;gBAC/E,CAAC;gBACD,MAAM;QACV,CAAC;IACH,CAAC;IAED,MAAM;IACN,uCAAuC;IAEvC,KAAK,MAAM,SAAS,IAAI,iBAAiB,CAAC,MAAM,EAAE,EAAE,CAAC;QACnD,WAAW,CAAC,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC,GAAG,EAAE,mBAAmB,CAAC,GAAG,CAAC,SAAS,CAAC,YAAY,CAAE,CAAC,CAAC;IAC1G,CAAC;IAED,MAAM;IACN,gCAAgC;IAEhC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,WAAW,CAAC,SAAS,CACnB,SAAS,CAAC,SAAS,EACnB,SAAS,CAAC,SAAS,GAAG,SAAS,CAAC,QAAQ,CAAC,MAAM,EAC/C,mBAAmB,CAAC,GAAG,CAAC,SAAS,CAAC,YAAY,CAAE,CACjD,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,WAAW,CAAC,QAAQ,EAAE;QAC5B,gBAAgB;QAChB,cAAc,EAAE,IAAI;QACpB,mBAAmB;QACnB,UAAU;KACX,CAAC;AACJ,CAAC"}
|
package/src/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../../../packages/transform/src/types.mts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { parseSync } from 'oxc-parser';
|
|
2
|
+
import MagicString from 'magic-string';
|
|
3
|
+
// Helper to access AST node properties without violating noPropertyAccessFromIndexSignature
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
const prop = (node, key) => node[key];
|
|
6
|
+
/**
|
|
7
|
+
* Extracts all declared binding names from a pattern node (Identifier, ObjectPattern, ArrayPattern, AssignmentPattern).
|
|
8
|
+
*/
|
|
9
|
+
function extractDeclaredNames(node) {
|
|
10
|
+
switch (node.type) {
|
|
11
|
+
case 'Identifier':
|
|
12
|
+
return [prop(node, 'name')];
|
|
13
|
+
case 'ObjectPattern':
|
|
14
|
+
return prop(node, 'properties').flatMap(p => {
|
|
15
|
+
if (p.type === 'RestElement') {
|
|
16
|
+
return extractDeclaredNames(prop(p, 'argument'));
|
|
17
|
+
}
|
|
18
|
+
return extractDeclaredNames(prop(p, 'value'));
|
|
19
|
+
});
|
|
20
|
+
case 'ArrayPattern':
|
|
21
|
+
return prop(node, 'elements').filter(Boolean).flatMap(el => extractDeclaredNames(el));
|
|
22
|
+
case 'AssignmentPattern':
|
|
23
|
+
return extractDeclaredNames(prop(node, 'left'));
|
|
24
|
+
default:
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Converts ESM import/export syntax to CJS require/exports equivalents.
|
|
30
|
+
* This is needed because the VM evaluator wraps code in `(function(exports) { ... })(exports)`
|
|
31
|
+
* which cannot contain ESM syntax.
|
|
32
|
+
*/
|
|
33
|
+
export function convertESMtoCJS(code, filename) {
|
|
34
|
+
const parseResult = parseSync(filename, code, { sourceType: 'module' });
|
|
35
|
+
if (parseResult.errors.length > 0) {
|
|
36
|
+
throw new Error(`convertESMtoCJS: failed to parse "${filename}": ${parseResult.errors.map(e => e.message).join(', ')}`);
|
|
37
|
+
}
|
|
38
|
+
const program = parseResult.program;
|
|
39
|
+
let hasESM = false;
|
|
40
|
+
for (const node of program.body) {
|
|
41
|
+
if (node.type === 'ImportDeclaration' ||
|
|
42
|
+
node.type === 'ExportNamedDeclaration' ||
|
|
43
|
+
node.type === 'ExportDefaultDeclaration' ||
|
|
44
|
+
node.type === 'ExportAllDeclaration') {
|
|
45
|
+
hasESM = true;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!hasESM) {
|
|
50
|
+
return code;
|
|
51
|
+
}
|
|
52
|
+
const ms = new MagicString(code);
|
|
53
|
+
const deferredExports = [];
|
|
54
|
+
for (const node of program.body) {
|
|
55
|
+
switch (node.type) {
|
|
56
|
+
case 'ImportDeclaration': {
|
|
57
|
+
const source = prop(prop(node, 'source'), 'value');
|
|
58
|
+
const specifiers = prop(node, 'specifiers');
|
|
59
|
+
if (specifiers.length === 0) {
|
|
60
|
+
ms.overwrite(node.start, node.end, `require(${JSON.stringify(source)});`);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
let defaultName = null;
|
|
64
|
+
let namespaceName = null;
|
|
65
|
+
const namedImports = [];
|
|
66
|
+
for (const spec of specifiers) {
|
|
67
|
+
if (spec.type === 'ImportDefaultSpecifier') {
|
|
68
|
+
defaultName = prop(prop(spec, 'local'), 'name');
|
|
69
|
+
}
|
|
70
|
+
else if (spec.type === 'ImportNamespaceSpecifier') {
|
|
71
|
+
namespaceName = prop(prop(spec, 'local'), 'name');
|
|
72
|
+
}
|
|
73
|
+
else if (spec.type === 'ImportSpecifier') {
|
|
74
|
+
const importedNode = prop(spec, 'imported');
|
|
75
|
+
const imported = importedNode.type === 'Identifier'
|
|
76
|
+
? prop(importedNode, 'name')
|
|
77
|
+
: prop(importedNode, 'value');
|
|
78
|
+
const local = prop(prop(spec, 'local'), 'name');
|
|
79
|
+
if (imported === local) {
|
|
80
|
+
namedImports.push(imported);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
namedImports.push(`${imported}: ${local}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const parts = [];
|
|
88
|
+
if (namespaceName) {
|
|
89
|
+
parts.push(`const ${namespaceName} = require(${JSON.stringify(source)});`);
|
|
90
|
+
}
|
|
91
|
+
else if (defaultName && namedImports.length > 0) {
|
|
92
|
+
const tempVar = `_require$${defaultName}`;
|
|
93
|
+
parts.push(`const ${tempVar} = require(${JSON.stringify(source)});`);
|
|
94
|
+
parts.push(`const ${defaultName} = ${tempVar}.__esModule ? ${tempVar}.default : ${tempVar};`);
|
|
95
|
+
parts.push(`const { ${namedImports.join(', ')} } = ${tempVar};`);
|
|
96
|
+
}
|
|
97
|
+
else if (defaultName) {
|
|
98
|
+
const tempVar = `_require$${defaultName}`;
|
|
99
|
+
parts.push(`const ${tempVar} = require(${JSON.stringify(source)});`);
|
|
100
|
+
parts.push(`const ${defaultName} = ${tempVar}.__esModule ? ${tempVar}.default : ${tempVar};`);
|
|
101
|
+
}
|
|
102
|
+
else if (namedImports.length > 0) {
|
|
103
|
+
parts.push(`const { ${namedImports.join(', ')} } = require(${JSON.stringify(source)});`);
|
|
104
|
+
}
|
|
105
|
+
ms.overwrite(node.start, node.end, parts.join('\n'));
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case 'ExportNamedDeclaration': {
|
|
110
|
+
const decl = prop(node, 'declaration');
|
|
111
|
+
if (decl) {
|
|
112
|
+
ms.overwrite(node.start, decl.start, '');
|
|
113
|
+
if (decl.type === 'VariableDeclaration') {
|
|
114
|
+
const names = prop(decl, 'declarations').flatMap(d => extractDeclaredNames(prop(d, 'id')));
|
|
115
|
+
deferredExports.push(...names);
|
|
116
|
+
}
|
|
117
|
+
else if (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') {
|
|
118
|
+
const id = prop(decl, 'id');
|
|
119
|
+
if (id) {
|
|
120
|
+
deferredExports.push(prop(id, 'name'));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else if (prop(node, 'source')) {
|
|
125
|
+
const source = prop(prop(node, 'source'), 'value');
|
|
126
|
+
const parts = [];
|
|
127
|
+
for (const spec of prop(node, 'specifiers')) {
|
|
128
|
+
const localNode = prop(spec, 'local');
|
|
129
|
+
const exportedNode = prop(spec, 'exported');
|
|
130
|
+
const local = localNode.type === 'Identifier'
|
|
131
|
+
? prop(localNode, 'name')
|
|
132
|
+
: prop(localNode, 'value');
|
|
133
|
+
const exported = exportedNode.type === 'Identifier'
|
|
134
|
+
? prop(exportedNode, 'name')
|
|
135
|
+
: prop(exportedNode, 'value');
|
|
136
|
+
parts.push(`exports[${JSON.stringify(exported)}] = require(${JSON.stringify(source)})[${JSON.stringify(local)}];`);
|
|
137
|
+
}
|
|
138
|
+
ms.overwrite(node.start, node.end, parts.join('\n'));
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const parts = [];
|
|
142
|
+
for (const spec of prop(node, 'specifiers')) {
|
|
143
|
+
const localNode = prop(spec, 'local');
|
|
144
|
+
const exportedNode = prop(spec, 'exported');
|
|
145
|
+
const local = localNode.type === 'Identifier'
|
|
146
|
+
? prop(localNode, 'name')
|
|
147
|
+
: prop(localNode, 'value');
|
|
148
|
+
const exported = exportedNode.type === 'Identifier'
|
|
149
|
+
? prop(exportedNode, 'name')
|
|
150
|
+
: prop(exportedNode, 'value');
|
|
151
|
+
parts.push(`exports[${JSON.stringify(exported)}] = ${local};`);
|
|
152
|
+
}
|
|
153
|
+
ms.overwrite(node.start, node.end, parts.join('\n'));
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
case 'ExportDefaultDeclaration': {
|
|
158
|
+
const decl = prop(node, 'declaration');
|
|
159
|
+
if (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') {
|
|
160
|
+
const id = prop(decl, 'id');
|
|
161
|
+
if (id) {
|
|
162
|
+
ms.overwrite(node.start, decl.start, '');
|
|
163
|
+
ms.appendLeft(node.end, `\nexports.default = ${prop(id, 'name')};`);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
ms.overwrite(node.start, decl.start, 'exports.default = ');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
ms.overwrite(node.start, decl.start, 'exports.default = ');
|
|
171
|
+
if (code[node.end - 1] !== ';') {
|
|
172
|
+
ms.appendLeft(node.end, ';');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case 'ExportAllDeclaration': {
|
|
178
|
+
const source = prop(prop(node, 'source'), 'value');
|
|
179
|
+
const exportedNode = prop(node, 'exported');
|
|
180
|
+
if (exportedNode) {
|
|
181
|
+
const name = exportedNode.type === 'Identifier'
|
|
182
|
+
? prop(exportedNode, 'name')
|
|
183
|
+
: prop(exportedNode, 'value');
|
|
184
|
+
ms.overwrite(node.start, node.end, `exports[${JSON.stringify(name)}] = require(${JSON.stringify(source)});`);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
ms.overwrite(node.start, node.end, `Object.assign(exports, require(${JSON.stringify(source)}));`);
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Append deferred exports at end-of-file so that IIFEs (e.g. TS compiled enums)
|
|
194
|
+
// can populate variables before they are captured by `exports.X = X`.
|
|
195
|
+
// ESM uses live bindings; deferring to end-of-file approximates that for CJS.
|
|
196
|
+
if (deferredExports.length > 0) {
|
|
197
|
+
ms.append('\n' + deferredExports.map(name => `exports.${name} = ${name};`).join('\n'));
|
|
198
|
+
}
|
|
199
|
+
// Mark the module as ESM-converted for interop
|
|
200
|
+
ms.prepend('Object.defineProperty(exports, "__esModule", { value: true });\n');
|
|
201
|
+
return ms.toString();
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=convertESMtoCJS.mjs.map
|