@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
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EvalResolverKind, OxcOptions, StrictOptions } from '@wyw-in-js/shared';
|
|
2
|
+
export type NativeResolverParams = {
|
|
3
|
+
conditionNames?: string[];
|
|
4
|
+
extensions: StrictOptions['extensions'];
|
|
5
|
+
importer: string;
|
|
6
|
+
kind: EvalResolverKind;
|
|
7
|
+
oxcOptions?: OxcOptions;
|
|
8
|
+
specifier: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const expandNativeResolverConditions: (kind: EvalResolverKind, conditionNames?: readonly string[]) => string[];
|
|
11
|
+
export declare const clearNativeResolverCacheForTest: () => void;
|
|
12
|
+
export declare const getNativeResolverCacheSizeForTest: () => number;
|
|
13
|
+
export declare const resolveWithNativeResolver: ({ conditionNames, extensions, importer, kind, oxcOptions, specifier, }: NativeResolverParams) => string;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { ResolverFactory } from 'oxc-resolver';
|
|
4
|
+
import { parseRequest } from './parseRequest';
|
|
5
|
+
const CJS_DEFAULT_CONDITIONS = ['require', 'node', 'default'];
|
|
6
|
+
const ESM_DEFAULT_CONDITIONS = ['node', 'import', 'default'];
|
|
7
|
+
const FALLBACK_EXTENSIONS = ['.json', '.node'];
|
|
8
|
+
const MAX_RESOLVER_CACHE_SIZE = 64;
|
|
9
|
+
const resolverCache = new Map();
|
|
10
|
+
const unique = (items) => Array.from(new Set(items));
|
|
11
|
+
export const expandNativeResolverConditions = (kind, conditionNames) => {
|
|
12
|
+
const defaults = kind === 'require' ? CJS_DEFAULT_CONDITIONS : ESM_DEFAULT_CONDITIONS;
|
|
13
|
+
const names = conditionNames?.length ? conditionNames : ['...'];
|
|
14
|
+
const result = [];
|
|
15
|
+
names.forEach((name) => {
|
|
16
|
+
if (name === '...') {
|
|
17
|
+
defaults.forEach((condition) => result.push(condition));
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
result.push(name);
|
|
21
|
+
});
|
|
22
|
+
return unique(result);
|
|
23
|
+
};
|
|
24
|
+
const createResolverOptions = ({ conditionNames, extensions, kind, oxcOptions, }) => {
|
|
25
|
+
const configuredResolver = (oxcOptions?.resolver ?? {});
|
|
26
|
+
const hasConfiguredTsconfig = Object.prototype.hasOwnProperty.call(configuredResolver, 'tsconfig');
|
|
27
|
+
const configuredConditionNames = Array.isArray(configuredResolver.conditionNames)
|
|
28
|
+
? configuredResolver.conditionNames
|
|
29
|
+
: undefined;
|
|
30
|
+
const configuredExtensions = Array.isArray(configuredResolver.extensions)
|
|
31
|
+
? configuredResolver.extensions
|
|
32
|
+
: [];
|
|
33
|
+
return {
|
|
34
|
+
...configuredResolver,
|
|
35
|
+
...(hasConfiguredTsconfig ? {} : { tsconfig: 'auto' }),
|
|
36
|
+
conditionNames: expandNativeResolverConditions(kind, conditionNames ?? configuredConditionNames),
|
|
37
|
+
extensions: unique([
|
|
38
|
+
...configuredExtensions,
|
|
39
|
+
...extensions,
|
|
40
|
+
...FALLBACK_EXTENSIONS,
|
|
41
|
+
]),
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const getResolver = (options) => {
|
|
45
|
+
const key = JSON.stringify(options);
|
|
46
|
+
const cached = resolverCache.get(key);
|
|
47
|
+
if (cached) {
|
|
48
|
+
resolverCache.delete(key);
|
|
49
|
+
resolverCache.set(key, cached);
|
|
50
|
+
return cached;
|
|
51
|
+
}
|
|
52
|
+
const resolver = new ResolverFactory(options);
|
|
53
|
+
if (resolverCache.size >= MAX_RESOLVER_CACHE_SIZE) {
|
|
54
|
+
const oldestKey = resolverCache.keys().next().value;
|
|
55
|
+
if (oldestKey !== undefined) {
|
|
56
|
+
resolverCache.delete(oldestKey);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
resolverCache.set(key, resolver);
|
|
60
|
+
return resolver;
|
|
61
|
+
};
|
|
62
|
+
export const clearNativeResolverCacheForTest = () => {
|
|
63
|
+
resolverCache.clear();
|
|
64
|
+
};
|
|
65
|
+
export const getNativeResolverCacheSizeForTest = () => resolverCache.size;
|
|
66
|
+
const preferJsOverCjsForExtensionlessFileSpecifier = (specifier, resolved) => {
|
|
67
|
+
if ((specifier.startsWith('.') || path.isAbsolute(specifier)) &&
|
|
68
|
+
path.extname(specifier) === '' &&
|
|
69
|
+
resolved.endsWith('.cjs') &&
|
|
70
|
+
fs.existsSync(`${resolved.slice(0, -4)}.js`)) {
|
|
71
|
+
return `${resolved.slice(0, -4)}.js`;
|
|
72
|
+
}
|
|
73
|
+
return resolved;
|
|
74
|
+
};
|
|
75
|
+
export const resolveWithNativeResolver = ({ conditionNames, extensions, importer, kind, oxcOptions, specifier, }) => {
|
|
76
|
+
const { filename, query, hash } = parseRequest(specifier);
|
|
77
|
+
const options = createResolverOptions({
|
|
78
|
+
conditionNames,
|
|
79
|
+
extensions,
|
|
80
|
+
kind,
|
|
81
|
+
oxcOptions,
|
|
82
|
+
});
|
|
83
|
+
const resolver = getResolver(options);
|
|
84
|
+
const result = resolver.resolveFileSync(importer, filename);
|
|
85
|
+
if (!result.path) {
|
|
86
|
+
throw new Error(result.error ?? `Cannot resolve module ${specifier}`);
|
|
87
|
+
}
|
|
88
|
+
const resolved = preferJsOverCjsForExtensionlessFileSpecifier(filename, result.path);
|
|
89
|
+
const suffix = `${query ? `?${query}` : ''}${hash ? `#${hash}` : ''}`;
|
|
90
|
+
return `${resolved}${suffix}`;
|
|
91
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
export declare const isOxcNode: (value: unknown) => value is Node;
|
|
3
|
+
export declare const getOxcNodeChildren: (node: Node) => Node[];
|
|
4
|
+
export declare const walkOxc: (node: Node, enter: (node: Node, parent: Node | null) => void, parent?: Node | null) => void;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const isOxcNode = (value) => !!value &&
|
|
2
|
+
typeof value === 'object' &&
|
|
3
|
+
'type' in value &&
|
|
4
|
+
typeof value.type === 'string';
|
|
5
|
+
export const getOxcNodeChildren = (node) => {
|
|
6
|
+
const result = [];
|
|
7
|
+
const record = node;
|
|
8
|
+
Object.keys(record).forEach((key) => {
|
|
9
|
+
if (key === 'comments' ||
|
|
10
|
+
key === 'end' ||
|
|
11
|
+
key === 'errors' ||
|
|
12
|
+
key === 'parent' ||
|
|
13
|
+
key === 'range' ||
|
|
14
|
+
key === 'span' ||
|
|
15
|
+
key === 'start' ||
|
|
16
|
+
key === 'type') {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const value = record[key];
|
|
20
|
+
if (isOxcNode(value)) {
|
|
21
|
+
result.push(value);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(value)) {
|
|
25
|
+
value.forEach((item) => {
|
|
26
|
+
if (isOxcNode(item)) {
|
|
27
|
+
result.push(item);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
export const walkOxc = (node, enter, parent = null) => {
|
|
35
|
+
enter(node, parent);
|
|
36
|
+
getOxcNodeChildren(node).forEach((child) => walkOxc(child, enter, node));
|
|
37
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type OxcValueReplacement = {
|
|
2
|
+
end: number;
|
|
3
|
+
start: number;
|
|
4
|
+
value: string;
|
|
5
|
+
};
|
|
6
|
+
export type OxcTextReplacement = {
|
|
7
|
+
end: number;
|
|
8
|
+
start: number;
|
|
9
|
+
text: string;
|
|
10
|
+
};
|
|
11
|
+
export type OxcReplacement = OxcTextReplacement | OxcValueReplacement;
|
|
12
|
+
export declare const applyOxcReplacements: (code: string, replacements: OxcReplacement[]) => string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const getReplacementValue = (replacement) => {
|
|
2
|
+
if ('value' in replacement) {
|
|
3
|
+
return replacement.value;
|
|
4
|
+
}
|
|
5
|
+
return replacement.text;
|
|
6
|
+
};
|
|
7
|
+
export const applyOxcReplacements = (code, replacements) => {
|
|
8
|
+
let result = code;
|
|
9
|
+
[...replacements]
|
|
10
|
+
.sort((a, b) => b.start - a.start)
|
|
11
|
+
.forEach((replacement) => {
|
|
12
|
+
result =
|
|
13
|
+
result.slice(0, replacement.start) +
|
|
14
|
+
getReplacementValue(replacement) +
|
|
15
|
+
result.slice(replacement.end);
|
|
16
|
+
});
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SourceLocation } from '@wyw-in-js/shared';
|
|
2
|
+
export type OxcLocationLookup = (offset: number) => SourceLocation['start'];
|
|
3
|
+
export declare const createOxcLocationLookup: (code: string) => OxcLocationLookup;
|
|
4
|
+
export declare const createOxcSourceLocation: (start: number, end: number, loc: OxcLocationLookup, filename?: string | null, identifierName?: string | null) => SourceLocation;
|
|
5
|
+
export declare const buildOxcCodeFrameError: (code: string, location: SourceLocation, message: string) => Error;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export const createOxcLocationLookup = (code) => {
|
|
2
|
+
const lineStarts = [0];
|
|
3
|
+
for (let idx = 0; idx < code.length; idx += 1) {
|
|
4
|
+
if (code[idx] === '\n') {
|
|
5
|
+
lineStarts.push(idx + 1);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return (offset) => {
|
|
9
|
+
let low = 0;
|
|
10
|
+
let high = lineStarts.length - 1;
|
|
11
|
+
while (low <= high) {
|
|
12
|
+
const mid = Math.floor((low + high) / 2);
|
|
13
|
+
const next = lineStarts[mid + 1] ?? Infinity;
|
|
14
|
+
if (lineStarts[mid] <= offset && offset < next) {
|
|
15
|
+
return {
|
|
16
|
+
column: offset - lineStarts[mid],
|
|
17
|
+
line: mid + 1,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (offset < lineStarts[mid]) {
|
|
21
|
+
high = mid - 1;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
low = mid + 1;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const lastLine = lineStarts.length - 1;
|
|
28
|
+
return {
|
|
29
|
+
column: Math.max(0, offset - lineStarts[lastLine]),
|
|
30
|
+
line: lastLine + 1,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export const createOxcSourceLocation = (start, end, loc, filename, identifierName) => ({
|
|
35
|
+
end: loc(end),
|
|
36
|
+
filename: filename ?? undefined,
|
|
37
|
+
identifierName,
|
|
38
|
+
start: loc(start),
|
|
39
|
+
});
|
|
40
|
+
export const buildOxcCodeFrameError = (code, location, message) => {
|
|
41
|
+
const lines = code.split('\n');
|
|
42
|
+
const startLine = location.start.line;
|
|
43
|
+
const endLine = location.end.line;
|
|
44
|
+
const frameStart = Math.max(1, startLine - 2);
|
|
45
|
+
const frameEnd = Math.min(lines.length, endLine + 2);
|
|
46
|
+
const lineNoWidth = String(frameEnd).length;
|
|
47
|
+
const frame = [];
|
|
48
|
+
for (let lineNo = frameStart; lineNo <= frameEnd; lineNo += 1) {
|
|
49
|
+
const marker = lineNo === startLine ? '>' : ' ';
|
|
50
|
+
const line = lines[lineNo - 1] ?? '';
|
|
51
|
+
frame.push(line.length > 0
|
|
52
|
+
? `${marker} ${String(lineNo).padStart(lineNoWidth)} | ${line}`
|
|
53
|
+
: `${marker} ${String(lineNo).padStart(lineNoWidth)} |`);
|
|
54
|
+
if (lineNo === startLine) {
|
|
55
|
+
const pointerLength = startLine === endLine
|
|
56
|
+
? Math.max(1, location.end.column - location.start.column)
|
|
57
|
+
: 1;
|
|
58
|
+
frame.push(` ${' '.repeat(lineNoWidth)} | ${' '.repeat(location.start.column)}${'^'.repeat(pointerLength)}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const prefix = location.filename ? `${location.filename}: ` : '';
|
|
62
|
+
return new Error(`${prefix}${message}\n${frame.join('\n')}`);
|
|
63
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { isFeatureEnabled } from '@wyw-in-js/shared';
|
|
2
|
+
import { removeDangerousCodeWithOxc, replaceImportMetaEnvWithOxc, rewriteDynamicImportsAndAddRequireFallbackWithOxc, } from '../oxcPreevalTransforms';
|
|
3
|
+
const DYNAMIC_IMPORT_RE = /\bimport(?:\s|\/\*[\s\S]*?\*\/)*\(/;
|
|
4
|
+
const REQUIRE_CALL_RE = /\brequire(?:\s|\/\*[\s\S]*?\*\/)*\(/;
|
|
5
|
+
export const prepareOxcPreevalCode = (code, filename, options, eventEmitter) => {
|
|
6
|
+
let nextCode = eventEmitter.perf('transform:preeval:importMetaEnv', () => replaceImportMetaEnvWithOxc(code, filename));
|
|
7
|
+
if (isFeatureEnabled(options.features, 'dangerousCodeRemover', filename)) {
|
|
8
|
+
nextCode = eventEmitter.perf('transform:preeval:removeDangerousCode', () => removeDangerousCodeWithOxc(nextCode, filename, options.codeRemover));
|
|
9
|
+
}
|
|
10
|
+
const shouldRewriteDynamicImports = DYNAMIC_IMPORT_RE.test(nextCode);
|
|
11
|
+
const shouldAddRequireFallback = REQUIRE_CALL_RE.test(nextCode);
|
|
12
|
+
if (shouldRewriteDynamicImports || shouldAddRequireFallback) {
|
|
13
|
+
nextCode = rewriteDynamicImportsAndAddRequireFallbackWithOxc(nextCode, filename, {
|
|
14
|
+
addRequireFallback: shouldAddRequireFallback,
|
|
15
|
+
eventEmitter,
|
|
16
|
+
rewriteDynamicImports: shouldRewriteDynamicImports,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return nextCode;
|
|
20
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const appendOxcWywPreval: (code: string, filename: string, dependencyNames: string[]) => string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { parseSync } from 'oxc-parser';
|
|
2
|
+
const parseSourceType = (code, filename) => {
|
|
3
|
+
const parsed = parseSync(filename, code, {
|
|
4
|
+
astType: filename.endsWith('.ts') || filename.endsWith('.tsx') ? 'ts' : 'js',
|
|
5
|
+
range: true,
|
|
6
|
+
sourceType: 'unambiguous',
|
|
7
|
+
});
|
|
8
|
+
const fatalError = parsed.errors.find((error) => error.severity === 'Error');
|
|
9
|
+
if (fatalError) {
|
|
10
|
+
throw new Error(fatalError.message);
|
|
11
|
+
}
|
|
12
|
+
return parsed.program.sourceType === 'script' ? 'script' : 'module';
|
|
13
|
+
};
|
|
14
|
+
export const appendOxcWywPreval = (code, filename, dependencyNames) => {
|
|
15
|
+
const uniqueNames = [...new Set(dependencyNames)];
|
|
16
|
+
const properties = uniqueNames.map((name) => `${name}: ${name}`).join(', ');
|
|
17
|
+
const object = uniqueNames.length > 0 ? `{ ${properties} }` : '{}';
|
|
18
|
+
if (parseSourceType(code, filename) === 'script') {
|
|
19
|
+
return `${code}\nexports.__wywPreval = ${object};`;
|
|
20
|
+
}
|
|
21
|
+
return `${code}\nexport const __wywPreval = ${object};`;
|
|
22
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { IFileContext } from '@wyw-in-js/processor-utils';
|
|
2
|
+
import type { EventEmitter } from '../EventEmitter';
|
|
3
|
+
import type { ApplyOxcProcessorsResult } from '../applyOxcProcessors/types';
|
|
4
|
+
import type { OxcPreevalOptions } from './types';
|
|
5
|
+
type PreevalProcessorCollection = {
|
|
6
|
+
dependencyNames: string[];
|
|
7
|
+
processed: ApplyOxcProcessorsResult;
|
|
8
|
+
};
|
|
9
|
+
export declare const collectPreevalProcessors: (code: string, fileContext: IFileContext, options: OxcPreevalOptions, eventEmitter: EventEmitter) => PreevalProcessorCollection;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { applyOxcProcessors } from '../applyOxcProcessors';
|
|
2
|
+
export const collectPreevalProcessors = (code, fileContext, options, eventEmitter) => {
|
|
3
|
+
const dependencyNames = [];
|
|
4
|
+
const processed = eventEmitter.perf('transform:preeval:processTemplate', () => applyOxcProcessors(code, fileContext, options, (processor) => {
|
|
5
|
+
processor.dependencies.forEach((dependency) => {
|
|
6
|
+
if (dependency.ex.type === 'Identifier') {
|
|
7
|
+
dependencyNames.push(dependency.ex.name);
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
processor.doEvaltimeReplacement();
|
|
11
|
+
}));
|
|
12
|
+
return {
|
|
13
|
+
dependencyNames,
|
|
14
|
+
processed,
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ApplyOxcProcessorsResult } from '../applyOxcProcessors/types';
|
|
2
|
+
import type { OxcPreevalOptions, StaticPreevalOverlay } from './types';
|
|
3
|
+
export declare const createStaticPreevalOverlay: (processed: ApplyOxcProcessorsResult, dependencyNames: string[], options: OxcPreevalOptions) => StaticPreevalOverlay;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { usesStaticEvaluation } from './evalStrategy';
|
|
2
|
+
export const createStaticPreevalOverlay = (processed, dependencyNames, options) => {
|
|
3
|
+
const staticValuesEnabled = usesStaticEvaluation(options);
|
|
4
|
+
const staticValueNames = staticValuesEnabled
|
|
5
|
+
? new Set(processed.staticValues.map((item) => item.name))
|
|
6
|
+
: null;
|
|
7
|
+
const evalDependencyNames = staticValuesEnabled
|
|
8
|
+
? dependencyNames.filter((name) => !staticValueNames.has(name))
|
|
9
|
+
: dependencyNames;
|
|
10
|
+
const staticValueCache = new Map();
|
|
11
|
+
if (staticValuesEnabled) {
|
|
12
|
+
processed.staticValues.forEach(({ name, value }) => {
|
|
13
|
+
staticValueCache.set(name, value);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
evalDependencyNames,
|
|
18
|
+
staticValueCache,
|
|
19
|
+
staticValueCandidates: staticValuesEnabled
|
|
20
|
+
? processed.staticValueCandidates
|
|
21
|
+
: [],
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { StrictOptions } from '@wyw-in-js/shared';
|
|
2
|
+
import type { EventEmitter } from '../EventEmitter';
|
|
3
|
+
import type { WYWTransformMetadata } from '../TransformMetadata';
|
|
4
|
+
import type { OxcStaticValueCandidate } from '../collectOxcTemplateDependencies';
|
|
5
|
+
export type OxcPreevalOptions = Pick<StrictOptions, 'classNameSlug' | 'codeRemover' | 'displayName' | 'eval' | 'extensions' | 'features' | 'staticBindings' | 'tagResolver'> & {
|
|
6
|
+
eventEmitter?: EventEmitter;
|
|
7
|
+
};
|
|
8
|
+
export type OxcPreevalResult = {
|
|
9
|
+
baseCode: string;
|
|
10
|
+
code: string;
|
|
11
|
+
dependencyNames: string[];
|
|
12
|
+
metadata: WYWTransformMetadata | null;
|
|
13
|
+
processorClassNames: Record<string, string>;
|
|
14
|
+
staticDependencies: string[];
|
|
15
|
+
staticValueCache: Map<string, unknown>;
|
|
16
|
+
staticValueCandidates: OxcStaticValueCandidate[];
|
|
17
|
+
};
|
|
18
|
+
export type StaticPreevalOverlay = {
|
|
19
|
+
evalDependencyNames: string[];
|
|
20
|
+
staticValueCache: Map<string, unknown>;
|
|
21
|
+
staticValueCandidates: OxcStaticValueCandidate[];
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,20 +1,4 @@
|
|
|
1
1
|
import type { IFileContext } from '@wyw-in-js/processor-utils';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
import type { WYWTransformMetadata } from './TransformMetadata';
|
|
5
|
-
import type { OxcStaticValueCandidate } from './collectOxcTemplateDependencies';
|
|
6
|
-
type OxcPreevalOptions = Pick<StrictOptions, 'classNameSlug' | 'codeRemover' | 'displayName' | 'evaluate' | 'extensions' | 'features' | 'tagResolver'> & {
|
|
7
|
-
eventEmitter?: EventEmitter;
|
|
8
|
-
};
|
|
9
|
-
type OxcPreevalResult = {
|
|
10
|
-
baseCode: string;
|
|
11
|
-
code: string;
|
|
12
|
-
dependencyNames: string[];
|
|
13
|
-
metadata: WYWTransformMetadata | null;
|
|
14
|
-
staticDependencies: string[];
|
|
15
|
-
staticValueCache: Map<string, unknown>;
|
|
16
|
-
staticValueCandidates: OxcStaticValueCandidate[];
|
|
17
|
-
};
|
|
18
|
-
export declare const appendOxcWywPreval: (code: string, filename: string, dependencyNames: string[]) => string;
|
|
2
|
+
import type { OxcPreevalOptions, OxcPreevalResult } from './oxcPreevalStage/types';
|
|
3
|
+
export { appendOxcWywPreval } from './oxcPreevalStage/prevalExport';
|
|
19
4
|
export declare const runOxcPreevalStage: (code: string, fileContext: IFileContext, options: OxcPreevalOptions) => OxcPreevalResult;
|
|
20
|
-
export {};
|
|
@@ -1,102 +1,40 @@
|
|
|
1
|
-
import { parseSync } from 'oxc-parser';
|
|
2
|
-
import { isFeatureEnabled } from '@wyw-in-js/shared';
|
|
3
1
|
import { EventEmitter } from './EventEmitter';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const isStaticImportValuesEnabled = (features, filename) => {
|
|
10
|
-
const envValue = process.env.WYW_STATIC_IMPORT_VALUES?.trim().toLowerCase();
|
|
11
|
-
if (envValue) {
|
|
12
|
-
return !isEnvDisabled(envValue);
|
|
13
|
-
}
|
|
14
|
-
return isFeatureEnabled(features, 'staticImportValues', filename);
|
|
15
|
-
};
|
|
16
|
-
const parseSourceType = (code, filename) => {
|
|
17
|
-
const parsed = parseSync(filename, code, {
|
|
18
|
-
astType: filename.endsWith('.ts') || filename.endsWith('.tsx') ? 'ts' : 'js',
|
|
19
|
-
range: true,
|
|
20
|
-
sourceType: 'unambiguous',
|
|
21
|
-
});
|
|
22
|
-
const fatalError = parsed.errors.find((error) => error.severity === 'Error');
|
|
23
|
-
if (fatalError) {
|
|
24
|
-
throw new Error(fatalError.message);
|
|
25
|
-
}
|
|
26
|
-
return parsed.program.sourceType === 'script' ? 'script' : 'module';
|
|
27
|
-
};
|
|
28
|
-
export const appendOxcWywPreval = (code, filename, dependencyNames) => {
|
|
29
|
-
const uniqueNames = [...new Set(dependencyNames)];
|
|
30
|
-
const properties = uniqueNames.map((name) => `${name}: ${name}`).join(', ');
|
|
31
|
-
const object = uniqueNames.length > 0 ? `{ ${properties} }` : '{}';
|
|
32
|
-
if (parseSourceType(code, filename) === 'script') {
|
|
33
|
-
return `${code}\nexports.__wywPreval = ${object};`;
|
|
34
|
-
}
|
|
35
|
-
return `${code}\nexport const __wywPreval = ${object};`;
|
|
36
|
-
};
|
|
2
|
+
import { appendOxcWywPreval } from './oxcPreevalStage/prevalExport';
|
|
3
|
+
import { prepareOxcPreevalCode } from './oxcPreevalStage/prepareCode';
|
|
4
|
+
import { collectPreevalProcessors } from './oxcPreevalStage/processors';
|
|
5
|
+
import { createStaticPreevalOverlay } from './oxcPreevalStage/staticOverlay';
|
|
6
|
+
export { appendOxcWywPreval } from './oxcPreevalStage/prevalExport';
|
|
37
7
|
export const runOxcPreevalStage = (code, fileContext, options) => {
|
|
38
8
|
const filename = fileContext.filename ?? 'unknown.js';
|
|
39
|
-
const dependencyNames = [];
|
|
40
9
|
const eventEmitter = options.eventEmitter ?? EventEmitter.dummy;
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
dependencyNames.push(dependency.ex.name);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
processor.doEvaltimeReplacement();
|
|
48
|
-
}));
|
|
49
|
-
const staticValuesEnabled = isStaticImportValuesEnabled(options.features, filename);
|
|
50
|
-
const staticValueNames = staticValuesEnabled
|
|
51
|
-
? new Set(processed.staticValues.map((item) => item.name))
|
|
52
|
-
: null;
|
|
53
|
-
const evalDependencyNames = staticValuesEnabled
|
|
54
|
-
? dependencyNames.filter((name) => !staticValueNames.has(name))
|
|
55
|
-
: dependencyNames;
|
|
56
|
-
let nextCode = eventEmitter.perf('transform:preeval:importMetaEnv', () => replaceImportMetaEnvWithOxc(processed.code, filename));
|
|
57
|
-
if (isFeatureEnabled(options.features, 'dangerousCodeRemover', filename)) {
|
|
58
|
-
nextCode = eventEmitter.perf('transform:preeval:removeDangerousCode', () => removeDangerousCodeWithOxc(nextCode, filename, options.codeRemover));
|
|
59
|
-
}
|
|
60
|
-
const shouldRewriteDynamicImports = DYNAMIC_IMPORT_RE.test(nextCode);
|
|
61
|
-
const shouldAddRequireFallback = REQUIRE_CALL_RE.test(nextCode);
|
|
62
|
-
if (shouldRewriteDynamicImports || shouldAddRequireFallback) {
|
|
63
|
-
nextCode = rewriteDynamicImportsAndAddRequireFallbackWithOxc(nextCode, filename, {
|
|
64
|
-
addRequireFallback: shouldAddRequireFallback,
|
|
65
|
-
eventEmitter,
|
|
66
|
-
rewriteDynamicImports: shouldRewriteDynamicImports,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
10
|
+
const { dependencyNames, processed } = collectPreevalProcessors(code, fileContext, options, eventEmitter);
|
|
11
|
+
const staticOverlay = createStaticPreevalOverlay(processed, dependencyNames, options);
|
|
12
|
+
const baseCode = prepareOxcPreevalCode(processed.code, filename, options, eventEmitter);
|
|
69
13
|
if (processed.processors.length === 0) {
|
|
70
14
|
return {
|
|
71
|
-
baseCode
|
|
72
|
-
code:
|
|
15
|
+
baseCode,
|
|
16
|
+
code: baseCode,
|
|
73
17
|
dependencyNames: [],
|
|
74
18
|
metadata: null,
|
|
19
|
+
processorClassNames: {},
|
|
75
20
|
staticDependencies: [],
|
|
76
21
|
staticValueCandidates: [],
|
|
77
22
|
staticValueCache: new Map(),
|
|
78
23
|
};
|
|
79
24
|
}
|
|
80
|
-
const staticValueCache = new Map();
|
|
81
|
-
if (staticValuesEnabled) {
|
|
82
|
-
processed.staticValues.forEach(({ name, value }) => {
|
|
83
|
-
staticValueCache.set(name, value);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
25
|
return {
|
|
87
|
-
baseCode
|
|
88
|
-
code: appendOxcWywPreval(
|
|
89
|
-
dependencyNames: evalDependencyNames,
|
|
26
|
+
baseCode,
|
|
27
|
+
code: appendOxcWywPreval(baseCode, filename, staticOverlay.evalDependencyNames),
|
|
28
|
+
dependencyNames: staticOverlay.evalDependencyNames,
|
|
90
29
|
metadata: {
|
|
91
30
|
dependencies: [],
|
|
92
31
|
processors: processed.processors,
|
|
93
32
|
replacements: [],
|
|
94
33
|
rules: {},
|
|
95
34
|
},
|
|
35
|
+
processorClassNames: Object.fromEntries(processed.processorClassNamesByLocal),
|
|
96
36
|
staticDependencies: [],
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
: [],
|
|
100
|
-
staticValueCache,
|
|
37
|
+
staticValueCache: staticOverlay.staticValueCache,
|
|
38
|
+
staticValueCandidates: staticOverlay.staticValueCandidates,
|
|
101
39
|
};
|
|
102
40
|
};
|
|
@@ -40,6 +40,7 @@ const defaultReactComponentTypes = [
|
|
|
40
40
|
'MemoExoticComponent',
|
|
41
41
|
'NamedExoticComponent',
|
|
42
42
|
];
|
|
43
|
+
const defaultReactHocs = ['forwardRef', 'memo'];
|
|
43
44
|
const generatedProcessorHelperNameRe = /^_exp\d*$/;
|
|
44
45
|
const requireCallRe = /\brequire\s*\(/;
|
|
45
46
|
const windowTokenRe = /\bwindow\b/;
|
|
@@ -876,6 +877,7 @@ const isHocCall = (call, hocs, imports) => {
|
|
|
876
877
|
const matched = getImportForExpression(getInnermostCallee(call), imports);
|
|
877
878
|
return !!matched && hocs[matched[0]]?.includes(matched[1]);
|
|
878
879
|
};
|
|
880
|
+
const isInsideHocCall = (ancestors, hocs, imports) => ancestors.some((ancestor) => ancestor.type === 'CallExpression' && isHocCall(ancestor, hocs, imports));
|
|
879
881
|
const getComponentTypes = (options) => {
|
|
880
882
|
const componentTypes = {
|
|
881
883
|
...(options?.componentTypes ?? { react: [defaultPlaceholder] }),
|
|
@@ -888,6 +890,14 @@ const getComponentTypes = (options) => {
|
|
|
888
890
|
}
|
|
889
891
|
return componentTypes;
|
|
890
892
|
};
|
|
893
|
+
const getHocs = (options) => {
|
|
894
|
+
const hocs = {
|
|
895
|
+
...(options?.hocs ?? {}),
|
|
896
|
+
};
|
|
897
|
+
const reactHocs = new Set([...defaultReactHocs, ...(hocs.react ?? [])]);
|
|
898
|
+
hocs.react = [...reactHocs];
|
|
899
|
+
return hocs;
|
|
900
|
+
};
|
|
891
901
|
const getTypeImport = (node, imports) => {
|
|
892
902
|
if (node.type === 'Identifier') {
|
|
893
903
|
const matched = getImportBinding(imports, node.name);
|
|
@@ -1019,7 +1029,7 @@ export const removeDangerousCodeWithOxc = (code, filename, options) => {
|
|
|
1019
1029
|
const program = parseOxc(code, filename);
|
|
1020
1030
|
const imports = collectImportBindings(code, filename, program);
|
|
1021
1031
|
const componentTypes = getComponentTypes(options);
|
|
1022
|
-
const hocs = options
|
|
1032
|
+
const hocs = getHocs(options);
|
|
1023
1033
|
const hasHocs = Object.keys(hocs).length > 0;
|
|
1024
1034
|
const windowScopedNames = windowTokenRe.test(code)
|
|
1025
1035
|
? collectWindowScopedNames(program)
|
|
@@ -1122,6 +1132,9 @@ export const removeDangerousCodeWithOxc = (code, filename, options) => {
|
|
|
1122
1132
|
if (!isAlwaysForbidden && isInDeferredFunctionScope(ancestors)) {
|
|
1123
1133
|
return;
|
|
1124
1134
|
}
|
|
1135
|
+
if (hasHocs && isInsideHocCall(ancestors, hocs, imports)) {
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1125
1138
|
if (isBindingPosition(node, parent) && !isAlwaysForbidden) {
|
|
1126
1139
|
return;
|
|
1127
1140
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BaseProcessor, Expression as ProcessorExpression, ProcessorStaticContext, ProcessorStaticValue } from '@wyw-in-js/processor-utils';
|
|
2
|
+
export declare const unknownProcessorStaticValue: unique symbol;
|
|
3
|
+
export type UnknownProcessorStaticValue = typeof unknownProcessorStaticValue;
|
|
4
|
+
export declare const createProcessorStaticContext: (processor: BaseProcessor) => ProcessorStaticContext;
|
|
5
|
+
export declare const isProcessorStaticValue: (value: unknown) => value is ProcessorStaticValue;
|
|
6
|
+
export declare const getProcessorStaticValue: (processor: BaseProcessor) => ProcessorStaticValue | null;
|
|
7
|
+
export declare const processorStaticValueToRuntimeValue: (value: ProcessorStaticValue) => unknown | UnknownProcessorStaticValue;
|
|
8
|
+
export declare const resolveProcessorStaticRuntimeValue: (processor: BaseProcessor) => unknown | UnknownProcessorStaticValue;
|
|
9
|
+
export declare const resolveProcessorStaticClassName: (processor: BaseProcessor) => string | null;
|
|
10
|
+
export declare const processorLiteralValue: (expression: ProcessorExpression) => unknown | UnknownProcessorStaticValue;
|
|
11
|
+
export declare const processorPropertyKeyName: (key: ProcessorExpression) => string | null;
|
|
12
|
+
export declare const processorObjectPropertyValue: (expression: ProcessorExpression, name: string) => ProcessorExpression | null;
|
|
13
|
+
export declare const processorExpressionToStaticValue: (expression: ProcessorExpression, resolveHelperCall: (name: string) => unknown | UnknownProcessorStaticValue) => unknown | UnknownProcessorStaticValue;
|