rolldown-plugin-require-cjs 0.1.2 → 0.1.3
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/dist/index.js +8 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -10900,6 +10900,7 @@ function RequireCJS(userOptions = {}) {
|
|
|
10900
10900
|
const { body } = parseAst(code, { lang: void 0 }, id);
|
|
10901
10901
|
const s = new MagicStringAST(code);
|
|
10902
10902
|
for (const stmt of body) if (stmt.type === "ImportDeclaration") {
|
|
10903
|
+
if (stmt.importKind === "type") continue;
|
|
10903
10904
|
const source = stmt.source.value;
|
|
10904
10905
|
const resolution = await this.resolve(source, id);
|
|
10905
10906
|
if (resolution && resolution.external === false) continue;
|
|
@@ -10908,12 +10909,16 @@ function RequireCJS(userOptions = {}) {
|
|
|
10908
10909
|
else {
|
|
10909
10910
|
const mapping = {};
|
|
10910
10911
|
let namespaceId;
|
|
10912
|
+
let defaultId;
|
|
10911
10913
|
for (const specifier of stmt.specifiers) if (specifier.type === "ImportNamespaceSpecifier") namespaceId = s.sliceNode(specifier.local);
|
|
10912
|
-
else
|
|
10914
|
+
else if (specifier.type === "ImportSpecifier") mapping[s.sliceNode(specifier.imported)] = s.sliceNode(specifier.local);
|
|
10915
|
+
else defaultId = s.sliceNode(specifier.local);
|
|
10913
10916
|
const requireCode = `require(${s.sliceNode(stmt.source)})`;
|
|
10914
10917
|
let str = "";
|
|
10915
|
-
if (namespaceId)
|
|
10916
|
-
if (
|
|
10918
|
+
if (namespaceId) defaultId ||= `_cjs_${namespaceId}_default`;
|
|
10919
|
+
if (defaultId) str += `const ${defaultId} = ${requireCode};`;
|
|
10920
|
+
if (namespaceId) str += `const ${namespaceId} = { ...${defaultId}, default: ${defaultId} };`;
|
|
10921
|
+
if (Object.keys(mapping).length > 0) str += `const { ${Object.entries(mapping).map(([k, v]) => `${k}: ${v}`).join(", ")} } = ${defaultId || requireCode};`;
|
|
10917
10922
|
s.overwriteNode(stmt, str);
|
|
10918
10923
|
}
|
|
10919
10924
|
}
|