@griffel/transform 3.0.5 → 3.0.6
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 +6 -5
- package/src/constants.d.mts +2 -0
- package/src/{constants.mts → constants.mjs} +1 -0
- package/src/constants.mjs.map +1 -0
- package/src/evaluation/astEvaluator.d.mts +20 -0
- package/src/evaluation/astEvaluator.mjs +90 -0
- package/src/evaluation/astEvaluator.mjs.map +1 -0
- package/src/evaluation/batchEvaluator.d.mts +13 -0
- package/src/evaluation/batchEvaluator.mjs +54 -0
- package/src/evaluation/batchEvaluator.mjs.map +1 -0
- package/src/evaluation/evalCache.d.mts +9 -0
- package/src/evaluation/evalCache.mjs +65 -0
- package/src/evaluation/evalCache.mjs.map +1 -0
- package/src/evaluation/fluentTokensPlugin.d.mts +2 -0
- package/src/evaluation/fluentTokensPlugin.mjs +70 -0
- package/src/evaluation/fluentTokensPlugin.mjs.map +1 -0
- package/src/evaluation/module.d.mts +44 -0
- package/src/evaluation/module.mjs +207 -0
- package/src/evaluation/module.mjs.map +1 -0
- package/src/evaluation/process.d.mts +24 -0
- package/src/evaluation/{process.mts → process.mjs} +7 -12
- package/src/evaluation/process.mjs.map +1 -0
- package/src/evaluation/types.d.mts +34 -0
- package/src/evaluation/types.mjs +2 -0
- package/src/evaluation/types.mjs.map +1 -0
- package/src/evaluation/vmEvaluator.d.mts +3 -0
- package/src/evaluation/vmEvaluator.mjs +33 -0
- package/src/evaluation/vmEvaluator.mjs.map +1 -0
- package/src/{index.mts → index.d.mts} +0 -4
- package/src/index.mjs +9 -0
- package/src/index.mjs.map +1 -0
- package/src/transformSync.d.mts +41 -0
- package/src/transformSync.mjs +253 -0
- package/src/transformSync.mjs.map +1 -0
- package/src/types.d.mts +12 -0
- package/src/types.mjs +2 -0
- package/src/types.mjs.map +1 -0
- package/src/utils/convertESMtoCJS.d.mts +6 -0
- package/src/utils/convertESMtoCJS.mjs +203 -0
- package/src/utils/convertESMtoCJS.mjs.map +1 -0
- package/src/utils/dedupeCSSRules.d.mts +6 -0
- package/src/utils/dedupeCSSRules.mjs +19 -0
- package/src/utils/dedupeCSSRules.mjs.map +1 -0
- package/CHANGELOG.json +0 -404
- package/CHANGELOG.md +0 -130
- package/__fixtures__/assets/blank.jpg +0 -0
- package/__fixtures__/assets/code.ts +0 -12
- package/__fixtures__/assets/empty.jpg +0 -0
- package/__fixtures__/assets/output.meta.json +0 -12
- package/__fixtures__/assets/output.ts +0 -12
- package/__fixtures__/assets-multiple-declarations/blank.jpg +0 -0
- package/__fixtures__/assets-multiple-declarations/code.ts +0 -8
- package/__fixtures__/assets-multiple-declarations/empty.jpg +0 -0
- package/__fixtures__/assets-multiple-declarations/output.meta.json +0 -9
- package/__fixtures__/assets-multiple-declarations/output.ts +0 -6
- package/__fixtures__/assets-reset-styles/blank.jpg +0 -0
- package/__fixtures__/assets-reset-styles/code.ts +0 -11
- package/__fixtures__/assets-reset-styles/empty.jpg +0 -0
- package/__fixtures__/assets-reset-styles/output.meta.json +0 -11
- package/__fixtures__/assets-reset-styles/output.ts +0 -7
- package/__fixtures__/assets-urls/code.ts +0 -13
- package/__fixtures__/assets-urls/output.meta.json +0 -14
- package/__fixtures__/assets-urls/output.ts +0 -10
- package/__fixtures__/at-rules/code.ts +0 -14
- package/__fixtures__/at-rules/output.meta.json +0 -15
- package/__fixtures__/at-rules/output.ts +0 -10
- package/__fixtures__/config-classname-hash-salt/code.ts +0 -5
- package/__fixtures__/config-classname-hash-salt/output.meta.json +0 -7
- package/__fixtures__/config-classname-hash-salt/output.ts +0 -3
- package/__fixtures__/config-evaluation-rules/code.ts +0 -8
- package/__fixtures__/config-evaluation-rules/consts.ts +0 -1
- package/__fixtures__/config-evaluation-rules/output.meta.json +0 -7
- package/__fixtures__/config-evaluation-rules/output.ts +0 -4
- package/__fixtures__/config-evaluation-rules/sampleEvaluator.cjs +0 -12
- package/__fixtures__/error-argument-count/fixture.js +0 -4
- package/__fixtures__/error-cjs/fixture.js +0 -4
- package/__fixtures__/error-on-undefined/fixture.ts +0 -7
- package/__fixtures__/error-on-undefined/tokens.ts +0 -1
- package/__fixtures__/export-default/code.ts +0 -6
- package/__fixtures__/export-default/output.meta.json +0 -14
- package/__fixtures__/export-default/output.ts +0 -6
- package/__fixtures__/function-mixin/code.ts +0 -6
- package/__fixtures__/function-mixin/mixins.ts +0 -7
- package/__fixtures__/function-mixin/output.meta.json +0 -7
- package/__fixtures__/function-mixin/output.ts +0 -4
- package/__fixtures__/function-mixin/tokens.ts +0 -3
- package/__fixtures__/import-alias/code.ts +0 -5
- package/__fixtures__/import-alias/output.meta.json +0 -7
- package/__fixtures__/import-alias/output.ts +0 -3
- package/__fixtures__/import-custom-module/code.ts +0 -6
- package/__fixtures__/import-custom-module/output.meta.json +0 -7
- package/__fixtures__/import-custom-module/output.ts +0 -4
- package/__fixtures__/keyframes/code.ts +0 -22
- package/__fixtures__/keyframes/output.meta.json +0 -17
- package/__fixtures__/keyframes/output.ts +0 -6
- package/__fixtures__/multiple-declarations/code.ts +0 -8
- package/__fixtures__/multiple-declarations/output.meta.json +0 -7
- package/__fixtures__/multiple-declarations/output.ts +0 -4
- package/__fixtures__/non-existing-module-call/code.ts +0 -10
- package/__fixtures__/non-existing-module-call/module.ts +0 -13
- package/__fixtures__/non-existing-module-call/output.meta.json +0 -7
- package/__fixtures__/non-existing-module-call/output.ts +0 -6
- package/__fixtures__/object/code.ts +0 -6
- package/__fixtures__/object/output.meta.json +0 -14
- package/__fixtures__/object/output.ts +0 -6
- package/__fixtures__/object-computed-keys/code.ts +0 -8
- package/__fixtures__/object-computed-keys/output.meta.json +0 -14
- package/__fixtures__/object-computed-keys/output.ts +0 -8
- package/__fixtures__/object-imported-keys/code.ts +0 -9
- package/__fixtures__/object-imported-keys/consts.ts +0 -2
- package/__fixtures__/object-imported-keys/output.meta.json +0 -7
- package/__fixtures__/object-imported-keys/output.ts +0 -4
- package/__fixtures__/object-mixins/code.ts +0 -11
- package/__fixtures__/object-mixins/mixins.ts +0 -17
- package/__fixtures__/object-mixins/output.meta.json +0 -16
- package/__fixtures__/object-mixins/output.ts +0 -10
- package/__fixtures__/object-mixins/tokens.ts +0 -3
- package/__fixtures__/object-nesting/code.ts +0 -13
- package/__fixtures__/object-nesting/consts.ts +0 -1
- package/__fixtures__/object-nesting/output.meta.json +0 -9
- package/__fixtures__/object-nesting/output.ts +0 -6
- package/__fixtures__/object-reset/code.ts +0 -8
- package/__fixtures__/object-reset/output.meta.json +0 -7
- package/__fixtures__/object-reset/output.ts +0 -5
- package/__fixtures__/object-sequence-expr/code.ts +0 -16
- package/__fixtures__/object-sequence-expr/output.meta.json +0 -7
- package/__fixtures__/object-sequence-expr/output.ts +0 -7
- package/__fixtures__/object-shorthands/code.ts +0 -8
- package/__fixtures__/object-shorthands/output.meta.json +0 -20
- package/__fixtures__/object-shorthands/output.ts +0 -5
- package/__fixtures__/object-variables/code.ts +0 -9
- package/__fixtures__/object-variables/output.meta.json +0 -15
- package/__fixtures__/object-variables/output.ts +0 -9
- package/__fixtures__/object-variables/vars.ts +0 -5
- package/__fixtures__/reset-styles/code.ts +0 -6
- package/__fixtures__/reset-styles/output.meta.json +0 -7
- package/__fixtures__/reset-styles/output.ts +0 -3
- package/__fixtures__/reset-styles-at-rules/code.ts +0 -7
- package/__fixtures__/reset-styles-at-rules/output.meta.json +0 -8
- package/__fixtures__/reset-styles-at-rules/output.ts +0 -3
- package/__fixtures__/rules-with-metadata/code.ts +0 -9
- package/__fixtures__/rules-with-metadata/output.meta.json +0 -14
- package/__fixtures__/rules-with-metadata/output.ts +0 -3
- package/__fixtures__/shared-mixins/code.ts +0 -7
- package/__fixtures__/shared-mixins/mixins.ts +0 -6
- package/__fixtures__/shared-mixins/output.meta.json +0 -7
- package/__fixtures__/shared-mixins/output.ts +0 -8
- package/__fixtures__/static-styles/code.ts +0 -7
- package/__fixtures__/static-styles/output.meta.json +0 -7
- package/__fixtures__/static-styles/output.ts +0 -3
- package/__fixtures__/static-styles-array/code.ts +0 -10
- package/__fixtures__/static-styles-array/output.meta.json +0 -7
- package/__fixtures__/static-styles-array/output.ts +0 -3
- package/__fixtures__/static-styles-string/code.ts +0 -3
- package/__fixtures__/static-styles-string/output.meta.json +0 -7
- package/__fixtures__/static-styles-string/output.ts +0 -3
- package/__fixtures__/tokens/code.ts +0 -11
- package/__fixtures__/tokens/output.meta.json +0 -12
- package/__fixtures__/tokens/output.ts +0 -7
- package/__fixtures__/tokens/tokens.ts +0 -4
- package/__fixtures__/unsupported-css-properties/fixture.ts +0 -16
- package/__fixtures__/unsupported-css-properties/output.meta.json +0 -5
- package/__fixtures__/unsupported-css-properties/output.ts +0 -3
- package/eslint.config.mjs +0 -31
- package/project.json +0 -51
- package/src/evaluation/astEvaluator.mts +0 -109
- package/src/evaluation/astEvaluator.test.mts +0 -126
- package/src/evaluation/batchEvaluator.mts +0 -79
- package/src/evaluation/evalCache.mts +0 -84
- package/src/evaluation/fluentTokensPlugin.mts +0 -82
- package/src/evaluation/fluentTokensPlugin.test.mts +0 -130
- package/src/evaluation/module.mts +0 -271
- package/src/evaluation/module.test.mts +0 -133
- package/src/evaluation/types.mts +0 -49
- package/src/evaluation/vmEvaluator.mts +0 -45
- package/src/evaluation/vmEvaluator.test.mts +0 -30
- package/src/transformSync.mts +0 -425
- package/src/transformSync.test.mts +0 -429
- package/src/types.mts +0 -13
- package/src/utils/convertESMtoCJS.mts +0 -226
- package/src/utils/convertESMtoCJS.test.mts +0 -159
- package/src/utils/dedupeCSSRules.mts +0 -25
- package/src/utils/dedupeCSSRules.test.mts +0 -39
- package/tsconfig.json +0 -19
- package/tsconfig.lib.json +0 -30
- package/tsconfig.spec.json +0 -24
- package/vitest.config.mts +0 -18
|
@@ -1,429 +0,0 @@
|
|
|
1
|
-
import * as fs from 'node:fs';
|
|
2
|
-
import NativeModule from 'node:module';
|
|
3
|
-
import * as path from 'node:path';
|
|
4
|
-
import { format } from 'prettier';
|
|
5
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
6
|
-
|
|
7
|
-
import { ASSET_TAG_OPEN, ASSET_TAG_CLOSE } from './constants.mjs';
|
|
8
|
-
import type { TransformResolver } from './evaluation/module.mjs';
|
|
9
|
-
import { transformSync, type TransformOptions } from './transformSync.mjs';
|
|
10
|
-
|
|
11
|
-
const EXTRA_EXTENSIONS = ['.ts', '.tsx', '.jsx', '.cjs'];
|
|
12
|
-
|
|
13
|
-
const nodeResolve: TransformResolver = (id, opts) => {
|
|
14
|
-
const extensions = (NativeModule as unknown as { _extensions: Record<string, () => void> })._extensions;
|
|
15
|
-
const added: string[] = [];
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
for (const ext of EXTRA_EXTENSIONS) {
|
|
19
|
-
if (!(ext in extensions)) {
|
|
20
|
-
extensions[ext] = () => {};
|
|
21
|
-
added.push(ext);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
path: (NativeModule as unknown as { _resolveFilename: (id: string, options: unknown) => string })._resolveFilename(id, opts),
|
|
27
|
-
builtin: false,
|
|
28
|
-
};
|
|
29
|
-
} finally {
|
|
30
|
-
for (const ext of added) {
|
|
31
|
-
delete extensions[ext];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
type TestCase = {
|
|
37
|
-
title: string;
|
|
38
|
-
fixture: string;
|
|
39
|
-
|
|
40
|
-
error?: RegExp;
|
|
41
|
-
outputFixture?: string;
|
|
42
|
-
|
|
43
|
-
only?: boolean;
|
|
44
|
-
transformOptions?: Omit<TransformOptions, 'filename' | 'resolveModule'>;
|
|
45
|
-
|
|
46
|
-
setup?: () => (() => void) | void;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const prettierConfig = JSON.parse(
|
|
50
|
-
fs.readFileSync(path.resolve(__dirname, '../../../.prettierrc'), { encoding: 'utf-8' }),
|
|
51
|
-
);
|
|
52
|
-
const fixturesDir = path.join(__dirname, '..', '__fixtures__');
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Asset paths in `<griffel-asset>` tags contain machine-specific absolute paths which also
|
|
56
|
-
* cause class name hashes to differ. This normalizes both outputs by replacing absolute
|
|
57
|
-
* paths with relative ones and hashes with ordered placeholders.
|
|
58
|
-
*
|
|
59
|
-
* Only applied when the meta output contains asset tags; other fixtures pass through unchanged.
|
|
60
|
-
*/
|
|
61
|
-
function normalizeAssetOutputs(
|
|
62
|
-
code: string,
|
|
63
|
-
meta: string,
|
|
64
|
-
fixtureDir: string,
|
|
65
|
-
): { code: string; meta: string } {
|
|
66
|
-
if (meta.indexOf(ASSET_TAG_OPEN) === -1) {
|
|
67
|
-
return { code, meta };
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// 1. Replace absolute paths inside asset tags with fixture-relative paths
|
|
71
|
-
let normalizedMeta = '';
|
|
72
|
-
let searchFrom = 0;
|
|
73
|
-
|
|
74
|
-
while (searchFrom < meta.length) {
|
|
75
|
-
const openIdx = meta.indexOf(ASSET_TAG_OPEN, searchFrom);
|
|
76
|
-
|
|
77
|
-
if (openIdx === -1) {
|
|
78
|
-
normalizedMeta += meta.slice(searchFrom);
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
normalizedMeta += meta.slice(searchFrom, openIdx + ASSET_TAG_OPEN.length);
|
|
83
|
-
|
|
84
|
-
const contentStart = openIdx + ASSET_TAG_OPEN.length;
|
|
85
|
-
const closeIdx = meta.indexOf(ASSET_TAG_CLOSE, contentStart);
|
|
86
|
-
|
|
87
|
-
if (closeIdx === -1) {
|
|
88
|
-
normalizedMeta += meta.slice(contentStart);
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
normalizedMeta += path.relative(fixtureDir, meta.slice(contentStart, closeIdx));
|
|
93
|
-
searchFrom = closeIdx;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// 2. Collect unique class-name hashes from meta (e.g. ".fwvq0l6{" or ".r1abc23:")
|
|
97
|
-
const hashRegex = /\.([fr][a-z0-9]{4,})(?=[{:])/g;
|
|
98
|
-
const hashes: string[] = [];
|
|
99
|
-
let match;
|
|
100
|
-
|
|
101
|
-
// eslint-disable-next-line no-cond-assign
|
|
102
|
-
while ((match = hashRegex.exec(normalizedMeta)) !== null) {
|
|
103
|
-
if (!hashes.includes(match[1])) {
|
|
104
|
-
hashes.push(match[1]);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// 3. Replace hashes with deterministic ordered placeholders in both outputs
|
|
109
|
-
let normalizedCode = code;
|
|
110
|
-
|
|
111
|
-
for (let i = 0; i < hashes.length; i++) {
|
|
112
|
-
const placeholder = `${hashes[i][0]}___${i}`;
|
|
113
|
-
|
|
114
|
-
normalizedMeta = normalizedMeta.split(hashes[i]).join(placeholder);
|
|
115
|
-
normalizedCode = normalizedCode.split(hashes[i]).join(placeholder);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return { code: normalizedCode, meta: normalizedMeta };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const TESTS: TestCase[] = [
|
|
122
|
-
// 🎩 Tip: use "only: true" to run a single test
|
|
123
|
-
// https://github.com/babel-utils/babel-plugin-tester#only
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
// Fixtures
|
|
127
|
-
//
|
|
128
|
-
//
|
|
129
|
-
{
|
|
130
|
-
title: 'at rules',
|
|
131
|
-
fixture: path.resolve(fixturesDir, 'at-rules', 'code.ts'),
|
|
132
|
-
outputFixture: path.resolve(fixturesDir, 'at-rules', 'output.ts'),
|
|
133
|
-
},
|
|
134
|
-
{
|
|
135
|
-
title: 'multiple declarations',
|
|
136
|
-
fixture: path.resolve(fixturesDir, 'multiple-declarations', 'code.ts'),
|
|
137
|
-
outputFixture: path.resolve(fixturesDir, 'multiple-declarations', 'output.ts'),
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
title: 'call of non existing module',
|
|
141
|
-
fixture: path.resolve(fixturesDir, 'non-existing-module-call', 'code.ts'),
|
|
142
|
-
outputFixture: path.resolve(fixturesDir, 'non-existing-module-call', 'output.ts'),
|
|
143
|
-
},
|
|
144
|
-
{
|
|
145
|
-
title: 'syntax: animationName',
|
|
146
|
-
fixture: path.resolve(fixturesDir, 'keyframes', 'code.ts'),
|
|
147
|
-
outputFixture: path.resolve(fixturesDir, 'keyframes', 'output.ts'),
|
|
148
|
-
},
|
|
149
|
-
{
|
|
150
|
-
title: 'syntax: CSS shorthands',
|
|
151
|
-
fixture: path.resolve(fixturesDir, 'object-shorthands', 'code.ts'),
|
|
152
|
-
outputFixture: path.resolve(fixturesDir, 'object-shorthands', 'output.ts'),
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
title: 'syntax: reset (null values)',
|
|
156
|
-
fixture: path.resolve(fixturesDir, 'object-reset', 'code.ts'),
|
|
157
|
-
outputFixture: path.resolve(fixturesDir, 'object-reset', 'output.ts'),
|
|
158
|
-
},
|
|
159
|
-
|
|
160
|
-
// Assets
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
{
|
|
164
|
-
title: 'assets: import handling',
|
|
165
|
-
fixture: path.resolve(fixturesDir, 'assets', 'code.ts'),
|
|
166
|
-
outputFixture: path.resolve(fixturesDir, 'assets', 'output.ts'),
|
|
167
|
-
},
|
|
168
|
-
{
|
|
169
|
-
title: 'assets: multiple url()',
|
|
170
|
-
fixture: path.resolve(fixturesDir, 'assets-multiple-declarations', 'code.ts'),
|
|
171
|
-
outputFixture: path.resolve(fixturesDir, 'assets-multiple-declarations', 'output.ts'),
|
|
172
|
-
},
|
|
173
|
-
{
|
|
174
|
-
title: 'assets: url() without imports',
|
|
175
|
-
fixture: path.resolve(fixturesDir, 'assets-urls', 'code.ts'),
|
|
176
|
-
outputFixture: path.resolve(fixturesDir, 'assets-urls', 'output.ts'),
|
|
177
|
-
},
|
|
178
|
-
|
|
179
|
-
// Evaluation
|
|
180
|
-
//
|
|
181
|
-
//
|
|
182
|
-
{
|
|
183
|
-
title: 'evaluation: mixins via functions',
|
|
184
|
-
fixture: path.resolve(fixturesDir, 'function-mixin', 'code.ts'),
|
|
185
|
-
outputFixture: path.resolve(fixturesDir, 'function-mixin', 'output.ts'),
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
title: 'evaluation: object',
|
|
189
|
-
fixture: path.resolve(fixturesDir, 'object', 'code.ts'),
|
|
190
|
-
outputFixture: path.resolve(fixturesDir, 'object', 'output.ts'),
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
title: 'evaluation: object with computed keys',
|
|
194
|
-
fixture: path.resolve(fixturesDir, 'object-computed-keys', 'code.ts'),
|
|
195
|
-
outputFixture: path.resolve(fixturesDir, 'object-computed-keys', 'output.ts'),
|
|
196
|
-
},
|
|
197
|
-
{
|
|
198
|
-
title: 'evaluation: object with imported keys',
|
|
199
|
-
fixture: path.resolve(fixturesDir, 'object-imported-keys', 'code.ts'),
|
|
200
|
-
outputFixture: path.resolve(fixturesDir, 'object-imported-keys', 'output.ts'),
|
|
201
|
-
},
|
|
202
|
-
{
|
|
203
|
-
title: 'evaluation: object with mixins',
|
|
204
|
-
fixture: path.resolve(fixturesDir, 'object-mixins', 'code.ts'),
|
|
205
|
-
outputFixture: path.resolve(fixturesDir, 'object-mixins', 'output.ts'),
|
|
206
|
-
},
|
|
207
|
-
{
|
|
208
|
-
title: 'evaluation: nested objects',
|
|
209
|
-
fixture: path.resolve(fixturesDir, 'object-nesting', 'code.ts'),
|
|
210
|
-
outputFixture: path.resolve(fixturesDir, 'object-nesting', 'output.ts'),
|
|
211
|
-
},
|
|
212
|
-
{
|
|
213
|
-
title: 'evaluation: objects with sequence expression',
|
|
214
|
-
fixture: path.resolve(fixturesDir, 'object-sequence-expr', 'code.ts'),
|
|
215
|
-
outputFixture: path.resolve(fixturesDir, 'object-sequence-expr', 'output.ts'),
|
|
216
|
-
},
|
|
217
|
-
{
|
|
218
|
-
title: 'evaluation: objects with variables',
|
|
219
|
-
fixture: path.resolve(fixturesDir, 'object-variables', 'code.ts'),
|
|
220
|
-
outputFixture: path.resolve(fixturesDir, 'object-variables', 'output.ts'),
|
|
221
|
-
},
|
|
222
|
-
{
|
|
223
|
-
title: 'evaluation: rules with metadata',
|
|
224
|
-
fixture: path.resolve(fixturesDir, 'rules-with-metadata', 'code.ts'),
|
|
225
|
-
outputFixture: path.resolve(fixturesDir, 'rules-with-metadata', 'output.ts'),
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
title: 'evaluation: shared mixins',
|
|
229
|
-
fixture: path.resolve(fixturesDir, 'shared-mixins', 'code.ts'),
|
|
230
|
-
outputFixture: path.resolve(fixturesDir, 'shared-mixins', 'output.ts'),
|
|
231
|
-
},
|
|
232
|
-
{
|
|
233
|
-
title: 'evaluation: tokens scenario',
|
|
234
|
-
fixture: path.resolve(fixturesDir, 'tokens', 'code.ts'),
|
|
235
|
-
outputFixture: path.resolve(fixturesDir, 'tokens', 'output.ts'),
|
|
236
|
-
},
|
|
237
|
-
// Configs
|
|
238
|
-
//
|
|
239
|
-
//
|
|
240
|
-
{
|
|
241
|
-
title: 'config: classNameHashSalt',
|
|
242
|
-
fixture: path.resolve(fixturesDir, 'config-classname-hash-salt', 'code.ts'),
|
|
243
|
-
outputFixture: path.resolve(fixturesDir, 'config-classname-hash-salt', 'output.ts'),
|
|
244
|
-
transformOptions: {
|
|
245
|
-
classNameHashSalt: 'prefix',
|
|
246
|
-
},
|
|
247
|
-
},
|
|
248
|
-
{
|
|
249
|
-
title: 'config: evaluationRules',
|
|
250
|
-
fixture: path.resolve(fixturesDir, 'config-evaluation-rules', 'code.ts'),
|
|
251
|
-
outputFixture: path.resolve(fixturesDir, 'config-evaluation-rules', 'output.ts'),
|
|
252
|
-
transformOptions: {
|
|
253
|
-
evaluationRules: [{ action: require(path.resolve(fixturesDir, 'config-evaluation-rules', 'sampleEvaluator.cjs')).default }],
|
|
254
|
-
},
|
|
255
|
-
},
|
|
256
|
-
|
|
257
|
-
// Reset styles
|
|
258
|
-
//
|
|
259
|
-
//
|
|
260
|
-
{
|
|
261
|
-
title: 'reset: default',
|
|
262
|
-
fixture: path.resolve(fixturesDir, 'reset-styles', 'code.ts'),
|
|
263
|
-
outputFixture: path.resolve(fixturesDir, 'reset-styles', 'output.ts'),
|
|
264
|
-
},
|
|
265
|
-
{
|
|
266
|
-
title: 'reset: assets',
|
|
267
|
-
fixture: path.resolve(fixturesDir, 'assets-reset-styles', 'code.ts'),
|
|
268
|
-
outputFixture: path.resolve(fixturesDir, 'assets-reset-styles', 'output.ts'),
|
|
269
|
-
},
|
|
270
|
-
{
|
|
271
|
-
title: 'reset: at rules',
|
|
272
|
-
fixture: path.resolve(fixturesDir, 'reset-styles-at-rules', 'code.ts'),
|
|
273
|
-
outputFixture: path.resolve(fixturesDir, 'reset-styles-at-rules', 'output.ts'),
|
|
274
|
-
},
|
|
275
|
-
|
|
276
|
-
// Static styles
|
|
277
|
-
//
|
|
278
|
-
//
|
|
279
|
-
{
|
|
280
|
-
title: 'static: object',
|
|
281
|
-
fixture: path.resolve(fixturesDir, 'static-styles', 'code.ts'),
|
|
282
|
-
outputFixture: path.resolve(fixturesDir, 'static-styles', 'output.ts'),
|
|
283
|
-
},
|
|
284
|
-
{
|
|
285
|
-
title: 'static: string',
|
|
286
|
-
fixture: path.resolve(fixturesDir, 'static-styles-string', 'code.ts'),
|
|
287
|
-
outputFixture: path.resolve(fixturesDir, 'static-styles-string', 'output.ts'),
|
|
288
|
-
},
|
|
289
|
-
{
|
|
290
|
-
title: 'static: array',
|
|
291
|
-
fixture: path.resolve(fixturesDir, 'static-styles-array', 'code.ts'),
|
|
292
|
-
outputFixture: path.resolve(fixturesDir, 'static-styles-array', 'output.ts'),
|
|
293
|
-
},
|
|
294
|
-
|
|
295
|
-
// Imports
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
{
|
|
299
|
-
title: 'imports: alias usage',
|
|
300
|
-
fixture: path.resolve(fixturesDir, 'import-alias', 'code.ts'),
|
|
301
|
-
outputFixture: path.resolve(fixturesDir, 'import-alias', 'output.ts'),
|
|
302
|
-
},
|
|
303
|
-
{
|
|
304
|
-
title: 'imports: custom module',
|
|
305
|
-
fixture: path.resolve(fixturesDir, 'import-custom-module', 'code.ts'),
|
|
306
|
-
outputFixture: path.resolve(fixturesDir, 'import-custom-module', 'output.ts'),
|
|
307
|
-
transformOptions: {
|
|
308
|
-
importsToTransform: ['custom-package'],
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
|
|
312
|
-
{
|
|
313
|
-
title: 'errors: throws on CJS',
|
|
314
|
-
fixture: path.resolve(fixturesDir, 'error-cjs', 'fixture.js'),
|
|
315
|
-
error: /is not an ES module/,
|
|
316
|
-
},
|
|
317
|
-
|
|
318
|
-
// Exports
|
|
319
|
-
//
|
|
320
|
-
|
|
321
|
-
{
|
|
322
|
-
title: 'exports: handles default export',
|
|
323
|
-
fixture: path.resolve(fixturesDir, 'export-default', 'code.ts'),
|
|
324
|
-
outputFixture: path.resolve(fixturesDir, 'export-default', 'output.ts'),
|
|
325
|
-
},
|
|
326
|
-
|
|
327
|
-
// Errors
|
|
328
|
-
//
|
|
329
|
-
//
|
|
330
|
-
|
|
331
|
-
{
|
|
332
|
-
title: 'errors: unsupported shorthand CSS properties',
|
|
333
|
-
fixture: path.resolve(fixturesDir, 'unsupported-css-properties', 'fixture.ts'),
|
|
334
|
-
outputFixture: path.resolve(fixturesDir, 'unsupported-css-properties', 'output.ts'),
|
|
335
|
-
setup() {
|
|
336
|
-
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {
|
|
337
|
-
// Suppress console errors during test
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
return function teardown() {
|
|
341
|
-
consoleSpy.mockRestore();
|
|
342
|
-
};
|
|
343
|
-
},
|
|
344
|
-
},
|
|
345
|
-
{
|
|
346
|
-
title: 'errors: throws on invalid argument count',
|
|
347
|
-
fixture: path.resolve(fixturesDir, 'error-argument-count', 'fixture.js'),
|
|
348
|
-
error: /function accepts only a single param/,
|
|
349
|
-
},
|
|
350
|
-
{
|
|
351
|
-
title: 'errors: throws on undefined',
|
|
352
|
-
fixture: path.resolve(fixturesDir, 'error-on-undefined', 'fixture.ts'),
|
|
353
|
-
error: /Cannot read properties of undefined/,
|
|
354
|
-
},
|
|
355
|
-
];
|
|
356
|
-
|
|
357
|
-
describe('transformSync', () => {
|
|
358
|
-
it('astEvaluationPlugins: fluentTokensPlugin is enabled by default', () => {
|
|
359
|
-
const sourceCode = `
|
|
360
|
-
import { makeStyles } from '@griffel/react';
|
|
361
|
-
|
|
362
|
-
const tokens = { colorBrandBackground: 'var(--colorBrandBackground)' };
|
|
363
|
-
|
|
364
|
-
export const useStyles = makeStyles({
|
|
365
|
-
root: {
|
|
366
|
-
color: tokens.colorBrandBackground,
|
|
367
|
-
margin: \`\${tokens.spacingVerticalS} 0\`,
|
|
368
|
-
display: 'flex',
|
|
369
|
-
},
|
|
370
|
-
});
|
|
371
|
-
`;
|
|
372
|
-
|
|
373
|
-
const result = transformSync(sourceCode, {
|
|
374
|
-
filename: 'test-plugins.ts',
|
|
375
|
-
resolveModule: nodeResolve,
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
expect(result.usedProcessing).toBe(true);
|
|
379
|
-
// fluentTokensPlugin is on by default — tokens are evaluated statically, no VM needed
|
|
380
|
-
expect(result.usedVMForEvaluation).toBe(false);
|
|
381
|
-
expect(result.code).toContain('__css');
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
for (const testCase of TESTS) {
|
|
385
|
-
const testFn = testCase.only ? it.only : it;
|
|
386
|
-
|
|
387
|
-
testFn(testCase.title, async () => {
|
|
388
|
-
const sourceCode = fs.readFileSync(testCase.fixture, { encoding: 'utf-8' });
|
|
389
|
-
const teardown = testCase.setup?.();
|
|
390
|
-
|
|
391
|
-
const transformOptions = testCase.transformOptions || {};
|
|
392
|
-
|
|
393
|
-
if (testCase.error) {
|
|
394
|
-
expect(() =>
|
|
395
|
-
transformSync(sourceCode, {
|
|
396
|
-
filename: testCase.fixture,
|
|
397
|
-
resolveModule: nodeResolve,
|
|
398
|
-
...transformOptions,
|
|
399
|
-
}),
|
|
400
|
-
).toThrow(testCase.error);
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
const { code, cssRulesByBucket, usedProcessing, usedVMForEvaluation } = transformSync(sourceCode, {
|
|
405
|
-
filename: testCase.fixture,
|
|
406
|
-
resolveModule: nodeResolve,
|
|
407
|
-
...transformOptions,
|
|
408
|
-
});
|
|
409
|
-
const outputCode = await format(code, { ...prettierConfig, parser: 'typescript' });
|
|
410
|
-
const outputMeta = await format(
|
|
411
|
-
JSON.stringify({ usedProcessing, usedVMForEvaluation, cssRulesByBucket }, null, 2),
|
|
412
|
-
{
|
|
413
|
-
...prettierConfig,
|
|
414
|
-
parser: 'json',
|
|
415
|
-
},
|
|
416
|
-
);
|
|
417
|
-
|
|
418
|
-
const fixtureDir = path.dirname(testCase.fixture);
|
|
419
|
-
const normalized = normalizeAssetOutputs(outputCode, outputMeta, fixtureDir);
|
|
420
|
-
|
|
421
|
-
await expect(normalized.code).toMatchFileSnapshot(testCase.outputFixture!);
|
|
422
|
-
await expect(normalized.meta).toMatchFileSnapshot(testCase.outputFixture!.replace(/\.ts$/, '.meta.json'));
|
|
423
|
-
|
|
424
|
-
if (typeof teardown === 'function') {
|
|
425
|
-
teardown?.();
|
|
426
|
-
}
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
});
|
package/src/types.mts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Node } from 'oxc-parser';
|
|
2
|
-
|
|
3
|
-
export interface StyleCall {
|
|
4
|
-
declaratorId: string;
|
|
5
|
-
functionKind: 'makeStyles' | 'makeResetStyles' | 'makeStaticStyles';
|
|
6
|
-
argumentStart: number;
|
|
7
|
-
argumentEnd: number;
|
|
8
|
-
argumentCode: string;
|
|
9
|
-
argumentNode: Node;
|
|
10
|
-
callStart: number;
|
|
11
|
-
callEnd: number;
|
|
12
|
-
importId: string;
|
|
13
|
-
}
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { parseSync } from 'oxc-parser';
|
|
2
|
-
import type { Node } from 'oxc-parser';
|
|
3
|
-
import MagicString from 'magic-string';
|
|
4
|
-
|
|
5
|
-
// Helper to access AST node properties without violating noPropertyAccessFromIndexSignature
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
-
const prop = (node: Node, key: string): any => (node as unknown as Record<string, unknown>)[key];
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Extracts all declared binding names from a pattern node (Identifier, ObjectPattern, ArrayPattern, AssignmentPattern).
|
|
11
|
-
*/
|
|
12
|
-
function extractDeclaredNames(node: Node): string[] {
|
|
13
|
-
switch (node.type) {
|
|
14
|
-
case 'Identifier':
|
|
15
|
-
return [prop(node, 'name') as string];
|
|
16
|
-
case 'ObjectPattern':
|
|
17
|
-
return (prop(node, 'properties') as Node[]).flatMap(p => {
|
|
18
|
-
if (p.type === 'RestElement') {
|
|
19
|
-
return extractDeclaredNames(prop(p, 'argument') as Node);
|
|
20
|
-
}
|
|
21
|
-
return extractDeclaredNames(prop(p, 'value') as Node);
|
|
22
|
-
});
|
|
23
|
-
case 'ArrayPattern':
|
|
24
|
-
return (prop(node, 'elements') as (Node | null)[]).filter(Boolean).flatMap(el => extractDeclaredNames(el!));
|
|
25
|
-
case 'AssignmentPattern':
|
|
26
|
-
return extractDeclaredNames(prop(node, 'left') as Node);
|
|
27
|
-
default:
|
|
28
|
-
return [];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Converts ESM import/export syntax to CJS require/exports equivalents.
|
|
34
|
-
* This is needed because the VM evaluator wraps code in `(function(exports) { ... })(exports)`
|
|
35
|
-
* which cannot contain ESM syntax.
|
|
36
|
-
*/
|
|
37
|
-
export function convertESMtoCJS(code: string, filename: string): string {
|
|
38
|
-
const parseResult = parseSync(filename, code, { sourceType: 'module' });
|
|
39
|
-
|
|
40
|
-
if (parseResult.errors.length > 0) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
`convertESMtoCJS: failed to parse "${filename}": ${parseResult.errors.map(e => e.message).join(', ')}`,
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const program = parseResult.program;
|
|
47
|
-
|
|
48
|
-
let hasESM = false;
|
|
49
|
-
for (const node of program.body) {
|
|
50
|
-
if (
|
|
51
|
-
node.type === 'ImportDeclaration' ||
|
|
52
|
-
node.type === 'ExportNamedDeclaration' ||
|
|
53
|
-
node.type === 'ExportDefaultDeclaration' ||
|
|
54
|
-
node.type === 'ExportAllDeclaration'
|
|
55
|
-
) {
|
|
56
|
-
hasESM = true;
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (!hasESM) {
|
|
62
|
-
return code;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const ms = new MagicString(code);
|
|
66
|
-
const deferredExports: string[] = [];
|
|
67
|
-
|
|
68
|
-
for (const node of program.body) {
|
|
69
|
-
switch (node.type) {
|
|
70
|
-
case 'ImportDeclaration': {
|
|
71
|
-
const source = prop(prop(node, 'source'), 'value') as string;
|
|
72
|
-
const specifiers = prop(node, 'specifiers') as Node[];
|
|
73
|
-
|
|
74
|
-
if (specifiers.length === 0) {
|
|
75
|
-
ms.overwrite(node.start, node.end, `require(${JSON.stringify(source)});`);
|
|
76
|
-
} else {
|
|
77
|
-
let defaultName: string | null = null;
|
|
78
|
-
let namespaceName: string | null = null;
|
|
79
|
-
const namedImports: string[] = [];
|
|
80
|
-
|
|
81
|
-
for (const spec of specifiers) {
|
|
82
|
-
if (spec.type === 'ImportDefaultSpecifier') {
|
|
83
|
-
defaultName = prop(prop(spec, 'local'), 'name') as string;
|
|
84
|
-
} else if (spec.type === 'ImportNamespaceSpecifier') {
|
|
85
|
-
namespaceName = prop(prop(spec, 'local'), 'name') as string;
|
|
86
|
-
} else if (spec.type === 'ImportSpecifier') {
|
|
87
|
-
const importedNode = prop(spec, 'imported') as Node;
|
|
88
|
-
const imported =
|
|
89
|
-
importedNode.type === 'Identifier'
|
|
90
|
-
? (prop(importedNode, 'name') as string)
|
|
91
|
-
: (prop(importedNode, 'value') as string);
|
|
92
|
-
const local = prop(prop(spec, 'local'), 'name') as string;
|
|
93
|
-
if (imported === local) {
|
|
94
|
-
namedImports.push(imported);
|
|
95
|
-
} else {
|
|
96
|
-
namedImports.push(`${imported}: ${local}`);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const parts: string[] = [];
|
|
102
|
-
|
|
103
|
-
if (namespaceName) {
|
|
104
|
-
parts.push(`const ${namespaceName} = require(${JSON.stringify(source)});`);
|
|
105
|
-
} else if (defaultName && namedImports.length > 0) {
|
|
106
|
-
const tempVar = `_require$${defaultName}`;
|
|
107
|
-
parts.push(`const ${tempVar} = require(${JSON.stringify(source)});`);
|
|
108
|
-
parts.push(`const ${defaultName} = ${tempVar}.__esModule ? ${tempVar}.default : ${tempVar};`);
|
|
109
|
-
parts.push(`const { ${namedImports.join(', ')} } = ${tempVar};`);
|
|
110
|
-
} else if (defaultName) {
|
|
111
|
-
const tempVar = `_require$${defaultName}`;
|
|
112
|
-
parts.push(`const ${tempVar} = require(${JSON.stringify(source)});`);
|
|
113
|
-
parts.push(`const ${defaultName} = ${tempVar}.__esModule ? ${tempVar}.default : ${tempVar};`);
|
|
114
|
-
} else if (namedImports.length > 0) {
|
|
115
|
-
parts.push(`const { ${namedImports.join(', ')} } = require(${JSON.stringify(source)});`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
ms.overwrite(node.start, node.end, parts.join('\n'));
|
|
119
|
-
}
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
case 'ExportNamedDeclaration': {
|
|
124
|
-
const decl = prop(node, 'declaration') as Node | null;
|
|
125
|
-
|
|
126
|
-
if (decl) {
|
|
127
|
-
ms.overwrite(node.start, decl.start, '');
|
|
128
|
-
|
|
129
|
-
if (decl.type === 'VariableDeclaration') {
|
|
130
|
-
const names = (prop(decl, 'declarations') as Node[]).flatMap(d =>
|
|
131
|
-
extractDeclaredNames(prop(d, 'id') as Node),
|
|
132
|
-
);
|
|
133
|
-
deferredExports.push(...names);
|
|
134
|
-
} else if (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') {
|
|
135
|
-
const id = prop(decl, 'id') as Node | null;
|
|
136
|
-
if (id) {
|
|
137
|
-
deferredExports.push(prop(id, 'name') as string);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
} else if (prop(node, 'source')) {
|
|
141
|
-
const source = prop(prop(node, 'source'), 'value') as string;
|
|
142
|
-
const parts: string[] = [];
|
|
143
|
-
for (const spec of prop(node, 'specifiers') as Node[]) {
|
|
144
|
-
const localNode = prop(spec, 'local') as Node;
|
|
145
|
-
const exportedNode = prop(spec, 'exported') as Node;
|
|
146
|
-
const local =
|
|
147
|
-
localNode.type === 'Identifier'
|
|
148
|
-
? (prop(localNode, 'name') as string)
|
|
149
|
-
: (prop(localNode, 'value') as string);
|
|
150
|
-
const exported =
|
|
151
|
-
exportedNode.type === 'Identifier'
|
|
152
|
-
? (prop(exportedNode, 'name') as string)
|
|
153
|
-
: (prop(exportedNode, 'value') as string);
|
|
154
|
-
parts.push(
|
|
155
|
-
`exports[${JSON.stringify(exported)}] = require(${JSON.stringify(source)})[${JSON.stringify(local)}];`,
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
ms.overwrite(node.start, node.end, parts.join('\n'));
|
|
159
|
-
} else {
|
|
160
|
-
const parts: string[] = [];
|
|
161
|
-
for (const spec of prop(node, 'specifiers') as Node[]) {
|
|
162
|
-
const localNode = prop(spec, 'local') as Node;
|
|
163
|
-
const exportedNode = prop(spec, 'exported') as Node;
|
|
164
|
-
const local =
|
|
165
|
-
localNode.type === 'Identifier'
|
|
166
|
-
? (prop(localNode, 'name') as string)
|
|
167
|
-
: (prop(localNode, 'value') as string);
|
|
168
|
-
const exported =
|
|
169
|
-
exportedNode.type === 'Identifier'
|
|
170
|
-
? (prop(exportedNode, 'name') as string)
|
|
171
|
-
: (prop(exportedNode, 'value') as string);
|
|
172
|
-
parts.push(`exports[${JSON.stringify(exported)}] = ${local};`);
|
|
173
|
-
}
|
|
174
|
-
ms.overwrite(node.start, node.end, parts.join('\n'));
|
|
175
|
-
}
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
case 'ExportDefaultDeclaration': {
|
|
180
|
-
const decl = prop(node, 'declaration') as Node;
|
|
181
|
-
if (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') {
|
|
182
|
-
const id = prop(decl, 'id') as Node | null;
|
|
183
|
-
if (id) {
|
|
184
|
-
ms.overwrite(node.start, decl.start, '');
|
|
185
|
-
ms.appendLeft(node.end, `\nexports.default = ${prop(id, 'name') as string};`);
|
|
186
|
-
} else {
|
|
187
|
-
ms.overwrite(node.start, decl.start, 'exports.default = ');
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
ms.overwrite(node.start, decl.start, 'exports.default = ');
|
|
191
|
-
if (code[node.end - 1] !== ';') {
|
|
192
|
-
ms.appendLeft(node.end, ';');
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
break;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
case 'ExportAllDeclaration': {
|
|
199
|
-
const source = prop(prop(node, 'source'), 'value') as string;
|
|
200
|
-
const exportedNode = prop(node, 'exported') as Node | null;
|
|
201
|
-
if (exportedNode) {
|
|
202
|
-
const name =
|
|
203
|
-
exportedNode.type === 'Identifier'
|
|
204
|
-
? (prop(exportedNode, 'name') as string)
|
|
205
|
-
: (prop(exportedNode, 'value') as string);
|
|
206
|
-
ms.overwrite(node.start, node.end, `exports[${JSON.stringify(name)}] = require(${JSON.stringify(source)});`);
|
|
207
|
-
} else {
|
|
208
|
-
ms.overwrite(node.start, node.end, `Object.assign(exports, require(${JSON.stringify(source)}));`);
|
|
209
|
-
}
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Append deferred exports at end-of-file so that IIFEs (e.g. TS compiled enums)
|
|
216
|
-
// can populate variables before they are captured by `exports.X = X`.
|
|
217
|
-
// ESM uses live bindings; deferring to end-of-file approximates that for CJS.
|
|
218
|
-
if (deferredExports.length > 0) {
|
|
219
|
-
ms.append('\n' + deferredExports.map(name => `exports.${name} = ${name};`).join('\n'));
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Mark the module as ESM-converted for interop
|
|
223
|
-
ms.prepend('Object.defineProperty(exports, "__esModule", { value: true });\n');
|
|
224
|
-
|
|
225
|
-
return ms.toString();
|
|
226
|
-
}
|