@griffel/transform 3.0.0 → 3.0.2
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 +10 -7
- 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} +4 -3
- 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 -80
- package/transform.js +0 -933
- /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
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@griffel/transform",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "A package that performs build time transforms for Griffel",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -8,19 +8,22 @@
|
|
|
8
8
|
"url": "https://github.com/microsoft/griffel"
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
|
+
"main": "./src/index.mjs",
|
|
12
|
+
"types": "./src/index.d.mts",
|
|
11
13
|
"exports": {
|
|
12
14
|
".": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
+
"types": "./src/index.d.mts",
|
|
16
|
+
"default": "./src/index.mjs"
|
|
15
17
|
},
|
|
16
18
|
"./package.json": "./package.json"
|
|
17
19
|
},
|
|
18
20
|
"dependencies": {
|
|
19
|
-
"@griffel/core": "^1.20.
|
|
20
|
-
"@griffel/transform-shaker": "^1.0.
|
|
21
|
+
"@griffel/core": "^1.20.2",
|
|
22
|
+
"@griffel/transform-shaker": "^1.0.4",
|
|
21
23
|
"debug": "^4.3.0",
|
|
22
24
|
"magic-string": "^0.30.19",
|
|
23
25
|
"oxc-parser": "^0.116.0",
|
|
24
26
|
"oxc-walker": "^0.7.0"
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
+
},
|
|
28
|
+
"module": "./src/index.mts.js"
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../../../../packages/transform/src/constants.mts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,cAAc,GAAG,iBAAiB,CAAC;AAChD,MAAM,CAAC,MAAM,eAAe,GAAG,kBAAkB,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Node, Program } from 'oxc-parser';
|
|
2
|
-
import { EvaluationResult, AstEvaluatorPlugin } from './types.mjs';
|
|
1
|
+
import type { Node, Program } from 'oxc-parser';
|
|
2
|
+
import type { EvaluationResult, AstEvaluatorPlugin } from './types.mjs';
|
|
3
3
|
/**
|
|
4
4
|
* Sentinel value returned by plugins and evaluateNode to signal "can't handle this node".
|
|
5
5
|
* Using a symbol avoids the cost of Error construction and stack trace capture.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentinel value returned by plugins and evaluateNode to signal "can't handle this node".
|
|
3
|
+
* Using a symbol avoids the cost of Error construction and stack trace capture.
|
|
4
|
+
*/
|
|
5
|
+
export const DEOPT = Symbol('deopt');
|
|
6
|
+
/**
|
|
7
|
+
* Simple static evaluator for object expressions with nested objects.
|
|
8
|
+
* Based on Babel's evaluation approach but simplified for our specific use case.
|
|
9
|
+
*
|
|
10
|
+
* Handles:
|
|
11
|
+
* - Objects with nested objects: { root: { color: 'red', padding: 0 } }
|
|
12
|
+
* - String literals, numeric literals, boolean literals, null
|
|
13
|
+
* - Simple property access
|
|
14
|
+
*
|
|
15
|
+
* Plugins can extend evaluation to handle additional node types.
|
|
16
|
+
*/
|
|
17
|
+
export function astEvaluator(node, programAst, plugins = []) {
|
|
18
|
+
const context = {
|
|
19
|
+
programAst,
|
|
20
|
+
evaluateNode,
|
|
21
|
+
};
|
|
22
|
+
function evaluateNode(node) {
|
|
23
|
+
// Base cases
|
|
24
|
+
switch (node.type) {
|
|
25
|
+
case 'Literal':
|
|
26
|
+
return node.value;
|
|
27
|
+
case 'ObjectExpression':
|
|
28
|
+
return evaluateObjectExpression(node);
|
|
29
|
+
case 'TemplateLiteral':
|
|
30
|
+
if (node.expressions.length === 0) {
|
|
31
|
+
return node.quasis[0].value.cooked;
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
// Try plugins in order
|
|
36
|
+
// ---
|
|
37
|
+
for (const plugin of plugins) {
|
|
38
|
+
const result = plugin.evaluateNode(node, context);
|
|
39
|
+
if (result !== DEOPT) {
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return DEOPT;
|
|
44
|
+
}
|
|
45
|
+
function evaluateObjectExpression(node) {
|
|
46
|
+
const obj = {};
|
|
47
|
+
for (const prop of node.properties) {
|
|
48
|
+
if (prop.type !== 'Property' || prop.kind !== 'init') {
|
|
49
|
+
return DEOPT;
|
|
50
|
+
}
|
|
51
|
+
let key;
|
|
52
|
+
if (prop.computed) {
|
|
53
|
+
return DEOPT;
|
|
54
|
+
}
|
|
55
|
+
else if (prop.key.type === 'Identifier') {
|
|
56
|
+
key = prop.key.name;
|
|
57
|
+
}
|
|
58
|
+
else if (prop.key.type === 'Literal') {
|
|
59
|
+
const keyLiteral = prop.key;
|
|
60
|
+
if (typeof keyLiteral.value === 'string' || typeof keyLiteral.value === 'number') {
|
|
61
|
+
key = String(keyLiteral.value);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return DEOPT;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return DEOPT;
|
|
69
|
+
}
|
|
70
|
+
const value = evaluateNode(prop.value);
|
|
71
|
+
if (value === DEOPT) {
|
|
72
|
+
return DEOPT;
|
|
73
|
+
}
|
|
74
|
+
obj[key] = value;
|
|
75
|
+
}
|
|
76
|
+
return obj;
|
|
77
|
+
}
|
|
78
|
+
const result = evaluateNode(node);
|
|
79
|
+
if (result === DEOPT) {
|
|
80
|
+
return {
|
|
81
|
+
confident: false,
|
|
82
|
+
value: undefined,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
confident: true,
|
|
87
|
+
value: result,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=astEvaluator.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"astEvaluator.mjs","sourceRoot":"","sources":["../../../../../packages/transform/src/evaluation/astEvaluator.mts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,CAAC,MAAM,KAAK,GAAkB,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C;iBAAM,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAC1C,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;YACtB,CAAC;iBAAM,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBACvC,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC;gBAE5B,IAAI,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ,IAAI,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;oBACjF,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;gBACjC,CAAC;qBAAM,CAAC;oBACN,OAAO,KAAK,CAAC;gBACf,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,OAAO,KAAK,CAAC;YACf,CAAC;YAED,MAAM,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAEvC,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;gBACpB,OAAO,KAAK,CAAC;YACf,CAAC;YAED,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACnB,CAAC;QAED,OAAO,GAAG,CAAC;IACb,CAAC;IAED,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAElC,IAAI,MAAM,KAAK,KAAK,EAAE,CAAC;QACrB,OAAO;YACL,SAAS,EAAE,KAAK;YAChB,KAAK,EAAE,SAAS;SACjB,CAAC;IACJ,CAAC;IAED,OAAO;QACL,SAAS,EAAE,IAAI;QACf,KAAK,EAAE,MAAM;KACd,CAAC;AACJ,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Program } from 'oxc-parser';
|
|
2
|
-
import { StyleCall } from '../types.mjs';
|
|
3
|
-
import { TransformResolver } from './module.mjs';
|
|
4
|
-
import { AstEvaluatorPlugin, EvalRule } from './types.mjs';
|
|
1
|
+
import type { Program } from 'oxc-parser';
|
|
2
|
+
import type { StyleCall } from '../types.mjs';
|
|
3
|
+
import type { TransformResolver } from './module.mjs';
|
|
4
|
+
import type { AstEvaluatorPlugin, EvalRule } from './types.mjs';
|
|
5
5
|
/**
|
|
6
6
|
* Batch evaluates all style calls in a file for better performance.
|
|
7
7
|
* Uses static evaluation first, then falls back to VM evaluation for complex expressions.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { astEvaluator } from './astEvaluator.mjs';
|
|
2
|
+
import { vmEvaluator } from './vmEvaluator.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Batch evaluates all style calls in a file for better performance.
|
|
5
|
+
* Uses static evaluation first, then falls back to VM evaluation for complex expressions.
|
|
6
|
+
* Optimizes VM evaluation by sharing module loading and parsing overhead.
|
|
7
|
+
*/
|
|
8
|
+
export function batchEvaluator(sourceCode, filename, styleCalls, evaluationRules, resolveFilename, programAst, astEvaluationPlugins = []) {
|
|
9
|
+
const evaluationResults = new Array(styleCalls.length);
|
|
10
|
+
// Track which indices need VM evaluation and build the expression code string
|
|
11
|
+
const vmIndices = [];
|
|
12
|
+
let expressionCode = '';
|
|
13
|
+
// First pass: try static evaluation for all calls
|
|
14
|
+
for (let i = 0; i < styleCalls.length; i++) {
|
|
15
|
+
const styleCall = styleCalls[i];
|
|
16
|
+
const staticResult = astEvaluator(styleCall.argumentNode, programAst, astEvaluationPlugins);
|
|
17
|
+
if (staticResult.confident) {
|
|
18
|
+
evaluationResults[i] = staticResult.value;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
// Mark for VM evaluation
|
|
22
|
+
if (expressionCode.length > 0) {
|
|
23
|
+
expressionCode += ',';
|
|
24
|
+
}
|
|
25
|
+
expressionCode += styleCall.argumentCode;
|
|
26
|
+
vmIndices.push(i);
|
|
27
|
+
}
|
|
28
|
+
if (vmIndices.length === 0) {
|
|
29
|
+
return {
|
|
30
|
+
usedVMForEvaluation: false,
|
|
31
|
+
evaluationResults,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
// Second pass: batch VM evaluation for complex expressions
|
|
35
|
+
const vmResult = vmEvaluator(sourceCode, filename, expressionCode, evaluationRules, resolveFilename);
|
|
36
|
+
if (!vmResult.confident) {
|
|
37
|
+
if (vmResult.error) {
|
|
38
|
+
throw vmResult.error;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
throw new Error('Evaluation of a code fragment failed, this is a bug, please report it');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Map VM results back to correct indices
|
|
45
|
+
const vmValues = vmResult.value;
|
|
46
|
+
for (let i = 0; i < vmIndices.length; i++) {
|
|
47
|
+
evaluationResults[vmIndices[i]] = vmValues[i];
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
usedVMForEvaluation: true,
|
|
51
|
+
evaluationResults,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=batchEvaluator.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batchEvaluator.mjs","sourceRoot":"","sources":["../../../../../packages/transform/src/evaluation/batchEvaluator.mts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC5B,UAAkB,EAClB,QAAgB,EAChB,UAAuB,EACvB,eAA2B,EAC3B,eAAkC,EAClC,UAAmB,EACnB,uBAA6C,EAAE;IAK/C,MAAM,iBAAiB,GAAc,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAElE,8EAA8E;IAC9E,MAAM,SAAS,GAAa,EAAE,CAAC;IAC/B,IAAI,cAAc,GAAG,EAAE,CAAC;IAExB,kDAAkD;IAClD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,C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AAC,CAAC,CAAC;IAChD,CAAC;IAED,OAAO;QACL,mBAAmB,EAAE,IAAI;QACzB,iBAAiB;KAClB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evaluation cache for module results.
|
|
3
|
+
* Copied from @linaria/babel-preset v3.0.0-beta.19, adapted to use `debug` package.
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import createDebug from 'debug';
|
|
7
|
+
const debug = createDebug('griffel:eval-cache');
|
|
8
|
+
const fileHashes = new Map();
|
|
9
|
+
const evalCache = new Map();
|
|
10
|
+
const fileKeys = new Map();
|
|
11
|
+
const hash = (text) => createHash('sha1').update(text).digest('base64');
|
|
12
|
+
let lastText = '';
|
|
13
|
+
let lastHash = hash(lastText);
|
|
14
|
+
const memoizedHash = (text) => {
|
|
15
|
+
if (lastText !== text) {
|
|
16
|
+
lastHash = hash(text);
|
|
17
|
+
lastText = text;
|
|
18
|
+
}
|
|
19
|
+
return lastHash;
|
|
20
|
+
};
|
|
21
|
+
const toKey = (filename, exports) => exports.length > 0 ? `${filename}:${exports.join(',')}` : filename;
|
|
22
|
+
export const clear = () => {
|
|
23
|
+
fileHashes.clear();
|
|
24
|
+
evalCache.clear();
|
|
25
|
+
fileKeys.clear();
|
|
26
|
+
};
|
|
27
|
+
export const clearForFile = (filename) => {
|
|
28
|
+
const keys = fileKeys.get(filename) ?? [];
|
|
29
|
+
if (keys.length === 0) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
debug('clear-for-file', filename);
|
|
33
|
+
keys.forEach(key => {
|
|
34
|
+
fileHashes.delete(key);
|
|
35
|
+
evalCache.delete(key);
|
|
36
|
+
});
|
|
37
|
+
fileKeys.set(filename, []);
|
|
38
|
+
};
|
|
39
|
+
export const has = ([filename, ...exports], text) => {
|
|
40
|
+
const key = toKey(filename, exports);
|
|
41
|
+
const textHash = memoizedHash(text);
|
|
42
|
+
debug('has', `${key} ${textHash}`);
|
|
43
|
+
return fileHashes.get(key) === textHash;
|
|
44
|
+
};
|
|
45
|
+
export const get = ([filename, ...exports], text) => {
|
|
46
|
+
const key = toKey(filename, exports);
|
|
47
|
+
const textHash = memoizedHash(text);
|
|
48
|
+
debug('get', `${key} ${textHash}`);
|
|
49
|
+
if (fileHashes.get(key) !== textHash) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
return evalCache.get(key);
|
|
53
|
+
};
|
|
54
|
+
export const set = ([filename, ...exports], text, value) => {
|
|
55
|
+
const key = toKey(filename, exports);
|
|
56
|
+
const textHash = memoizedHash(text);
|
|
57
|
+
debug('set', `${key} ${textHash}`);
|
|
58
|
+
fileHashes.set(key, textHash);
|
|
59
|
+
evalCache.set(key, value);
|
|
60
|
+
if (!fileKeys.has(filename)) {
|
|
61
|
+
fileKeys.set(filename, []);
|
|
62
|
+
}
|
|
63
|
+
fileKeys.get(filename).push(key);
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=evalCache.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evalCache.mjs","sourceRoot":"","sources":["../../../../../packages/transform/src/evaluation/evalCache.mts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,WAAW,MAAM,OAAO,CAAC;AAEhC,MAAM,KAAK,GAAG,WAAW,CAAC,oBAAoB,CAAC,CAAC;AAEhD,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;AAC7C,MAAM,SAAS,GAAG,IAAI,GAAG,EAAmB,CAAC;AAC7C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAoB,CAAC;AAE7C,MAAM,IAAI,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AA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AAC,CAAC;IAC9B,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAE1B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5B,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACpC,CAAC,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { AstEvaluatorPlugin } from './types.mjs';
|
|
1
|
+
import type { AstEvaluatorPlugin } from './types.mjs';
|
|
2
2
|
export declare const fluentTokensPlugin: AstEvaluatorPlugin;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DEOPT } from './astEvaluator.mjs';
|
|
2
|
+
// zIndex tokens are not part of the theme and include default fallback values.
|
|
3
|
+
// See: https://github.com/microsoft/fluentui/blob/master/packages/tokens/src/global/zIndexes.ts
|
|
4
|
+
const Z_INDEX_DEFAULTS = {
|
|
5
|
+
zIndexBackground: 0,
|
|
6
|
+
zIndexContent: 1,
|
|
7
|
+
zIndexOverlay: 1000,
|
|
8
|
+
zIndexPopup: 2000,
|
|
9
|
+
zIndexMessages: 3000,
|
|
10
|
+
zIndexFloating: 4000,
|
|
11
|
+
zIndexPriority: 5000,
|
|
12
|
+
zIndexDebug: 6000,
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Evaluates template literals that contain Fluent UI design tokens.
|
|
16
|
+
* Transforms `${tokens.propertyName}` expressions into CSS custom properties: `var(--propertyName)`
|
|
17
|
+
*/
|
|
18
|
+
function evaluateTemplateLiteralWithTokens(node) {
|
|
19
|
+
let result = '';
|
|
20
|
+
for (let i = 0; i < node.quasis.length; i++) {
|
|
21
|
+
result += node.quasis[i].value.cooked;
|
|
22
|
+
if (i < node.expressions.length) {
|
|
23
|
+
const expression = node.expressions[i];
|
|
24
|
+
if (expression.type === 'MemberExpression' &&
|
|
25
|
+
expression.object.type === 'Identifier' &&
|
|
26
|
+
expression.object.name === 'tokens' &&
|
|
27
|
+
expression.property.type === 'Identifier' &&
|
|
28
|
+
!expression.computed) {
|
|
29
|
+
const name = expression.property.name;
|
|
30
|
+
const fallback = Z_INDEX_DEFAULTS[name];
|
|
31
|
+
result += fallback !== undefined ? `var(--${name}, ${fallback})` : `var(--${name})`;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
return DEOPT;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Evaluates member expressions that reference Fluent UI design tokens.
|
|
42
|
+
* Transforms `tokens.propertyName` expressions into CSS custom properties: `var(--propertyName)`
|
|
43
|
+
*/
|
|
44
|
+
function evaluateTokensMemberExpression(node) {
|
|
45
|
+
if (node.object.type === 'Identifier' &&
|
|
46
|
+
node.object.name === 'tokens' &&
|
|
47
|
+
node.property.type === 'Identifier' &&
|
|
48
|
+
!node.computed) {
|
|
49
|
+
const name = node.property.name;
|
|
50
|
+
const fallback = Z_INDEX_DEFAULTS[name];
|
|
51
|
+
return fallback !== undefined ? `var(--${name}, ${fallback})` : `var(--${name})`;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
return DEOPT;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export const fluentTokensPlugin = {
|
|
58
|
+
name: 'fluentTokensPlugin',
|
|
59
|
+
evaluateNode(node) {
|
|
60
|
+
switch (node.type) {
|
|
61
|
+
case 'TemplateLiteral':
|
|
62
|
+
return evaluateTemplateLiteralWithTokens(node);
|
|
63
|
+
case 'MemberExpression':
|
|
64
|
+
return evaluateTokensMemberExpression(node);
|
|
65
|
+
default:
|
|
66
|
+
return DEOPT;
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=fluentTokensPlugin.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fluentTokensPlugin.mjs","sourceRoot":"","sources":["../../../../../packages/transform/src/evaluation/fluentTokensPlugin.mts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAc,MAAM,oBAAoB,CAAC;AAGvD,+EAA+E;AAC/E,gGAAgG;AAChG,MAAM,gBAAgB,GAA2B;IAC/C,gBAAgB,EAAE,CAAC;IACnB,aAAa,EAAE,CAAC;IAChB,aAAa,EAAE,IAAI;IACnB,WAAW,EAAE,IAAI;IACjB,cAAc,EAAE,IAAI;IACpB,cAAc,EAAE,IAAI;IACpB,cAAc,EAAE,IAAI;IACpB,WAAW,EAAE,IAAI;CAClB,CAAC;AAEF;;;GAGG;AACH,SAAS,iCAAiC,CAAC,IAAqB;IAC9D,IAAI,MAAM,GAAG,EAAE,CAAC;IAEhB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5C,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC;QAEtC,IAAI,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC;YAChC,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YAEvC,IACE,UAAU,CAAC,IAAI,KAAK,kBAAkB;gBACtC,UAAU,CAAC,MAAM,CAAC,IAAI,KAAK,YAAY;gBACvC,UAAU,CAAC,MAAM,CAAC,IAAI,KAAK,QAAQ;gBACnC,UAAU,CAAC,QAAQ,CAAC,IAAI,KAAK,YAAY;gBACzC,CAAC,UAAU,CAAC,QAAQ,EACpB,CAAC;gBACD,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACtC,MAAM,QAAQ,GAAG,gBAAgB,CAAC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sandboxed VM context.
|
|
3
|
+
* Copied from @linaria/babel-preset v3.0.0-beta.19, adapted to use `debug` package
|
|
4
|
+
* and local imports instead of @linaria/* packages.
|
|
5
|
+
*
|
|
6
|
+
* This serves 2 purposes:
|
|
7
|
+
* - Avoid leakage from evaluated code to module cache in current context, e.g. `babel-register`
|
|
8
|
+
* - Allow us to invalidate the module cache without affecting other stuff, necessary for rebuilds
|
|
9
|
+
*
|
|
10
|
+
* We also use it to transpile the code with Babel by default.
|
|
11
|
+
* We also store source maps for it to provide correct error stacktraces.
|
|
12
|
+
*/
|
|
13
|
+
import type { EvalRule } from './types.mjs';
|
|
2
14
|
export type TransformResolver = (id: string, options: {
|
|
3
15
|
filename: string;
|
|
4
16
|
paths: readonly string[];
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom module system for evaluating code in a sandboxed VM context.
|
|
3
|
+
* Copied from @linaria/babel-preset v3.0.0-beta.19, adapted to use `debug` package
|
|
4
|
+
* and local imports instead of @linaria/* packages.
|
|
5
|
+
*
|
|
6
|
+
* This serves 2 purposes:
|
|
7
|
+
* - Avoid leakage from evaluated code to module cache in current context, e.g. `babel-register`
|
|
8
|
+
* - Allow us to invalidate the module cache without affecting other stuff, necessary for rebuilds
|
|
9
|
+
*
|
|
10
|
+
* We also use it to transpile the code with Babel by default.
|
|
11
|
+
* We also store source maps for it to provide correct error stacktraces.
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import NativeModule from 'node:module';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import vm from 'node:vm';
|
|
17
|
+
import createDebug from 'debug';
|
|
18
|
+
import { ASSET_TAG_OPEN, ASSET_TAG_CLOSE } from '../constants.mjs';
|
|
19
|
+
import { convertESMtoCJS } from '../utils/convertESMtoCJS.mjs';
|
|
20
|
+
import * as EvalCache from './evalCache.mjs';
|
|
21
|
+
import * as mockProcess from './process.mjs';
|
|
22
|
+
const debug = createDebug('griffel:module');
|
|
23
|
+
// Separate cache for evaluated modules
|
|
24
|
+
let cache = {};
|
|
25
|
+
const NOOP = () => { };
|
|
26
|
+
/** Checks if a value is an Error-like object (works across VM contexts where `instanceof Error` fails). */
|
|
27
|
+
function isError(e) {
|
|
28
|
+
return e != null && typeof e === 'object' && 'message' in e && 'stack' in e;
|
|
29
|
+
}
|
|
30
|
+
const createCustomDebug = (depth) => (namespaces, arg1, ...args) => {
|
|
31
|
+
const modulePrefix = depth === 0 ? 'module' : `sub-module-${depth}`;
|
|
32
|
+
debug(`${modulePrefix}:${namespaces}`, arg1, ...args);
|
|
33
|
+
};
|
|
34
|
+
export class Module {
|
|
35
|
+
id;
|
|
36
|
+
filename;
|
|
37
|
+
rules;
|
|
38
|
+
imports = null;
|
|
39
|
+
dependencies = null;
|
|
40
|
+
transform = null;
|
|
41
|
+
static extensions = new Set(['.json', '.js', '.jsx', '.ts', '.tsx', '.cjs', '.mjs', '.mts', '.cts']);
|
|
42
|
+
exports = {};
|
|
43
|
+
resolveFilename;
|
|
44
|
+
debug;
|
|
45
|
+
debuggerDepth;
|
|
46
|
+
constructor(filename, rules, resolveFilename, debuggerDepth = 0) {
|
|
47
|
+
this.id = filename;
|
|
48
|
+
this.filename = filename;
|
|
49
|
+
this.rules = rules;
|
|
50
|
+
this.resolveFilename = resolveFilename;
|
|
51
|
+
this.debuggerDepth = debuggerDepth;
|
|
52
|
+
this.debug = createCustomDebug(debuggerDepth);
|
|
53
|
+
Object.defineProperties(this, {
|
|
54
|
+
id: {
|
|
55
|
+
value: filename,
|
|
56
|
+
writable: false,
|
|
57
|
+
},
|
|
58
|
+
filename: {
|
|
59
|
+
value: filename,
|
|
60
|
+
writable: false,
|
|
61
|
+
},
|
|
62
|
+
paths: {
|
|
63
|
+
value: Object.freeze(NativeModule._nodeModulePaths(path.dirname(filename))),
|
|
64
|
+
writable: false,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
this.exports = {};
|
|
68
|
+
this.debug('prepare', filename);
|
|
69
|
+
}
|
|
70
|
+
require = Object.assign((id) => {
|
|
71
|
+
this.debug('require', id);
|
|
72
|
+
// Resolve module id (and filename) relatively to parent module
|
|
73
|
+
const resolved = this.resolveFilename(id, this);
|
|
74
|
+
if (resolved.builtin) {
|
|
75
|
+
throw new Error(`Unable to import "${id}". Importing Node builtins is not supported in the sandbox.`);
|
|
76
|
+
}
|
|
77
|
+
const filename = resolved.path;
|
|
78
|
+
this.dependencies?.push(id);
|
|
79
|
+
let cacheKey = filename;
|
|
80
|
+
let only = [];
|
|
81
|
+
if (this.imports?.has(id)) {
|
|
82
|
+
// We know what exactly we need from this module. Let's shake it!
|
|
83
|
+
only = this.imports.get(id).sort();
|
|
84
|
+
if (only.length === 0) {
|
|
85
|
+
// Probably the module is used as a value itself
|
|
86
|
+
// like `'The answer is ' + require('./module')`
|
|
87
|
+
only = ['default'];
|
|
88
|
+
}
|
|
89
|
+
cacheKey += `:${only.join(',')}`;
|
|
90
|
+
}
|
|
91
|
+
let m = cache[cacheKey];
|
|
92
|
+
if (!m) {
|
|
93
|
+
this.debug('cached:not-exist', id);
|
|
94
|
+
// Create the module if cached module is not available
|
|
95
|
+
m = new Module(filename, this.rules, this.resolveFilename, this.debuggerDepth + 1);
|
|
96
|
+
m.transform = this.transform;
|
|
97
|
+
// Store it in cache at this point, otherwise
|
|
98
|
+
// we would end up in infinite loop with cyclic dependencies
|
|
99
|
+
cache[cacheKey] = m;
|
|
100
|
+
const ext = path.extname(filename).toLowerCase();
|
|
101
|
+
if (Module.extensions.has(ext)) {
|
|
102
|
+
// To evaluate the file, we need to read it first
|
|
103
|
+
const code = fs.readFileSync(filename, 'utf-8');
|
|
104
|
+
if (ext === '.json') {
|
|
105
|
+
// For JSON files, parse it to a JS object similar to Node
|
|
106
|
+
m.exports = JSON.parse(code);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// For JS/TS files, evaluate the module
|
|
110
|
+
// The module will be transpiled using provided transform
|
|
111
|
+
m.evaluate(code, only.includes('*') ? ['*'] : only);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// For non JS/JSON requires, export the resolved absolute path wrapped in asset tags.
|
|
116
|
+
// This allows downstream tools (e.g. webpack plugin) to resolve the path correctly
|
|
117
|
+
// when CSS is extracted to a different location.
|
|
118
|
+
m.exports = ASSET_TAG_OPEN + filename + ASSET_TAG_CLOSE;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
this.debug('cached:exist', id);
|
|
123
|
+
}
|
|
124
|
+
return m.exports;
|
|
125
|
+
}, {
|
|
126
|
+
ensure: NOOP,
|
|
127
|
+
cache,
|
|
128
|
+
resolve: (id) => this.resolveFilename(id, this).path,
|
|
129
|
+
});
|
|
130
|
+
evaluate(text, only = null, useEvalCache = true) {
|
|
131
|
+
const { filename } = this;
|
|
132
|
+
// Find last matching rule (iterate backwards, break on first match)
|
|
133
|
+
let action = 'ignore';
|
|
134
|
+
for (let i = this.rules.length - 1; i >= 0; i--) {
|
|
135
|
+
const { test } = this.rules[i];
|
|
136
|
+
if (!test || (typeof test === 'function' ? test(filename) : test.test(filename))) {
|
|
137
|
+
action = this.rules[i].action;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const cacheKey = [this.filename, ...(only ?? [])];
|
|
142
|
+
if (useEvalCache && EvalCache.has(cacheKey, text)) {
|
|
143
|
+
this.exports = EvalCache.get(cacheKey, text);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
let code;
|
|
147
|
+
if (action === 'ignore') {
|
|
148
|
+
this.debug('ignore', `${filename}`);
|
|
149
|
+
code = text;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
// For JavaScript files, we need to transpile it and to get the exports of the module
|
|
153
|
+
this.debug('prepare-evaluation', this.filename, 'using', action.name);
|
|
154
|
+
const result = action(this.filename, text, only);
|
|
155
|
+
code = result.code;
|
|
156
|
+
this.imports = result.imports;
|
|
157
|
+
// Convert ESM syntax to CJS so it can run inside a function wrapper in vm.Script
|
|
158
|
+
if (result.moduleKind === 'esm') {
|
|
159
|
+
code = convertESMtoCJS(code, this.filename);
|
|
160
|
+
}
|
|
161
|
+
this.debug('evaluate', `${this.filename} (only ${(only || []).join(', ')}):\n${code}`);
|
|
162
|
+
}
|
|
163
|
+
const script = new vm.Script(`(function (exports) { ${code}\n})(exports);`, {
|
|
164
|
+
filename: this.filename,
|
|
165
|
+
});
|
|
166
|
+
try {
|
|
167
|
+
script.runInContext(vm.createContext({
|
|
168
|
+
clearImmediate: NOOP,
|
|
169
|
+
clearInterval: NOOP,
|
|
170
|
+
clearTimeout: NOOP,
|
|
171
|
+
setImmediate: NOOP,
|
|
172
|
+
setInterval: NOOP,
|
|
173
|
+
setTimeout: NOOP,
|
|
174
|
+
fetch: NOOP,
|
|
175
|
+
global,
|
|
176
|
+
process: mockProcess,
|
|
177
|
+
module: this,
|
|
178
|
+
exports: this.exports,
|
|
179
|
+
require: this.require,
|
|
180
|
+
__filename: this.filename,
|
|
181
|
+
__dirname: path.dirname(this.filename),
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
catch (vmError) {
|
|
185
|
+
// Errors thrown inside vm.runInContext() use the VM context's Error constructor,
|
|
186
|
+
// so they fail `instanceof Error` in the host context (e.g. webpack wraps them as NonErrorEmittedError).
|
|
187
|
+
// Re-create as a host Error with filename context.
|
|
188
|
+
const message = isError(vmError) ? vmError.message : String(vmError);
|
|
189
|
+
const hostError = new Error(message);
|
|
190
|
+
if (isError(vmError)) {
|
|
191
|
+
hostError.stack = vmError.stack;
|
|
192
|
+
}
|
|
193
|
+
throw hostError;
|
|
194
|
+
}
|
|
195
|
+
if (useEvalCache) {
|
|
196
|
+
EvalCache.set(cacheKey, text, this.exports);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
static invalidate = () => {
|
|
200
|
+
cache = {};
|
|
201
|
+
};
|
|
202
|
+
static invalidateEvalCache = () => {
|
|
203
|
+
EvalCache.clear();
|
|
204
|
+
};
|
|
205
|
+
static _nodeModulePaths = (filename) => NativeModule._nodeModulePaths(filename);
|
|
206
|
+
}
|
|
207
|
+
//# sourceMappingURL=module.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.mjs","sourceRoot":"","sources":["../../../../../packages/transform/src/evaluation/module.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,YAAY,MAAM,aAAa,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,WAAW,MAAM,OAAO,CAAC;AAEhC,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,WAAW,MAAM,eAAe,CAAC;AAQ7C,MAAM,KAAK,GAAG,WAAW,CAAC,gBAAgB,CAAC,CAAC;AAE5C,uCAAuC;AACvC,IAAI,KAAK,GAA2B,EAAE,CAAC;AAEvC,MAAM,IAAI,GAAG,GAAG,EAAE,GAAc,CAAC,CAAC;AAElC,2GAA2G;AAC3G,SAAS,OAAO,CAAC,CAAU;IACzB,OAAO,CAAC,IAAI,IAAI,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,SAAS,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,CAAC;AAC9E,CAAC;AAED,MAAM,iBAAiB,GACrB,CAAC,KAAa,EAAE,EAAE,CAClB,CAAC,UAAkB,EAAE,IAAa,EAAE,GAAG,IAAe,EAAE,EAAE;IACxD,MAAM,YAAY,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,cAAc,KAAK,E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