@wyw-in-js/transform 0.8.0 → 1.0.0
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/babel.js.map +1 -1
- package/esm/cache.js +15 -4
- package/esm/cache.js.map +1 -1
- package/esm/debug/fileReporter.js.map +1 -1
- package/esm/evaluators/index.js.map +1 -1
- package/esm/index.js.map +1 -1
- package/esm/module.js +174 -15
- package/esm/module.js.map +1 -1
- package/esm/options/buildOptions.js +123 -4
- package/esm/options/buildOptions.js.map +1 -1
- package/esm/options/buildOptions.test.js +41 -0
- package/esm/options/buildOptions.test.js.map +1 -0
- package/esm/options/loadBabelOptions.js.map +1 -1
- package/esm/plugins/babel-transform.js.map +1 -1
- package/esm/plugins/collector.js.map +1 -1
- package/esm/plugins/dynamic-import.js +35 -12
- package/esm/plugins/dynamic-import.js.map +1 -1
- package/esm/plugins/preeval.js +4 -0
- package/esm/plugins/preeval.js.map +1 -1
- package/esm/plugins/shaker.js +170 -23
- package/esm/plugins/shaker.js.map +1 -1
- package/esm/shaker.js +6 -3
- package/esm/shaker.js.map +1 -1
- package/esm/transform/BaseEntrypoint.js.map +1 -1
- package/esm/transform/Entrypoint.helpers.js +105 -15
- package/esm/transform/Entrypoint.helpers.js.map +1 -1
- package/esm/transform/Entrypoint.js +13 -2
- package/esm/transform/Entrypoint.js.map +1 -1
- package/esm/transform/Entrypoint.types.js.map +1 -1
- package/esm/transform/EvaluatedEntrypoint.js.map +1 -1
- package/esm/transform/actions/AbortError.js.map +1 -1
- package/esm/transform/actions/BaseAction.js +5 -0
- package/esm/transform/actions/BaseAction.js.map +1 -1
- package/esm/transform/actions/UnprocessedEntrypointError.js.map +1 -1
- package/esm/transform/actions/actionRunner.js.map +1 -1
- package/esm/transform/actions/types.js.map +1 -1
- package/esm/transform/generators/baseProcessingHandlers.js.map +1 -1
- package/esm/transform/generators/collect.js.map +1 -1
- package/esm/transform/generators/createStylisPreprocessor.js +118 -2
- package/esm/transform/generators/createStylisPreprocessor.js.map +1 -1
- package/esm/transform/generators/evalFile.js.map +1 -1
- package/esm/transform/generators/explodeReexports.js +10 -10
- package/esm/transform/generators/explodeReexports.js.map +1 -1
- package/esm/transform/generators/extract.js.map +1 -1
- package/esm/transform/generators/getExports.js.map +1 -1
- package/esm/transform/generators/index.js.map +1 -1
- package/esm/transform/generators/processEntrypoint.js +5 -8
- package/esm/transform/generators/processEntrypoint.js.map +1 -1
- package/esm/transform/generators/processImports.js.map +1 -1
- package/esm/transform/generators/resolveImports.js +41 -2
- package/esm/transform/generators/resolveImports.js.map +1 -1
- package/esm/transform/generators/transform.js +7 -3
- package/esm/transform/generators/transform.js.map +1 -1
- package/esm/transform/generators/workflow.js.map +1 -1
- package/esm/transform/helpers/loadWywOptions.js +15 -2
- package/esm/transform/helpers/loadWywOptions.js.map +1 -1
- package/esm/transform/helpers/withDefaultServices.js +2 -0
- package/esm/transform/helpers/withDefaultServices.js.map +1 -1
- package/esm/transform/isStaticallyEvaluatableModule.js +153 -0
- package/esm/transform/isStaticallyEvaluatableModule.js.map +1 -0
- package/esm/transform/rootLog.js.map +1 -1
- package/esm/transform/types.js.map +1 -1
- package/esm/transform.js +15 -6
- package/esm/transform.js.map +1 -1
- package/esm/types.js.map +1 -1
- package/esm/utils/EventEmitter.js.map +1 -1
- package/esm/utils/ShakerMetadata.js.map +1 -1
- package/esm/utils/TransformMetadata.js.map +1 -1
- package/esm/utils/addIdentifierToWywPreval.js.map +1 -1
- package/esm/utils/collectExportsAndImports.js +97 -6
- package/esm/utils/collectExportsAndImports.js.map +1 -1
- package/esm/utils/collectTemplateDependencies.js +1 -1
- package/esm/utils/collectTemplateDependencies.js.map +1 -1
- package/esm/utils/createId.js.map +1 -1
- package/esm/utils/dispose-polyfill.js.map +1 -1
- package/esm/utils/findIdentifiers.js.map +1 -1
- package/esm/utils/getConstantStringValue.js +58 -0
- package/esm/utils/getConstantStringValue.js.map +1 -0
- package/esm/utils/getFileIdx.js.map +1 -1
- package/esm/utils/getMemberExpressionPropertyName.js +11 -0
- package/esm/utils/getMemberExpressionPropertyName.js.map +1 -0
- package/esm/utils/getPluginKey.js.map +1 -1
- package/esm/utils/getScope.js.map +1 -1
- package/esm/utils/getSource.js.map +1 -1
- package/esm/utils/getTagProcessor.js +34 -6
- package/esm/utils/getTagProcessor.js.map +1 -1
- package/esm/utils/getVisitorKeys.js.map +1 -1
- package/esm/utils/hasWywPreval.js.map +1 -1
- package/esm/utils/importOverrides.js +47 -0
- package/esm/utils/importOverrides.js.map +1 -0
- package/esm/utils/isExports.js.map +1 -1
- package/esm/utils/isGlobal.js.map +1 -1
- package/esm/utils/isNode.js.map +1 -1
- package/esm/utils/isNodePath.js.map +1 -1
- package/esm/utils/isNotNull.js.map +1 -1
- package/esm/utils/isRemoved.js.map +1 -1
- package/esm/utils/isRequire.js.map +1 -1
- package/esm/utils/isSerializable.js.map +1 -1
- package/esm/utils/isTypedNode.js.map +1 -1
- package/esm/utils/isUnnecessaryReactCall.js.map +1 -1
- package/esm/utils/parseRequest.js +29 -0
- package/esm/utils/parseRequest.js.map +1 -0
- package/esm/utils/peek.js.map +1 -1
- package/esm/utils/removeDangerousCode.js +32 -2
- package/esm/utils/removeDangerousCode.js.map +1 -1
- package/esm/utils/replaceImportMetaEnv.js +44 -0
- package/esm/utils/replaceImportMetaEnv.js.map +1 -0
- package/esm/utils/scopeHelpers.js.map +1 -1
- package/esm/utils/traversalCache.js.map +1 -1
- package/esm/utils/unwrapExpression.js +18 -0
- package/esm/utils/unwrapExpression.js.map +1 -0
- package/esm/utils/unwrapSequence.js.map +1 -1
- package/esm/utils/valueToLiteral.js.map +1 -1
- package/esm/utils/visitors/JSXElementsRemover.js.map +1 -1
- package/esm/vm/createVmContext.js +44 -0
- package/esm/vm/createVmContext.js.map +1 -1
- package/esm/vm/process.js.map +1 -1
- package/lib/babel.js.map +1 -1
- package/lib/cache.js +16 -5
- package/lib/cache.js.map +1 -1
- package/lib/debug/fileReporter.js +1 -1
- package/lib/debug/fileReporter.js.map +1 -1
- package/lib/evaluators/index.js.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/module.js +177 -16
- package/lib/module.js.map +1 -1
- package/lib/options/buildOptions.js +127 -6
- package/lib/options/buildOptions.js.map +1 -1
- package/lib/options/buildOptions.test.js +44 -0
- package/lib/options/buildOptions.test.js.map +1 -0
- package/lib/options/loadBabelOptions.js.map +1 -1
- package/lib/plugins/babel-transform.js.map +1 -1
- package/lib/plugins/collector.js.map +1 -1
- package/lib/plugins/dynamic-import.js +34 -12
- package/lib/plugins/dynamic-import.js.map +1 -1
- package/lib/plugins/preeval.js +4 -0
- package/lib/plugins/preeval.js.map +1 -1
- package/lib/plugins/shaker.js +172 -22
- package/lib/plugins/shaker.js.map +1 -1
- package/lib/shaker.js +8 -4
- package/lib/shaker.js.map +1 -1
- package/lib/transform/BaseEntrypoint.js.map +1 -1
- package/lib/transform/Entrypoint.helpers.js +106 -15
- package/lib/transform/Entrypoint.helpers.js.map +1 -1
- package/lib/transform/Entrypoint.js +13 -2
- package/lib/transform/Entrypoint.js.map +1 -1
- package/lib/transform/Entrypoint.types.js.map +1 -1
- package/lib/transform/EvaluatedEntrypoint.js.map +1 -1
- package/lib/transform/actions/AbortError.js.map +1 -1
- package/lib/transform/actions/BaseAction.js +5 -0
- package/lib/transform/actions/BaseAction.js.map +1 -1
- package/lib/transform/actions/UnprocessedEntrypointError.js.map +1 -1
- package/lib/transform/actions/actionRunner.js.map +1 -1
- package/lib/transform/actions/types.js.map +1 -1
- package/lib/transform/generators/baseProcessingHandlers.js.map +1 -1
- package/lib/transform/generators/collect.js.map +1 -1
- package/lib/transform/generators/createStylisPreprocessor.js +119 -3
- package/lib/transform/generators/createStylisPreprocessor.js.map +1 -1
- package/lib/transform/generators/evalFile.js +1 -1
- package/lib/transform/generators/evalFile.js.map +1 -1
- package/lib/transform/generators/explodeReexports.js +11 -11
- package/lib/transform/generators/explodeReexports.js.map +1 -1
- package/lib/transform/generators/extract.js.map +1 -1
- package/lib/transform/generators/getExports.js.map +1 -1
- package/lib/transform/generators/index.js.map +1 -1
- package/lib/transform/generators/processEntrypoint.js +5 -8
- package/lib/transform/generators/processEntrypoint.js.map +1 -1
- package/lib/transform/generators/processImports.js.map +1 -1
- package/lib/transform/generators/resolveImports.js +41 -2
- package/lib/transform/generators/resolveImports.js.map +1 -1
- package/lib/transform/generators/transform.js +8 -3
- package/lib/transform/generators/transform.js.map +1 -1
- package/lib/transform/generators/workflow.js.map +1 -1
- package/lib/transform/helpers/loadWywOptions.js +16 -2
- package/lib/transform/helpers/loadWywOptions.js.map +1 -1
- package/lib/transform/helpers/withDefaultServices.js +3 -2
- package/lib/transform/helpers/withDefaultServices.js.map +1 -1
- package/lib/transform/isStaticallyEvaluatableModule.js +159 -0
- package/lib/transform/isStaticallyEvaluatableModule.js.map +1 -0
- package/lib/transform/rootLog.js.map +1 -1
- package/lib/transform/types.js.map +1 -1
- package/lib/transform.js +15 -6
- package/lib/transform.js.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib/utils/EventEmitter.js.map +1 -1
- package/lib/utils/ShakerMetadata.js.map +1 -1
- package/lib/utils/TransformMetadata.js.map +1 -1
- package/lib/utils/addIdentifierToWywPreval.js.map +1 -1
- package/lib/utils/collectExportsAndImports.js +97 -6
- package/lib/utils/collectExportsAndImports.js.map +1 -1
- package/lib/utils/collectTemplateDependencies.js +1 -1
- package/lib/utils/collectTemplateDependencies.js.map +1 -1
- package/lib/utils/createId.js.map +1 -1
- package/lib/utils/dispose-polyfill.js.map +1 -1
- package/lib/utils/findIdentifiers.js.map +1 -1
- package/lib/utils/getConstantStringValue.js +66 -0
- package/lib/utils/getConstantStringValue.js.map +1 -0
- package/lib/utils/getFileIdx.js.map +1 -1
- package/lib/utils/getMemberExpressionPropertyName.js +18 -0
- package/lib/utils/getMemberExpressionPropertyName.js.map +1 -0
- package/lib/utils/getPluginKey.js.map +1 -1
- package/lib/utils/getScope.js.map +1 -1
- package/lib/utils/getSource.js +1 -1
- package/lib/utils/getSource.js.map +1 -1
- package/lib/utils/getTagProcessor.js +33 -6
- package/lib/utils/getTagProcessor.js.map +1 -1
- package/lib/utils/getVisitorKeys.js.map +1 -1
- package/lib/utils/hasWywPreval.js.map +1 -1
- package/lib/utils/importOverrides.js +57 -0
- package/lib/utils/importOverrides.js.map +1 -0
- package/lib/utils/isExports.js.map +1 -1
- package/lib/utils/isGlobal.js.map +1 -1
- package/lib/utils/isNode.js.map +1 -1
- package/lib/utils/isNodePath.js.map +1 -1
- package/lib/utils/isNotNull.js.map +1 -1
- package/lib/utils/isRemoved.js.map +1 -1
- package/lib/utils/isRequire.js.map +1 -1
- package/lib/utils/isSerializable.js.map +1 -1
- package/lib/utils/isTypedNode.js.map +1 -1
- package/lib/utils/isUnnecessaryReactCall.js.map +1 -1
- package/lib/utils/parseRequest.js +37 -0
- package/lib/utils/parseRequest.js.map +1 -0
- package/lib/utils/peek.js.map +1 -1
- package/lib/utils/removeDangerousCode.js +32 -2
- package/lib/utils/removeDangerousCode.js.map +1 -1
- package/lib/utils/replaceImportMetaEnv.js +50 -0
- package/lib/utils/replaceImportMetaEnv.js.map +1 -0
- package/lib/utils/scopeHelpers.js.map +1 -1
- package/lib/utils/traversalCache.js.map +1 -1
- package/lib/utils/unwrapExpression.js +25 -0
- package/lib/utils/unwrapExpression.js.map +1 -0
- package/lib/utils/unwrapSequence.js.map +1 -1
- package/lib/utils/valueToLiteral.js.map +1 -1
- package/lib/utils/visitors/JSXElementsRemover.js.map +1 -1
- package/lib/vm/createVmContext.js +45 -2
- package/lib/vm/createVmContext.js.map +1 -1
- package/lib/vm/process.js.map +1 -1
- package/package.json +13 -15
- package/types/cache.d.ts +1 -1
- package/types/cache.js +15 -4
- package/types/evaluators/index.js +1 -1
- package/types/module.d.ts +1 -2
- package/types/module.js +189 -11
- package/types/options/buildOptions.d.ts +0 -1
- package/types/options/buildOptions.js +125 -9
- package/types/options/loadBabelOptions.js +1 -2
- package/types/plugins/babel-transform.js +1 -1
- package/types/plugins/collector.js +3 -3
- package/types/plugins/dynamic-import.js +38 -18
- package/types/plugins/preeval.js +5 -2
- package/types/plugins/shaker.d.ts +4 -1
- package/types/plugins/shaker.js +189 -24
- package/types/shaker.js +9 -3
- package/types/transform/BaseEntrypoint.d.ts +0 -1
- package/types/transform/Entrypoint.helpers.d.ts +0 -1
- package/types/transform/Entrypoint.helpers.js +122 -25
- package/types/transform/Entrypoint.js +14 -2
- package/types/transform/Entrypoint.types.d.ts +0 -1
- package/types/transform/EvaluatedEntrypoint.d.ts +0 -1
- package/types/transform/actions/BaseAction.d.ts +1 -1
- package/types/transform/actions/BaseAction.js +5 -0
- package/types/transform/actions/actionRunner.js +2 -3
- package/types/transform/generators/collect.js +1 -2
- package/types/transform/generators/createStylisPreprocessor.d.ts +0 -1
- package/types/transform/generators/createStylisPreprocessor.js +168 -13
- package/types/transform/generators/evalFile.js +1 -2
- package/types/transform/generators/explodeReexports.js +12 -15
- package/types/transform/generators/extract.js +1 -2
- package/types/transform/generators/getExports.js +2 -3
- package/types/transform/generators/processEntrypoint.js +13 -7
- package/types/transform/generators/processImports.js +1 -2
- package/types/transform/generators/resolveImports.js +41 -5
- package/types/transform/generators/transform.d.ts +1 -1
- package/types/transform/generators/transform.js +16 -6
- package/types/transform/generators/workflow.js +1 -2
- package/types/transform/helpers/loadWywOptions.js +16 -5
- package/types/transform/helpers/withDefaultServices.d.ts +1 -1
- package/types/transform/helpers/withDefaultServices.js +19 -8
- package/types/transform/isStaticallyEvaluatableModule.d.ts +2 -0
- package/types/transform/isStaticallyEvaluatableModule.js +177 -0
- package/types/transform/rootLog.d.ts +0 -1
- package/types/transform/types.d.ts +1 -1
- package/types/transform.js +17 -9
- package/types/types.d.ts +1 -1
- package/types/utils/addIdentifierToWywPreval.js +2 -3
- package/types/utils/collectExportsAndImports.js +102 -20
- package/types/utils/collectTemplateDependencies.js +3 -4
- package/types/utils/findIdentifiers.js +2 -3
- package/types/utils/getConstantStringValue.d.ts +2 -0
- package/types/utils/getConstantStringValue.js +94 -0
- package/types/utils/getFileIdx.js +1 -2
- package/types/utils/getMemberExpressionPropertyName.d.ts +2 -0
- package/types/utils/getMemberExpressionPropertyName.js +46 -0
- package/types/utils/getScope.js +1 -2
- package/types/utils/getTagProcessor.js +35 -9
- package/types/utils/getVisitorKeys.js +1 -2
- package/types/utils/hasWywPreval.js +1 -1
- package/types/utils/importOverrides.d.ts +19 -0
- package/types/utils/importOverrides.js +40 -0
- package/types/utils/isExports.js +1 -2
- package/types/utils/isNodePath.js +1 -2
- package/types/utils/isNotNull.js +1 -2
- package/types/utils/isRemoved.js +1 -2
- package/types/utils/isRequire.js +1 -2
- package/types/utils/isSerializable.js +1 -2
- package/types/utils/isUnnecessaryReactCall.js +1 -2
- package/types/utils/parseRequest.d.ts +7 -0
- package/types/utils/parseRequest.js +29 -0
- package/types/utils/removeDangerousCode.js +37 -2
- package/types/utils/replaceImportMetaEnv.d.ts +3 -0
- package/types/utils/replaceImportMetaEnv.js +39 -0
- package/types/utils/scopeHelpers.js +7 -8
- package/types/utils/traversalCache.d.ts +1 -1
- package/types/utils/unwrapExpression.d.ts +2 -0
- package/types/utils/unwrapExpression.js +57 -0
- package/types/utils/unwrapSequence.js +1 -2
- package/types/utils/valueToLiteral.js +1 -2
- package/types/utils/visitors/JSXElementsRemover.js +1 -2
- package/types/vm/createVmContext.d.ts +0 -1
- package/types/vm/createVmContext.js +68 -9
- package/types/vm/process.d.ts +0 -2
- package/LICENSE +0 -21
- package/esm/options/babel-merge.d.js +0 -2
- package/esm/options/babel-merge.d.js.map +0 -1
- package/lib/options/babel-merge.d.js +0 -2
- package/lib/options/babel-merge.d.js.map +0 -1
|
@@ -1,14 +1,131 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
3
|
-
/// <reference path="./babel-merge.d.ts" />
|
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
-
};
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.buildOptions =
|
|
9
|
-
const babel_merge_1 = __importDefault(require("babel-merge"));
|
|
3
|
+
exports.buildOptions = buildOptions;
|
|
10
4
|
const isNotNull_1 = require("../utils/isNotNull");
|
|
11
5
|
const cache = new WeakMap();
|
|
6
|
+
const isPlainObject = (value) => {
|
|
7
|
+
if (typeof value !== 'object' || value === null)
|
|
8
|
+
return false;
|
|
9
|
+
const prototype = Object.getPrototypeOf(value);
|
|
10
|
+
return prototype === Object.prototype || prototype === null;
|
|
11
|
+
};
|
|
12
|
+
const mergeArraysUnique = (source = [], overrides = []) => [
|
|
13
|
+
...new Set([...source, ...overrides]),
|
|
14
|
+
];
|
|
15
|
+
const mergeUnknown = (source, overrides) => {
|
|
16
|
+
if (Array.isArray(source) && Array.isArray(overrides)) {
|
|
17
|
+
return mergeArraysUnique(source, overrides);
|
|
18
|
+
}
|
|
19
|
+
if (isPlainObject(source) && isPlainObject(overrides)) {
|
|
20
|
+
const result = { ...source };
|
|
21
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
22
|
+
result[key] = key in source ? mergeUnknown(source[key], value) : value;
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
return overrides;
|
|
27
|
+
};
|
|
28
|
+
const getItemKey = (item) => {
|
|
29
|
+
if (typeof item === 'string')
|
|
30
|
+
return item;
|
|
31
|
+
if (Array.isArray(item))
|
|
32
|
+
return getItemKey(item[0]);
|
|
33
|
+
if (typeof item === 'object' && item !== null && 'key' in item) {
|
|
34
|
+
const { key } = item;
|
|
35
|
+
return typeof key === 'string' ? key : null;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
const getItemOptions = (item) => {
|
|
40
|
+
if (!Array.isArray(item))
|
|
41
|
+
return {};
|
|
42
|
+
const options = item[1];
|
|
43
|
+
return isPlainObject(options) ? options : {};
|
|
44
|
+
};
|
|
45
|
+
const toItemWithOptions = (key, options) => Object.keys(options).length ? [key, options] : key;
|
|
46
|
+
const normalizeKey = (key) => key.replace(/\\/g, '/');
|
|
47
|
+
const extractPackageNameFromPath = (key) => {
|
|
48
|
+
const match = normalizeKey(key).match(/\/node_modules\/(@[^/]+\/[^/]+|[^/]+)\//);
|
|
49
|
+
return match?.[1] ?? null;
|
|
50
|
+
};
|
|
51
|
+
const addBabelVariants = (kind, variants, key) => {
|
|
52
|
+
const prefix = kind === 'preset'
|
|
53
|
+
? ['@babel/preset-', 'babel-preset-']
|
|
54
|
+
: ['@babel/plugin-', 'babel-plugin-'];
|
|
55
|
+
const normalized = normalizeKey(key);
|
|
56
|
+
variants.add(normalized);
|
|
57
|
+
const packageNameFromPath = extractPackageNameFromPath(normalized);
|
|
58
|
+
if (packageNameFromPath) {
|
|
59
|
+
variants.add(packageNameFromPath);
|
|
60
|
+
}
|
|
61
|
+
for (const p of prefix) {
|
|
62
|
+
if (normalized.startsWith(p)) {
|
|
63
|
+
variants.add(normalized.slice(p.length));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (!normalized.includes('/') && !normalized.startsWith('.')) {
|
|
67
|
+
variants.add(`${prefix[0]}${normalized}`);
|
|
68
|
+
variants.add(`${prefix[1]}${normalized}`);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const areKeysCompatible = (kind, a, b) => {
|
|
72
|
+
const aVariants = new Set();
|
|
73
|
+
addBabelVariants(kind, aVariants, a);
|
|
74
|
+
const bVariants = new Set();
|
|
75
|
+
addBabelVariants(kind, bVariants, b);
|
|
76
|
+
for (const aVar of aVariants) {
|
|
77
|
+
if (bVariants.has(aVar))
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
};
|
|
82
|
+
const mergeConfigItems = (kind, source, overrides) => {
|
|
83
|
+
const combined = [...(source ?? []), ...(overrides ?? [])];
|
|
84
|
+
return combined.reduce((reduction, override) => {
|
|
85
|
+
const overrideKey = getItemKey(override);
|
|
86
|
+
if (!overrideKey) {
|
|
87
|
+
reduction.push(override);
|
|
88
|
+
return reduction;
|
|
89
|
+
}
|
|
90
|
+
const overrideOptions = getItemOptions(override);
|
|
91
|
+
const base = reduction.find((candidate) => {
|
|
92
|
+
const baseKey = getItemKey(candidate);
|
|
93
|
+
return baseKey ? areKeysCompatible(kind, baseKey, overrideKey) : false;
|
|
94
|
+
});
|
|
95
|
+
if (!base) {
|
|
96
|
+
reduction.push(toItemWithOptions(overrideKey, overrideOptions));
|
|
97
|
+
return reduction;
|
|
98
|
+
}
|
|
99
|
+
const index = reduction.indexOf(base);
|
|
100
|
+
const baseKey = getItemKey(base) ?? overrideKey;
|
|
101
|
+
const baseOptions = getItemOptions(base);
|
|
102
|
+
const options = mergeUnknown(baseOptions, overrideOptions);
|
|
103
|
+
reduction.splice(index, 1, toItemWithOptions(baseKey, options));
|
|
104
|
+
return reduction;
|
|
105
|
+
}, []);
|
|
106
|
+
};
|
|
107
|
+
const babelMerge = (source, overrides) => {
|
|
108
|
+
const { plugins: sourcePlugins, presets: sourcePresets, env: sourceEnv, ...sourceRest } = source;
|
|
109
|
+
const { plugins: overridesPlugins, presets: overridesPresets, env: overridesEnv, ...overridesRest } = overrides;
|
|
110
|
+
const plugins = mergeConfigItems('plugin', sourcePlugins, overridesPlugins);
|
|
111
|
+
const presets = mergeConfigItems('preset', sourcePresets, overridesPresets);
|
|
112
|
+
const merged = mergeUnknown(sourceRest, overridesRest);
|
|
113
|
+
if (presets.length)
|
|
114
|
+
merged.presets = presets;
|
|
115
|
+
if (plugins.length)
|
|
116
|
+
merged.plugins = plugins;
|
|
117
|
+
const envNames = new Set([
|
|
118
|
+
...Object.keys(sourceEnv ?? {}),
|
|
119
|
+
...Object.keys(overridesEnv ?? {}),
|
|
120
|
+
]);
|
|
121
|
+
if (envNames.size) {
|
|
122
|
+
merged.env = {};
|
|
123
|
+
for (const name of envNames) {
|
|
124
|
+
merged.env[name] = babelMerge(sourceEnv?.[name] ?? {}, overridesEnv?.[name] ?? {});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return merged;
|
|
128
|
+
};
|
|
12
129
|
const merge = (a, b) => {
|
|
13
130
|
if (!cache.has(a)) {
|
|
14
131
|
cache.set(a, new WeakMap());
|
|
@@ -17,7 +134,7 @@ const merge = (a, b) => {
|
|
|
17
134
|
if (cacheForA.has(b)) {
|
|
18
135
|
return cacheForA.get(b);
|
|
19
136
|
}
|
|
20
|
-
const result = (
|
|
137
|
+
const result = babelMerge(a, b);
|
|
21
138
|
cacheForA.set(b, result);
|
|
22
139
|
return result;
|
|
23
140
|
};
|
|
@@ -32,4 +149,3 @@ function buildOptions(...configs) {
|
|
|
32
149
|
.filter(isNotNull_1.isNotNull)
|
|
33
150
|
.reduce(merge);
|
|
34
151
|
}
|
|
35
|
-
exports.buildOptions = buildOptions;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadBabelOptions =
|
|
3
|
+
exports.loadBabelOptions = loadBabelOptions;
|
|
4
4
|
const cache = new WeakMap();
|
|
5
5
|
const empty = {};
|
|
6
6
|
function loadBabelOptions(babel, filename, overrides = empty) {
|
|
@@ -24,4 +24,3 @@ function loadBabelOptions(babel, filename, overrides = empty) {
|
|
|
24
24
|
cache.set(overrides, fileCache);
|
|
25
25
|
return babelOptions;
|
|
26
26
|
}
|
|
27
|
-
exports.loadBabelOptions = loadBabelOptions;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = babelTransform;
|
|
3
4
|
const shared_1 = require("@wyw-in-js/shared");
|
|
4
5
|
const loadWywOptions_1 = require("../transform/helpers/loadWywOptions");
|
|
5
6
|
const cache_1 = require("../cache");
|
|
@@ -46,4 +47,3 @@ function babelTransform(babel, options) {
|
|
|
46
47
|
},
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
|
-
exports.default = babelTransform;
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* all usages of WYW-processors.
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.
|
|
7
|
+
exports.filename = void 0;
|
|
8
|
+
exports.collector = collector;
|
|
9
|
+
exports.default = collectorPlugin;
|
|
8
10
|
const shared_1 = require("@wyw-in-js/shared");
|
|
9
11
|
const EventEmitter_1 = require("../utils/EventEmitter");
|
|
10
12
|
const getTagProcessor_1 = require("../utils/getTagProcessor");
|
|
@@ -32,7 +34,6 @@ function collector(file, options, values) {
|
|
|
32
34
|
}
|
|
33
35
|
return processors;
|
|
34
36
|
}
|
|
35
|
-
exports.collector = collector;
|
|
36
37
|
function collectorPlugin(babel, options) {
|
|
37
38
|
const values = options.values ?? new Map();
|
|
38
39
|
const debug = shared_1.logger.extend('collector');
|
|
@@ -59,4 +60,3 @@ function collectorPlugin(babel, options) {
|
|
|
59
60
|
},
|
|
60
61
|
};
|
|
61
62
|
}
|
|
62
|
-
exports.default = collectorPlugin;
|
|
@@ -1,40 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = dynamicImport;
|
|
4
|
+
const getMemberExpressionPropertyName_1 = require("../utils/getMemberExpressionPropertyName");
|
|
5
|
+
const unwrapExpression_1 = require("../utils/unwrapExpression");
|
|
3
6
|
/**
|
|
4
7
|
* The plugin that replaces `import()` with `__wyw_dynamic_import` as Node VM does not support dynamic imports yet.
|
|
5
8
|
*/
|
|
6
9
|
function dynamicImport(babel) {
|
|
7
10
|
const { types: t } = babel;
|
|
11
|
+
const isStringLikeExpression = (node) => {
|
|
12
|
+
const expression = (0, unwrapExpression_1.unwrapExpression)(node);
|
|
13
|
+
if (t.isStringLiteral(expression) || t.isTemplateLiteral(expression)) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
if (t.isBinaryExpression(expression) && expression.operator === '+') {
|
|
17
|
+
return ((t.isExpression(expression.left) &&
|
|
18
|
+
isStringLikeExpression(expression.left)) ||
|
|
19
|
+
(t.isExpression(expression.right) &&
|
|
20
|
+
isStringLikeExpression(expression.right)));
|
|
21
|
+
}
|
|
22
|
+
if (t.isCallExpression(expression)) {
|
|
23
|
+
const { callee } = expression;
|
|
24
|
+
if (!t.isMemberExpression(callee)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const propertyName = (0, getMemberExpressionPropertyName_1.getMemberExpressionPropertyName)(callee);
|
|
28
|
+
if (propertyName !== 'concat') {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
if (!t.isExpression(callee.object)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return isStringLikeExpression(callee.object);
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
};
|
|
8
38
|
return {
|
|
9
39
|
name: '@wyw-in-js/transform/dynamic-import',
|
|
10
40
|
visitor: {
|
|
11
41
|
CallExpression(path) {
|
|
12
42
|
if (path.get('callee').isImport()) {
|
|
13
43
|
const moduleName = path.get('arguments.0');
|
|
14
|
-
|
|
44
|
+
const argument = moduleName.isExpression() ? moduleName.node : null;
|
|
45
|
+
if (argument) {
|
|
46
|
+
const unwrappedArgument = (0, unwrapExpression_1.unwrapExpression)(argument);
|
|
47
|
+
const nextArgument = isStringLikeExpression(argument)
|
|
48
|
+
? t.cloneNode(unwrappedArgument, true, true)
|
|
49
|
+
: t.cloneNode(argument, true, true);
|
|
15
50
|
path.replaceWith(t.callExpression(t.identifier('__wyw_dynamic_import'), [
|
|
16
|
-
|
|
51
|
+
nextArgument,
|
|
17
52
|
]));
|
|
18
53
|
return;
|
|
19
54
|
}
|
|
20
|
-
|
|
21
|
-
path.replaceWith(t.callExpression(t.identifier('__wyw_dynamic_import'), [
|
|
22
|
-
t.cloneNode(moduleName.node, true, true),
|
|
23
|
-
]));
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
// Throw an error if this import will be reached during evaluation
|
|
27
|
-
// throw new Error(
|
|
28
|
-
// 'Dynamic import argument must be a string or a template literal'
|
|
29
|
-
// );
|
|
30
|
-
path.replaceWith(t.callExpression(t.arrowFunctionExpression([], t.blockStatement([
|
|
31
|
-
t.throwStatement(t.newExpression(t.identifier('Error'), [
|
|
32
|
-
t.stringLiteral('Dynamic import argument must be a string or a template literal'),
|
|
33
|
-
])),
|
|
34
|
-
])), []));
|
|
55
|
+
path.replaceWith(t.callExpression(t.identifier('__wyw_dynamic_import'), []));
|
|
35
56
|
}
|
|
36
57
|
},
|
|
37
58
|
},
|
|
38
59
|
};
|
|
39
60
|
}
|
|
40
|
-
exports.default = dynamicImport;
|
package/types/plugins/preeval.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.preeval =
|
|
3
|
+
exports.preeval = preeval;
|
|
4
4
|
const shared_1 = require("@wyw-in-js/shared");
|
|
5
5
|
const getTagProcessor_1 = require("../utils/getTagProcessor");
|
|
6
6
|
const EventEmitter_1 = require("../utils/EventEmitter");
|
|
7
7
|
const addIdentifierToWywPreval_1 = require("../utils/addIdentifierToWywPreval");
|
|
8
8
|
const getFileIdx_1 = require("../utils/getFileIdx");
|
|
9
9
|
const removeDangerousCode_1 = require("../utils/removeDangerousCode");
|
|
10
|
+
const replaceImportMetaEnv_1 = require("../utils/replaceImportMetaEnv");
|
|
10
11
|
const traversalCache_1 = require("../utils/traversalCache");
|
|
11
12
|
function preeval(babel, options) {
|
|
12
13
|
const { types: t } = babel;
|
|
@@ -30,6 +31,9 @@ function preeval(babel, options) {
|
|
|
30
31
|
this.processors.push(processor);
|
|
31
32
|
});
|
|
32
33
|
});
|
|
34
|
+
eventEmitter.perf('transform:preeval:importMetaEnv', () => {
|
|
35
|
+
(0, replaceImportMetaEnv_1.replaceImportMetaEnv)(file.path, t);
|
|
36
|
+
});
|
|
33
37
|
if ((0, shared_1.isFeatureEnabled)(options.features, 'dangerousCodeRemover', filename)) {
|
|
34
38
|
log('start', 'Strip all JSX and browser related stuff');
|
|
35
39
|
eventEmitter.perf('transform:preeval:removeDangerousCode', () => (0, removeDangerousCode_1.removeDangerousCode)(file.path, options.codeRemover));
|
|
@@ -62,5 +66,4 @@ function preeval(babel, options) {
|
|
|
62
66
|
},
|
|
63
67
|
};
|
|
64
68
|
}
|
|
65
|
-
exports.preeval = preeval;
|
|
66
69
|
exports.default = preeval;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import type { PluginObj } from '@babel/core';
|
|
2
|
+
import type { ImportOverrides } from '@wyw-in-js/shared';
|
|
2
3
|
import type { Core } from '../babel';
|
|
3
4
|
import type { IState } from '../utils/collectExportsAndImports';
|
|
4
5
|
export interface IShakerOptions {
|
|
5
6
|
ifUnknownExport?: 'error' | 'ignore' | 'reexport-all' | 'skip-shaking';
|
|
7
|
+
importOverrides?: ImportOverrides;
|
|
6
8
|
keepSideEffects?: boolean;
|
|
7
9
|
onlyExports: string[];
|
|
10
|
+
root?: string;
|
|
8
11
|
}
|
|
9
|
-
export default function shakerPlugin(babel: Core, { keepSideEffects, ifUnknownExport, onlyExports, }: IShakerOptions): PluginObj<IState & {
|
|
12
|
+
export default function shakerPlugin(babel: Core, { keepSideEffects, ifUnknownExport, importOverrides, onlyExports, root, }: IShakerOptions): PluginObj<IState & {
|
|
10
13
|
filename: string;
|
|
11
14
|
}>;
|
package/types/plugins/shaker.js
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = shakerPlugin;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
3
8
|
const shared_1 = require("@wyw-in-js/shared");
|
|
4
9
|
const collectExportsAndImports_1 = require("../utils/collectExportsAndImports");
|
|
5
10
|
const getFileIdx_1 = require("../utils/getFileIdx");
|
|
6
11
|
const isRemoved_1 = require("../utils/isRemoved");
|
|
7
12
|
const scopeHelpers_1 = require("../utils/scopeHelpers");
|
|
8
13
|
const traversalCache_1 = require("../utils/traversalCache");
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
+
const parseRequest_1 = require("../utils/parseRequest");
|
|
15
|
+
const importOverrides_1 = require("../utils/importOverrides");
|
|
16
|
+
function getNonParamBinding(exportPath, name) {
|
|
17
|
+
const binding = exportPath.scope.getBinding(name);
|
|
18
|
+
if (binding && binding.kind !== 'param') {
|
|
19
|
+
return binding;
|
|
20
|
+
}
|
|
21
|
+
// When `exportPath` is inside a function scope, a parameter can shadow
|
|
22
|
+
// the actual export binding (e.g. `export function fallback(fallback) {}`).
|
|
23
|
+
// In such cases we need the binding from the declaration scope.
|
|
24
|
+
return exportPath.scope.parent?.getBinding(name) ?? binding;
|
|
14
25
|
}
|
|
15
26
|
function getBindingForExport(exportPath) {
|
|
16
27
|
if (exportPath.isIdentifier()) {
|
|
17
|
-
return
|
|
28
|
+
return getNonParamBinding(exportPath, exportPath.node.name);
|
|
18
29
|
}
|
|
19
30
|
const variableDeclarator = exportPath.findParent((p) => p.isVariableDeclarator());
|
|
20
31
|
if (variableDeclarator) {
|
|
@@ -26,11 +37,11 @@ function getBindingForExport(exportPath) {
|
|
|
26
37
|
if (exportPath.isAssignmentExpression()) {
|
|
27
38
|
const left = exportPath.get('left');
|
|
28
39
|
if (left.isIdentifier()) {
|
|
29
|
-
return exportPath
|
|
40
|
+
return getNonParamBinding(exportPath, left.node.name);
|
|
30
41
|
}
|
|
31
42
|
}
|
|
32
43
|
if (exportPath.isFunctionDeclaration() || exportPath.isClassDeclaration()) {
|
|
33
|
-
return
|
|
44
|
+
return getNonParamBinding(exportPath, exportPath.node.id.name);
|
|
34
45
|
}
|
|
35
46
|
return undefined;
|
|
36
47
|
}
|
|
@@ -88,7 +99,44 @@ function rearrangeExports({ types: t }, root, exportRefs, exports) {
|
|
|
88
99
|
});
|
|
89
100
|
return rearranged;
|
|
90
101
|
}
|
|
91
|
-
|
|
102
|
+
const getPropertyAssignmentStatement = (ref, bindingName) => {
|
|
103
|
+
const assignment = ref.findParent((parent) => parent.isAssignmentExpression());
|
|
104
|
+
if (!assignment?.isAssignmentExpression())
|
|
105
|
+
return null;
|
|
106
|
+
const left = assignment.get('left');
|
|
107
|
+
if (!left.isMemberExpression())
|
|
108
|
+
return null;
|
|
109
|
+
const object = left.get('object');
|
|
110
|
+
if (!object.isIdentifier({ name: bindingName }))
|
|
111
|
+
return null;
|
|
112
|
+
const statement = assignment.parentPath;
|
|
113
|
+
return statement?.isExpressionStatement() ? statement : null;
|
|
114
|
+
};
|
|
115
|
+
const isWithinAliveExport = (ref, aliveExports) => [...aliveExports].some((alive) => alive === ref || alive.isAncestor(ref));
|
|
116
|
+
function stripExportKeepDeclaration(path) {
|
|
117
|
+
const exportDeclaration = path.findParent((p) => p.isExportNamedDeclaration());
|
|
118
|
+
if (!exportDeclaration)
|
|
119
|
+
return false;
|
|
120
|
+
const declaration = exportDeclaration.get('declaration');
|
|
121
|
+
if (!declaration.node)
|
|
122
|
+
return false;
|
|
123
|
+
if (declaration.isFunctionDeclaration() ||
|
|
124
|
+
declaration.isClassDeclaration() ||
|
|
125
|
+
declaration.isTSEnumDeclaration()) {
|
|
126
|
+
exportDeclaration.replaceWith(declaration.node);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
if (declaration.isVariableDeclaration()) {
|
|
130
|
+
const declarators = declaration.get('declarations');
|
|
131
|
+
if (declarators.length !== 1) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
exportDeclaration.replaceWith(declaration.node);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
function shakerPlugin(babel, { keepSideEffects = false, ifUnknownExport = 'skip-shaking', importOverrides, onlyExports, root, }) {
|
|
92
140
|
const shakerLogger = shared_1.logger.extend('shaker');
|
|
93
141
|
return {
|
|
94
142
|
name: '@wyw-in-js/transform/shaker',
|
|
@@ -97,6 +145,51 @@ function shakerPlugin(babel, { keepSideEffects = false, ifUnknownExport = 'skip-
|
|
|
97
145
|
const log = shakerLogger.extend((0, getFileIdx_1.getFileIdx)(this.filename));
|
|
98
146
|
log('start', `${this.filename}, onlyExports: ${onlyExports.join(',')}`);
|
|
99
147
|
const onlyExportsSet = new Set(onlyExports);
|
|
148
|
+
const shouldKeepOverride = (override) => !!override && ('mock' in override || 'noShake' in override);
|
|
149
|
+
const hasImportOverride = (() => {
|
|
150
|
+
if (!importOverrides || Object.keys(importOverrides).length === 0) {
|
|
151
|
+
return () => false;
|
|
152
|
+
}
|
|
153
|
+
const cache = new Map();
|
|
154
|
+
return (source) => {
|
|
155
|
+
const cached = cache.get(source);
|
|
156
|
+
if (cached !== undefined) {
|
|
157
|
+
return cached;
|
|
158
|
+
}
|
|
159
|
+
const strippedSource = (0, parseRequest_1.stripQueryAndHash)(source);
|
|
160
|
+
const direct = importOverrides[source] ??
|
|
161
|
+
(strippedSource !== source
|
|
162
|
+
? importOverrides[strippedSource]
|
|
163
|
+
: null);
|
|
164
|
+
if (direct) {
|
|
165
|
+
const result = shouldKeepOverride(direct);
|
|
166
|
+
cache.set(source, result);
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
const isFileImport = strippedSource.startsWith('.') ||
|
|
170
|
+
path_1.default.isAbsolute(strippedSource);
|
|
171
|
+
if (!isFileImport) {
|
|
172
|
+
cache.set(source, false);
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
try {
|
|
176
|
+
const resolved = (0, shared_1.syncResolve)(strippedSource, this.filename, []);
|
|
177
|
+
const { key } = (0, importOverrides_1.toImportKey)({
|
|
178
|
+
source: strippedSource,
|
|
179
|
+
resolved,
|
|
180
|
+
root,
|
|
181
|
+
});
|
|
182
|
+
const override = importOverrides[key];
|
|
183
|
+
const result = shouldKeepOverride(override);
|
|
184
|
+
cache.set(source, result);
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
cache.set(source, false);
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
})();
|
|
100
193
|
const collected = (0, collectExportsAndImports_1.collectExportsAndImports)(file.path);
|
|
101
194
|
const sideEffectImports = collected.imports.filter(collectExportsAndImports_1.sideEffectImport);
|
|
102
195
|
log('import-and-exports', [
|
|
@@ -157,11 +250,18 @@ function shakerPlugin(babel, { keepSideEffects = false, ifUnknownExport = 'skip-
|
|
|
157
250
|
Object.entries(exports).forEach(([exported, local]) => {
|
|
158
251
|
if (onlyExportsSet.has(exported)) {
|
|
159
252
|
aliveExports.add(local);
|
|
253
|
+
return;
|
|
160
254
|
}
|
|
161
|
-
|
|
255
|
+
const binding = local.isIdentifier() && local.scope.getBinding(local.node.name);
|
|
256
|
+
if (binding &&
|
|
257
|
+
(binding.path.isImportSpecifier() ||
|
|
258
|
+
binding.path.isImportDefaultSpecifier() ||
|
|
259
|
+
binding.path.isImportNamespaceSpecifier()) &&
|
|
260
|
+
importNames.includes(local.node.name || '')) {
|
|
162
261
|
aliveExports.add(local);
|
|
262
|
+
return;
|
|
163
263
|
}
|
|
164
|
-
|
|
264
|
+
if ([...aliveExports].some((alive) => alive === local)) {
|
|
165
265
|
// It's possible to export multiple values from a single variable initializer, e.g
|
|
166
266
|
// export const { foo, bar } = baz();
|
|
167
267
|
// We need to treat all of them as used if any of them are used, since otherwise
|
|
@@ -211,18 +311,29 @@ function shakerPlugin(babel, { keepSideEffects = false, ifUnknownExport = 'skip-
|
|
|
211
311
|
...Object.values(exports),
|
|
212
312
|
...collected.reexports.map((i) => i.local),
|
|
213
313
|
].filter((exp) => !aliveExports.has(exp));
|
|
314
|
+
const forDeletingSet = new Set(forDeleting);
|
|
315
|
+
const queueForDeleting = (path) => {
|
|
316
|
+
if ((0, isRemoved_1.isRemoved)(path) || forDeletingSet.has(path)) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
forDeletingSet.add(path);
|
|
320
|
+
forDeleting.push(path);
|
|
321
|
+
return true;
|
|
322
|
+
};
|
|
214
323
|
if (!keepSideEffects && !importedAsSideEffect) {
|
|
215
|
-
//
|
|
324
|
+
// Drop side-effect imports for eval-only builds unless they were explicitly requested.
|
|
325
|
+
// This prevents evaluating unrelated runtime code (e.g. Radix) during __wywPreval eval.
|
|
216
326
|
sideEffectImports.forEach((i) => {
|
|
217
|
-
if (
|
|
218
|
-
|
|
327
|
+
if (hasImportOverride(i.source)) {
|
|
328
|
+
return;
|
|
219
329
|
}
|
|
330
|
+
queueForDeleting(i.local);
|
|
220
331
|
});
|
|
221
332
|
}
|
|
222
333
|
const deleted = new Set();
|
|
223
334
|
let dereferenced = [];
|
|
224
335
|
let changed = true;
|
|
225
|
-
while (changed
|
|
336
|
+
while (changed) {
|
|
226
337
|
changed = false;
|
|
227
338
|
// eslint-disable-next-line no-restricted-syntax
|
|
228
339
|
for (const path of forDeleting) {
|
|
@@ -233,14 +344,50 @@ function shakerPlugin(babel, { keepSideEffects = false, ifUnknownExport = 'skip-
|
|
|
233
344
|
const binding = getBindingForExport(path);
|
|
234
345
|
const action = (0, scopeHelpers_1.findActionForNode)(path);
|
|
235
346
|
const parent = action?.[1];
|
|
236
|
-
const outerReferences = (binding?.referencePaths || []).filter((ref) =>
|
|
237
|
-
|
|
347
|
+
const outerReferences = (binding?.referencePaths || []).filter((ref) => {
|
|
348
|
+
if (ref === parent || parent?.isAncestor(ref)) {
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
return !forDeleting.some((candidate) => candidate !== path &&
|
|
352
|
+
!(0, isRemoved_1.isRemoved)(candidate) &&
|
|
353
|
+
(candidate === ref || candidate.isAncestor(ref)));
|
|
354
|
+
});
|
|
355
|
+
const bindingName = binding?.identifier.name;
|
|
356
|
+
const removableAssignmentStatements = new Set();
|
|
357
|
+
const removableOuterReferences = outerReferences.filter((ref) => {
|
|
358
|
+
if (!bindingName)
|
|
359
|
+
return false;
|
|
360
|
+
const statement = getPropertyAssignmentStatement(ref, bindingName);
|
|
361
|
+
if (!statement || isWithinAliveExport(statement, aliveExports)) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
removableAssignmentStatements.add(statement);
|
|
365
|
+
return true;
|
|
366
|
+
});
|
|
367
|
+
const blockingReferences = outerReferences.filter((ref) => !removableOuterReferences.includes(ref));
|
|
368
|
+
if (blockingReferences.length > 0 && path.isIdentifier()) {
|
|
238
369
|
// Temporary deref it in order to simplify further checks.
|
|
239
370
|
(0, scopeHelpers_1.dereference)(path);
|
|
240
371
|
dereferenced.push(path);
|
|
241
372
|
}
|
|
242
373
|
if (!deleted.has(path) &&
|
|
243
|
-
|
|
374
|
+
binding &&
|
|
375
|
+
blockingReferences.length > 0 &&
|
|
376
|
+
stripExportKeepDeclaration(path)) {
|
|
377
|
+
deleted.add(path);
|
|
378
|
+
changed = true;
|
|
379
|
+
// eslint-disable-next-line no-continue
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (!deleted.has(path) &&
|
|
383
|
+
(!binding || blockingReferences.length === 0)) {
|
|
384
|
+
if (removableAssignmentStatements.size > 0) {
|
|
385
|
+
for (const statement of removableAssignmentStatements) {
|
|
386
|
+
if (queueForDeleting(statement)) {
|
|
387
|
+
changed = true;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
244
391
|
if (action) {
|
|
245
392
|
(0, scopeHelpers_1.applyAction)(action);
|
|
246
393
|
}
|
|
@@ -261,11 +408,30 @@ function shakerPlugin(babel, { keepSideEffects = false, ifUnknownExport = 'skip-
|
|
|
261
408
|
// Find and mark for deleting all unreferenced variables
|
|
262
409
|
const unreferenced = Object.values(file.scope.getAllBindings()).filter((i) => !i.referenced);
|
|
263
410
|
for (const binding of unreferenced) {
|
|
264
|
-
if (binding.path.isVariableDeclarator()
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
411
|
+
if (binding.path.isVariableDeclarator()) {
|
|
412
|
+
const id = binding.path.get('id');
|
|
413
|
+
if (!(0, isRemoved_1.isRemoved)(id) && !forDeletingSet.has(id)) {
|
|
414
|
+
// Drop dead variable declarations, e.g. `const foo = make();` when `foo` is no longer referenced.
|
|
415
|
+
for (const violation of binding.constantViolations) {
|
|
416
|
+
if (queueForDeleting(violation)) {
|
|
417
|
+
changed = true;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (queueForDeleting(id)) {
|
|
421
|
+
changed = true;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
// Drop import specifiers whose bindings lost all references during shaking
|
|
426
|
+
// (e.g. when we keep only __wywPreval and the rest of the module is removed).
|
|
427
|
+
if ((binding.path.isImportSpecifier() ||
|
|
428
|
+
binding.path.isImportDefaultSpecifier() ||
|
|
429
|
+
binding.path.isImportNamespaceSpecifier()) &&
|
|
430
|
+
!(0, isRemoved_1.isRemoved)(binding.path) &&
|
|
431
|
+
!forDeletingSet.has(binding.path)) {
|
|
432
|
+
if (queueForDeleting(binding.path)) {
|
|
433
|
+
changed = true;
|
|
434
|
+
}
|
|
269
435
|
}
|
|
270
436
|
}
|
|
271
437
|
}
|
|
@@ -311,4 +477,3 @@ function shakerPlugin(babel, { keepSideEffects = false, ifUnknownExport = 'skip-
|
|
|
311
477
|
},
|
|
312
478
|
};
|
|
313
479
|
}
|
|
314
|
-
exports.default = shakerPlugin;
|
package/types/shaker.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.shaker = void 0;
|
|
7
|
+
const shaker_1 = __importDefault(require("./plugins/shaker"));
|
|
4
8
|
const ShakerMetadata_1 = require("./utils/ShakerMetadata");
|
|
5
9
|
const getPluginKey_1 = require("./utils/getPluginKey");
|
|
6
10
|
const hasKeyInList = (plugin, list) => {
|
|
@@ -22,7 +26,7 @@ const shaker = (evalConfig, ast, code, { highPriorityPlugins, ...config }, babel
|
|
|
22
26
|
[];
|
|
23
27
|
const plugins = [
|
|
24
28
|
...preShakePlugins,
|
|
25
|
-
[
|
|
29
|
+
[shaker_1.default, config],
|
|
26
30
|
...(evalConfig.plugins ?? []).filter((i) => !hasKeyInList(i, highPriorityPlugins)),
|
|
27
31
|
];
|
|
28
32
|
const hasCommonjsPlugin = evalConfig.plugins?.some((i) => (0, getPluginKey_1.getPluginKey)(i) === 'transform-modules-commonjs');
|
|
@@ -30,7 +34,9 @@ const shaker = (evalConfig, ast, code, { highPriorityPlugins, ...config }, babel
|
|
|
30
34
|
plugins.push(require.resolve('@babel/plugin-transform-modules-commonjs'));
|
|
31
35
|
}
|
|
32
36
|
if (evalConfig.filename?.endsWith('.ts') ||
|
|
33
|
-
evalConfig.filename?.endsWith('.tsx')
|
|
37
|
+
evalConfig.filename?.endsWith('.tsx') ||
|
|
38
|
+
evalConfig.filename?.endsWith('.mts') ||
|
|
39
|
+
evalConfig.filename?.endsWith('.cts')) {
|
|
34
40
|
const hasTypescriptPlugin = evalConfig.plugins?.some((i) => (0, getPluginKey_1.getPluginKey)(i) === 'transform-typescript');
|
|
35
41
|
if (!hasTypescriptPlugin) {
|
|
36
42
|
const preset = safeResolve('@babel/preset-typescript', [
|
|
@@ -41,7 +47,7 @@ const shaker = (evalConfig, ast, code, { highPriorityPlugins, ...config }, babel
|
|
|
41
47
|
preset,
|
|
42
48
|
]);
|
|
43
49
|
if (plugin) {
|
|
44
|
-
plugins.push(plugin);
|
|
50
|
+
plugins.push([plugin, { allowDeclareFields: true }]);
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
53
|
}
|