@wyw-in-js/transform 2.0.0-alpha.0 → 2.0.0-alpha.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/esm/cache.js +7 -0
- package/esm/cache.js.map +1 -1
- package/esm/debug/fileReporter.js +35 -1
- package/esm/debug/fileReporter.js.map +1 -1
- package/esm/eval/broker.js +230 -89
- package/esm/eval/broker.js.map +1 -1
- package/esm/eval/protocol.js.map +1 -1
- package/esm/eval/runner.js +143 -24
- package/esm/eval/runner.js.map +1 -1
- package/esm/module.js +70 -43
- package/esm/module.js.map +1 -1
- package/esm/transform/Entrypoint.types.js.map +1 -1
- package/esm/transform/generators/processEntrypoint.js +35 -20
- package/esm/transform/generators/processEntrypoint.js.map +1 -1
- package/esm/transform/generators/resolveImports.js +48 -2
- package/esm/transform/generators/resolveImports.js.map +1 -1
- package/esm/transform/generators/resolveStaticOxcValues/cache.js +182 -0
- package/esm/transform/generators/resolveStaticOxcValues/cache.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js +146 -0
- package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/dependencies.js +33 -0
- package/esm/transform/generators/resolveStaticOxcValues/dependencies.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/environment.js +29 -0
- package/esm/transform/generators/resolveStaticOxcValues/environment.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js +351 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js +253 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js +310 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +74 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +229 -0
- package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js +172 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js +476 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js +55 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js +123 -0
- package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/prune.js +186 -0
- package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +183 -0
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js +298 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +298 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/types.js +3 -0
- package/esm/transform/generators/resolveStaticOxcValues/types.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +59 -0
- package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues.js +1 -2910
- package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -1
- package/esm/transform/generators/transform.js +57 -45
- package/esm/transform/generators/transform.js.map +1 -1
- package/esm/transform/helpers/loadWywOptions.js +33 -4
- package/esm/transform/helpers/loadWywOptions.js.map +1 -1
- package/esm/transform.js +3 -1
- package/esm/transform.js.map +1 -1
- package/esm/utils/EventEmitter.js +42 -9
- package/esm/utils/EventEmitter.js.map +1 -1
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js +160 -0
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js.map +1 -0
- package/esm/utils/applyOxcProcessors/cleanupBindings.js +157 -0
- package/esm/utils/applyOxcProcessors/cleanupBindings.js.map +1 -0
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js +431 -0
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js.map +1 -0
- package/esm/utils/applyOxcProcessors/displayName.js +93 -0
- package/esm/utils/applyOxcProcessors/displayName.js.map +1 -0
- package/esm/utils/applyOxcProcessors/expressionValues.js +152 -0
- package/esm/utils/applyOxcProcessors/expressionValues.js.map +1 -0
- package/esm/utils/applyOxcProcessors/processorFactory.js +62 -0
- package/esm/utils/applyOxcProcessors/processorFactory.js.map +1 -0
- package/esm/utils/applyOxcProcessors/processorUsages.js +143 -0
- package/esm/utils/applyOxcProcessors/processorUsages.js.map +1 -0
- package/esm/utils/applyOxcProcessors/sameFileStaticValues.js +207 -0
- package/esm/utils/applyOxcProcessors/sameFileStaticValues.js.map +1 -0
- package/esm/utils/applyOxcProcessors/shared.js +29 -0
- package/esm/utils/applyOxcProcessors/shared.js.map +1 -0
- package/esm/utils/applyOxcProcessors/types.js +2 -0
- package/esm/utils/applyOxcProcessors/types.js.map +1 -0
- package/esm/utils/applyOxcProcessors.js +1 -1216
- package/esm/utils/applyOxcProcessors.js.map +1 -1
- package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js +157 -0
- package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js.map +1 -0
- package/esm/utils/collectOxcRuntime/sourceMap.js +36 -0
- package/esm/utils/collectOxcRuntime/sourceMap.js.map +1 -0
- package/esm/utils/collectOxcRuntime/types.js +2 -0
- package/esm/utils/collectOxcRuntime/types.js.map +1 -0
- package/esm/utils/collectOxcRuntime.js +5 -193
- package/esm/utils/collectOxcRuntime.js.map +1 -1
- package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js +496 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js +113 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js +387 -0
- package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/staticBindings.js +17 -0
- package/esm/utils/collectOxcTemplateDependencies/staticBindings.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js +540 -0
- package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/types.js +2 -0
- package/esm/utils/collectOxcTemplateDependencies/types.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies.js +3 -1397
- package/esm/utils/collectOxcTemplateDependencies.js.map +1 -1
- package/esm/utils/nativeResolver.js +93 -0
- package/esm/utils/nativeResolver.js.map +1 -0
- package/esm/utils/oxc/ast.js +28 -0
- package/esm/utils/oxc/ast.js.map +1 -0
- package/esm/utils/oxc/parse.js +3 -0
- package/esm/utils/oxc/parse.js.map +1 -0
- package/esm/utils/oxc/replacements.js +14 -0
- package/esm/utils/oxc/replacements.js.map +1 -0
- package/esm/utils/oxc/sourceLocations.js +59 -0
- package/esm/utils/oxc/sourceLocations.js.map +1 -0
- package/esm/utils/oxcPreevalStage/evalStrategy.js +3 -0
- package/esm/utils/oxcPreevalStage/evalStrategy.js.map +1 -0
- package/esm/utils/oxcPreevalStage/prepareCode.js +21 -0
- package/esm/utils/oxcPreevalStage/prepareCode.js.map +1 -0
- package/esm/utils/oxcPreevalStage/prevalExport.js +23 -0
- package/esm/utils/oxcPreevalStage/prevalExport.js.map +1 -0
- package/esm/utils/oxcPreevalStage/processors.js +17 -0
- package/esm/utils/oxcPreevalStage/processors.js.map +1 -0
- package/esm/utils/oxcPreevalStage/staticOverlay.js +18 -0
- package/esm/utils/oxcPreevalStage/staticOverlay.js.map +1 -0
- package/esm/utils/oxcPreevalStage/types.js +2 -0
- package/esm/utils/oxcPreevalStage/types.js.map +1 -0
- package/esm/utils/oxcPreevalStage.js +17 -73
- package/esm/utils/oxcPreevalStage.js.map +1 -1
- package/esm/utils/oxcPreevalTransforms.js +12 -1
- package/esm/utils/oxcPreevalTransforms.js.map +1 -1
- package/esm/utils/processorStaticSemantics.js +157 -0
- package/esm/utils/processorStaticSemantics.js.map +1 -0
- package/esm/utils/resolveWithConditions.js +3 -3
- package/esm/utils/resolveWithConditions.js.map +1 -1
- package/package.json +4 -3
- package/types/cache.js +8 -0
- package/types/debug/fileReporter.js +46 -1
- package/types/eval/broker.d.ts +6 -2
- package/types/eval/broker.js +244 -95
- package/types/eval/protocol.d.ts +15 -1
- package/types/module.d.ts +4 -1
- package/types/module.js +97 -48
- package/types/transform/Entrypoint.types.d.ts +3 -0
- package/types/transform/generators/resolveImports.d.ts +3 -1
- package/types/transform/generators/resolveImports.js +49 -2
- package/types/transform/generators/resolveStaticOxcValues/cache.d.ts +25 -0
- package/types/transform/generators/resolveStaticOxcValues/cache.js +182 -0
- package/types/transform/generators/resolveStaticOxcValues/candidateResolver.d.ts +5 -0
- package/types/transform/generators/resolveStaticOxcValues/candidateResolver.js +149 -0
- package/types/transform/generators/resolveStaticOxcValues/dependencies.d.ts +6 -0
- package/types/transform/generators/resolveStaticOxcValues/dependencies.js +37 -0
- package/types/transform/generators/resolveStaticOxcValues/environment.d.ts +12 -0
- package/types/transform/generators/resolveStaticOxcValues/environment.js +32 -0
- package/types/transform/generators/resolveStaticOxcValues/exportResolver.d.ts +4 -0
- package/types/transform/generators/resolveStaticOxcValues/exportResolver.js +358 -0
- package/types/transform/generators/resolveStaticOxcValues/exportTargets.d.ts +20 -0
- package/types/transform/generators/resolveStaticOxcValues/exportTargets.js +285 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssign.d.ts +41 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssign.js +352 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.d.ts +6 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +88 -0
- package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.d.ts +13 -0
- package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +263 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.d.ts +4 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.js +191 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.d.ts +40 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.js +516 -0
- package/types/transform/generators/resolveStaticOxcValues/processorTarget.d.ts +5 -0
- package/types/transform/generators/resolveStaticOxcValues/processorTarget.js +62 -0
- package/types/transform/generators/resolveStaticOxcValues/programAnalysis.d.ts +11 -0
- package/types/transform/generators/resolveStaticOxcValues/programAnalysis.js +133 -0
- package/types/transform/generators/resolveStaticOxcValues/prune.d.ts +15 -0
- package/types/transform/generators/resolveStaticOxcValues/prune.js +220 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.d.ts +2 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +197 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpression.d.ts +25 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpression.js +391 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.d.ts +14 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +304 -0
- package/types/transform/generators/resolveStaticOxcValues/types.d.ts +99 -0
- package/types/transform/generators/resolveStaticOxcValues/types.js +2 -0
- package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.d.ts +7 -0
- package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +64 -0
- package/types/transform/generators/resolveStaticOxcValues.d.ts +1 -2
- package/types/transform/generators/resolveStaticOxcValues.js +1 -3235
- package/types/transform/generators/transform.js +63 -49
- package/types/transform/helpers/loadWywOptions.js +23 -3
- package/types/transform.js +3 -1
- package/types/utils/EventEmitter.d.ts +16 -1
- package/types/utils/EventEmitter.js +44 -9
- package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +8 -0
- package/types/utils/applyOxcProcessors/applyOxcProcessors.js +167 -0
- package/types/utils/applyOxcProcessors/cleanupBindings.d.ts +12 -0
- package/types/utils/applyOxcProcessors/cleanupBindings.js +189 -0
- package/types/utils/applyOxcProcessors/cleanupRemovals.d.ts +16 -0
- package/types/utils/applyOxcProcessors/cleanupRemovals.js +482 -0
- package/types/utils/applyOxcProcessors/displayName.d.ts +9 -0
- package/types/utils/applyOxcProcessors/displayName.js +113 -0
- package/types/utils/applyOxcProcessors/expressionValues.d.ts +11 -0
- package/types/utils/applyOxcProcessors/expressionValues.js +170 -0
- package/types/utils/applyOxcProcessors/processorFactory.d.ts +7 -0
- package/types/utils/applyOxcProcessors/processorFactory.js +70 -0
- package/types/utils/applyOxcProcessors/processorUsages.d.ts +16 -0
- package/types/utils/applyOxcProcessors/processorUsages.js +163 -0
- package/types/utils/applyOxcProcessors/sameFileStaticValues.d.ts +23 -0
- package/types/utils/applyOxcProcessors/sameFileStaticValues.js +211 -0
- package/types/utils/applyOxcProcessors/shared.d.ts +10 -0
- package/types/utils/applyOxcProcessors/shared.js +37 -0
- package/types/utils/applyOxcProcessors/types.d.ts +88 -0
- package/types/utils/applyOxcProcessors/types.js +1 -0
- package/types/utils/applyOxcProcessors.d.ts +1 -16
- package/types/utils/applyOxcProcessors.js +1 -1391
- package/types/utils/collectOxcRuntime/normalizeRuntimeCode.d.ts +1 -0
- package/types/utils/collectOxcRuntime/normalizeRuntimeCode.js +185 -0
- package/types/utils/collectOxcRuntime/sourceMap.d.ts +2 -0
- package/types/utils/collectOxcRuntime/sourceMap.js +37 -0
- package/types/utils/collectOxcRuntime/types.d.ts +16 -0
- package/types/utils/collectOxcRuntime/types.js +1 -0
- package/types/utils/collectOxcRuntime.d.ts +2 -12
- package/types/utils/collectOxcRuntime.js +5 -224
- package/types/utils/collectOxcTemplateDependencies/expressionExtraction.d.ts +6 -0
- package/types/utils/collectOxcTemplateDependencies/expressionExtraction.js +550 -0
- package/types/utils/collectOxcTemplateDependencies/expressionReplacements.d.ts +11 -0
- package/types/utils/collectOxcTemplateDependencies/expressionReplacements.js +131 -0
- package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.d.ts +20 -0
- package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.js +434 -0
- package/types/utils/collectOxcTemplateDependencies/staticBindings.d.ts +7 -0
- package/types/utils/collectOxcTemplateDependencies/staticBindings.js +13 -0
- package/types/utils/collectOxcTemplateDependencies/staticEvaluator.d.ts +13 -0
- package/types/utils/collectOxcTemplateDependencies/staticEvaluator.js +626 -0
- package/types/utils/collectOxcTemplateDependencies/types.d.ts +116 -0
- package/types/utils/collectOxcTemplateDependencies/types.js +1 -0
- package/types/utils/collectOxcTemplateDependencies.d.ts +4 -38
- package/types/utils/collectOxcTemplateDependencies.js +3 -1580
- package/types/utils/nativeResolver.d.ts +13 -0
- package/types/utils/nativeResolver.js +91 -0
- package/types/utils/oxc/ast.d.ts +4 -0
- package/types/utils/oxc/ast.js +37 -0
- package/types/utils/oxc/parse.d.ts +3 -0
- package/types/utils/oxc/parse.js +2 -0
- package/types/utils/oxc/replacements.d.ts +12 -0
- package/types/utils/oxc/replacements.js +18 -0
- package/types/utils/oxc/sourceLocations.d.ts +5 -0
- package/types/utils/oxc/sourceLocations.js +63 -0
- package/types/utils/oxcPreevalStage/evalStrategy.d.ts +3 -0
- package/types/utils/oxcPreevalStage/evalStrategy.js +2 -0
- package/types/utils/oxcPreevalStage/prepareCode.d.ts +3 -0
- package/types/utils/oxcPreevalStage/prepareCode.js +20 -0
- package/types/utils/oxcPreevalStage/prevalExport.d.ts +1 -0
- package/types/utils/oxcPreevalStage/prevalExport.js +22 -0
- package/types/utils/oxcPreevalStage/processors.d.ts +10 -0
- package/types/utils/oxcPreevalStage/processors.js +16 -0
- package/types/utils/oxcPreevalStage/staticOverlay.d.ts +3 -0
- package/types/utils/oxcPreevalStage/staticOverlay.js +23 -0
- package/types/utils/oxcPreevalStage/types.d.ts +22 -0
- package/types/utils/oxcPreevalStage/types.js +1 -0
- package/types/utils/oxcPreevalStage.d.ts +2 -18
- package/types/utils/oxcPreevalStage.js +17 -79
- package/types/utils/oxcPreevalTransforms.js +14 -1
- package/types/utils/processorStaticSemantics.d.ts +13 -0
- package/types/utils/processorStaticSemantics.js +191 -0
- package/types/utils/resolveWithConditions.js +3 -3
- package/esm/eval/resolverStrategy.js +0 -51
- package/esm/eval/resolverStrategy.js.map +0 -1
- package/types/eval/resolverStrategy.d.ts +0 -13
- package/types/eval/resolverStrategy.js +0 -46
|
@@ -1,195 +1,6 @@
|
|
|
1
|
-
/* eslint-disable no-restricted-syntax */
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import { parseSync } from "oxc-parser";
|
|
4
|
-
import { SourceMapGenerator } from "source-map";
|
|
5
1
|
import { applyOxcProcessors } from "./applyOxcProcessors.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const parseOxc = (code, filename) => {
|
|
9
|
-
const parsed = parseSync(filename, code, {
|
|
10
|
-
astType: filename.endsWith(".ts") || filename.endsWith(".tsx") ? "ts" : "js",
|
|
11
|
-
range: true,
|
|
12
|
-
sourceType: "module"
|
|
13
|
-
});
|
|
14
|
-
const fatalError = parsed.errors.find((error) => error.severity === "Error");
|
|
15
|
-
if (fatalError) {
|
|
16
|
-
throw new Error(fatalError.message);
|
|
17
|
-
}
|
|
18
|
-
return parsed.program;
|
|
19
|
-
};
|
|
20
|
-
const applyReplacements = (code, replacements) => {
|
|
21
|
-
let result = code;
|
|
22
|
-
replacements.sort((a, b) => b.start - a.start).forEach((replacement) => {
|
|
23
|
-
result = result.slice(0, replacement.start) + replacement.value + result.slice(replacement.end);
|
|
24
|
-
});
|
|
25
|
-
return result;
|
|
26
|
-
};
|
|
27
|
-
const shouldTerminateWithSemicolon = (node, parent, key) => {
|
|
28
|
-
if (node.type === "VariableDeclaration" && parent) {
|
|
29
|
-
if ((parent.type === "ForStatement" || parent.type === "ForInStatement" || parent.type === "ForOfStatement") && key && ["init", "left"].includes(key)) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
if (parent.type === "ExportNamedDeclaration") {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (node.type === "ImportDeclaration" || node.type === "ExpressionStatement" || node.type === "ReturnStatement" || node.type === "ThrowStatement" || node.type === "VariableDeclaration") {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
if (node.type === "ExportNamedDeclaration") {
|
|
40
|
-
if (node.declaration) {
|
|
41
|
-
return node.declaration.type === "VariableDeclaration";
|
|
42
|
-
}
|
|
43
|
-
return Array.isArray(node.specifiers) && node.specifiers.length > 0;
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
};
|
|
47
|
-
const getChildren = (node) => {
|
|
48
|
-
const result = [];
|
|
49
|
-
const record = node;
|
|
50
|
-
Object.keys(record).forEach((key) => {
|
|
51
|
-
if (key === "type" || key === "start" || key === "end" || key === "range") {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const value = record[key];
|
|
55
|
-
if (value && typeof value === "object" && "type" in value) {
|
|
56
|
-
result.push({
|
|
57
|
-
key,
|
|
58
|
-
node: value
|
|
59
|
-
});
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (Array.isArray(value)) {
|
|
63
|
-
value.forEach((item) => {
|
|
64
|
-
if (item && typeof item === "object" && "type" in item) {
|
|
65
|
-
result.push({
|
|
66
|
-
key,
|
|
67
|
-
node: item
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
return result;
|
|
74
|
-
};
|
|
75
|
-
const getLineIndent = (code, offset) => {
|
|
76
|
-
const lineStart = code.lastIndexOf("\n", offset - 1) + 1;
|
|
77
|
-
let idx = lineStart;
|
|
78
|
-
while (idx < code.length && (code[idx] === " " || code[idx] === " ")) {
|
|
79
|
-
idx += 1;
|
|
80
|
-
}
|
|
81
|
-
return code.slice(lineStart, idx).replace(/\t/g, " ");
|
|
82
|
-
};
|
|
83
|
-
const isPlainObjectProperty = (property) => property.type === "Property" && property.kind === "init" && property.method !== true;
|
|
84
|
-
const isFormattableObjectExpression = (node) => node.type === "ObjectExpression" && node.properties.every((property) => property.type === "SpreadElement" || isPlainObjectProperty(property));
|
|
85
|
-
const hasNestedObjectValue = (node) => node.type === "ObjectExpression" && node.properties.some((property) => isPlainObjectProperty(property) && property.value.type === "ObjectExpression");
|
|
86
|
-
const printFormattedObjectExpression = (node, code, baseIndent) => {
|
|
87
|
-
if (node.type !== "ObjectExpression" || node.properties.length === 0 || !isFormattableObjectExpression(node)) {
|
|
88
|
-
return code.slice(node.start, node.end);
|
|
89
|
-
}
|
|
90
|
-
const lines = node.properties.map((property) => {
|
|
91
|
-
if (property.type === "SpreadElement") {
|
|
92
|
-
return `${baseIndent} ...${code.slice(property.argument.start, property.argument.end)}`;
|
|
93
|
-
}
|
|
94
|
-
const keySource = property.computed ? `[${code.slice(property.key.start, property.key.end)}]` : code.slice(property.key.start, property.key.end);
|
|
95
|
-
const valueSource = property.value.type === "ObjectExpression" ? printFormattedObjectExpression(property.value, code, `${baseIndent} `) : code.slice(property.value.start, property.value.end).trim();
|
|
96
|
-
return `${baseIndent} ${keySource}: ${valueSource}`;
|
|
97
|
-
});
|
|
98
|
-
return `{\n${lines.join(",\n")}\n${baseIndent}}`;
|
|
99
|
-
};
|
|
100
|
-
const formatRuntimeObjectLiterals = (code, filename) => {
|
|
101
|
-
const replacements = [];
|
|
102
|
-
const walk = (node, parent = null) => {
|
|
103
|
-
if (node.type === "ObjectExpression") {
|
|
104
|
-
const shouldFormat = parent?.type === "VariableDeclarator" && hasNestedObjectValue(node) || parent?.type === "CallExpression";
|
|
105
|
-
if (shouldFormat) {
|
|
106
|
-
replacements.push({
|
|
107
|
-
end: node.end,
|
|
108
|
-
start: node.start,
|
|
109
|
-
value: printFormattedObjectExpression(node, code, getLineIndent(code, node.start))
|
|
110
|
-
});
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
getChildren(node).forEach((child) => walk(child.node, node));
|
|
115
|
-
};
|
|
116
|
-
walk(parseOxc(code, filename));
|
|
117
|
-
return replacements.length > 0 ? applyReplacements(code, replacements) : code;
|
|
118
|
-
};
|
|
119
|
-
const collapseRuntimeBlankLines = (code) => {
|
|
120
|
-
const lines = code.split("\n");
|
|
121
|
-
const result = [];
|
|
122
|
-
for (let idx = 0; idx < lines.length; idx += 1) {
|
|
123
|
-
const line = lines[idx];
|
|
124
|
-
if (line.trim() !== "") {
|
|
125
|
-
result.push(line);
|
|
126
|
-
} else {
|
|
127
|
-
let nextIdx = idx;
|
|
128
|
-
while (nextIdx < lines.length && lines[nextIdx]?.trim() === "") {
|
|
129
|
-
nextIdx += 1;
|
|
130
|
-
}
|
|
131
|
-
const previousNonEmpty = [...result].reverse().find((entry) => entry.trim() !== "");
|
|
132
|
-
const nextNonEmpty = lines.slice(nextIdx).find((entry) => entry.trim() !== "");
|
|
133
|
-
if (previousNonEmpty && nextNonEmpty) {
|
|
134
|
-
const trimmedPrevious = previousNonEmpty.trim();
|
|
135
|
-
if (trimmedPrevious.startsWith("//") || trimmedPrevious.endsWith("*/")) {
|
|
136
|
-
result.push("");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
idx = nextIdx - 1;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return result.join("\n");
|
|
143
|
-
};
|
|
144
|
-
const ensureBlankLineAfterLeadingBlockComment = (code) => code.replace(/^(\/\*[\s\S]*?\*\/)\n(?!\n)/, "$1\n\n");
|
|
145
|
-
const insertMissingSemicolons = (code, filename) => {
|
|
146
|
-
const replacements = [];
|
|
147
|
-
const hasTrailingSemicolon = (node) => code.slice(node.start, node.end).trimEnd().endsWith(";");
|
|
148
|
-
const walk = (node, parent = null, key = null) => {
|
|
149
|
-
if (shouldTerminateWithSemicolon(node, parent, key) && !hasTrailingSemicolon(node)) {
|
|
150
|
-
replacements.push({
|
|
151
|
-
end: node.end,
|
|
152
|
-
start: node.end,
|
|
153
|
-
value: ";"
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
getChildren(node).forEach((child) => walk(child.node, node, child.key));
|
|
157
|
-
};
|
|
158
|
-
walk(parseOxc(code, filename));
|
|
159
|
-
return replacements.length > 0 ? applyReplacements(code, replacements) : code;
|
|
160
|
-
};
|
|
161
|
-
const createLineSourceMap = (generatedCode, originalCode, filename) => {
|
|
162
|
-
const generator = new SourceMapGenerator({ file: filename });
|
|
163
|
-
const generatedLines = countLines(generatedCode);
|
|
164
|
-
const originalLines = countLines(originalCode);
|
|
165
|
-
for (let line = 1; line <= generatedLines; line += 1) {
|
|
166
|
-
generator.addMapping({
|
|
167
|
-
generated: {
|
|
168
|
-
column: 0,
|
|
169
|
-
line
|
|
170
|
-
},
|
|
171
|
-
original: {
|
|
172
|
-
column: 0,
|
|
173
|
-
line: Math.min(line, originalLines)
|
|
174
|
-
},
|
|
175
|
-
source: filename
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
generator.setSourceContent(filename, originalCode);
|
|
179
|
-
return generator.toJSON();
|
|
180
|
-
};
|
|
181
|
-
const createComposedRuntimeSourceMap = (generatedCode, originalCode, filename, inputSourceMap) => {
|
|
182
|
-
const runtimeMap = createLineSourceMap(generatedCode, originalCode, filename);
|
|
183
|
-
if (!inputSourceMap) {
|
|
184
|
-
return runtimeMap;
|
|
185
|
-
}
|
|
186
|
-
const composed = remapping([runtimeMap, inputSourceMap], () => null);
|
|
187
|
-
return {
|
|
188
|
-
...composed,
|
|
189
|
-
file: runtimeMap.file
|
|
190
|
-
};
|
|
191
|
-
};
|
|
192
|
-
const normalizeRuntimeCode = (code, filename) => insertMissingSemicolons(ensureBlankLineAfterLeadingBlockComment(collapseRuntimeBlankLines(formatRuntimeObjectLiterals(code.replace(/^\n+/, "").replace(/\n+$/, "").replace(/[ \t]+\n/g, "\n"), filename))), filename);
|
|
2
|
+
import { normalizeRuntimeCode } from "./collectOxcRuntime/normalizeRuntimeCode.js";
|
|
3
|
+
import { createComposedRuntimeSourceMap } from "./collectOxcRuntime/sourceMap.js";
|
|
193
4
|
export const collectOxcRuntime = (code, filename, root, options, values, inputSourceMap) => {
|
|
194
5
|
const result = applyOxcProcessors(code, {
|
|
195
6
|
filename,
|
|
@@ -199,16 +10,17 @@ export const collectOxcRuntime = (code, filename, root, options, values, inputSo
|
|
|
199
10
|
processor.doRuntimeReplacement();
|
|
200
11
|
}, true);
|
|
201
12
|
const normalizedCode = normalizeRuntimeCode(result.code, filename);
|
|
13
|
+
const map = createComposedRuntimeSourceMap(normalizedCode, code, filename, inputSourceMap);
|
|
202
14
|
if (result.processors.length === 0) {
|
|
203
15
|
return {
|
|
204
16
|
code: normalizedCode,
|
|
205
|
-
map
|
|
17
|
+
map,
|
|
206
18
|
metadata: null
|
|
207
19
|
};
|
|
208
20
|
}
|
|
209
21
|
return {
|
|
210
22
|
code: normalizedCode,
|
|
211
|
-
map
|
|
23
|
+
map,
|
|
212
24
|
metadata: {
|
|
213
25
|
dependencies: [],
|
|
214
26
|
processors: result.processors,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";AAEA,SAAS,qBAAqB;AAC9B,SAAS,iBAAiB;AAI1B,SAAS,0BAA6C;AAGtD,SAAS,0BAA0B;AAwCn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AAO,GAAG,QAAQ,gBAAgB,QAAQ,GAAG;AACpD,YAAU,WAAW;GACnB,WAAW;IACT,QAAQ;IACR;IACD;GACD,UAAU;IACR,QAAQ;IACR,MAAM,KAAK,IAAI,MAAM,cAAc;IACpC;GACD,QAAQ;GACT,CAAC;;AAGJ,WAAU,iBAAiB,UAAU,aAAa;AAElD,QAAO,UAAU,QAAQ;;AAG3B,MAAM,kCACJ,eACA,cACA,UACA,mBACiB;CACjB,MAAM,aAAa,oBAAoB,eAAe,cAAc,SAAS;AAC7E,KAAI,CAAC,gBAAgB;AACnB,SAAO;;CAGT,MAAM,WAAW,UAAU,CAAC,YAAY,eAAe,QAAQ,KAAK;AACpE,QAAO;EACL,GAAG;EACH,MAAM,WAAW;EAClB;;AAGH,MAAM,wBAAwB,MAAc,aAC1C,wBACE,wCACE,0BACE,4BACE,KACG,QAAQ,QAAQ,GAAG,CACnB,QAAQ,QAAQ,GAAG,CACnB,QAAQ,aAAa,KAAK,EAC7B,SACD,CACF,CACF,EACD,SACD;AAEH,OAAO,MAAM,qBACX,MACA,UACA,MACA,SACA,QACA,mBACqB;CACrB,MAAM,SAAS,mBACb,MACA;EACE;EACA;EACD,EACD,UACC,cAAc;AACb,YAAU,MAAM,OAAO;AACvB,YAAU,sBAAsB;IAElC,KACD;CACD,MAAM,iBAAiB,qBAAqB,OAAO,MAAM,SAAS;AAElE,KAAI,OAAO,WAAW,WAAW,GAAG;AAClC,SAAO;GACL,MAAM;GACN,KAAK,+BACH,gBACA,MACA,UACA,eACD;GACD,UAAU;GACX;;AAGH,QAAO;EACL,MAAM;EACN,KAAK,+BACH,gBACA,MACA,UACA,eACD;EACD,UAAU;GACR,cAAc,EAAE;GAChB,YAAY,OAAO;GACnB,cAAc,EAAE;GAChB,OAAO,EAAE;GACV;EACF","names":[],"sources":["../../src/utils/collectOxcRuntime.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax */\n\nimport { createRequire } from 'node:module';\nimport { parseSync } from 'oxc-parser';\nimport type { Node, Program } from 'oxc-parser';\nimport type { ValueCache } from '@wyw-in-js/processor-utils';\nimport type { StrictOptions } from '@wyw-in-js/shared';\nimport { SourceMapGenerator, type RawSourceMap } from 'source-map';\n\nimport type { WYWTransformMetadata } from './TransformMetadata';\nimport { applyOxcProcessors } from './applyOxcProcessors';\n\ntype OxcCollectOptions = Pick<\n StrictOptions,\n | 'classNameSlug'\n | 'displayName'\n | 'evaluate'\n | 'extensions'\n | 'tagResolver'\n | 'variableNameConfig'\n> & {\n preserveSideEffectImportLocals?: Set<string>;\n};\n\ntype OxcCollectResult = {\n code: string;\n map: RawSourceMap;\n metadata: WYWTransformMetadata | null;\n};\n\ntype Replacement = {\n end: number;\n start: number;\n value: string;\n};\n\ntype AnyNode = Node & Record<string, unknown>;\ntype AnyProperty = AnyNode & {\n computed?: boolean;\n key: Node;\n kind?: string;\n method?: boolean;\n value: Node;\n};\n\ntype RemappingFn = (\n input: RawSourceMap | RawSourceMap[],\n loader: (source: string, context: unknown) => RawSourceMap | null | undefined\n) => RawSourceMap;\n\nconst remapping = createRequire(import.meta.url)(\n '@jridgewell/remapping'\n) as RemappingFn;\n\nconst countLines = (code: string): number => code.split('\\n').length;\n\nconst parseOxc = (code: string, filename: string): Program => {\n const parsed = parseSync(filename, code, {\n astType:\n filename.endsWith('.ts') || filename.endsWith('.tsx') ? 'ts' : 'js',\n range: true,\n sourceType: 'module',\n });\n const fatalError = parsed.errors.find((error) => error.severity === 'Error');\n if (fatalError) {\n throw new Error(fatalError.message);\n }\n\n return parsed.program as Program;\n};\n\nconst applyReplacements = (\n code: string,\n replacements: Replacement[]\n): string => {\n let result = code;\n replacements\n .sort((a, b) => b.start - a.start)\n .forEach((replacement) => {\n result =\n result.slice(0, replacement.start) +\n replacement.value +\n result.slice(replacement.end);\n });\n\n return result;\n};\n\nconst shouldTerminateWithSemicolon = (\n node: Node,\n parent: Node | null,\n key: string | null\n): boolean => {\n if (node.type === 'VariableDeclaration' && parent) {\n if (\n (parent.type === 'ForStatement' ||\n parent.type === 'ForInStatement' ||\n parent.type === 'ForOfStatement') &&\n key &&\n ['init', 'left'].includes(key)\n ) {\n return false;\n }\n\n if (parent.type === 'ExportNamedDeclaration') {\n return false;\n }\n }\n\n if (\n node.type === 'ImportDeclaration' ||\n node.type === 'ExpressionStatement' ||\n node.type === 'ReturnStatement' ||\n node.type === 'ThrowStatement' ||\n node.type === 'VariableDeclaration'\n ) {\n return true;\n }\n\n if (node.type === 'ExportNamedDeclaration') {\n if (node.declaration) {\n return node.declaration.type === 'VariableDeclaration';\n }\n\n return Array.isArray(node.specifiers) && node.specifiers.length > 0;\n }\n\n return false;\n};\n\nconst getChildren = (node: Node): Array<{ key: string | null; node: Node }> => {\n const result: Array<{ key: string | null; node: Node }> = [];\n const record = node as Node & Record<string, unknown>;\n\n Object.keys(record).forEach((key) => {\n if (key === 'type' || key === 'start' || key === 'end' || key === 'range') {\n return;\n }\n\n const value = record[key];\n if (value && typeof value === 'object' && 'type' in (value as object)) {\n result.push({ key, node: value as Node });\n return;\n }\n\n if (Array.isArray(value)) {\n value.forEach((item) => {\n if (item && typeof item === 'object' && 'type' in (item as object)) {\n result.push({ key, node: item as Node });\n }\n });\n }\n });\n\n return result;\n};\n\nconst getLineIndent = (code: string, offset: number): string => {\n const lineStart = code.lastIndexOf('\\n', offset - 1) + 1;\n let idx = lineStart;\n while (idx < code.length && (code[idx] === ' ' || code[idx] === '\\t')) {\n idx += 1;\n }\n\n return code.slice(lineStart, idx).replace(/\\t/g, ' ');\n};\n\nconst isPlainObjectProperty = (property: Node): property is AnyProperty =>\n property.type === 'Property' &&\n (property as AnyProperty).kind === 'init' &&\n (property as AnyProperty).method !== true;\n\nconst isFormattableObjectExpression = (node: Node): boolean =>\n node.type === 'ObjectExpression' &&\n node.properties.every(\n (property) =>\n property.type === 'SpreadElement' || isPlainObjectProperty(property)\n );\n\nconst hasNestedObjectValue = (node: Node): boolean =>\n node.type === 'ObjectExpression' &&\n node.properties.some(\n (property) =>\n isPlainObjectProperty(property) &&\n property.value.type === 'ObjectExpression'\n );\n\nconst printFormattedObjectExpression = (\n node: Node,\n code: string,\n baseIndent: string\n): string => {\n if (\n node.type !== 'ObjectExpression' ||\n node.properties.length === 0 ||\n !isFormattableObjectExpression(node)\n ) {\n return code.slice(node.start, node.end);\n }\n\n const lines = node.properties.map((property) => {\n if (property.type === 'SpreadElement') {\n return `${baseIndent} ...${code.slice(\n property.argument.start,\n property.argument.end\n )}`;\n }\n\n const keySource = property.computed\n ? `[${code.slice(property.key.start, property.key.end)}]`\n : code.slice(property.key.start, property.key.end);\n const valueSource =\n property.value.type === 'ObjectExpression'\n ? printFormattedObjectExpression(\n property.value,\n code,\n `${baseIndent} `\n )\n : code.slice(property.value.start, property.value.end).trim();\n\n return `${baseIndent} ${keySource}: ${valueSource}`;\n });\n\n return `{\\n${lines.join(',\\n')}\\n${baseIndent}}`;\n};\n\nconst formatRuntimeObjectLiterals = (\n code: string,\n filename: string\n): string => {\n const replacements: Replacement[] = [];\n\n const walk = (node: Node, parent: Node | null = null): void => {\n if (node.type === 'ObjectExpression') {\n const shouldFormat =\n (parent?.type === 'VariableDeclarator' && hasNestedObjectValue(node)) ||\n parent?.type === 'CallExpression';\n\n if (shouldFormat) {\n replacements.push({\n end: node.end,\n start: node.start,\n value: printFormattedObjectExpression(\n node,\n code,\n getLineIndent(code, node.start)\n ),\n });\n return;\n }\n }\n\n getChildren(node).forEach((child) => walk(child.node, node));\n };\n\n walk(parseOxc(code, filename));\n return replacements.length > 0 ? applyReplacements(code, replacements) : code;\n};\n\nconst collapseRuntimeBlankLines = (code: string): string => {\n const lines = code.split('\\n');\n const result: string[] = [];\n\n for (let idx = 0; idx < lines.length; idx += 1) {\n const line = lines[idx]!;\n if (line.trim() !== '') {\n result.push(line);\n } else {\n let nextIdx = idx;\n while (nextIdx < lines.length && lines[nextIdx]?.trim() === '') {\n nextIdx += 1;\n }\n\n const previousNonEmpty = [...result]\n .reverse()\n .find((entry) => entry.trim() !== '');\n const nextNonEmpty = lines\n .slice(nextIdx)\n .find((entry) => entry.trim() !== '');\n\n if (previousNonEmpty && nextNonEmpty) {\n const trimmedPrevious = previousNonEmpty.trim();\n if (\n trimmedPrevious.startsWith('//') ||\n trimmedPrevious.endsWith('*/')\n ) {\n result.push('');\n }\n }\n\n idx = nextIdx - 1;\n }\n }\n\n return result.join('\\n');\n};\n\nconst ensureBlankLineAfterLeadingBlockComment = (code: string): string =>\n code.replace(/^(\\/\\*[\\s\\S]*?\\*\\/)\\n(?!\\n)/, '$1\\n\\n');\n\nconst insertMissingSemicolons = (code: string, filename: string): string => {\n const replacements: Replacement[] = [];\n const hasTrailingSemicolon = (node: Node): boolean =>\n code.slice(node.start, node.end).trimEnd().endsWith(';');\n\n const walk = (\n node: Node,\n parent: Node | null = null,\n key: string | null = null\n ): void => {\n if (\n shouldTerminateWithSemicolon(node, parent, key) &&\n !hasTrailingSemicolon(node)\n ) {\n replacements.push({\n end: node.end,\n start: node.end,\n value: ';',\n });\n }\n\n getChildren(node).forEach((child) => walk(child.node, node, child.key));\n };\n\n walk(parseOxc(code, filename));\n return replacements.length > 0 ? applyReplacements(code, replacements) : code;\n};\n\nconst createLineSourceMap = (\n generatedCode: string,\n originalCode: string,\n filename: string\n): RawSourceMap => {\n const generator = new SourceMapGenerator({\n file: filename,\n });\n const generatedLines = countLines(generatedCode);\n const originalLines = countLines(originalCode);\n\n for (let line = 1; line <= generatedLines; line += 1) {\n generator.addMapping({\n generated: {\n column: 0,\n line,\n },\n original: {\n column: 0,\n line: Math.min(line, originalLines),\n },\n source: filename,\n });\n }\n\n generator.setSourceContent(filename, originalCode);\n\n return generator.toJSON() as RawSourceMap;\n};\n\nconst createComposedRuntimeSourceMap = (\n generatedCode: string,\n originalCode: string,\n filename: string,\n inputSourceMap?: RawSourceMap\n): RawSourceMap => {\n const runtimeMap = createLineSourceMap(generatedCode, originalCode, filename);\n if (!inputSourceMap) {\n return runtimeMap;\n }\n\n const composed = remapping([runtimeMap, inputSourceMap], () => null);\n return {\n ...composed,\n file: runtimeMap.file,\n } as RawSourceMap;\n};\n\nconst normalizeRuntimeCode = (code: string, filename: string): string =>\n insertMissingSemicolons(\n ensureBlankLineAfterLeadingBlockComment(\n collapseRuntimeBlankLines(\n formatRuntimeObjectLiterals(\n code\n .replace(/^\\n+/, '')\n .replace(/\\n+$/, '')\n .replace(/[ \\t]+\\n/g, '\\n'),\n filename\n )\n )\n ),\n filename\n );\n\nexport const collectOxcRuntime = (\n code: string,\n filename: string,\n root: string,\n options: OxcCollectOptions,\n values: ValueCache,\n inputSourceMap?: RawSourceMap\n): OxcCollectResult => {\n const result = applyOxcProcessors(\n code,\n {\n filename,\n root,\n },\n options,\n (processor) => {\n processor.build(values);\n processor.doRuntimeReplacement();\n },\n true\n );\n const normalizedCode = normalizeRuntimeCode(result.code, filename);\n\n if (result.processors.length === 0) {\n return {\n code: normalizedCode,\n map: createComposedRuntimeSourceMap(\n normalizedCode,\n code,\n filename,\n inputSourceMap\n ),\n metadata: null,\n };\n }\n\n return {\n code: normalizedCode,\n map: createComposedRuntimeSourceMap(\n normalizedCode,\n code,\n filename,\n inputSourceMap\n ),\n metadata: {\n dependencies: [],\n processors: result.processors,\n replacements: [],\n rules: {},\n },\n };\n};\n"],"file":"collectOxcRuntime.js"}
|
|
1
|
+
{"mappings":"AAGA,SAAS,0BAA0B;AACnC,SAAS,4BAA4B;AACrC,SAAS,sCAAsC;AAM/C,OAAO,MAAM,qBACX,MACA,UACA,MACA,SACA,QACA,mBACqB;CACrB,MAAM,SAAS,mBACb,MACA;EACE;EACA;EACD,EACD,UACC,cAAc;AACb,YAAU,MAAM,OAAO;AACvB,YAAU,sBAAsB;IAElC,KACD;CACD,MAAM,iBAAiB,qBAAqB,OAAO,MAAM,SAAS;CAClE,MAAM,MAAM,+BACV,gBACA,MACA,UACA,eACD;AAED,KAAI,OAAO,WAAW,WAAW,GAAG;AAClC,SAAO;GACL,MAAM;GACN;GACA,UAAU;GACX;;AAGH,QAAO;EACL,MAAM;EACN;EACA,UAAU;GACR,cAAc,EAAE;GAChB,YAAY,OAAO;GACnB,cAAc,EAAE;GAChB,OAAO,EAAE;GACV;EACF","names":[],"sources":["../../src/utils/collectOxcRuntime.ts"],"version":3,"sourcesContent":["import type { ValueCache } from '@wyw-in-js/processor-utils';\nimport type { RawSourceMap } from 'source-map';\n\nimport { applyOxcProcessors } from './applyOxcProcessors';\nimport { normalizeRuntimeCode } from './collectOxcRuntime/normalizeRuntimeCode';\nimport { createComposedRuntimeSourceMap } from './collectOxcRuntime/sourceMap';\nimport type {\n OxcCollectOptions,\n OxcCollectResult,\n} from './collectOxcRuntime/types';\n\nexport const collectOxcRuntime = (\n code: string,\n filename: string,\n root: string,\n options: OxcCollectOptions,\n values: ValueCache,\n inputSourceMap?: RawSourceMap\n): OxcCollectResult => {\n const result = applyOxcProcessors(\n code,\n {\n filename,\n root,\n },\n options,\n (processor) => {\n processor.build(values);\n processor.doRuntimeReplacement();\n },\n true\n );\n const normalizedCode = normalizeRuntimeCode(result.code, filename);\n const map = createComposedRuntimeSourceMap(\n normalizedCode,\n code,\n filename,\n inputSourceMap\n );\n\n if (result.processors.length === 0) {\n return {\n code: normalizedCode,\n map,\n metadata: null,\n };\n }\n\n return {\n code: normalizedCode,\n map,\n metadata: {\n dependencies: [],\n processors: result.processors,\n replacements: [],\n rules: {},\n },\n };\n};\n"],"file":"collectOxcRuntime.js"}
|