{"version":3,"names":["_helperPluginUtils","require","_helperModuleTransforms","_core","buildWrapper","template","statement","buildAnonymousWrapper","injectWrapper","path","wrapper","body","directives","node","amdFactoryCall","pushContainer","get","amdFactoryCallArgs","amdFactory","length","_default","declare","api","options","_api$assumption","_api$assumption2","assertVersion","allowTopLevelThis","strict","strictMode","importInterop","noInterop","constantReexports","assumption","loose","enumerableModuleMeta","name","pre","file","set","visitor","types","importExpression","state","has","isCallExpression","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","exports","default"],"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, type PluginPass } 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,\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;\n const amdFactoryCallArgs = amdFactoryCall.get(\"arguments\");\n const amdFactory = (\n amdFactoryCallArgs[\n amdFactoryCallArgs.length - 1\n ] as NodePath\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((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\" +\n (api.types.importExpression ? \"|ImportExpression\" : \"\")](\n this: State & PluginPass,\n path: NodePath,\n state: State,\n ) {\n if (!this.file.has(\"@babel/plugin-proposal-dynamic-import\")) return;\n if (path.isCallExpression() && !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.resolveId = resolveId;\n state.rejectId = rejectId;\n }\n\n let result: t.Node = t.identifier(\"imported\");\n if (!noInterop) {\n result = wrapInterop(this.file.path, result, \"namespace\");\n }\n\n path.replaceWith(\n buildDynamicImport(\n path.node,\n false,\n false,\n specifier => template.expression.ast`\n new Promise((${resolveId}, ${rejectId}) =>\n ${requireId}(\n [${specifier}],\n imported => ${t.cloneNode(resolveId)}(${result}),\n ${t.cloneNode(rejectId)}\n )\n )\n `,\n ),\n );\n },\n Program: {\n exit(path, { requireId }) {\n if (!isModule(path)) {\n if (requireId) {\n injectWrapper(\n path,\n buildAnonymousWrapper({\n REQUIRE: t.cloneNode(requireId),\n }) as t.ExpressionStatement,\n );\n }\n return;\n }\n\n const amdArgs = [];\n const importNames = [];\n if (requireId) {\n amdArgs.push(t.stringLiteral(\"require\"));\n importNames.push(t.cloneNode(requireId));\n }\n\n let moduleName = getModuleName(this.file.opts, options);\n // @ts-expect-error todo(flow->ts): do not reuse variables\n if (moduleName) moduleName = t.stringLiteral(moduleName);\n\n const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(\n path,\n {\n enumerableModuleMeta,\n constantReexports,\n strict,\n strictMode,\n allowTopLevelThis,\n importInterop,\n noInterop,\n filename: this.file.opts.filename,\n },\n );\n\n if (hasExports(meta)) {\n amdArgs.push(t.stringLiteral(\"exports\"));\n\n importNames.push(t.identifier(meta.exportName));\n }\n\n for (const [source, metadata] of meta.source) {\n amdArgs.push(t.stringLiteral(source));\n importNames.push(t.identifier(metadata.name));\n\n if (!isSideEffectImport(metadata)) {\n const interop = wrapInterop(\n path,\n t.identifier(metadata.name),\n metadata.interop,\n );\n if (interop) {\n const header = t.expressionStatement(\n t.assignmentExpression(\n \"=\",\n t.identifier(metadata.name),\n interop,\n ),\n );\n header.loc = metadata.loc;\n headers.push(header);\n }\n }\n\n headers.push(\n ...buildNamespaceInitStatements(\n meta,\n metadata,\n constantReexports,\n ),\n );\n }\n\n ensureStatementsHoisted(headers);\n path.unshiftContainer(\"body\", headers);\n\n injectWrapper(\n path,\n buildWrapper({\n MODULE_NAME: moduleName,\n\n AMD_ARGUMENTS: t.arrayExpression(amdArgs),\n IMPORT_NAMES: importNames,\n }) as t.ExpressionStatement,\n );\n },\n },\n },\n };\n});\n"],"mappings":";;;;;;AAAA,IAAAA,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