1 line
12 KiB
Plaintext
1 line
12 KiB
Plaintext
|
{"version":3,"names":["buildWrapper","template","statement","buildAnonymousWrapper","injectWrapper","path","wrapper","body","directives","node","amdFactoryCall","pushContainer","get","amdFactoryCallArgs","amdFactory","length","declare","api","options","assertVersion","allowTopLevelThis","strict","strictMode","importInterop","noInterop","constantReexports","assumption","loose","enumerableModuleMeta","name","pre","file","set","visitor","CallExpression","state","has","isImport","requireId","resolveId","rejectId","scope","generateUidIdentifier","result","t","identifier","wrapInterop","replaceWith","buildDynamicImport","specifier","expression","ast","cloneNode","Program","exit","isModule","REQUIRE","amdArgs","importNames","push","stringLiteral","moduleName","getModuleName","opts","meta","headers","rewriteModuleStatementsAndPrepareHeader","filename","hasExports","exportName","source","metadata","isSideEffectImport","interop","header","expressionStatement","assignmentExpression","loc","buildNamespaceInitStatements","ensureStatementsHoisted","unshiftContainer","MODULE_NAME","AMD_ARGUMENTS","arrayExpression","IMPORT_NAMES"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport {\n buildDynamicImport,\n isModule,\n rewriteModuleStatementsAndPrepareHeader,\n type RewriteModuleStatementsAndPrepareHeaderOptions,\n hasExports,\n isSideEffectImport,\n buildNamespaceInitStatements,\n ensureStatementsHoisted,\n wrapInterop,\n getModuleName,\n} from \"@babel/helper-module-transforms\";\nimport { template, types as t } from \"@babel/core\";\nimport type { PluginOptions } from \"@babel/helper-module-transforms\";\nimport type { NodePath } from \"@babel/traverse\";\n\nconst buildWrapper = template.statement(`\n define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) {\n })\n`);\n\nconst buildAnonymousWrapper = template.statement(`\n define([\"require\"], function(REQUIRE) {\n })\n`);\n\nfunction injectWrapper(\n path: NodePath<t.Program>,\n wrapper: t.ExpressionStatement,\n) {\n const { body, directives } = path.node;\n path.node.directives = [];\n path.node.body = [];\n const amdFactoryCall = path\n .pushContainer(\"body\", wrapper)[0]\n .get(\"expression\") as NodePath<t.CallExpression>;\n const amdFactoryCallArgs = amdFactoryCall.get(\"arguments\");\n const amdFactory = (\n amdFactoryCallArgs[\n amdFactoryCallArgs.length - 1\n ] as NodePath<t.FunctionExpression>\n ).get(\"body\");\n amdFactory.pushContainer(\"directives\", directives);\n amdFactory.pushContainer(\"body\", body);\n}\n\nexport interface Options extends PluginOptions {\n allowTopLevelThis?: boolean;\n importInterop?: RewriteModuleStatementsAndPrepareHeaderOptions[\"importInterop\"];\n loose?: boolean;\n noInterop?: boolean;\n strict?: boolean;\n strictMode?: boolean;\n}\n\ntype State = {\n requireId?: t.Identifier;\n resolveId?: t.Identifier;\n rejectId?: t.Identifier;\n};\n\nexport default declare<State>((api, options: Options) => {\n api.assertVersion(7);\n\n const { allowTopLevelThis, strict, strictMode, importInterop, noInterop } =\n options;\n\n const constantReexports =\n api.assumption(\"constantReexports\") ?? options.loose;\n const enumerableModuleMeta =\n api.assumption(\"enumerableModuleMeta\") ?? options.loose;\n\n return {\n name: \"transform-modules-amd\",\n\n pre() {\n this.file.set(\"@babel/plugin-transform-modules-*\", \"amd\");\n },\n\n visitor: {\n CallExpression(path, state) {\n if (!this.file.has(\"@babel/plugin-proposal-dynamic-import\")) return;\n if (!path.get(\"callee\").isImport()) return;\n\n let { requireId, resolveId, rejectId } = state;\n if (!requireId) {\n requireId = path.scope.generateUidIdentifier(\"require\");\n state.requireId = requireId;\n }\n if (!resolveId || !rejectId) {\n resolveId = path.scope.generateUidIdentifier(\"resolve\");\n rejectId = path.scope.generateUidIdentifier(\"reject\");\n state.resol
|