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.
Files changed (2) hide show
  1. package/dist/index.js +8 -3
  2. 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 mapping[specifier.type === "ImportSpecifier" ? s.sliceNode(specifier.imported) : "default"] = s.sliceNode(specifier.local);
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) str += `const ${namespaceId} = ${requireCode};`;
10916
- if (Object.keys(mapping).length > 0) str += `const { ${Object.entries(mapping).map(([k, v]) => `${k}: ${v}`).join(", ")} } = ${namespaceId || requireCode};`;
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-require-cjs",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Transform ESM imports to CJS requires when the imported module is pure CJS.",
5
5
  "type": "module",
6
6
  "license": "MIT",