rolldown-plugin-require-cjs 0.1.3 → 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 +18 -15
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -10905,22 +10905,25 @@ function RequireCJS(userOptions = {}) {
10905
10905
  const resolution = await this.resolve(source, id);
10906
10906
  if (resolution && resolution.external === false) continue;
10907
10907
  if (!(await shouldTransform?.(source, id) ?? await isPureCJS(source, id))) continue;
10908
- if (stmt.specifiers.length === 0) s.overwriteNode(stmt, `require(${s.sliceNode(stmt.source)});`);
10909
- else {
10910
- const mapping = {};
10911
- let namespaceId;
10912
- let defaultId;
10913
- for (const specifier of stmt.specifiers) if (specifier.type === "ImportNamespaceSpecifier") namespaceId = 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);
10916
- const requireCode = `require(${s.sliceNode(stmt.source)})`;
10917
- let str = "";
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};`;
10922
- s.overwriteNode(stmt, str);
10908
+ if (stmt.specifiers.length === 0) {
10909
+ s.overwriteNode(stmt, `require(${s.sliceNode(stmt.source)});`);
10910
+ continue;
10923
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);
10924
10927
  }
10925
10928
  return generateTransform(s, id);
10926
10929
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-require-cjs",
3
- "version": "0.1.3",
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",