@wyw-in-js/transform 1.1.0 → 2.0.0-alpha.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/cache.js +426 -289
- package/esm/cache.js.map +1 -1
- package/esm/debug/fileReporter.js +137 -134
- package/esm/debug/fileReporter.js.map +1 -1
- package/esm/eval/broker.js +2134 -0
- package/esm/eval/broker.js.map +1 -0
- package/esm/eval/lru.js +36 -0
- package/esm/eval/lru.js.map +1 -0
- package/esm/eval/prepareModuleOnDemand.js +21 -0
- package/esm/eval/prepareModuleOnDemand.js.map +1 -0
- package/esm/eval/protocol.js +2 -0
- package/esm/eval/protocol.js.map +1 -0
- package/esm/eval/resolverStrategy.js +51 -0
- package/esm/eval/resolverStrategy.js.map +1 -0
- package/esm/eval/runner.js +1759 -0
- package/esm/eval/runner.js.map +1 -0
- package/esm/eval/serialize.js +333 -0
- package/esm/eval/serialize.js.map +1 -0
- package/esm/eval/writeQueue.js +81 -0
- package/esm/eval/writeQueue.js.map +1 -0
- package/esm/evaluators/index.js +11 -12
- package/esm/evaluators/index.js.map +1 -1
- package/esm/index.js +25 -29
- package/esm/index.js.map +1 -1
- package/esm/module.js +896 -520
- package/esm/module.js.map +1 -1
- package/esm/shaker.js +14 -80
- package/esm/shaker.js.map +1 -1
- package/esm/transform/BaseEntrypoint.js +162 -164
- package/esm/transform/BaseEntrypoint.js.map +1 -1
- package/esm/transform/Entrypoint.helpers.js +96 -253
- package/esm/transform/Entrypoint.helpers.js.map +1 -1
- package/esm/transform/Entrypoint.js +336 -270
- package/esm/transform/Entrypoint.js.map +1 -1
- package/esm/transform/Entrypoint.types.js +1 -1
- package/esm/transform/Entrypoint.types.js.map +1 -1
- package/esm/transform/EvaluatedEntrypoint.js +10 -4
- package/esm/transform/EvaluatedEntrypoint.js.map +1 -1
- package/esm/transform/actions/AbortError.js +6 -6
- package/esm/transform/actions/AbortError.js.map +1 -1
- package/esm/transform/actions/BaseAction.js +140 -136
- package/esm/transform/actions/BaseAction.js.map +1 -1
- package/esm/transform/actions/UnprocessedEntrypointError.js +6 -6
- package/esm/transform/actions/UnprocessedEntrypointError.js.map +1 -1
- package/esm/transform/actions/actionRunner.js +63 -66
- package/esm/transform/actions/actionRunner.js.map +1 -1
- package/esm/transform/actions/types.js +1 -1
- package/esm/transform/actions/types.js.map +1 -1
- package/esm/transform/barrelManifest.types.js +2 -0
- package/esm/transform/barrelManifest.types.js.map +1 -0
- package/esm/transform/generators/baseProcessingHandlers.js +15 -17
- package/esm/transform/generators/baseProcessingHandlers.js.map +1 -1
- package/esm/transform/generators/collect.js +27 -55
- package/esm/transform/generators/collect.js.map +1 -1
- package/esm/transform/generators/createStylisPreprocessor.js +353 -321
- package/esm/transform/generators/createStylisPreprocessor.js.map +1 -1
- package/esm/transform/generators/evalFile.js +48 -47
- package/esm/transform/generators/evalFile.js.map +1 -1
- package/esm/transform/generators/extract.js +78 -90
- package/esm/transform/generators/extract.js.map +1 -1
- package/esm/transform/generators/getExports.js +57 -74
- package/esm/transform/generators/getExports.js.map +1 -1
- package/esm/transform/generators/index.js +11 -11
- package/esm/transform/generators/index.js.map +1 -1
- package/esm/transform/generators/processEntrypoint.js +78 -67
- package/esm/transform/generators/processEntrypoint.js.map +1 -1
- package/esm/transform/generators/processImports.js +102 -67
- package/esm/transform/generators/processImports.js.map +1 -1
- package/esm/transform/generators/resolveImports.js +165 -196
- package/esm/transform/generators/resolveImports.js.map +1 -1
- package/esm/transform/generators/resolveStaticOxcValues.js +2911 -0
- package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -0
- package/esm/transform/generators/rewriteOxcBarrelImports.js +822 -0
- package/esm/transform/generators/rewriteOxcBarrelImports.js.map +1 -0
- package/esm/transform/generators/transform.js +239 -248
- package/esm/transform/generators/transform.js.map +1 -1
- package/esm/transform/generators/workflow.js +87 -90
- package/esm/transform/generators/workflow.js.map +1 -1
- package/esm/transform/helpers/loadWywOptions.js +154 -74
- package/esm/transform/helpers/loadWywOptions.js.map +1 -1
- package/esm/transform/helpers/withDefaultServices.js +13 -22
- package/esm/transform/helpers/withDefaultServices.js.map +1 -1
- package/esm/transform/isStaticallyEvaluatableModule.js +140 -152
- package/esm/transform/isStaticallyEvaluatableModule.js.map +1 -1
- package/esm/transform/oxcBarrelManifest.js +349 -0
- package/esm/transform/oxcBarrelManifest.js.map +1 -0
- package/esm/transform/rootLog.js +3 -3
- package/esm/transform/rootLog.js.map +1 -1
- package/esm/transform/syntax.js +2 -0
- package/esm/transform/syntax.js.map +1 -0
- package/esm/transform/types.js +2 -2
- package/esm/transform/types.js.map +1 -1
- package/esm/transform.js +123 -147
- package/esm/transform.js.map +1 -1
- package/esm/types.js +4 -1
- package/esm/types.js.map +1 -1
- package/esm/utils/EventEmitter.js +46 -48
- package/esm/utils/EventEmitter.js.map +1 -1
- package/esm/utils/ShakerMetadata.js +2 -2
- package/esm/utils/ShakerMetadata.js.map +1 -1
- package/esm/utils/TransformDiagnostics.js +9 -9
- package/esm/utils/TransformDiagnostics.js.map +1 -1
- package/esm/utils/TransformMetadata.js +24 -26
- package/esm/utils/TransformMetadata.js.map +1 -1
- package/esm/utils/applyOxcProcessors.js +1217 -0
- package/esm/utils/applyOxcProcessors.js.map +1 -0
- package/esm/utils/collectOxcExportsAndImports.js +934 -0
- package/esm/utils/collectOxcExportsAndImports.js.map +1 -0
- package/esm/utils/collectOxcRuntime.js +220 -0
- package/esm/utils/collectOxcRuntime.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies.js +1398 -0
- package/esm/utils/collectOxcTemplateDependencies.js.map +1 -0
- package/esm/utils/dispose-polyfill.js +3 -4
- package/esm/utils/dispose-polyfill.js.map +1 -1
- package/esm/utils/getFileIdx.js +6 -6
- package/esm/utils/getFileIdx.js.map +1 -1
- package/esm/utils/getPluginKey.js +12 -12
- package/esm/utils/getPluginKey.js.map +1 -1
- package/esm/utils/getVisitorKeys.js +9 -3
- package/esm/utils/getVisitorKeys.js.map +1 -1
- package/esm/utils/hasCachedWywPrevalExport.js +23 -0
- package/esm/utils/hasCachedWywPrevalExport.js.map +1 -0
- package/esm/utils/hasWywPreval.js +5 -5
- package/esm/utils/hasWywPreval.js.map +1 -1
- package/esm/utils/importOverrides.js +75 -90
- package/esm/utils/importOverrides.js.map +1 -1
- package/esm/utils/isNode.js +2 -2
- package/esm/utils/isNode.js.map +1 -1
- package/esm/utils/isNotNull.js +2 -2
- package/esm/utils/isNotNull.js.map +1 -1
- package/esm/utils/isSerializable.js +11 -11
- package/esm/utils/isSerializable.js.map +1 -1
- package/esm/utils/oxcAstService.js +121 -0
- package/esm/utils/oxcAstService.js.map +1 -0
- package/esm/utils/oxcEmit.js +447 -0
- package/esm/utils/oxcEmit.js.map +1 -0
- package/esm/utils/oxcPreevalStage.js +97 -0
- package/esm/utils/oxcPreevalStage.js.map +1 -0
- package/esm/utils/oxcPreevalTransforms.js +1054 -0
- package/esm/utils/oxcPreevalTransforms.js.map +1 -0
- package/esm/utils/oxcShaker.js +662 -0
- package/esm/utils/oxcShaker.js.map +1 -0
- package/esm/utils/parseOxc.js +37 -0
- package/esm/utils/parseOxc.js.map +1 -0
- package/esm/utils/parseRequest.js +27 -27
- package/esm/utils/parseRequest.js.map +1 -1
- package/esm/utils/peek.js +1 -1
- package/esm/utils/peek.js.map +1 -1
- package/esm/utils/processorLookup.js +125 -0
- package/esm/utils/processorLookup.js.map +1 -0
- package/esm/utils/resolveWithConditions.js +99 -0
- package/esm/utils/resolveWithConditions.js.map +1 -0
- package/esm/vm/createVmContext.js +140 -141
- package/esm/vm/createVmContext.js.map +1 -1
- package/esm/vm/process.js +11 -13
- package/esm/vm/process.js.map +1 -1
- package/package.json +18 -26
- package/types/cache.d.ts +17 -8
- package/types/cache.js +237 -95
- package/types/debug/fileReporter.js +22 -22
- package/types/eval/broker.d.ts +88 -0
- package/types/eval/broker.js +2262 -0
- package/types/eval/lru.d.ts +10 -0
- package/types/eval/lru.js +36 -0
- package/types/eval/prepareModuleOnDemand.d.ts +7 -0
- package/types/eval/prepareModuleOnDemand.js +24 -0
- package/types/eval/protocol.d.ts +97 -0
- package/types/eval/protocol.js +1 -0
- package/types/eval/resolverStrategy.d.ts +13 -0
- package/types/eval/resolverStrategy.js +46 -0
- package/types/eval/serialize.d.ts +78 -0
- package/types/eval/serialize.js +357 -0
- package/types/eval/writeQueue.d.ts +13 -0
- package/types/eval/writeQueue.js +80 -0
- package/types/evaluators/index.d.ts +2 -2
- package/types/evaluators/index.js +6 -9
- package/types/index.d.ts +3 -6
- package/types/index.js +24 -82
- package/types/module.d.ts +35 -7
- package/types/module.js +535 -163
- package/types/shaker.d.ts +2 -10
- package/types/shaker.js +10 -100
- package/types/transform/BaseEntrypoint.js +6 -11
- package/types/transform/Entrypoint.d.ts +15 -15
- package/types/transform/Entrypoint.helpers.d.ts +2 -5
- package/types/transform/Entrypoint.helpers.js +43 -203
- package/types/transform/Entrypoint.js +130 -53
- package/types/transform/Entrypoint.types.d.ts +28 -6
- package/types/transform/Entrypoint.types.js +1 -2
- package/types/transform/EvaluatedEntrypoint.d.ts +13 -2
- package/types/transform/EvaluatedEntrypoint.js +7 -6
- package/types/transform/actions/AbortError.js +2 -7
- package/types/transform/actions/BaseAction.js +4 -8
- package/types/transform/actions/UnprocessedEntrypointError.js +2 -7
- package/types/transform/actions/actionRunner.js +8 -12
- package/types/transform/actions/types.d.ts +2 -2
- package/types/transform/actions/types.js +1 -2
- package/types/transform/{barrelManifest.d.ts → barrelManifest.types.d.ts} +0 -2
- package/types/transform/barrelManifest.types.js +1 -0
- package/types/transform/generators/baseProcessingHandlers.d.ts +4 -5
- package/types/transform/generators/baseProcessingHandlers.js +10 -14
- package/types/transform/generators/collect.js +13 -39
- package/types/transform/generators/createStylisPreprocessor.js +19 -60
- package/types/transform/generators/evalFile.d.ts +2 -2
- package/types/transform/generators/evalFile.js +26 -28
- package/types/transform/generators/extract.js +5 -8
- package/types/transform/generators/getExports.js +23 -30
- package/types/transform/generators/index.d.ts +2 -2
- package/types/transform/generators/index.js +11 -14
- package/types/transform/generators/processEntrypoint.d.ts +2 -2
- package/types/transform/generators/processEntrypoint.js +60 -26
- package/types/transform/generators/processImports.d.ts +0 -3
- package/types/transform/generators/processImports.js +60 -20
- package/types/transform/generators/resolveImports.js +18 -22
- package/types/transform/generators/resolveStaticOxcValues.d.ts +2 -0
- package/types/transform/generators/resolveStaticOxcValues.js +3235 -0
- package/types/transform/generators/{rewriteBarrelImports.d.ts → rewriteOxcBarrelImports.d.ts} +2 -3
- package/types/transform/generators/{rewriteBarrelImports.js → rewriteOxcBarrelImports.js} +282 -225
- package/types/transform/generators/transform.d.ts +3 -7
- package/types/transform/generators/transform.js +203 -199
- package/types/transform/generators/workflow.js +62 -45
- package/types/transform/helpers/loadWywOptions.js +94 -20
- package/types/transform/helpers/withDefaultServices.d.ts +1 -1
- package/types/transform/helpers/withDefaultServices.js +6 -44
- package/types/transform/isStaticallyEvaluatableModule.d.ts +1 -2
- package/types/transform/isStaticallyEvaluatableModule.js +125 -126
- package/types/transform/oxcBarrelManifest.d.ts +2 -0
- package/types/transform/{barrelManifest.js → oxcBarrelManifest.js} +156 -97
- package/types/transform/rootLog.js +2 -5
- package/types/transform/syntax.d.ts +38 -0
- package/types/transform/syntax.js +1 -0
- package/types/transform/types.d.ts +9 -6
- package/types/transform/types.js +1 -4
- package/types/transform.d.ts +2 -2
- package/types/transform.js +88 -101
- package/types/types.d.ts +0 -23
- package/types/types.js +1 -2
- package/types/utils/EventEmitter.js +3 -9
- package/types/utils/ShakerMetadata.js +1 -5
- package/types/utils/TransformDiagnostics.js +3 -7
- package/types/utils/TransformMetadata.js +8 -16
- package/types/utils/applyOxcProcessors.d.ts +16 -0
- package/types/utils/applyOxcProcessors.js +1391 -0
- package/types/utils/collectOxcExportsAndImports.d.ts +35 -0
- package/types/utils/collectOxcExportsAndImports.js +957 -0
- package/types/utils/collectOxcRuntime.d.ts +14 -0
- package/types/utils/collectOxcRuntime.js +250 -0
- package/types/utils/collectOxcTemplateDependencies.d.ts +38 -0
- package/types/utils/collectOxcTemplateDependencies.js +1580 -0
- package/types/utils/getFileIdx.js +1 -4
- package/types/utils/getPluginKey.d.ts +5 -2
- package/types/utils/getPluginKey.js +2 -6
- package/types/utils/getVisitorKeys.d.ts +4 -4
- package/types/utils/getVisitorKeys.js +9 -6
- package/types/utils/hasCachedWywPrevalExport.d.ts +14 -0
- package/types/utils/hasCachedWywPrevalExport.js +30 -0
- package/types/utils/hasWywPreval.js +1 -4
- package/types/utils/importOverrides.js +17 -27
- package/types/utils/isNode.d.ts +2 -2
- package/types/utils/isNode.js +2 -6
- package/types/utils/isNotNull.js +1 -4
- package/types/utils/isSerializable.js +3 -6
- package/types/utils/oxcAstService.d.ts +11 -0
- package/types/utils/oxcAstService.js +79 -0
- package/types/utils/oxcEmit.d.ts +19 -0
- package/types/utils/oxcEmit.js +506 -0
- package/types/utils/oxcPreevalStage.d.ts +20 -0
- package/types/utils/oxcPreevalStage.js +102 -0
- package/types/utils/oxcPreevalTransforms.d.ts +13 -0
- package/types/utils/oxcPreevalTransforms.js +1179 -0
- package/types/utils/oxcShaker.d.ts +13 -0
- package/types/utils/oxcShaker.js +751 -0
- package/types/utils/parseOxc.d.ts +11 -0
- package/types/utils/parseOxc.js +38 -0
- package/types/utils/parseRequest.js +2 -7
- package/types/utils/peek.js +1 -5
- package/types/utils/processorLookup.d.ts +8 -0
- package/types/utils/processorLookup.js +135 -0
- package/types/utils/resolveWithConditions.d.ts +12 -0
- package/types/utils/resolveWithConditions.js +103 -0
- package/types/vm/createVmContext.d.ts +2 -2
- package/types/vm/createVmContext.js +25 -62
- package/types/vm/process.js +20 -26
- package/esm/babel.js +0 -2
- package/esm/babel.js.map +0 -1
- package/esm/options/buildOptions.js +0 -168
- package/esm/options/buildOptions.js.map +0 -1
- package/esm/options/buildOptions.test.js +0 -138
- package/esm/options/buildOptions.test.js.map +0 -1
- package/esm/options/loadBabelOptions.js +0 -24
- package/esm/options/loadBabelOptions.js.map +0 -1
- package/esm/plugins/babel-transform.js +0 -53
- package/esm/plugins/babel-transform.js.map +0 -1
- package/esm/plugins/collector.js +0 -60
- package/esm/plugins/collector.js.map +0 -1
- package/esm/plugins/dynamic-import.js +0 -56
- package/esm/plugins/dynamic-import.js.map +0 -1
- package/esm/plugins/preeval.js +0 -73
- package/esm/plugins/preeval.js.map +0 -1
- package/esm/plugins/shaker.js +0 -680
- package/esm/plugins/shaker.js.map +0 -1
- package/esm/transform/barrelManifest.js +0 -291
- package/esm/transform/barrelManifest.js.map +0 -1
- package/esm/transform/generators/explodeReexports.js +0 -64
- package/esm/transform/generators/explodeReexports.js.map +0 -1
- package/esm/transform/generators/rewriteBarrelImports.js +0 -733
- package/esm/transform/generators/rewriteBarrelImports.js.map +0 -1
- package/esm/utils/addIdentifierToWywPreval.js +0 -68
- package/esm/utils/addIdentifierToWywPreval.js.map +0 -1
- package/esm/utils/collectExportsAndImports.js +0 -1157
- package/esm/utils/collectExportsAndImports.js.map +0 -1
- package/esm/utils/collectTemplateDependencies.js +0 -228
- package/esm/utils/collectTemplateDependencies.js.map +0 -1
- package/esm/utils/createId.js +0 -6
- package/esm/utils/createId.js.map +0 -1
- package/esm/utils/findIdentifiers.js +0 -62
- package/esm/utils/findIdentifiers.js.map +0 -1
- package/esm/utils/getConstantStringValue.js +0 -58
- package/esm/utils/getConstantStringValue.js.map +0 -1
- package/esm/utils/getMemberExpressionPropertyName.js +0 -11
- package/esm/utils/getMemberExpressionPropertyName.js.map +0 -1
- package/esm/utils/getScope.js +0 -6
- package/esm/utils/getScope.js.map +0 -1
- package/esm/utils/getSource.js +0 -15
- package/esm/utils/getSource.js.map +0 -1
- package/esm/utils/getTagProcessor.js +0 -404
- package/esm/utils/getTagProcessor.js.map +0 -1
- package/esm/utils/isExports.js +0 -22
- package/esm/utils/isExports.js.map +0 -1
- package/esm/utils/isGlobal.js +0 -6
- package/esm/utils/isGlobal.js.map +0 -1
- package/esm/utils/isNodePath.js +0 -4
- package/esm/utils/isNodePath.js.map +0 -1
- package/esm/utils/isRemoved.js +0 -46
- package/esm/utils/isRemoved.js.map +0 -1
- package/esm/utils/isRequire.js +0 -13
- package/esm/utils/isRequire.js.map +0 -1
- package/esm/utils/isTypedNode.js +0 -6
- package/esm/utils/isTypedNode.js.map +0 -1
- package/esm/utils/isUnnecessaryReactCall.js +0 -72
- package/esm/utils/isUnnecessaryReactCall.js.map +0 -1
- package/esm/utils/removeDangerousCode.js +0 -276
- package/esm/utils/removeDangerousCode.js.map +0 -1
- package/esm/utils/replaceImportMetaEnv.js +0 -44
- package/esm/utils/replaceImportMetaEnv.js.map +0 -1
- package/esm/utils/scopeHelpers.js +0 -527
- package/esm/utils/scopeHelpers.js.map +0 -1
- package/esm/utils/traversalCache.js +0 -23
- package/esm/utils/traversalCache.js.map +0 -1
- package/esm/utils/unwrapExpression.js +0 -18
- package/esm/utils/unwrapExpression.js.map +0 -1
- package/esm/utils/unwrapSequence.js +0 -14
- package/esm/utils/unwrapSequence.js.map +0 -1
- package/esm/utils/valueToLiteral.js +0 -59
- package/esm/utils/valueToLiteral.js.map +0 -1
- package/esm/utils/visitors/JSXElementsRemover.js +0 -51
- package/esm/utils/visitors/JSXElementsRemover.js.map +0 -1
- package/lib/babel.js +0 -2
- package/lib/babel.js.map +0 -1
- package/lib/cache.js +0 -308
- package/lib/cache.js.map +0 -1
- package/lib/debug/fileReporter.js +0 -153
- package/lib/debug/fileReporter.js.map +0 -1
- package/lib/evaluators/index.js +0 -20
- package/lib/evaluators/index.js.map +0 -1
- package/lib/index.js +0 -286
- package/lib/index.js.map +0 -1
- package/lib/module.js +0 -552
- package/lib/module.js.map +0 -1
- package/lib/options/buildOptions.js +0 -176
- package/lib/options/buildOptions.js.map +0 -1
- package/lib/options/buildOptions.test.js +0 -141
- package/lib/options/buildOptions.test.js.map +0 -1
- package/lib/options/loadBabelOptions.js +0 -31
- package/lib/options/loadBabelOptions.js.map +0 -1
- package/lib/plugins/babel-transform.js +0 -60
- package/lib/plugins/babel-transform.js.map +0 -1
- package/lib/plugins/collector.js +0 -70
- package/lib/plugins/collector.js.map +0 -1
- package/lib/plugins/dynamic-import.js +0 -61
- package/lib/plugins/dynamic-import.js.map +0 -1
- package/lib/plugins/preeval.js +0 -81
- package/lib/plugins/preeval.js.map +0 -1
- package/lib/plugins/shaker.js +0 -691
- package/lib/plugins/shaker.js.map +0 -1
- package/lib/shaker.js +0 -95
- package/lib/shaker.js.map +0 -1
- package/lib/transform/BaseEntrypoint.js +0 -179
- package/lib/transform/BaseEntrypoint.js.map +0 -1
- package/lib/transform/Entrypoint.helpers.js +0 -279
- package/lib/transform/Entrypoint.helpers.js.map +0 -1
- package/lib/transform/Entrypoint.js +0 -289
- package/lib/transform/Entrypoint.js.map +0 -1
- package/lib/transform/Entrypoint.types.js +0 -2
- package/lib/transform/Entrypoint.types.js.map +0 -1
- package/lib/transform/EvaluatedEntrypoint.js +0 -13
- package/lib/transform/EvaluatedEntrypoint.js.map +0 -1
- package/lib/transform/actions/AbortError.js +0 -16
- package/lib/transform/actions/AbortError.js.map +0 -1
- package/lib/transform/actions/BaseAction.js +0 -150
- package/lib/transform/actions/BaseAction.js.map +0 -1
- package/lib/transform/actions/UnprocessedEntrypointError.js +0 -16
- package/lib/transform/actions/UnprocessedEntrypointError.js.map +0 -1
- package/lib/transform/actions/actionRunner.js +0 -82
- package/lib/transform/actions/actionRunner.js.map +0 -1
- package/lib/transform/actions/types.js +0 -2
- package/lib/transform/actions/types.js.map +0 -1
- package/lib/transform/barrelManifest.js +0 -300
- package/lib/transform/barrelManifest.js.map +0 -1
- package/lib/transform/generators/baseProcessingHandlers.js +0 -27
- package/lib/transform/generators/baseProcessingHandlers.js.map +0 -1
- package/lib/transform/generators/collect.js +0 -66
- package/lib/transform/generators/collect.js.map +0 -1
- package/lib/transform/generators/createStylisPreprocessor.js +0 -372
- package/lib/transform/generators/createStylisPreprocessor.js.map +0 -1
- package/lib/transform/generators/evalFile.js +0 -57
- package/lib/transform/generators/evalFile.js.map +0 -1
- package/lib/transform/generators/explodeReexports.js +0 -71
- package/lib/transform/generators/explodeReexports.js.map +0 -1
- package/lib/transform/generators/extract.js +0 -102
- package/lib/transform/generators/extract.js.map +0 -1
- package/lib/transform/generators/getExports.js +0 -85
- package/lib/transform/generators/getExports.js.map +0 -1
- package/lib/transform/generators/index.js +0 -19
- package/lib/transform/generators/index.js.map +0 -1
- package/lib/transform/generators/processEntrypoint.js +0 -76
- package/lib/transform/generators/processEntrypoint.js.map +0 -1
- package/lib/transform/generators/processImports.js +0 -82
- package/lib/transform/generators/processImports.js.map +0 -1
- package/lib/transform/generators/resolveImports.js +0 -221
- package/lib/transform/generators/resolveImports.js.map +0 -1
- package/lib/transform/generators/rewriteBarrelImports.js +0 -743
- package/lib/transform/generators/rewriteBarrelImports.js.map +0 -1
- package/lib/transform/generators/transform.js +0 -272
- package/lib/transform/generators/transform.js.map +0 -1
- package/lib/transform/generators/workflow.js +0 -100
- package/lib/transform/generators/workflow.js.map +0 -1
- package/lib/transform/helpers/loadWywOptions.js +0 -88
- package/lib/transform/helpers/loadWywOptions.js.map +0 -1
- package/lib/transform/helpers/withDefaultServices.js +0 -31
- package/lib/transform/helpers/withDefaultServices.js.map +0 -1
- package/lib/transform/isStaticallyEvaluatableModule.js +0 -159
- package/lib/transform/isStaticallyEvaluatableModule.js.map +0 -1
- package/lib/transform/rootLog.js +0 -9
- package/lib/transform/rootLog.js.map +0 -1
- package/lib/transform/types.js +0 -8
- package/lib/transform/types.js.map +0 -1
- package/lib/transform.js +0 -160
- package/lib/transform.js.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/lib/utils/EventEmitter.js +0 -61
- package/lib/utils/EventEmitter.js.map +0 -1
- package/lib/utils/ShakerMetadata.js +0 -9
- package/lib/utils/ShakerMetadata.js.map +0 -1
- package/lib/utils/TransformDiagnostics.js +0 -20
- package/lib/utils/TransformDiagnostics.js.map +0 -1
- package/lib/utils/TransformMetadata.js +0 -45
- package/lib/utils/TransformMetadata.js.map +0 -1
- package/lib/utils/addIdentifierToWywPreval.js +0 -75
- package/lib/utils/addIdentifierToWywPreval.js.map +0 -1
- package/lib/utils/collectExportsAndImports.js +0 -1173
- package/lib/utils/collectExportsAndImports.js.map +0 -1
- package/lib/utils/collectTemplateDependencies.js +0 -242
- package/lib/utils/collectTemplateDependencies.js.map +0 -1
- package/lib/utils/createId.js +0 -13
- package/lib/utils/createId.js.map +0 -1
- package/lib/utils/dispose-polyfill.js +0 -9
- package/lib/utils/dispose-polyfill.js.map +0 -1
- package/lib/utils/findIdentifiers.js +0 -73
- package/lib/utils/findIdentifiers.js.map +0 -1
- package/lib/utils/getConstantStringValue.js +0 -66
- package/lib/utils/getConstantStringValue.js.map +0 -1
- package/lib/utils/getFileIdx.js +0 -16
- package/lib/utils/getFileIdx.js.map +0 -1
- package/lib/utils/getMemberExpressionPropertyName.js +0 -18
- package/lib/utils/getMemberExpressionPropertyName.js.map +0 -1
- package/lib/utils/getPluginKey.js +0 -21
- package/lib/utils/getPluginKey.js.map +0 -1
- package/lib/utils/getScope.js +0 -12
- package/lib/utils/getScope.js.map +0 -1
- package/lib/utils/getSource.js +0 -24
- package/lib/utils/getSource.js.map +0 -1
- package/lib/utils/getTagProcessor.js +0 -424
- package/lib/utils/getTagProcessor.js.map +0 -1
- package/lib/utils/getVisitorKeys.js +0 -11
- package/lib/utils/getVisitorKeys.js.map +0 -1
- package/lib/utils/hasWywPreval.js +0 -13
- package/lib/utils/hasWywPreval.js.map +0 -1
- package/lib/utils/importOverrides.js +0 -119
- package/lib/utils/importOverrides.js.map +0 -1
- package/lib/utils/isExports.js +0 -27
- package/lib/utils/isExports.js.map +0 -1
- package/lib/utils/isGlobal.js +0 -13
- package/lib/utils/isGlobal.js.map +0 -1
- package/lib/utils/isNode.js +0 -9
- package/lib/utils/isNode.js.map +0 -1
- package/lib/utils/isNodePath.js +0 -10
- package/lib/utils/isNodePath.js.map +0 -1
- package/lib/utils/isNotNull.js +0 -10
- package/lib/utils/isNotNull.js.map +0 -1
- package/lib/utils/isRemoved.js +0 -52
- package/lib/utils/isRemoved.js.map +0 -1
- package/lib/utils/isRequire.js +0 -18
- package/lib/utils/isRequire.js.map +0 -1
- package/lib/utils/isSerializable.js +0 -19
- package/lib/utils/isSerializable.js.map +0 -1
- package/lib/utils/isTypedNode.js +0 -13
- package/lib/utils/isTypedNode.js.map +0 -1
- package/lib/utils/isUnnecessaryReactCall.js +0 -81
- package/lib/utils/isUnnecessaryReactCall.js.map +0 -1
- package/lib/utils/parseRequest.js +0 -37
- package/lib/utils/parseRequest.js.map +0 -1
- package/lib/utils/peek.js +0 -9
- package/lib/utils/peek.js.map +0 -1
- package/lib/utils/removeDangerousCode.js +0 -284
- package/lib/utils/removeDangerousCode.js.map +0 -1
- package/lib/utils/replaceImportMetaEnv.js +0 -50
- package/lib/utils/replaceImportMetaEnv.js.map +0 -1
- package/lib/utils/scopeHelpers.js +0 -557
- package/lib/utils/scopeHelpers.js.map +0 -1
- package/lib/utils/traversalCache.js +0 -31
- package/lib/utils/traversalCache.js.map +0 -1
- package/lib/utils/unwrapExpression.js +0 -25
- package/lib/utils/unwrapExpression.js.map +0 -1
- package/lib/utils/unwrapSequence.js +0 -20
- package/lib/utils/unwrapSequence.js.map +0 -1
- package/lib/utils/valueToLiteral.js +0 -65
- package/lib/utils/valueToLiteral.js.map +0 -1
- package/lib/utils/visitors/JSXElementsRemover.js +0 -57
- package/lib/utils/visitors/JSXElementsRemover.js.map +0 -1
- package/lib/vm/createVmContext.js +0 -166
- package/lib/vm/createVmContext.js.map +0 -1
- package/lib/vm/process.js +0 -38
- package/lib/vm/process.js.map +0 -1
- package/types/babel.d.ts +0 -2
- package/types/babel.js +0 -2
- package/types/options/buildOptions.d.ts +0 -6
- package/types/options/buildOptions.js +0 -178
- package/types/options/loadBabelOptions.d.ts +0 -3
- package/types/options/loadBabelOptions.js +0 -26
- package/types/plugins/babel-transform.d.ts +0 -4
- package/types/plugins/babel-transform.js +0 -49
- package/types/plugins/collector.d.ts +0 -23
- package/types/plugins/collector.js +0 -62
- package/types/plugins/dynamic-import.d.ts +0 -6
- package/types/plugins/dynamic-import.js +0 -60
- package/types/plugins/preeval.d.ts +0 -16
- package/types/plugins/preeval.js +0 -69
- package/types/plugins/shaker.d.ts +0 -14
- package/types/plugins/shaker.js +0 -724
- package/types/transform/generators/explodeReexports.d.ts +0 -7
- package/types/transform/generators/explodeReexports.js +0 -65
- package/types/utils/addIdentifierToWywPreval.d.ts +0 -4
- package/types/utils/addIdentifierToWywPreval.js +0 -74
- package/types/utils/collectExportsAndImports.d.ts +0 -31
- package/types/utils/collectExportsAndImports.js +0 -1147
- package/types/utils/collectTemplateDependencies.d.ts +0 -17
- package/types/utils/collectTemplateDependencies.js +0 -220
- package/types/utils/createId.d.ts +0 -2
- package/types/utils/createId.js +0 -9
- package/types/utils/findIdentifiers.d.ts +0 -6
- package/types/utils/findIdentifiers.js +0 -67
- package/types/utils/getConstantStringValue.d.ts +0 -2
- package/types/utils/getConstantStringValue.js +0 -94
- package/types/utils/getMemberExpressionPropertyName.d.ts +0 -2
- package/types/utils/getMemberExpressionPropertyName.js +0 -46
- package/types/utils/getScope.d.ts +0 -2
- package/types/utils/getScope.js +0 -10
- package/types/utils/getSource.d.ts +0 -2
- package/types/utils/getSource.js +0 -22
- package/types/utils/getTagProcessor.d.ts +0 -13
- package/types/utils/getTagProcessor.js +0 -411
- package/types/utils/isExports.d.ts +0 -6
- package/types/utils/isExports.js +0 -19
- package/types/utils/isGlobal.d.ts +0 -2
- package/types/utils/isGlobal.js +0 -9
- package/types/utils/isNodePath.d.ts +0 -3
- package/types/utils/isNodePath.js +0 -6
- package/types/utils/isRemoved.d.ts +0 -5
- package/types/utils/isRemoved.js +0 -41
- package/types/utils/isRequire.d.ts +0 -6
- package/types/utils/isRequire.js +0 -14
- package/types/utils/isTypedNode.d.ts +0 -5
- package/types/utils/isTypedNode.js +0 -9
- package/types/utils/isUnnecessaryReactCall.d.ts +0 -3
- package/types/utils/isUnnecessaryReactCall.js +0 -75
- package/types/utils/removeDangerousCode.d.ts +0 -4
- package/types/utils/removeDangerousCode.js +0 -326
- package/types/utils/replaceImportMetaEnv.d.ts +0 -3
- package/types/utils/replaceImportMetaEnv.js +0 -39
- package/types/utils/scopeHelpers.d.ts +0 -12
- package/types/utils/scopeHelpers.js +0 -580
- package/types/utils/traversalCache.d.ts +0 -4
- package/types/utils/traversalCache.js +0 -27
- package/types/utils/unwrapExpression.d.ts +0 -2
- package/types/utils/unwrapExpression.js +0 -57
- package/types/utils/unwrapSequence.d.ts +0 -8
- package/types/utils/unwrapSequence.js +0 -16
- package/types/utils/valueToLiteral.d.ts +0 -3
- package/types/utils/valueToLiteral.js +0 -63
- package/types/utils/visitors/JSXElementsRemover.d.ts +0 -3
- package/types/utils/visitors/JSXElementsRemover.js +0 -51
|
@@ -0,0 +1,1054 @@
|
|
|
1
|
+
import { collectOxcExportsAndImports } from "./collectOxcExportsAndImports.js";
|
|
2
|
+
import { EventEmitter } from "./EventEmitter.js";
|
|
3
|
+
import { parseOxcProgramCached } from "./parseOxc.js";
|
|
4
|
+
const ssrCheckFields = new Set([
|
|
5
|
+
"document",
|
|
6
|
+
"location",
|
|
7
|
+
"navigator",
|
|
8
|
+
"sessionStorage",
|
|
9
|
+
"localStorage",
|
|
10
|
+
"window"
|
|
11
|
+
]);
|
|
12
|
+
const forbiddenGlobals = new Set([
|
|
13
|
+
...ssrCheckFields,
|
|
14
|
+
"$RefreshReg$",
|
|
15
|
+
"$RefreshSig$",
|
|
16
|
+
"XMLHttpRequest",
|
|
17
|
+
"clearImmediate",
|
|
18
|
+
"clearInterval",
|
|
19
|
+
"clearTimeout",
|
|
20
|
+
"fetch",
|
|
21
|
+
"navigator",
|
|
22
|
+
"setImmediate",
|
|
23
|
+
"setInterval",
|
|
24
|
+
"setTimeout"
|
|
25
|
+
]);
|
|
26
|
+
const alwaysForbiddenIdentifiers = new Set(["$RefreshReg$", "$RefreshSig$"]);
|
|
27
|
+
const promiseCallbackMethods = new Set([
|
|
28
|
+
"then",
|
|
29
|
+
"catch",
|
|
30
|
+
"finally"
|
|
31
|
+
]);
|
|
32
|
+
const jsxRuntimeSources = new Set(["react/jsx-runtime", "react/jsx-dev-runtime"]);
|
|
33
|
+
const defaultPlaceholder = "...";
|
|
34
|
+
const defaultReactComponentTypes = [
|
|
35
|
+
"ExoticComponent",
|
|
36
|
+
"FC",
|
|
37
|
+
"ForwardRefExoticComponent",
|
|
38
|
+
"FunctionComponent",
|
|
39
|
+
"LazyExoticComponent",
|
|
40
|
+
"MemoExoticComponent",
|
|
41
|
+
"NamedExoticComponent"
|
|
42
|
+
];
|
|
43
|
+
const generatedProcessorHelperNameRe = /^_exp\d*$/;
|
|
44
|
+
const requireCallRe = /\brequire\s*\(/;
|
|
45
|
+
const windowTokenRe = /\bwindow\b/;
|
|
46
|
+
const removableOwnerTypes = new Set([
|
|
47
|
+
"DoWhileStatement",
|
|
48
|
+
"ExpressionStatement",
|
|
49
|
+
"ForInStatement",
|
|
50
|
+
"ForOfStatement",
|
|
51
|
+
"ForStatement",
|
|
52
|
+
"FunctionDeclaration",
|
|
53
|
+
"IfStatement",
|
|
54
|
+
"PropertyDefinition",
|
|
55
|
+
"ReturnStatement",
|
|
56
|
+
"VariableDeclaration",
|
|
57
|
+
"WhileStatement"
|
|
58
|
+
]);
|
|
59
|
+
const importMetaEnvRe = /\bimport\s*\.\s*meta\s*\.\s*env\b/;
|
|
60
|
+
const createScope = (parent, key) => ({
|
|
61
|
+
bindings: new Map(),
|
|
62
|
+
key,
|
|
63
|
+
names: new Set(),
|
|
64
|
+
parent
|
|
65
|
+
});
|
|
66
|
+
const createsScope = (node) => node.type === "Program" || node.type === "BlockStatement" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
67
|
+
const hasBinding = (scope, name) => {
|
|
68
|
+
let current = scope;
|
|
69
|
+
while (current) {
|
|
70
|
+
if (current.names.has(name)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
current = current.parent;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
};
|
|
77
|
+
const getBindingKey = (scope, name) => {
|
|
78
|
+
let current = scope;
|
|
79
|
+
while (current) {
|
|
80
|
+
if (current.names.has(name)) {
|
|
81
|
+
return `${current.key}\0${name}`;
|
|
82
|
+
}
|
|
83
|
+
current = current.parent;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
};
|
|
87
|
+
const getStaticBinding = (scope, name) => {
|
|
88
|
+
let current = scope;
|
|
89
|
+
while (current) {
|
|
90
|
+
if (current.bindings.has(name)) {
|
|
91
|
+
return current.bindings.get(name);
|
|
92
|
+
}
|
|
93
|
+
current = current.parent;
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
};
|
|
97
|
+
const isFileLikeRequireSpecifier = (value) => value.startsWith(".") || value.startsWith("/") || value.startsWith("file:");
|
|
98
|
+
const isNode = (value) => !!value && typeof value === "object" && "type" in value && typeof value.type === "string";
|
|
99
|
+
const getChildren = (node) => {
|
|
100
|
+
const result = [];
|
|
101
|
+
const record = node;
|
|
102
|
+
Object.keys(record).forEach((key) => {
|
|
103
|
+
if (key === "type" || key === "start" || key === "end" || key === "range") {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const value = record[key];
|
|
107
|
+
if (isNode(value)) {
|
|
108
|
+
result.push(value);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (Array.isArray(value)) {
|
|
112
|
+
value.forEach((item) => {
|
|
113
|
+
if (isNode(item)) {
|
|
114
|
+
result.push(item);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return result;
|
|
120
|
+
};
|
|
121
|
+
const parseOxc = (code, filename) => {
|
|
122
|
+
return parseOxcProgramCached(filename, code, "unambiguous");
|
|
123
|
+
};
|
|
124
|
+
const unwrapExpression = (node) => {
|
|
125
|
+
if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression" || node.type === "TSNonNullExpression" || node.type === "TSTypeAssertion" || node.type === "ParenthesizedExpression") {
|
|
126
|
+
return unwrapExpression(node.expression);
|
|
127
|
+
}
|
|
128
|
+
return node;
|
|
129
|
+
};
|
|
130
|
+
const getMemberPropertyName = (node) => {
|
|
131
|
+
if (node.type !== "MemberExpression") {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
if (node.computed) {
|
|
135
|
+
return node.property.type === "Literal" && typeof node.property.value === "string" ? node.property.value : null;
|
|
136
|
+
}
|
|
137
|
+
return node.property.type === "Identifier" ? node.property.name : null;
|
|
138
|
+
};
|
|
139
|
+
const isStringLikeExpression = (node) => {
|
|
140
|
+
const expression = unwrapExpression(node);
|
|
141
|
+
if (expression.type === "Literal" && typeof expression.value === "string") {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
if (expression.type === "TemplateLiteral") {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
if (expression.type === "BinaryExpression" && expression.operator === "+") {
|
|
148
|
+
return isStringLikeExpression(expression.left) || isStringLikeExpression(expression.right);
|
|
149
|
+
}
|
|
150
|
+
if (expression.type === "CallExpression" && expression.callee.type === "MemberExpression" && getMemberPropertyName(expression.callee) === "concat") {
|
|
151
|
+
return isStringLikeExpression(expression.callee.object);
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
};
|
|
155
|
+
const templateLiteralToConcat = (code, node) => {
|
|
156
|
+
if (node.type !== "TemplateLiteral" || node.expressions.length === 0) {
|
|
157
|
+
return code.slice(node.start, node.end);
|
|
158
|
+
}
|
|
159
|
+
const parts = [];
|
|
160
|
+
node.quasis.forEach((quasi, index) => {
|
|
161
|
+
const cooked = quasi.value.cooked ?? quasi.value.raw;
|
|
162
|
+
if (cooked !== "") {
|
|
163
|
+
parts.push(JSON.stringify(cooked));
|
|
164
|
+
}
|
|
165
|
+
const expression = node.expressions[index];
|
|
166
|
+
if (expression) {
|
|
167
|
+
parts.push(code.slice(expression.start, expression.end));
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return parts.length > 0 ? parts.join(" + ") : "\"\"";
|
|
171
|
+
};
|
|
172
|
+
const dynamicImportArgumentCode = (code, node) => {
|
|
173
|
+
if (node.type === "TemplateLiteral") {
|
|
174
|
+
return templateLiteralToConcat(code, node);
|
|
175
|
+
}
|
|
176
|
+
return code.slice(node.start, node.end);
|
|
177
|
+
};
|
|
178
|
+
const evaluateStaticValue = (node, scope, seen = new Set()) => {
|
|
179
|
+
const expression = unwrapExpression(node);
|
|
180
|
+
if (expression.type === "Literal") {
|
|
181
|
+
return expression.value;
|
|
182
|
+
}
|
|
183
|
+
if (expression.type === "TemplateLiteral") {
|
|
184
|
+
let result = "";
|
|
185
|
+
for (let idx = 0; idx < expression.quasis.length; idx += 1) {
|
|
186
|
+
result += expression.quasis[idx]?.value.cooked ?? "";
|
|
187
|
+
const nextExpression = expression.expressions[idx];
|
|
188
|
+
if (!nextExpression) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const value = evaluateStaticValue(nextExpression, scope, seen);
|
|
192
|
+
if (value === undefined || typeof value !== "string" && typeof value !== "number") {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
result += String(value);
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
if (expression.type === "Identifier") {
|
|
200
|
+
if (seen.has(expression.name)) {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
const binding = getStaticBinding(scope, expression.name);
|
|
204
|
+
if (!binding) {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
return evaluateStaticValue(binding, scope, new Set([...seen, expression.name]));
|
|
208
|
+
}
|
|
209
|
+
if (expression.type === "BinaryExpression" && expression.operator === "+") {
|
|
210
|
+
const left = evaluateStaticValue(expression.left, scope, seen);
|
|
211
|
+
const right = evaluateStaticValue(expression.right, scope, seen);
|
|
212
|
+
if (left === undefined || right === undefined) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
216
|
+
return left + right;
|
|
217
|
+
}
|
|
218
|
+
if ((typeof left === "string" || typeof left === "number") && (typeof right === "string" || typeof right === "number")) {
|
|
219
|
+
return `${left}${right}`;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (expression.type === "CallExpression" && expression.callee.type === "MemberExpression") {
|
|
223
|
+
const objectValue = evaluateStaticValue(expression.callee.object, scope, seen);
|
|
224
|
+
const propertyName = getMemberPropertyName(expression.callee);
|
|
225
|
+
if (!propertyName) {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
if (typeof objectValue === "string") {
|
|
229
|
+
if (propertyName === "toLowerCase" && expression.arguments.length === 0) {
|
|
230
|
+
return objectValue.toLowerCase();
|
|
231
|
+
}
|
|
232
|
+
if (propertyName === "toUpperCase" && expression.arguments.length === 0) {
|
|
233
|
+
return objectValue.toUpperCase();
|
|
234
|
+
}
|
|
235
|
+
if (propertyName === "trim" && expression.arguments.length === 0) {
|
|
236
|
+
return objectValue.trim();
|
|
237
|
+
}
|
|
238
|
+
if (propertyName === "concat") {
|
|
239
|
+
const args = expression.arguments.map((argument) => argument.type === "SpreadElement" ? undefined : evaluateStaticValue(argument, scope, seen));
|
|
240
|
+
if (args.some((value) => value === undefined) || args.some((value) => typeof value !== "string" && typeof value !== "number")) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
return objectValue.concat(...args.map((value) => String(value)));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return undefined;
|
|
248
|
+
};
|
|
249
|
+
const isLiteralRequireArg = (node) => {
|
|
250
|
+
const expression = unwrapExpression(node);
|
|
251
|
+
if (expression.type === "Literal" && typeof expression.value === "string") {
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
if (expression.type === "TemplateLiteral" && expression.expressions.length === 0) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
};
|
|
259
|
+
const applyReplacements = (code, replacements) => {
|
|
260
|
+
let result = code;
|
|
261
|
+
replacements.sort((a, b) => b.start - a.start).forEach((replacement) => {
|
|
262
|
+
result = result.slice(0, replacement.start) + replacement.value + result.slice(replacement.end);
|
|
263
|
+
});
|
|
264
|
+
return result;
|
|
265
|
+
};
|
|
266
|
+
const isIdentifierNamed = (value, name) => isNode(value) && value.type === "Identifier" && value.name === name;
|
|
267
|
+
const isImportMeta = (node) => {
|
|
268
|
+
if (!isNode(node) || node.type !== "MetaProperty") {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
const metaProperty = node;
|
|
272
|
+
return isIdentifierNamed(metaProperty.meta, "import") && isIdentifierNamed(metaProperty.property, "meta");
|
|
273
|
+
};
|
|
274
|
+
const isImportMetaEnv = (node) => {
|
|
275
|
+
if (node.type !== "MemberExpression") {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
if (node.computed || !isIdentifierNamed(node.property, "env")) {
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
return isImportMeta(node.object);
|
|
282
|
+
};
|
|
283
|
+
const collectBindingNames = (node) => {
|
|
284
|
+
if (node.type === "Identifier") {
|
|
285
|
+
return [node.name];
|
|
286
|
+
}
|
|
287
|
+
if (node.type === "RestElement") {
|
|
288
|
+
return collectBindingNames(node.argument);
|
|
289
|
+
}
|
|
290
|
+
if (node.type === "AssignmentPattern") {
|
|
291
|
+
return collectBindingNames(node.left);
|
|
292
|
+
}
|
|
293
|
+
if (node.type === "ObjectPattern") {
|
|
294
|
+
return node.properties.flatMap((property) => property.type === "RestElement" ? collectBindingNames(property.argument) : collectBindingNames(property.value));
|
|
295
|
+
}
|
|
296
|
+
if (node.type === "ArrayPattern") {
|
|
297
|
+
return node.elements.flatMap((element) => element ? collectBindingNames(element) : []);
|
|
298
|
+
}
|
|
299
|
+
if (node.type === "TSParameterProperty") {
|
|
300
|
+
return collectBindingNames(node.parameter);
|
|
301
|
+
}
|
|
302
|
+
return [];
|
|
303
|
+
};
|
|
304
|
+
const predeclareScopeNames = (node, scope) => {
|
|
305
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
306
|
+
if (node.type !== "ArrowFunctionExpression" && node.id) {
|
|
307
|
+
scope.names.add(node.id.name);
|
|
308
|
+
}
|
|
309
|
+
node.params.forEach((param) => {
|
|
310
|
+
collectBindingNames(param).forEach((name) => {
|
|
311
|
+
scope.names.add(name);
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
const visitScopeDescendants = (child) => {
|
|
316
|
+
if (child.type === "VariableDeclarator") {
|
|
317
|
+
collectBindingNames(child.id).forEach((name) => {
|
|
318
|
+
scope.names.add(name);
|
|
319
|
+
});
|
|
320
|
+
} else if (child.type === "FunctionDeclaration" && child.id) {
|
|
321
|
+
scope.names.add(child.id.name);
|
|
322
|
+
} else if (child.type === "ClassDeclaration" && child.id) {
|
|
323
|
+
scope.names.add(child.id.name);
|
|
324
|
+
} else if (child.type === "ImportDefaultSpecifier" || child.type === "ImportNamespaceSpecifier" || child.type === "ImportSpecifier") {
|
|
325
|
+
scope.names.add(child.local.name);
|
|
326
|
+
}
|
|
327
|
+
if (createsScope(child)) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
getChildren(child).forEach(visitScopeDescendants);
|
|
331
|
+
};
|
|
332
|
+
getChildren(node).forEach(visitScopeDescendants);
|
|
333
|
+
};
|
|
334
|
+
const declareBindings = (node, scope) => {
|
|
335
|
+
if (node.type === "VariableDeclarator") {
|
|
336
|
+
const names = collectBindingNames(node.id);
|
|
337
|
+
names.forEach((name) => {
|
|
338
|
+
scope.names.add(name);
|
|
339
|
+
scope.bindings.set(name, null);
|
|
340
|
+
});
|
|
341
|
+
if (node.id.type === "Identifier" && node.init) {
|
|
342
|
+
scope.bindings.set(node.id.name, node.init);
|
|
343
|
+
}
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (node.type === "FunctionDeclaration" && node.id) {
|
|
347
|
+
scope.names.add(node.id.name);
|
|
348
|
+
scope.bindings.set(node.id.name, null);
|
|
349
|
+
}
|
|
350
|
+
if (node.type === "ClassDeclaration" && node.id) {
|
|
351
|
+
scope.names.add(node.id.name);
|
|
352
|
+
scope.bindings.set(node.id.name, null);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier" || node.type === "ImportSpecifier") {
|
|
356
|
+
scope.names.add(node.local.name);
|
|
357
|
+
scope.bindings.set(node.local.name, null);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
361
|
+
node.params.forEach((param) => {
|
|
362
|
+
collectBindingNames(param).forEach((name) => {
|
|
363
|
+
scope.names.add(name);
|
|
364
|
+
scope.bindings.set(name, null);
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
const visit = (node, scope, enter, parent = null, ancestors = []) => {
|
|
370
|
+
let currentScope = scope;
|
|
371
|
+
if (createsScope(node)) {
|
|
372
|
+
currentScope = createScope(scope, `${node.type}:${node.start}:${node.end}`);
|
|
373
|
+
predeclareScopeNames(node, currentScope);
|
|
374
|
+
}
|
|
375
|
+
declareBindings(node, currentScope);
|
|
376
|
+
enter(node, currentScope, parent, ancestors);
|
|
377
|
+
getChildren(node).forEach((child) => visit(child, currentScope, enter, node, [...ancestors, node]));
|
|
378
|
+
};
|
|
379
|
+
export const replaceImportMetaEnvWithOxc = (code, filename) => {
|
|
380
|
+
if (!importMetaEnvRe.test(code)) {
|
|
381
|
+
return code;
|
|
382
|
+
}
|
|
383
|
+
const replacements = [];
|
|
384
|
+
visit(parseOxc(code, filename), createScope(null, "root"), (node) => {
|
|
385
|
+
if (!isImportMetaEnv(node)) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
replacements.push({
|
|
389
|
+
end: node.end,
|
|
390
|
+
start: node.start,
|
|
391
|
+
value: "__wyw_import_meta_env"
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
return applyReplacements(code, replacements);
|
|
395
|
+
};
|
|
396
|
+
function collectDynamicImportAndRequireFallbackReplacements(code, filename, options) {
|
|
397
|
+
const eventEmitter = options.eventEmitter ?? EventEmitter.dummy;
|
|
398
|
+
const dynamicImportCandidates = [];
|
|
399
|
+
const requireFallbackCandidates = [];
|
|
400
|
+
eventEmitter.perf("transform:preeval:dynamicImportRequireFallback:scan", () => {
|
|
401
|
+
visit(parseOxc(code, filename), createScope(null, "root"), (node, scope) => {
|
|
402
|
+
if (options.rewriteDynamicImports && node.type === "ImportExpression") {
|
|
403
|
+
dynamicImportCandidates.push(node);
|
|
404
|
+
}
|
|
405
|
+
if (!options.addRequireFallback || node.type !== "CallExpression") {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const call = node;
|
|
409
|
+
if (call.callee.type !== "Identifier" || call.callee.name !== "require" || hasBinding(scope, "require") || call.arguments.length !== 1) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const [firstArg] = call.arguments;
|
|
413
|
+
if (!firstArg || firstArg.type === "SpreadElement" || isLiteralRequireArg(firstArg)) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
requireFallbackCandidates.push({
|
|
417
|
+
call,
|
|
418
|
+
scope
|
|
419
|
+
});
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
const replacements = [];
|
|
423
|
+
eventEmitter.perf("transform:preeval:dynamicImport", () => {
|
|
424
|
+
dynamicImportCandidates.forEach((importExpression) => {
|
|
425
|
+
const argument = importExpression.source;
|
|
426
|
+
const nextArgument = isStringLikeExpression(argument) ? unwrapExpression(argument) : argument;
|
|
427
|
+
replacements.push({
|
|
428
|
+
end: importExpression.end,
|
|
429
|
+
start: importExpression.start,
|
|
430
|
+
value: `__wyw_dynamic_import(${dynamicImportArgumentCode(code, nextArgument)})`
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
eventEmitter.perf("transform:preeval:requireFallback", () => {
|
|
435
|
+
requireFallbackCandidates.forEach(({ call, scope }) => {
|
|
436
|
+
const [firstArg] = call.arguments;
|
|
437
|
+
if (!firstArg || firstArg.type === "SpreadElement") {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
const staticValue = evaluateStaticValue(firstArg, scope);
|
|
441
|
+
if (typeof staticValue === "string" && isFileLikeRequireSpecifier(staticValue)) {
|
|
442
|
+
replacements.push({
|
|
443
|
+
end: firstArg.end,
|
|
444
|
+
start: firstArg.start,
|
|
445
|
+
value: JSON.stringify(staticValue)
|
|
446
|
+
});
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
replacements.push({
|
|
450
|
+
end: firstArg.end,
|
|
451
|
+
start: firstArg.end,
|
|
452
|
+
value: ", true"
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
return replacements;
|
|
457
|
+
}
|
|
458
|
+
export const rewriteDynamicImportsWithOxc = (code, filename) => {
|
|
459
|
+
const replacements = collectDynamicImportAndRequireFallbackReplacements(code, filename, {
|
|
460
|
+
addRequireFallback: false,
|
|
461
|
+
rewriteDynamicImports: true
|
|
462
|
+
});
|
|
463
|
+
return applyReplacements(code, replacements);
|
|
464
|
+
};
|
|
465
|
+
export const addRequireFallbackWithOxc = (code, filename) => {
|
|
466
|
+
const replacements = collectDynamicImportAndRequireFallbackReplacements(code, filename, {
|
|
467
|
+
addRequireFallback: true,
|
|
468
|
+
rewriteDynamicImports: false
|
|
469
|
+
});
|
|
470
|
+
return applyReplacements(code, replacements);
|
|
471
|
+
};
|
|
472
|
+
export const rewriteDynamicImportsAndAddRequireFallbackWithOxc = (code, filename, options) => {
|
|
473
|
+
const replacements = collectDynamicImportAndRequireFallbackReplacements(code, filename, options);
|
|
474
|
+
return applyReplacements(code, replacements);
|
|
475
|
+
};
|
|
476
|
+
const isBindingPosition = (node, parent) => {
|
|
477
|
+
if (!parent) {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
if (parent.type === "VariableDeclarator" && parent.id === node) {
|
|
481
|
+
return true;
|
|
482
|
+
}
|
|
483
|
+
if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id === node) {
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
if (parent.type === "ImportSpecifier" && (parent.local === node || parent.imported === node)) {
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
if ((parent.type === "ImportDefaultSpecifier" || parent.type === "ImportNamespaceSpecifier") && parent.local === node) {
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
if (parent.type === "ExportSpecifier" && (parent.local === node || parent.exported === node)) {
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
return false;
|
|
496
|
+
};
|
|
497
|
+
const isPropertyOnlyIdentifier = (node, parent) => {
|
|
498
|
+
if (!parent) {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
if (parent.type === "MemberExpression" && !parent.computed && parent.property === node) {
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && !parent.computed && parent.key === node) {
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
return false;
|
|
508
|
+
};
|
|
509
|
+
const isTypeContext = (ancestors) => ancestors.some((ancestor) => ancestor.type.startsWith("TS") || ancestor.type.startsWith("JSDoc"));
|
|
510
|
+
const isInsideTypeof = (ancestors) => ancestors.some((ancestor) => ancestor.type === "UnaryExpression" && ancestor.operator === "typeof");
|
|
511
|
+
const isInsideImportDeclaration = (ancestors) => ancestors.some((ancestor) => ancestor.type === "ImportDeclaration");
|
|
512
|
+
function findLastAncestor(ancestors, predicate) {
|
|
513
|
+
for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
|
|
514
|
+
const ancestor = ancestors[idx];
|
|
515
|
+
if (predicate(ancestor)) {
|
|
516
|
+
return ancestor;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
const findRemovableOwner = (node, ancestors) => {
|
|
522
|
+
let owner = node;
|
|
523
|
+
let ownerAncestorIndex = -1;
|
|
524
|
+
if (!removableOwnerTypes.has(node.type)) {
|
|
525
|
+
for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
|
|
526
|
+
const ancestor = ancestors[idx];
|
|
527
|
+
if (removableOwnerTypes.has(ancestor.type)) {
|
|
528
|
+
owner = ancestor;
|
|
529
|
+
ownerAncestorIndex = idx;
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const parent = ownerAncestorIndex > 0 ? ancestors[ownerAncestorIndex - 1] : null;
|
|
535
|
+
if (parent?.type === "ExportNamedDeclaration" && "declaration" in parent && parent.declaration === owner) {
|
|
536
|
+
return parent;
|
|
537
|
+
}
|
|
538
|
+
return owner;
|
|
539
|
+
};
|
|
540
|
+
const collectReExportedLocalNames = (program) => {
|
|
541
|
+
const names = new Set();
|
|
542
|
+
for (const stmt of program.body) {
|
|
543
|
+
if (stmt.type !== "ExportNamedDeclaration" || stmt.declaration || stmt.source) {
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
for (const spec of stmt.specifiers) {
|
|
547
|
+
if (spec.type === "ExportSpecifier" && spec.local.type === "Identifier") {
|
|
548
|
+
names.add(spec.local.name);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return names;
|
|
553
|
+
};
|
|
554
|
+
// When an offending identifier sits inside the value of a top-level export,
|
|
555
|
+
// removing the surrounding declaration would strip the export binding too.
|
|
556
|
+
// ESM linker then fails on consumers' `import { name } from '...'` because the
|
|
557
|
+
// named export no longer exists. To keep the binding alive, replace just the
|
|
558
|
+
// value (declarator init or default-export expression) with `undefined`.
|
|
559
|
+
const findExportedBindingProtection = (node, ancestors, reExportedLocalNames) => {
|
|
560
|
+
for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
|
|
561
|
+
const ancestor = ancestors[idx];
|
|
562
|
+
if (ancestor.type === "ExportDefaultDeclaration") {
|
|
563
|
+
const decl = ancestor.declaration;
|
|
564
|
+
if (decl && decl !== node && "start" in decl && "end" in decl) {
|
|
565
|
+
return {
|
|
566
|
+
start: decl.start,
|
|
567
|
+
end: decl.end,
|
|
568
|
+
value: "undefined"
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
return null;
|
|
572
|
+
}
|
|
573
|
+
if (ancestor.type !== "VariableDeclarator") {
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
if (ancestor.id.type !== "Identifier" || !ancestor.init || ancestor.init === node) {
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
579
|
+
const varDecl = ancestors[idx - 1];
|
|
580
|
+
const wrapper = ancestors[idx - 2] ?? null;
|
|
581
|
+
if (varDecl?.type !== "VariableDeclaration") {
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
584
|
+
const isDirectExport = wrapper?.type === "ExportNamedDeclaration" && wrapper.declaration === varDecl;
|
|
585
|
+
const isTopLevelDeclaration = wrapper === null || wrapper?.type === "Program";
|
|
586
|
+
const isReExported = isTopLevelDeclaration && reExportedLocalNames.has(ancestor.id.name);
|
|
587
|
+
if (!isDirectExport && !isReExported) {
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
return {
|
|
591
|
+
start: ancestor.init.start,
|
|
592
|
+
end: ancestor.init.end,
|
|
593
|
+
value: "undefined"
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
return null;
|
|
597
|
+
};
|
|
598
|
+
const findPromiseCallbackOwner = (ancestors) => {
|
|
599
|
+
for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
|
|
600
|
+
const ancestor = ancestors[idx];
|
|
601
|
+
if (ancestor.type === "NewExpression" && ancestor.callee.type === "Identifier" && ancestor.callee.name === "Promise") {
|
|
602
|
+
return ancestor;
|
|
603
|
+
}
|
|
604
|
+
if (ancestor.type === "CallExpression" && ancestor.callee.type === "MemberExpression" && promiseCallbackMethods.has(getMemberPropertyName(ancestor.callee) ?? "")) {
|
|
605
|
+
return ancestor;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
return null;
|
|
609
|
+
};
|
|
610
|
+
const containsForbiddenIdentifier = (node) => {
|
|
611
|
+
if (node.type === "Identifier" && alwaysForbiddenIdentifiers.has(node.name)) {
|
|
612
|
+
return true;
|
|
613
|
+
}
|
|
614
|
+
return getChildren(node).some(containsForbiddenIdentifier);
|
|
615
|
+
};
|
|
616
|
+
const collectWindowScopedNames = (program) => {
|
|
617
|
+
const windowScopedNames = new Set();
|
|
618
|
+
visit(program, createScope(null, "root"), (node, scope) => {
|
|
619
|
+
if (node.type !== "MemberExpression" || node.object.type !== "Identifier" || node.object.name !== "window" || hasBinding(scope, "window")) {
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
const propertyName = getMemberPropertyName(node);
|
|
623
|
+
if (propertyName) {
|
|
624
|
+
windowScopedNames.add(propertyName);
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
return windowScopedNames;
|
|
628
|
+
};
|
|
629
|
+
const containsForbiddenReference = (node, scope, windowScopedNames, derivedForbiddenBindings = new Set()) => {
|
|
630
|
+
let found = false;
|
|
631
|
+
visit(node, scope, (child, childScope, parent, ancestors) => {
|
|
632
|
+
if (found || child.type !== "Identifier" || isTypeContext(ancestors) || isInsideTypeof(ancestors) || isPropertyOnlyIdentifier(child, parent) || isBindingPosition(child, parent)) {
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const bindingKey = getBindingKey(childScope, child.name);
|
|
636
|
+
if (alwaysForbiddenIdentifiers.has(child.name) || forbiddenGlobals.has(child.name) && !hasBinding(childScope, child.name) || windowScopedNames.has(child.name) && !hasBinding(childScope, child.name) || bindingKey !== null && derivedForbiddenBindings.has(bindingKey)) {
|
|
637
|
+
found = true;
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
return found;
|
|
641
|
+
};
|
|
642
|
+
const nameFromModuleExport = (node) => {
|
|
643
|
+
if (node.type === "Identifier") {
|
|
644
|
+
return node.name;
|
|
645
|
+
}
|
|
646
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
647
|
+
return node.value;
|
|
648
|
+
}
|
|
649
|
+
return null;
|
|
650
|
+
};
|
|
651
|
+
const collectImportBindings = (code, filename, program) => {
|
|
652
|
+
const bindings = new Map();
|
|
653
|
+
const addBinding = (binding) => {
|
|
654
|
+
bindings.set(`${binding.local}\0${binding.source}\0${binding.imported}`, binding);
|
|
655
|
+
};
|
|
656
|
+
program.body.forEach((statement) => {
|
|
657
|
+
if (statement.type !== "ImportDeclaration") {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
const source = statement.source.value;
|
|
661
|
+
statement.specifiers.forEach((specifier) => {
|
|
662
|
+
if (specifier.type === "ImportDefaultSpecifier") {
|
|
663
|
+
addBinding({
|
|
664
|
+
imported: "default",
|
|
665
|
+
local: specifier.local.name,
|
|
666
|
+
source
|
|
667
|
+
});
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
671
|
+
addBinding({
|
|
672
|
+
imported: "*",
|
|
673
|
+
local: specifier.local.name,
|
|
674
|
+
source
|
|
675
|
+
});
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
const imported = nameFromModuleExport(specifier.imported);
|
|
679
|
+
if (!imported) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
addBinding({
|
|
683
|
+
imported,
|
|
684
|
+
local: specifier.local.name,
|
|
685
|
+
source
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
});
|
|
689
|
+
if (!requireCallRe.test(code)) {
|
|
690
|
+
return [...bindings.values()];
|
|
691
|
+
}
|
|
692
|
+
collectOxcExportsAndImports(code, filename).imports.forEach((item) => {
|
|
693
|
+
if (item.imported === "side-effect") {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
addBinding({
|
|
697
|
+
imported: item.imported,
|
|
698
|
+
local: item.local.code,
|
|
699
|
+
source: item.source
|
|
700
|
+
});
|
|
701
|
+
if (item.local.name && item.local.name !== item.local.code) {
|
|
702
|
+
addBinding({
|
|
703
|
+
imported: item.imported,
|
|
704
|
+
local: item.local.name,
|
|
705
|
+
source: item.source
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
return [...bindings.values()];
|
|
710
|
+
};
|
|
711
|
+
const getImportBinding = (imports, local) => imports.find((item) => item.local === local);
|
|
712
|
+
function unwrapSequenceCallee(node) {
|
|
713
|
+
if (node.type === "SequenceExpression" && node.expressions.length === 2 && node.expressions[0]?.type === "Literal" && node.expressions[0].value === 0) {
|
|
714
|
+
return node.expressions[1];
|
|
715
|
+
}
|
|
716
|
+
return node;
|
|
717
|
+
}
|
|
718
|
+
const getExpressionImportKey = (node) => {
|
|
719
|
+
const expression = unwrapSequenceCallee(node);
|
|
720
|
+
if (expression.type === "Identifier") {
|
|
721
|
+
return expression.name;
|
|
722
|
+
}
|
|
723
|
+
if (expression.type !== "MemberExpression") {
|
|
724
|
+
return null;
|
|
725
|
+
}
|
|
726
|
+
if (expression.object.type !== "Identifier") {
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
const propertyName = getMemberPropertyName(expression);
|
|
730
|
+
return propertyName ? `${expression.object.name}.${propertyName}` : null;
|
|
731
|
+
};
|
|
732
|
+
const isHookOrCreateElement = (name) => name === "createElement" || /use[A-Z]/.test(name);
|
|
733
|
+
const getInnermostCallee = (call) => {
|
|
734
|
+
let callee = unwrapSequenceCallee(call.callee);
|
|
735
|
+
while (callee.type === "CallExpression") {
|
|
736
|
+
callee = unwrapSequenceCallee(callee.callee);
|
|
737
|
+
}
|
|
738
|
+
return callee;
|
|
739
|
+
};
|
|
740
|
+
const getImportForExpression = (node, imports) => {
|
|
741
|
+
const expression = unwrapSequenceCallee(node);
|
|
742
|
+
const directMatchKey = getExpressionImportKey(expression);
|
|
743
|
+
if (directMatchKey) {
|
|
744
|
+
const directMatch = getImportBinding(imports, directMatchKey);
|
|
745
|
+
if (directMatch) {
|
|
746
|
+
return [directMatch.source, directMatch.imported];
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
if (expression.type === "Identifier") {
|
|
750
|
+
const matched = getImportBinding(imports, expression.name);
|
|
751
|
+
return matched ? [matched.source, matched.imported] : undefined;
|
|
752
|
+
}
|
|
753
|
+
if (expression.type !== "MemberExpression") {
|
|
754
|
+
return undefined;
|
|
755
|
+
}
|
|
756
|
+
if (expression.object.type !== "Identifier") {
|
|
757
|
+
return undefined;
|
|
758
|
+
}
|
|
759
|
+
const propertyName = getMemberPropertyName(expression);
|
|
760
|
+
if (!propertyName) {
|
|
761
|
+
return undefined;
|
|
762
|
+
}
|
|
763
|
+
const objectImport = getImportBinding(imports, expression.object.name);
|
|
764
|
+
if (!objectImport) {
|
|
765
|
+
return undefined;
|
|
766
|
+
}
|
|
767
|
+
if (objectImport.imported === "default" || objectImport.imported === "*" || objectImport.source === "react") {
|
|
768
|
+
return [objectImport.source, propertyName];
|
|
769
|
+
}
|
|
770
|
+
return undefined;
|
|
771
|
+
};
|
|
772
|
+
const isReactRuntimeCall = (call, imports) => {
|
|
773
|
+
const callee = unwrapSequenceCallee(call.callee);
|
|
774
|
+
const matched = getImportForExpression(callee, imports);
|
|
775
|
+
if (!matched) {
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
const [source, imported] = matched;
|
|
779
|
+
if (jsxRuntimeSources.has(source)) {
|
|
780
|
+
return true;
|
|
781
|
+
}
|
|
782
|
+
return source === "react" && isHookOrCreateElement(imported);
|
|
783
|
+
};
|
|
784
|
+
const isHocCall = (call, hocs, imports) => {
|
|
785
|
+
const matched = getImportForExpression(getInnermostCallee(call), imports);
|
|
786
|
+
return !!matched && hocs[matched[0]]?.includes(matched[1]);
|
|
787
|
+
};
|
|
788
|
+
const getComponentTypes = (options) => {
|
|
789
|
+
const componentTypes = { ...options?.componentTypes ?? { react: [defaultPlaceholder] } };
|
|
790
|
+
const reactTypes = componentTypes.react;
|
|
791
|
+
if (Array.isArray(reactTypes) && reactTypes.includes(defaultPlaceholder)) {
|
|
792
|
+
const idx = reactTypes.indexOf(defaultPlaceholder);
|
|
793
|
+
componentTypes.react = [...reactTypes];
|
|
794
|
+
componentTypes.react.splice(idx, 1, ...defaultReactComponentTypes);
|
|
795
|
+
}
|
|
796
|
+
return componentTypes;
|
|
797
|
+
};
|
|
798
|
+
const getTypeImport = (node, imports) => {
|
|
799
|
+
if (node.type === "Identifier") {
|
|
800
|
+
const matched = getImportBinding(imports, node.name);
|
|
801
|
+
return matched ? [matched.source, matched.imported] : undefined;
|
|
802
|
+
}
|
|
803
|
+
if (node.type !== "TSQualifiedName") {
|
|
804
|
+
return undefined;
|
|
805
|
+
}
|
|
806
|
+
if (node.left.type !== "Identifier") {
|
|
807
|
+
return undefined;
|
|
808
|
+
}
|
|
809
|
+
const matched = getImportBinding(imports, node.left.name);
|
|
810
|
+
return matched ? [matched.source, node.right.name] : undefined;
|
|
811
|
+
};
|
|
812
|
+
const isComponentTypeMatch = (id, componentTypes, imports) => {
|
|
813
|
+
if (id.type !== "Identifier") {
|
|
814
|
+
return false;
|
|
815
|
+
}
|
|
816
|
+
const annotation = id.typeAnnotation;
|
|
817
|
+
if (!annotation || annotation.type !== "TSTypeAnnotation" || annotation.typeAnnotation.type !== "TSTypeReference") {
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
820
|
+
const matched = getTypeImport(annotation.typeAnnotation.typeName, imports);
|
|
821
|
+
return !!matched && componentTypes[matched[0]]?.includes(matched[1]);
|
|
822
|
+
};
|
|
823
|
+
const getClassName = (node) => node.type === "ClassDeclaration" && node.id ? node.id.name : null;
|
|
824
|
+
const isFunctionLikeNode = (node) => node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
825
|
+
const isImmediatelyInvokedFunction = (fnNode, ancestors, fnIndex) => {
|
|
826
|
+
// Walk past any ParenthesizedExpression / SequenceExpression wrappers that
|
|
827
|
+
// sit between the function and its enclosing CallExpression. oxc-parser
|
|
828
|
+
// preserves ParenthesizedExpression nodes, so `(() => {...})()` shows up
|
|
829
|
+
// as CallExpression -> ParenthesizedExpression -> ArrowFunctionExpression.
|
|
830
|
+
let child = fnNode;
|
|
831
|
+
for (let idx = fnIndex - 1; idx >= 0; idx -= 1) {
|
|
832
|
+
const ancestor = ancestors[idx];
|
|
833
|
+
if (ancestor.type === "ParenthesizedExpression" && "expression" in ancestor && ancestor.expression === child) {
|
|
834
|
+
child = ancestor;
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
if (ancestor.type === "SequenceExpression" && Array.isArray(ancestor.expressions) && ancestor.expressions[ancestor.expressions.length - 1] === child) {
|
|
838
|
+
child = ancestor;
|
|
839
|
+
continue;
|
|
840
|
+
}
|
|
841
|
+
return ancestor.type === "CallExpression" && ancestor.callee === child;
|
|
842
|
+
}
|
|
843
|
+
return false;
|
|
844
|
+
};
|
|
845
|
+
// A reference is "deferred" when it lives inside a function body that is
|
|
846
|
+
// not immediately invoked. Module preeval reads exports without calling
|
|
847
|
+
// them, so deferred references never run and must not be sanitized — that
|
|
848
|
+
// would silently drop the binding (or remove the entire export) and break
|
|
849
|
+
// importers that expect the function to be callable.
|
|
850
|
+
const isInDeferredFunctionScope = (ancestors) => {
|
|
851
|
+
for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
|
|
852
|
+
const ancestor = ancestors[idx];
|
|
853
|
+
if (!isFunctionLikeNode(ancestor)) {
|
|
854
|
+
continue;
|
|
855
|
+
}
|
|
856
|
+
if (!isImmediatelyInvokedFunction(ancestor, ancestors, idx)) {
|
|
857
|
+
return true;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
return false;
|
|
861
|
+
};
|
|
862
|
+
const findFunctionReplacement = (ancestors) => {
|
|
863
|
+
const renderMethod = findLastAncestor(ancestors, (ancestor) => ancestor.type === "MethodDefinition" && ancestor.key.type === "Identifier" && ancestor.key.name === "render");
|
|
864
|
+
if (renderMethod) {
|
|
865
|
+
const classDecl = findLastAncestor(ancestors, (ancestor) => ancestor.type === "ClassDeclaration");
|
|
866
|
+
const className = classDecl ? getClassName(classDecl) : null;
|
|
867
|
+
if (classDecl && className) {
|
|
868
|
+
return {
|
|
869
|
+
start: classDecl.start,
|
|
870
|
+
end: classDecl.end,
|
|
871
|
+
value: `function ${className}() { return null; }`
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
const functionNode = findLastAncestor(ancestors, isFunctionLikeNode);
|
|
876
|
+
if (!functionNode) {
|
|
877
|
+
return null;
|
|
878
|
+
}
|
|
879
|
+
if (functionNode.type === "ArrowFunctionExpression") {
|
|
880
|
+
return {
|
|
881
|
+
start: functionNode.start,
|
|
882
|
+
end: functionNode.end,
|
|
883
|
+
value: `${functionNode.async ? "async " : ""}() => { return null; }`
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
if (functionNode.type === "FunctionDeclaration") {
|
|
887
|
+
return {
|
|
888
|
+
start: functionNode.start,
|
|
889
|
+
end: functionNode.end,
|
|
890
|
+
value: `${functionNode.async ? "async " : ""}function ${functionNode.id?.name ?? ""}() { return null; }`
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
return {
|
|
894
|
+
start: functionNode.body.start,
|
|
895
|
+
end: functionNode.body.end,
|
|
896
|
+
value: "{ return null; }"
|
|
897
|
+
};
|
|
898
|
+
};
|
|
899
|
+
const normalizeReplacements = (replacements) => {
|
|
900
|
+
const sorted = [...replacements].sort((a, b) => a.start === b.start ? b.end - a.end : a.start - b.start);
|
|
901
|
+
const result = [];
|
|
902
|
+
sorted.forEach((replacement) => {
|
|
903
|
+
const last = result[result.length - 1];
|
|
904
|
+
if (last && replacement.start >= last.start && replacement.end <= last.end) {
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
result.push(replacement);
|
|
908
|
+
});
|
|
909
|
+
return result;
|
|
910
|
+
};
|
|
911
|
+
export const removeDangerousCodeWithOxc = (code, filename, options) => {
|
|
912
|
+
const replacements = [];
|
|
913
|
+
const derivedForbiddenBindings = new Set();
|
|
914
|
+
const program = parseOxc(code, filename);
|
|
915
|
+
const imports = collectImportBindings(code, filename, program);
|
|
916
|
+
const componentTypes = getComponentTypes(options);
|
|
917
|
+
const hocs = options?.hocs ?? {};
|
|
918
|
+
const hasHocs = Object.keys(hocs).length > 0;
|
|
919
|
+
const windowScopedNames = windowTokenRe.test(code) ? collectWindowScopedNames(program) : new Set();
|
|
920
|
+
const reExportedLocalNames = collectReExportedLocalNames(program);
|
|
921
|
+
const derivedBindingDiscovery = { found: true };
|
|
922
|
+
while (derivedBindingDiscovery.found) {
|
|
923
|
+
derivedBindingDiscovery.found = false;
|
|
924
|
+
visit(program, createScope(null, "root"), (node, scope) => {
|
|
925
|
+
if (node.type !== "VariableDeclarator" || node.id.type !== "Identifier" || !node.init) {
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
const bindingKey = getBindingKey(scope, node.id.name);
|
|
929
|
+
if (!bindingKey || derivedForbiddenBindings.has(bindingKey)) {
|
|
930
|
+
return;
|
|
931
|
+
}
|
|
932
|
+
if (containsForbiddenIdentifier(node.init) || containsForbiddenReference(node.init, scope, windowScopedNames, derivedForbiddenBindings)) {
|
|
933
|
+
derivedForbiddenBindings.add(bindingKey);
|
|
934
|
+
derivedBindingDiscovery.found = true;
|
|
935
|
+
}
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
visit(program, createScope(null, "root"), (node, scope, parent, ancestors) => {
|
|
939
|
+
if (node.type === "JSXElement" || node.type === "JSXFragment") {
|
|
940
|
+
replacements.push(findFunctionReplacement(ancestors) ?? {
|
|
941
|
+
start: node.start,
|
|
942
|
+
end: node.end,
|
|
943
|
+
value: "null"
|
|
944
|
+
});
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
if (node.type === "CallExpression") {
|
|
948
|
+
if (isReactRuntimeCall(node, imports)) {
|
|
949
|
+
const replacement = findFunctionReplacement(ancestors);
|
|
950
|
+
if (replacement) {
|
|
951
|
+
replacements.push(replacement);
|
|
952
|
+
}
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
if (hasHocs && isHocCall(node, hocs, imports)) {
|
|
956
|
+
replacements.push({
|
|
957
|
+
start: node.start,
|
|
958
|
+
end: node.end,
|
|
959
|
+
value: "() => null"
|
|
960
|
+
});
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
if (node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.init && isComponentTypeMatch(node.id, componentTypes, imports)) {
|
|
965
|
+
replacements.push({
|
|
966
|
+
start: node.init.start,
|
|
967
|
+
end: node.init.end,
|
|
968
|
+
value: "() => null"
|
|
969
|
+
});
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
if (node.type === "UnaryExpression" && node.operator === "typeof" && node.argument.type === "Identifier" && ssrCheckFields.has(node.argument.name) && !hasBinding(scope, node.argument.name)) {
|
|
973
|
+
replacements.push({
|
|
974
|
+
start: node.start,
|
|
975
|
+
end: node.end,
|
|
976
|
+
value: "\"undefined\""
|
|
977
|
+
});
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
if (node.type === "MetaProperty") {
|
|
981
|
+
const owner = findRemovableOwner(node, ancestors);
|
|
982
|
+
replacements.push({
|
|
983
|
+
start: owner.start,
|
|
984
|
+
end: owner.end,
|
|
985
|
+
value: ""
|
|
986
|
+
});
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
if (node.type !== "Identifier" || isTypeContext(ancestors) || isInsideTypeof(ancestors) || isInsideImportDeclaration(ancestors)) {
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
if (isPropertyOnlyIdentifier(node, parent)) {
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
const isAlwaysForbidden = alwaysForbiddenIdentifiers.has(node.name);
|
|
996
|
+
const bindingKey = getBindingKey(scope, node.name);
|
|
997
|
+
const isDerivedForbidden = bindingKey !== null && derivedForbiddenBindings.has(bindingKey);
|
|
998
|
+
const isWindowScoped = windowScopedNames.has(node.name) && !hasBinding(scope, node.name);
|
|
999
|
+
const isForbiddenGlobal = forbiddenGlobals.has(node.name) && !hasBinding(scope, node.name);
|
|
1000
|
+
if (!isAlwaysForbidden && !isDerivedForbidden && !isWindowScoped && !isForbiddenGlobal) {
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
if (!isAlwaysForbidden && isInDeferredFunctionScope(ancestors)) {
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
1006
|
+
if (isBindingPosition(node, parent) && !isAlwaysForbidden) {
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
if (parent?.type === "ExportDefaultDeclaration" && parent.declaration === node) {
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
if (generatedProcessorHelperNameRe.test(node.name) && hasBinding(scope, node.name)) {
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
const generatedHelperDeclarator = findLastAncestor(ancestors, (ancestor) => ancestor.type === "VariableDeclarator" && ancestor.id.type === "Identifier" && generatedProcessorHelperNameRe.test(ancestor.id.name));
|
|
1016
|
+
if (generatedHelperDeclarator) {
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
if (parent?.type === "Property" && parent.value === node && hasBinding(scope, node.name)) {
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
if (parent?.type === "Property" && parent.value === node) {
|
|
1023
|
+
replacements.push({
|
|
1024
|
+
start: node.start,
|
|
1025
|
+
end: node.end,
|
|
1026
|
+
value: parent.shorthand ? `${node.name}: undefined` : "undefined"
|
|
1027
|
+
});
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
const grandparent = ancestors[ancestors.length - 2] ?? null;
|
|
1031
|
+
if (parent?.type === "MemberExpression" && parent.object === node && grandparent?.type === "SpreadElement" && grandparent.argument === parent) {
|
|
1032
|
+
replacements.push({
|
|
1033
|
+
start: grandparent.start,
|
|
1034
|
+
end: grandparent.end,
|
|
1035
|
+
value: "...{}"
|
|
1036
|
+
});
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
const exportProtection = findExportedBindingProtection(node, ancestors, reExportedLocalNames);
|
|
1040
|
+
if (exportProtection) {
|
|
1041
|
+
replacements.push(exportProtection);
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
const promiseOwner = findPromiseCallbackOwner(ancestors);
|
|
1045
|
+
const owner = promiseOwner ? findRemovableOwner(promiseOwner, ancestors) : findRemovableOwner(node, ancestors);
|
|
1046
|
+
replacements.push({
|
|
1047
|
+
start: owner.start,
|
|
1048
|
+
end: owner.end,
|
|
1049
|
+
value: ""
|
|
1050
|
+
});
|
|
1051
|
+
});
|
|
1052
|
+
return applyReplacements(code, normalizeReplacements(replacements));
|
|
1053
|
+
};
|
|
1054
|
+
//# sourceMappingURL=oxcPreevalTransforms.js.map
|