@wyw-in-js/transform 0.1.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/LICENSE +21 -0
- package/esm/babel.js +2 -0
- package/esm/babel.js.map +1 -0
- package/esm/cache.js +80 -0
- package/esm/cache.js.map +1 -0
- package/esm/debug/fileReporter.js +144 -0
- package/esm/debug/fileReporter.js.map +1 -0
- package/esm/evaluators/index.js +14 -0
- package/esm/evaluators/index.js.map +1 -0
- package/esm/index.js +24 -0
- package/esm/index.js.map +1 -0
- package/esm/module.js +316 -0
- package/esm/module.js.map +1 -0
- package/esm/options/babel-merge.d.js +2 -0
- package/esm/options/babel-merge.d.js.map +1 -0
- package/esm/options/buildOptions.js +28 -0
- package/esm/options/buildOptions.js.map +1 -0
- package/esm/options/loadBabelOptions.js +24 -0
- package/esm/options/loadBabelOptions.js.map +1 -0
- package/esm/plugins/babel-transform.js +53 -0
- package/esm/plugins/babel-transform.js.map +1 -0
- package/esm/plugins/collector.js +66 -0
- package/esm/plugins/collector.js.map +1 -0
- package/esm/plugins/dynamic-import.js +28 -0
- package/esm/plugins/dynamic-import.js.map +1 -0
- package/esm/plugins/preeval.js +75 -0
- package/esm/plugins/preeval.js.map +1 -0
- package/esm/plugins/shaker.js +296 -0
- package/esm/plugins/shaker.js.map +1 -0
- package/esm/shaker.js +50 -0
- package/esm/shaker.js.map +1 -0
- package/esm/transform/BaseEntrypoint.js +164 -0
- package/esm/transform/BaseEntrypoint.js.map +1 -0
- package/esm/transform/Entrypoint.helpers.js +168 -0
- package/esm/transform/Entrypoint.helpers.js.map +1 -0
- package/esm/transform/Entrypoint.js +212 -0
- package/esm/transform/Entrypoint.js.map +1 -0
- package/esm/transform/Entrypoint.types.js +2 -0
- package/esm/transform/Entrypoint.types.js.map +1 -0
- package/esm/transform/EvaluatedEntrypoint.js +6 -0
- package/esm/transform/EvaluatedEntrypoint.js.map +1 -0
- package/esm/transform/actions/AbortError.js +8 -0
- package/esm/transform/actions/AbortError.js.map +1 -0
- package/esm/transform/actions/BaseAction.js +134 -0
- package/esm/transform/actions/BaseAction.js.map +1 -0
- package/esm/transform/actions/UnprocessedEntrypointError.js +8 -0
- package/esm/transform/actions/UnprocessedEntrypointError.js.map +1 -0
- package/esm/transform/actions/actionRunner.js +73 -0
- package/esm/transform/actions/actionRunner.js.map +1 -0
- package/esm/transform/actions/types.js +2 -0
- package/esm/transform/actions/types.js.map +1 -0
- package/esm/transform/generators/baseProcessingHandlers.js +22 -0
- package/esm/transform/generators/baseProcessingHandlers.js.map +1 -0
- package/esm/transform/generators/collect.js +58 -0
- package/esm/transform/generators/collect.js.map +1 -0
- package/esm/transform/generators/evalFile.js +50 -0
- package/esm/transform/generators/evalFile.js.map +1 -0
- package/esm/transform/generators/explodeReexports.js +63 -0
- package/esm/transform/generators/explodeReexports.js.map +1 -0
- package/esm/transform/generators/extract.js +120 -0
- package/esm/transform/generators/extract.js.map +1 -0
- package/esm/transform/generators/getExports.js +73 -0
- package/esm/transform/generators/getExports.js.map +1 -0
- package/esm/transform/generators/index.js +13 -0
- package/esm/transform/generators/index.js.map +1 -0
- package/esm/transform/generators/processEntrypoint.js +43 -0
- package/esm/transform/generators/processEntrypoint.js.map +1 -0
- package/esm/transform/generators/processImports.js +23 -0
- package/esm/transform/generators/processImports.js.map +1 -0
- package/esm/transform/generators/resolveImports.js +148 -0
- package/esm/transform/generators/resolveImports.js.map +1 -0
- package/esm/transform/generators/transform.js +117 -0
- package/esm/transform/generators/transform.js.map +1 -0
- package/esm/transform/generators/workflow.js +82 -0
- package/esm/transform/generators/workflow.js.map +1 -0
- package/esm/transform/helpers/loadLinariaOptions.js +66 -0
- package/esm/transform/helpers/loadLinariaOptions.js.map +1 -0
- package/esm/transform/helpers/withDefaultServices.js +21 -0
- package/esm/transform/helpers/withDefaultServices.js.map +1 -0
- package/esm/transform/rootLog.js +3 -0
- package/esm/transform/rootLog.js.map +1 -0
- package/esm/transform/types.js +2 -0
- package/esm/transform/types.js.map +1 -0
- package/esm/transform.js +121 -0
- package/esm/transform.js.map +1 -0
- package/esm/types.js +2 -0
- package/esm/types.js.map +1 -0
- package/esm/utils/EventEmitter.js +52 -0
- package/esm/utils/EventEmitter.js.map +1 -0
- package/esm/utils/ShakerMetadata.js +2 -0
- package/esm/utils/ShakerMetadata.js.map +1 -0
- package/esm/utils/TransformMetadata.js +11 -0
- package/esm/utils/TransformMetadata.js.map +1 -0
- package/esm/utils/addIdentifierToLinariaPreval.js +68 -0
- package/esm/utils/addIdentifierToLinariaPreval.js.map +1 -0
- package/esm/utils/collectExportsAndImports.js +974 -0
- package/esm/utils/collectExportsAndImports.js.map +1 -0
- package/esm/utils/collectTemplateDependencies.js +219 -0
- package/esm/utils/collectTemplateDependencies.js.map +1 -0
- package/esm/utils/createId.js +6 -0
- package/esm/utils/createId.js.map +1 -0
- package/esm/utils/dispose-polyfill.js +6 -0
- package/esm/utils/dispose-polyfill.js.map +1 -0
- package/esm/utils/findIdentifiers.js +62 -0
- package/esm/utils/findIdentifiers.js.map +1 -0
- package/esm/utils/findPackageJSON.js +22 -0
- package/esm/utils/findPackageJSON.js.map +1 -0
- package/esm/utils/getFileIdx.js +10 -0
- package/esm/utils/getFileIdx.js.map +1 -0
- package/esm/utils/getPluginKey.js +13 -0
- package/esm/utils/getPluginKey.js.map +1 -0
- package/esm/utils/getScope.js +6 -0
- package/esm/utils/getScope.js.map +1 -0
- package/esm/utils/getSource.js +15 -0
- package/esm/utils/getSource.js.map +1 -0
- package/esm/utils/getTagProcessor.js +304 -0
- package/esm/utils/getTagProcessor.js.map +1 -0
- package/esm/utils/getVisitorKeys.js +5 -0
- package/esm/utils/getVisitorKeys.js.map +1 -0
- package/esm/utils/hasLinariaPreval.js +7 -0
- package/esm/utils/hasLinariaPreval.js.map +1 -0
- package/esm/utils/isExports.js +22 -0
- package/esm/utils/isExports.js.map +1 -0
- package/esm/utils/isGlobal.js +6 -0
- package/esm/utils/isGlobal.js.map +1 -0
- package/esm/utils/isNode.js +2 -0
- package/esm/utils/isNode.js.map +1 -0
- package/esm/utils/isNodePath.js +4 -0
- package/esm/utils/isNodePath.js.map +1 -0
- package/esm/utils/isNotNull.js +4 -0
- package/esm/utils/isNotNull.js.map +1 -0
- package/esm/utils/isRemoved.js +46 -0
- package/esm/utils/isRemoved.js.map +1 -0
- package/esm/utils/isRequire.js +13 -0
- package/esm/utils/isRequire.js.map +1 -0
- package/esm/utils/isSerializable.js +13 -0
- package/esm/utils/isSerializable.js.map +1 -0
- package/esm/utils/isTypedNode.js +6 -0
- package/esm/utils/isTypedNode.js.map +1 -0
- package/esm/utils/isUnnecessaryReactCall.js +72 -0
- package/esm/utils/isUnnecessaryReactCall.js.map +1 -0
- package/esm/utils/peek.js +2 -0
- package/esm/utils/peek.js.map +1 -0
- package/esm/utils/processTemplateExpression.js +11 -0
- package/esm/utils/processTemplateExpression.js.map +1 -0
- package/esm/utils/removeDangerousCode.js +127 -0
- package/esm/utils/removeDangerousCode.js.map +1 -0
- package/esm/utils/scopeHelpers.js +514 -0
- package/esm/utils/scopeHelpers.js.map +1 -0
- package/esm/utils/traversalCache.js +28 -0
- package/esm/utils/traversalCache.js.map +1 -0
- package/esm/utils/unwrapSequence.js +14 -0
- package/esm/utils/unwrapSequence.js.map +1 -0
- package/esm/utils/valueToLiteral.js +59 -0
- package/esm/utils/valueToLiteral.js.map +1 -0
- package/esm/utils/visitors/JSXElementsRemover.js +51 -0
- package/esm/utils/visitors/JSXElementsRemover.js.map +1 -0
- package/esm/vm/createVmContext.js +72 -0
- package/esm/vm/createVmContext.js.map +1 -0
- package/esm/vm/process.js +29 -0
- package/esm/vm/process.js.map +1 -0
- package/lib/babel.js +2 -0
- package/lib/babel.js.map +1 -0
- package/lib/cache.js +87 -0
- package/lib/cache.js.map +1 -0
- package/lib/debug/fileReporter.js +153 -0
- package/lib/debug/fileReporter.js.map +1 -0
- package/lib/evaluators/index.js +20 -0
- package/lib/evaluators/index.js.map +1 -0
- package/lib/index.js +225 -0
- package/lib/index.js.map +1 -0
- package/lib/module.js +327 -0
- package/lib/module.js.map +1 -0
- package/lib/options/babel-merge.d.js +2 -0
- package/lib/options/babel-merge.d.js.map +1 -0
- package/lib/options/buildOptions.js +35 -0
- package/lib/options/buildOptions.js.map +1 -0
- package/lib/options/loadBabelOptions.js +31 -0
- package/lib/options/loadBabelOptions.js.map +1 -0
- package/lib/plugins/babel-transform.js +60 -0
- package/lib/plugins/babel-transform.js.map +1 -0
- package/lib/plugins/collector.js +76 -0
- package/lib/plugins/collector.js.map +1 -0
- package/lib/plugins/dynamic-import.js +34 -0
- package/lib/plugins/dynamic-import.js.map +1 -0
- package/lib/plugins/preeval.js +82 -0
- package/lib/plugins/preeval.js.map +1 -0
- package/lib/plugins/shaker.js +303 -0
- package/lib/plugins/shaker.js.map +1 -0
- package/lib/shaker.js +58 -0
- package/lib/shaker.js.map +1 -0
- package/lib/transform/BaseEntrypoint.js +175 -0
- package/lib/transform/BaseEntrypoint.js.map +1 -0
- package/lib/transform/Entrypoint.helpers.js +182 -0
- package/lib/transform/Entrypoint.helpers.js.map +1 -0
- package/lib/transform/Entrypoint.js +226 -0
- package/lib/transform/Entrypoint.js.map +1 -0
- package/lib/transform/Entrypoint.types.js +2 -0
- package/lib/transform/Entrypoint.types.js.map +1 -0
- package/lib/transform/EvaluatedEntrypoint.js +13 -0
- package/lib/transform/EvaluatedEntrypoint.js.map +1 -0
- package/lib/transform/actions/AbortError.js +16 -0
- package/lib/transform/actions/AbortError.js.map +1 -0
- package/lib/transform/actions/BaseAction.js +144 -0
- package/lib/transform/actions/BaseAction.js.map +1 -0
- package/lib/transform/actions/UnprocessedEntrypointError.js +16 -0
- package/lib/transform/actions/UnprocessedEntrypointError.js.map +1 -0
- package/lib/transform/actions/actionRunner.js +82 -0
- package/lib/transform/actions/actionRunner.js.map +1 -0
- package/lib/transform/actions/types.js +2 -0
- package/lib/transform/actions/types.js.map +1 -0
- package/lib/transform/generators/baseProcessingHandlers.js +27 -0
- package/lib/transform/generators/baseProcessingHandlers.js.map +1 -0
- package/lib/transform/generators/collect.js +66 -0
- package/lib/transform/generators/collect.js.map +1 -0
- package/lib/transform/generators/evalFile.js +57 -0
- package/lib/transform/generators/evalFile.js.map +1 -0
- package/lib/transform/generators/explodeReexports.js +70 -0
- package/lib/transform/generators/explodeReexports.js.map +1 -0
- package/lib/transform/generators/extract.js +128 -0
- package/lib/transform/generators/extract.js.map +1 -0
- package/lib/transform/generators/getExports.js +80 -0
- package/lib/transform/generators/getExports.js.map +1 -0
- package/lib/transform/generators/index.js +19 -0
- package/lib/transform/generators/index.js.map +1 -0
- package/lib/transform/generators/processEntrypoint.js +49 -0
- package/lib/transform/generators/processEntrypoint.js.map +1 -0
- package/lib/transform/generators/processImports.js +29 -0
- package/lib/transform/generators/processImports.js.map +1 -0
- package/lib/transform/generators/resolveImports.js +158 -0
- package/lib/transform/generators/resolveImports.js.map +1 -0
- package/lib/transform/generators/transform.js +130 -0
- package/lib/transform/generators/transform.js.map +1 -0
- package/lib/transform/generators/workflow.js +90 -0
- package/lib/transform/generators/workflow.js.map +1 -0
- package/lib/transform/helpers/loadLinariaOptions.js +73 -0
- package/lib/transform/helpers/loadLinariaOptions.js.map +1 -0
- package/lib/transform/helpers/withDefaultServices.js +30 -0
- package/lib/transform/helpers/withDefaultServices.js.map +1 -0
- package/lib/transform/rootLog.js +9 -0
- package/lib/transform/rootLog.js.map +1 -0
- package/lib/transform/types.js +8 -0
- package/lib/transform/types.js.map +1 -0
- package/lib/transform.js +128 -0
- package/lib/transform.js.map +1 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/lib/utils/EventEmitter.js +61 -0
- package/lib/utils/EventEmitter.js.map +1 -0
- package/lib/utils/ShakerMetadata.js +9 -0
- package/lib/utils/ShakerMetadata.js.map +1 -0
- package/lib/utils/TransformMetadata.js +19 -0
- package/lib/utils/TransformMetadata.js.map +1 -0
- package/lib/utils/addIdentifierToLinariaPreval.js +75 -0
- package/lib/utils/addIdentifierToLinariaPreval.js.map +1 -0
- package/lib/utils/collectExportsAndImports.js +990 -0
- package/lib/utils/collectExportsAndImports.js.map +1 -0
- package/lib/utils/collectTemplateDependencies.js +233 -0
- package/lib/utils/collectTemplateDependencies.js.map +1 -0
- package/lib/utils/createId.js +13 -0
- package/lib/utils/createId.js.map +1 -0
- package/lib/utils/dispose-polyfill.js +9 -0
- package/lib/utils/dispose-polyfill.js.map +1 -0
- package/lib/utils/findIdentifiers.js +73 -0
- package/lib/utils/findIdentifiers.js.map +1 -0
- package/lib/utils/findPackageJSON.js +29 -0
- package/lib/utils/findPackageJSON.js.map +1 -0
- package/lib/utils/getFileIdx.js +16 -0
- package/lib/utils/getFileIdx.js.map +1 -0
- package/lib/utils/getPluginKey.js +21 -0
- package/lib/utils/getPluginKey.js.map +1 -0
- package/lib/utils/getScope.js +12 -0
- package/lib/utils/getScope.js.map +1 -0
- package/lib/utils/getSource.js +24 -0
- package/lib/utils/getSource.js.map +1 -0
- package/lib/utils/getTagProcessor.js +318 -0
- package/lib/utils/getTagProcessor.js.map +1 -0
- package/lib/utils/getVisitorKeys.js +11 -0
- package/lib/utils/getVisitorKeys.js.map +1 -0
- package/lib/utils/hasLinariaPreval.js +13 -0
- package/lib/utils/hasLinariaPreval.js.map +1 -0
- package/lib/utils/isExports.js +27 -0
- package/lib/utils/isExports.js.map +1 -0
- package/lib/utils/isGlobal.js +13 -0
- package/lib/utils/isGlobal.js.map +1 -0
- package/lib/utils/isNode.js +9 -0
- package/lib/utils/isNode.js.map +1 -0
- package/lib/utils/isNodePath.js +10 -0
- package/lib/utils/isNodePath.js.map +1 -0
- package/lib/utils/isNotNull.js +10 -0
- package/lib/utils/isNotNull.js.map +1 -0
- package/lib/utils/isRemoved.js +52 -0
- package/lib/utils/isRemoved.js.map +1 -0
- package/lib/utils/isRequire.js +18 -0
- package/lib/utils/isRequire.js.map +1 -0
- package/lib/utils/isSerializable.js +19 -0
- package/lib/utils/isSerializable.js.map +1 -0
- package/lib/utils/isTypedNode.js +13 -0
- package/lib/utils/isTypedNode.js.map +1 -0
- package/lib/utils/isUnnecessaryReactCall.js +81 -0
- package/lib/utils/isUnnecessaryReactCall.js.map +1 -0
- package/lib/utils/peek.js +9 -0
- package/lib/utils/peek.js.map +1 -0
- package/lib/utils/processTemplateExpression.js +18 -0
- package/lib/utils/processTemplateExpression.js.map +1 -0
- package/lib/utils/removeDangerousCode.js +134 -0
- package/lib/utils/removeDangerousCode.js.map +1 -0
- package/lib/utils/scopeHelpers.js +544 -0
- package/lib/utils/scopeHelpers.js.map +1 -0
- package/lib/utils/traversalCache.js +38 -0
- package/lib/utils/traversalCache.js.map +1 -0
- package/lib/utils/unwrapSequence.js +20 -0
- package/lib/utils/unwrapSequence.js.map +1 -0
- package/lib/utils/valueToLiteral.js +65 -0
- package/lib/utils/valueToLiteral.js.map +1 -0
- package/lib/utils/visitors/JSXElementsRemover.js +57 -0
- package/lib/utils/visitors/JSXElementsRemover.js.map +1 -0
- package/lib/vm/createVmContext.js +80 -0
- package/lib/vm/createVmContext.js.map +1 -0
- package/lib/vm/process.js +38 -0
- package/lib/vm/process.js.map +1 -0
- package/package.json +63 -0
- package/types/babel.d.ts +2 -0
- package/types/babel.js +2 -0
- package/types/cache.d.ts +24 -0
- package/types/cache.js +85 -0
- package/types/debug/fileReporter.d.ts +27 -0
- package/types/debug/fileReporter.js +151 -0
- package/types/evaluators/index.d.ts +10 -0
- package/types/evaluators/index.js +15 -0
- package/types/index.d.ts +30 -0
- package/types/index.js +69 -0
- package/types/module.d.ts +62 -0
- package/types/module.js +318 -0
- package/types/options/buildOptions.d.ts +7 -0
- package/types/options/buildOptions.js +35 -0
- package/types/options/loadBabelOptions.d.ts +3 -0
- package/types/options/loadBabelOptions.js +27 -0
- package/types/plugins/babel-transform.d.ts +4 -0
- package/types/plugins/babel-transform.js +49 -0
- package/types/plugins/collector.d.ts +16 -0
- package/types/plugins/collector.js +67 -0
- package/types/plugins/dynamic-import.d.ts +6 -0
- package/types/plugins/dynamic-import.js +32 -0
- package/types/plugins/preeval.d.ts +16 -0
- package/types/plugins/preeval.js +69 -0
- package/types/plugins/shaker.d.ts +11 -0
- package/types/plugins/shaker.js +299 -0
- package/types/shaker.d.ts +3 -0
- package/types/shaker.js +64 -0
- package/types/transform/BaseEntrypoint.d.ts +23 -0
- package/types/transform/BaseEntrypoint.js +182 -0
- package/types/transform/Entrypoint.d.ts +50 -0
- package/types/transform/Entrypoint.helpers.d.ts +14 -0
- package/types/transform/Entrypoint.helpers.js +169 -0
- package/types/transform/Entrypoint.js +223 -0
- package/types/transform/Entrypoint.types.d.ts +23 -0
- package/types/transform/Entrypoint.types.js +2 -0
- package/types/transform/EvaluatedEntrypoint.d.ts +16 -0
- package/types/transform/EvaluatedEntrypoint.js +9 -0
- package/types/transform/actions/AbortError.d.ts +4 -0
- package/types/transform/actions/AbortError.js +12 -0
- package/types/transform/actions/BaseAction.d.ts +40 -0
- package/types/transform/actions/BaseAction.js +149 -0
- package/types/transform/actions/UnprocessedEntrypointError.d.ts +6 -0
- package/types/transform/actions/UnprocessedEntrypointError.js +13 -0
- package/types/transform/actions/actionRunner.d.ts +4 -0
- package/types/transform/actions/actionRunner.js +86 -0
- package/types/transform/actions/types.d.ts +15 -0
- package/types/transform/actions/types.js +2 -0
- package/types/transform/generators/baseProcessingHandlers.d.ts +17 -0
- package/types/transform/generators/baseProcessingHandlers.js +23 -0
- package/types/transform/generators/collect.d.ts +6 -0
- package/types/transform/generators/collect.js +57 -0
- package/types/transform/generators/evalFile.d.ts +6 -0
- package/types/transform/generators/evalFile.js +56 -0
- package/types/transform/generators/explodeReexports.d.ts +7 -0
- package/types/transform/generators/explodeReexports.js +67 -0
- package/types/transform/generators/extract.d.ts +8 -0
- package/types/transform/generators/extract.js +116 -0
- package/types/transform/generators/getExports.d.ts +8 -0
- package/types/transform/generators/getExports.js +65 -0
- package/types/transform/generators/index.d.ts +15 -0
- package/types/transform/generators/index.js +15 -0
- package/types/transform/generators/processEntrypoint.d.ts +9 -0
- package/types/transform/generators/processEntrypoint.js +88 -0
- package/types/transform/generators/processImports.d.ts +5 -0
- package/types/transform/generators/processImports.js +21 -0
- package/types/transform/generators/resolveImports.d.ts +9 -0
- package/types/transform/generators/resolveImports.js +124 -0
- package/types/transform/generators/transform.d.ts +17 -0
- package/types/transform/generators/transform.js +115 -0
- package/types/transform/generators/workflow.d.ts +7 -0
- package/types/transform/generators/workflow.js +77 -0
- package/types/transform/helpers/loadLinariaOptions.d.ts +6 -0
- package/types/transform/helpers/loadLinariaOptions.js +86 -0
- package/types/transform/helpers/withDefaultServices.d.ts +5 -0
- package/types/transform/helpers/withDefaultServices.js +40 -0
- package/types/transform/rootLog.d.ts +3 -0
- package/types/transform/rootLog.js +5 -0
- package/types/transform/types.d.ts +137 -0
- package/types/transform/types.js +4 -0
- package/types/transform.d.ts +20 -0
- package/types/transform.js +121 -0
- package/types/types.d.ts +72 -0
- package/types/types.js +5 -0
- package/types/utils/EventEmitter.d.ts +58 -0
- package/types/utils/EventEmitter.js +61 -0
- package/types/utils/ShakerMetadata.d.ts +7 -0
- package/types/utils/ShakerMetadata.js +5 -0
- package/types/utils/TransformMetadata.d.ts +13 -0
- package/types/utils/TransformMetadata.js +17 -0
- package/types/utils/addIdentifierToLinariaPreval.d.ts +4 -0
- package/types/utils/addIdentifierToLinariaPreval.js +75 -0
- package/types/utils/collectExportsAndImports.d.ts +31 -0
- package/types/utils/collectExportsAndImports.js +962 -0
- package/types/utils/collectTemplateDependencies.d.ts +17 -0
- package/types/utils/collectTemplateDependencies.js +212 -0
- package/types/utils/createId.d.ts +2 -0
- package/types/utils/createId.js +9 -0
- package/types/utils/dispose-polyfill.d.ts +0 -0
- package/types/utils/dispose-polyfill.js +5 -0
- package/types/utils/findIdentifiers.d.ts +6 -0
- package/types/utils/findIdentifiers.js +68 -0
- package/types/utils/findPackageJSON.d.ts +1 -0
- package/types/utils/findPackageJSON.js +29 -0
- package/types/utils/getFileIdx.d.ts +1 -0
- package/types/utils/getFileIdx.js +13 -0
- package/types/utils/getPluginKey.d.ts +2 -0
- package/types/utils/getPluginKey.js +16 -0
- package/types/utils/getScope.d.ts +2 -0
- package/types/utils/getScope.js +11 -0
- package/types/utils/getSource.d.ts +2 -0
- package/types/utils/getSource.js +22 -0
- package/types/utils/getTagProcessor.d.ts +6 -0
- package/types/utils/getTagProcessor.js +315 -0
- package/types/utils/getVisitorKeys.d.ts +5 -0
- package/types/utils/getVisitorKeys.js +8 -0
- package/types/utils/hasLinariaPreval.d.ts +4 -0
- package/types/utils/hasLinariaPreval.js +9 -0
- package/types/utils/isExports.d.ts +6 -0
- package/types/utils/isExports.js +20 -0
- package/types/utils/isGlobal.d.ts +2 -0
- package/types/utils/isGlobal.js +9 -0
- package/types/utils/isNode.d.ts +2 -0
- package/types/utils/isNode.js +7 -0
- package/types/utils/isNodePath.d.ts +3 -0
- package/types/utils/isNodePath.js +7 -0
- package/types/utils/isNotNull.d.ts +1 -0
- package/types/utils/isNotNull.js +7 -0
- package/types/utils/isRemoved.d.ts +5 -0
- package/types/utils/isRemoved.js +42 -0
- package/types/utils/isRequire.d.ts +6 -0
- package/types/utils/isRequire.js +15 -0
- package/types/utils/isSerializable.d.ts +2 -0
- package/types/utils/isSerializable.js +18 -0
- package/types/utils/isTypedNode.d.ts +5 -0
- package/types/utils/isTypedNode.js +9 -0
- package/types/utils/isUnnecessaryReactCall.d.ts +3 -0
- package/types/utils/isUnnecessaryReactCall.js +76 -0
- package/types/utils/peek.d.ts +1 -0
- package/types/utils/peek.js +5 -0
- package/types/utils/processTemplateExpression.d.ts +5 -0
- package/types/utils/processTemplateExpression.js +17 -0
- package/types/utils/removeDangerousCode.d.ts +3 -0
- package/types/utils/removeDangerousCode.js +144 -0
- package/types/utils/scopeHelpers.d.ts +12 -0
- package/types/utils/scopeHelpers.js +568 -0
- package/types/utils/traversalCache.d.ts +5 -0
- package/types/utils/traversalCache.js +36 -0
- package/types/utils/unwrapSequence.d.ts +8 -0
- package/types/utils/unwrapSequence.js +17 -0
- package/types/utils/valueToLiteral.d.ts +3 -0
- package/types/utils/valueToLiteral.js +64 -0
- package/types/utils/visitors/JSXElementsRemover.d.ts +3 -0
- package/types/utils/visitors/JSXElementsRemover.js +52 -0
- package/types/vm/createVmContext.d.ts +7 -0
- package/types/vm/createVmContext.js +92 -0
- package/types/vm/process.d.ts +25 -0
- package/types/vm/process.js +32 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isUnnecessaryReactCall.js","names":["collectExportsAndImports","getScope","getCallee","p","callee","get","isSequenceExpression","expressions","length","isNumericLiteral","value","JSXRuntimeSource","isJSXRuntime","imports","jsxRuntime","find","i","source","jsxRuntimeName","local","isIdentifier","node","name","isMemberExpression","isHookOrCreateElement","test","isClassicReactRuntime","reactImports","filter","imported","bindingPath","getBinding","path","some","isAncestor","object","property","defaultImport","isUnnecessaryReactCall","programPath","findParent","isProgram"],"sources":["../../src/utils/isUnnecessaryReactCall.ts"],"sourcesContent":["import type { NodePath } from '@babel/core';\nimport type { CallExpression, Program } from '@babel/types';\n\nimport type { IImport, ISideEffectImport } from './collectExportsAndImports';\nimport { collectExportsAndImports } from './collectExportsAndImports';\nimport { getScope } from './getScope';\n\nfunction getCallee(p: NodePath<CallExpression>) {\n const callee = p.get('callee');\n if (callee.isSequenceExpression()) {\n const expressions = callee.get('expressions');\n if (\n expressions.length === 2 &&\n expressions[0].isNumericLiteral({ value: 0 })\n ) {\n return expressions[1];\n }\n\n return callee;\n }\n\n return callee;\n}\n\nconst JSXRuntimeSource = 'react/jsx-runtime';\n\nfunction isJSXRuntime(\n p: NodePath<CallExpression>,\n imports: (IImport | ISideEffectImport)[]\n) {\n const jsxRuntime = imports.find((i) => i.source === JSXRuntimeSource);\n const jsxRuntimeName =\n jsxRuntime?.local?.isIdentifier() && jsxRuntime?.local?.node?.name;\n\n if (jsxRuntime) {\n const callee = getCallee(p);\n if (jsxRuntimeName && callee.isIdentifier({ name: jsxRuntimeName })) {\n return true;\n }\n\n if (\n callee.isMemberExpression() &&\n imports.find((i) => i.source === JSXRuntimeSource && i.local === callee)\n ) {\n return true;\n }\n }\n\n return false;\n}\n\nfunction isHookOrCreateElement(name: string): boolean {\n return name === 'createElement' || /use[A-Z]/.test(name);\n}\n\nfunction isClassicReactRuntime(\n p: NodePath<CallExpression>,\n imports: (IImport | ISideEffectImport)[]\n) {\n const reactImports = imports.filter(\n (i) =>\n i.source === 'react' &&\n (i.imported === 'default' ||\n (i.imported && isHookOrCreateElement(i.imported)))\n ) as IImport[];\n\n if (reactImports.length === 0) return false;\n const callee = getCallee(p);\n if (callee.isIdentifier() && isHookOrCreateElement(callee.node.name)) {\n const bindingPath = getScope(callee).getBinding(callee.node.name)?.path;\n return reactImports.some((i) => bindingPath?.isAncestor(i.local));\n }\n\n if (callee.isMemberExpression()) {\n if (reactImports.some((i) => i.local === callee)) {\n // It's React.createElement in CJS\n return true;\n }\n\n const object = callee.get('object');\n const property = callee.get('property');\n const defaultImport = reactImports.find((i) => i.imported === 'default');\n if (\n !defaultImport ||\n !defaultImport.local.isIdentifier() ||\n !property.isIdentifier() ||\n !isHookOrCreateElement(property.node.name) ||\n !object.isIdentifier({ name: defaultImport.local.node.name })\n ) {\n return false;\n }\n\n const bindingPath = getScope(object).getBinding(object.node.name)?.path;\n return bindingPath?.isAncestor(defaultImport.local) ?? false;\n }\n\n return false;\n}\n\nexport function isUnnecessaryReactCall(path: NodePath<CallExpression>) {\n const programPath = path.findParent((p) => p.isProgram()) as\n | NodePath<Program>\n | undefined;\n if (!programPath) {\n return false;\n }\n\n const { imports } = collectExportsAndImports(programPath);\n\n return isJSXRuntime(path, imports) || isClassicReactRuntime(path, imports);\n}\n"],"mappings":"AAIA,SAASA,wBAAwB,QAAQ,4BAA4B;AACrE,SAASC,QAAQ,QAAQ,YAAY;AAErC,SAASC,SAASA,CAACC,CAA2B,EAAE;EAC9C,MAAMC,MAAM,GAAGD,CAAC,CAACE,GAAG,CAAC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ACnC,CAACgB,QAAQ,CAAChB,YAAY,CAAC,CAAC,IACxB,CAACI,qBAAqB,CAACY,QAAQ,CAACf,IAAI,CAACC,IAAI,CAAC,IAC1C,CAACa,MAAM,CAACf,YAAY,CAAC;MAAEE,IAAI,EAAEe,aAAa,CAAClB,KAAK,CAACE,IAAI,CAACC;IAAK,CAAC,CAAC,EAC7D;MACA,OAAO,KAAK;IACd;IAEA,MAAMQ,WAAW,GAAG7B,QAAQ,CAACkC,MAAM,CAAC,CAACJ,UAAU,CAACI,MAAM,CAACd,IAAI,CAACC,IAAI,CAAC,EAAEU,IAAI;IACvE,OAAOF,WAAW,EAAEI,UAAU,CAACG,aAAa,CAAClB,KAAK,CAAC,IAAI,KAAK;EAC9D;EAEA,OAAO,KAAK;AACd;AAEA,OAAO,SAASmB,sBAAsBA,CAACN,IAA8B,EAAE;EACrE,MAAMO,WAAW,GAAGP,IAAI,CAACQ,UAAU,CAAErC,CAAC,IAAKA,CAAC,CAACsC,SAAS,CAAC,CAAC,CAE3C;EACb,IAAI,CAACF,WAAW,EAAE;IAChB,OAAO,KAAK;EACd;EAEA,MAAM;IAAE1B;EAAQ,CAAC,GAAGb,wBAAwB,CAACuC,WAAW,CAAC;EAEzD,OAAO3B,YAAY,CAACoB,IAAI,EAAEnB,OAAO,CAAC,IAAIa,qBAAqB,CAACM,IAAI,EAAEnB,OAAO,CAAC;AAC5E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peek.js","names":["peek","stack","offset","length"],"sources":["../../src/utils/peek.ts"],"sourcesContent":["export const peek = <T>(stack: T[], offset = 1): T =>\n stack[stack.length - offset];\n"],"mappings":"AAAA,OAAO,MAAMA,IAAI,GAAGA,CAAIC,KAAU,EAAEC,MAAM,GAAG,CAAC,KAC5CD,KAAK,CAACA,KAAK,CAACE,MAAM,GAAGD,MAAM,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getTagProcessor } from './getTagProcessor';
|
|
2
|
+
const processed = new WeakSet();
|
|
3
|
+
export const processTemplateExpression = (p, fileContext, options, emit) => {
|
|
4
|
+
if (p.parentPath.isExportSpecifier()) return;
|
|
5
|
+
if (processed.has(p.node)) return;
|
|
6
|
+
const tagProcessor = getTagProcessor(p, fileContext, options);
|
|
7
|
+
processed.add(p.node);
|
|
8
|
+
if (tagProcessor === null) return;
|
|
9
|
+
emit(tagProcessor);
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=processTemplateExpression.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processTemplateExpression.js","names":["getTagProcessor","processed","WeakSet","processTemplateExpression","p","fileContext","options","emit","parentPath","isExportSpecifier","has","node","tagProcessor","add"],"sources":["../../src/utils/processTemplateExpression.ts"],"sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { Identifier } from '@babel/types';\n\nimport type { BaseProcessor, IFileContext } from '@wyw-in-js/processor-utils';\nimport type { StrictOptions } from '@wyw-in-js/shared';\n\nimport { getTagProcessor } from './getTagProcessor';\n\nconst processed = new WeakSet<Identifier>();\n\nexport const processTemplateExpression = (\n p: NodePath<Identifier>,\n fileContext: IFileContext,\n options: Pick<\n StrictOptions,\n 'classNameSlug' | 'displayName' | 'evaluate' | 'tagResolver'\n >,\n emit: (processor: BaseProcessor) => void\n) => {\n if (p.parentPath.isExportSpecifier()) return;\n if (processed.has(p.node)) return;\n\n const tagProcessor = getTagProcessor(p, fileContext, options);\n\n processed.add(p.node);\n\n if (tagProcessor === null) return;\n\n emit(tagProcessor);\n};\n"],"mappings":"AAMA,SAASA,eAAe,QAAQ,mBAAmB;AAEnD,MAAMC,SAAS,GAAG,IAAIC,OAAO,CAAa,CAAC;AAE3C,OAAO,MAAMC,yBAAyB,GAAGA,CACvCC,CAAuB,EACvBC,WAAyB,EACzBC,OAGC,EACDC,IAAwC,KACrC;EACH,IAAIH,CAAC,CAACI,UAAU,CAACC,iBAAiB,CAAC,CAAC,EAAE;EACtC,IAAIR,SAAS,CAACS,GAAG,CAACN,CAAC,CAACO,IAAI,CAAC,EAAE;EAE3B,MAAMC,YAAY,GAAGZ,eAAe,CAACI,CAAC,EAAEC,WAAW,EAAEC,OAAO,CAAC;EAE7DL,SAAS,CAACY,GAAG,CAACT,CAAC,CAACO,IAAI,CAAC;EAErB,IAAIC,YAAY,KAAK,IAAI,EAAE;EAE3BL,IAAI,CAACK,YAAY,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { nonType } from './findIdentifiers';
|
|
2
|
+
import { isUnnecessaryReactCall } from './isUnnecessaryReactCall';
|
|
3
|
+
import { applyAction, removeWithRelated } from './scopeHelpers';
|
|
4
|
+
import { JSXElementsRemover } from './visitors/JSXElementsRemover';
|
|
5
|
+
const isGlobal = id => {
|
|
6
|
+
if (!nonType(id)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const {
|
|
10
|
+
scope
|
|
11
|
+
} = id;
|
|
12
|
+
const {
|
|
13
|
+
name
|
|
14
|
+
} = id.node;
|
|
15
|
+
return !scope.hasBinding(name) && scope.hasGlobal(name);
|
|
16
|
+
};
|
|
17
|
+
const ssrCheckFields = new Set(['document', 'location', 'navigator', 'sessionStorage', 'localStorage', 'window']);
|
|
18
|
+
const forbiddenGlobals = new Set([...ssrCheckFields, '$RefreshReg$', 'XMLHttpRequest', 'clearImmediate', 'clearInterval', 'clearTimeout', 'fetch', 'navigator', 'setImmediate', 'setInterval', 'setTimeout']);
|
|
19
|
+
const isBrowserGlobal = id => {
|
|
20
|
+
return forbiddenGlobals.has(id.node.name) && isGlobal(id);
|
|
21
|
+
};
|
|
22
|
+
const isSSRCheckField = id => {
|
|
23
|
+
return ssrCheckFields.has(id.node.name) && isGlobal(id);
|
|
24
|
+
};
|
|
25
|
+
const getPropertyName = path => {
|
|
26
|
+
if (path.isIdentifier()) {
|
|
27
|
+
return path.node.name;
|
|
28
|
+
}
|
|
29
|
+
if (path.isStringLiteral()) {
|
|
30
|
+
return path.node.value;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
};
|
|
34
|
+
export const removeDangerousCode = programPath => {
|
|
35
|
+
programPath.traverse({
|
|
36
|
+
// JSX can be replaced with a dummy value,
|
|
37
|
+
// but we have to do it after we processed template tags.
|
|
38
|
+
CallExpression: {
|
|
39
|
+
enter(p) {
|
|
40
|
+
if (isUnnecessaryReactCall(p)) {
|
|
41
|
+
JSXElementsRemover(p);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
JSXElement: {
|
|
46
|
+
enter: JSXElementsRemover
|
|
47
|
+
},
|
|
48
|
+
JSXFragment: {
|
|
49
|
+
enter: JSXElementsRemover
|
|
50
|
+
},
|
|
51
|
+
MemberExpression(p, state) {
|
|
52
|
+
const obj = p.get('object');
|
|
53
|
+
const prop = p.get('property');
|
|
54
|
+
if (!obj.isIdentifier({
|
|
55
|
+
name: 'window'
|
|
56
|
+
})) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const name = getPropertyName(prop);
|
|
60
|
+
if (!name) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
state.windowScoped.add(name);
|
|
64
|
+
// eslint-disable-next-line no-param-reassign
|
|
65
|
+
state.globals = state.globals.filter(id => {
|
|
66
|
+
if (id.node.name === name) {
|
|
67
|
+
removeWithRelated([id]);
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
MetaProperty(p) {
|
|
74
|
+
// Remove all references to `import.meta`
|
|
75
|
+
removeWithRelated([p]);
|
|
76
|
+
},
|
|
77
|
+
Identifier(p, state) {
|
|
78
|
+
if (p.find(parent => parent.isTSTypeReference())) {
|
|
79
|
+
// don't mess with TS type references
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (isBrowserGlobal(p)) {
|
|
83
|
+
if (p.find(parentPath => parentPath.isUnaryExpression({
|
|
84
|
+
operator: 'typeof'
|
|
85
|
+
}) || parentPath.isTSTypeQuery())) {
|
|
86
|
+
// Ignore `typeof window` expressions
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (p.parentPath.isClassProperty()) {
|
|
90
|
+
// ignore class property decls
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (p.parentPath.isMemberExpression() && p.key === 'property') {
|
|
94
|
+
// ignore e.g this.fetch()
|
|
95
|
+
// window.fetch will be handled by the windowScoped block below
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
removeWithRelated([p]);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (state.windowScoped.has(p.node.name)) {
|
|
102
|
+
removeWithRelated([p]);
|
|
103
|
+
} else if (isGlobal(p)) {
|
|
104
|
+
state.globals.push(p);
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
// Since we can use happy-dom, typical SSR checks may not work as expected.
|
|
108
|
+
// We need to detect them and replace with an "undefined" literal.
|
|
109
|
+
UnaryExpression(p) {
|
|
110
|
+
if (p.node.operator !== 'typeof') {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const arg = p.get('argument');
|
|
114
|
+
if (!arg.isIdentifier() || !isSSRCheckField(arg)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
applyAction(['replace', p, {
|
|
118
|
+
type: 'StringLiteral',
|
|
119
|
+
value: 'undefined'
|
|
120
|
+
}]);
|
|
121
|
+
}
|
|
122
|
+
}, {
|
|
123
|
+
globals: [],
|
|
124
|
+
windowScoped: new Set()
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=removeDangerousCode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"removeDangerousCode.js","names":["nonType","isUnnecessaryReactCall","applyAction","removeWithRelated","JSXElementsRemover","isGlobal","id","scope","name","node","hasBinding","hasGlobal","ssrCheckFields","Set","forbiddenGlobals","isBrowserGlobal","has","isSSRCheckField","getPropertyName","path","isIdentifier","isStringLiteral","value","removeDangerousCode","programPath","traverse","CallExpression","enter","p","JSXElement","JSXFragment","MemberExpression","state","obj","get","prop","windowScoped","add","globals","filter","MetaProperty","Identifier","find","parent","isTSTypeReference","parentPath","isUnaryExpression","operator","isTSTypeQuery","isClassProperty","isMemberExpression","key","push","UnaryExpression","arg","type"],"sources":["../../src/utils/removeDangerousCode.ts"],"sourcesContent":["import type { NodePath } from '@babel/core';\nimport type { Identifier, Program } from '@babel/types';\n\nimport { nonType } from './findIdentifiers';\nimport { isUnnecessaryReactCall } from './isUnnecessaryReactCall';\nimport { applyAction, removeWithRelated } from './scopeHelpers';\nimport { JSXElementsRemover } from './visitors/JSXElementsRemover';\n\nconst isGlobal = (id: NodePath<Identifier>): boolean => {\n if (!nonType(id)) {\n return false;\n }\n\n const { scope } = id;\n const { name } = id.node;\n return !scope.hasBinding(name) && scope.hasGlobal(name);\n};\n\nconst ssrCheckFields = new Set([\n 'document',\n 'location',\n 'navigator',\n 'sessionStorage',\n 'localStorage',\n 'window',\n]);\n\nconst forbiddenGlobals = new Set([\n ...ssrCheckFields,\n '$RefreshReg$',\n 'XMLHttpRequest',\n 'clearImmediate',\n 'clearInterval',\n 'clearTimeout',\n 'fetch',\n 'navigator',\n 'setImmediate',\n 'setInterval',\n 'setTimeout',\n]);\n\nconst isBrowserGlobal = (id: NodePath<Identifier>) => {\n return forbiddenGlobals.has(id.node.name) && isGlobal(id);\n};\n\nconst isSSRCheckField = (id: NodePath<Identifier>) => {\n return ssrCheckFields.has(id.node.name) && isGlobal(id);\n};\n\nconst getPropertyName = (path: NodePath): string | null => {\n if (path.isIdentifier()) {\n return path.node.name;\n }\n\n if (path.isStringLiteral()) {\n return path.node.value;\n }\n\n return null;\n};\n\nexport const removeDangerousCode = (programPath: NodePath<Program>) => {\n programPath.traverse(\n {\n // JSX can be replaced with a dummy value,\n // but we have to do it after we processed template tags.\n CallExpression: {\n enter(p) {\n if (isUnnecessaryReactCall(p)) {\n JSXElementsRemover(p);\n }\n },\n },\n JSXElement: {\n enter: JSXElementsRemover,\n },\n JSXFragment: {\n enter: JSXElementsRemover,\n },\n MemberExpression(p, state) {\n const obj = p.get('object');\n const prop = p.get('property');\n if (!obj.isIdentifier({ name: 'window' })) {\n return;\n }\n\n const name = getPropertyName(prop);\n if (!name) {\n return;\n }\n\n state.windowScoped.add(name);\n // eslint-disable-next-line no-param-reassign\n state.globals = state.globals.filter((id) => {\n if (id.node.name === name) {\n removeWithRelated([id]);\n return false;\n }\n\n return true;\n });\n },\n MetaProperty(p) {\n // Remove all references to `import.meta`\n removeWithRelated([p]);\n },\n Identifier(p, state) {\n if (p.find((parent) => parent.isTSTypeReference())) {\n // don't mess with TS type references\n return;\n }\n if (isBrowserGlobal(p)) {\n if (\n p.find(\n (parentPath) =>\n parentPath.isUnaryExpression({ operator: 'typeof' }) ||\n parentPath.isTSTypeQuery()\n )\n ) {\n // Ignore `typeof window` expressions\n return;\n }\n\n if (p.parentPath.isClassProperty()) {\n // ignore class property decls\n return;\n }\n if (p.parentPath.isMemberExpression() && p.key === 'property') {\n // ignore e.g this.fetch()\n // window.fetch will be handled by the windowScoped block below\n return;\n }\n\n removeWithRelated([p]);\n\n return;\n }\n\n if (state.windowScoped.has(p.node.name)) {\n removeWithRelated([p]);\n } else if (isGlobal(p)) {\n state.globals.push(p);\n }\n },\n\n // Since we can use happy-dom, typical SSR checks may not work as expected.\n // We need to detect them and replace with an \"undefined\" literal.\n UnaryExpression(p) {\n if (p.node.operator !== 'typeof') {\n return;\n }\n const arg = p.get('argument');\n if (!arg.isIdentifier() || !isSSRCheckField(arg)) {\n return;\n }\n\n applyAction([\n 'replace',\n p,\n { type: 'StringLiteral', value: 'undefined' },\n ]);\n },\n },\n {\n globals: [] as NodePath<Identifier>[],\n windowScoped: new Set<string>(),\n }\n );\n};\n"],"mappings":"AAGA,SAASA,OAAO,QAAQ,mBAAmB;AAC3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