index.js.map 12 KB

1
  1. {"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","expression","ast","getDynamicImportSource","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 isModule,\n rewriteModuleStatementsAndPrepareHeader,\n type RewriteModuleStatementsAndPrepareHeaderOptions,\n hasExports,\n isSideEffectImport,\n buildNamespaceInitStatements,\n ensureStatementsHoisted,\n wrapInterop,\n getModuleName,\n getDynamicImportSource,\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\") as NodePath<t.BlockStatement>;\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.resolveId = resolveId;\n state.rejectId = rejectId;\n }\n\n let result: t.Node = t.identifier(\"imported\");\n if (!noInterop) result = wrapInterop(path, result, \"namespace\");\n\n path.replaceWith(\n template.expression.ast`\n new Promise((${resolveId}, ${rejectId}) =>\n ${requireId}(\n [${getDynamicImportSource(path.node)}],\n imported => ${t.cloneNode(resolveId)}(${result}),\n ${t.cloneNode(rejectId)}\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;;AACA;;AAYA;;AAIA,MAAMA,YAAY,GAAGC,cAAA,CAASC,SAAT,CAAoB;AACzC;AACA;AACA,CAHqB,CAArB;;AAKA,MAAMC,qBAAqB,GAAGF,cAAA,CAASC,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