rolldown-plugin-require-cjs 0.1.2 → 0.1.4

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 +19 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -10900,22 +10900,30 @@ 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;
10906
10907
  if (!(await shouldTransform?.(source, id) ?? await isPureCJS(source, id))) continue;
10907
- if (stmt.specifiers.length === 0) s.overwriteNode(stmt, `require(${s.sliceNode(stmt.source)});`);
10908
- else {
10909
- const mapping = {};
10910
- let namespaceId;
10911
- 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);
10913
- const requireCode = `require(${s.sliceNode(stmt.source)})`;
10914
- 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};`;
10917
- s.overwriteNode(stmt, str);
10908
+ if (stmt.specifiers.length === 0) {
10909
+ s.overwriteNode(stmt, `require(${s.sliceNode(stmt.source)});`);
10910
+ continue;
10918
10911
  }
10912
+ const mapping = {};
10913
+ let namespaceId;
10914
+ let defaultId;
10915
+ for (const specifier of stmt.specifiers) if (specifier.type === "ImportNamespaceSpecifier") namespaceId = s.sliceNode(specifier.local);
10916
+ else if (specifier.type === "ImportSpecifier") {
10917
+ if (specifier.importKind === "type") continue;
10918
+ mapping[s.sliceNode(specifier.imported)] = s.sliceNode(specifier.local);
10919
+ } else defaultId = s.sliceNode(specifier.local);
10920
+ const requireCode = `require(${s.sliceNode(stmt.source)})`;
10921
+ let str = "";
10922
+ if (namespaceId) defaultId ||= `_cjs_${namespaceId}_default`;
10923
+ if (defaultId) str += `const ${defaultId} = ${requireCode};`;
10924
+ if (namespaceId) str += `const ${namespaceId} = { ...${defaultId}, default: ${defaultId} };`;
10925
+ if (Object.keys(mapping).length > 0) str += `const { ${Object.entries(mapping).map(([k, v]) => `${k}: ${v}`).join(", ")} } = ${defaultId || requireCode};`;
10926
+ s.overwriteNode(stmt, str);
10919
10927
  }
10920
10928
  return generateTransform(s, id);
10921
10929
  }
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.4",
4
4
  "description": "Transform ESM imports to CJS requires when the imported module is pure CJS.",
5
5
  "type": "module",
6
6
  "license": "MIT",