rolldown-plugin-require-cjs 0.1.1 → 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 +13 -6
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -10862,13 +10862,15 @@ function parseAst(sourceText, options, filename) {
10862
10862
  //#endregion
10863
10863
  //#region src/options.ts
10864
10864
  function resolveOptions(options) {
10865
- let { shouldTransform } = options;
10866
- if (Array.isArray(shouldTransform)) shouldTransform = (id) => shouldTransform.includes(id);
10865
+ if (Array.isArray(options.shouldTransform)) {
10866
+ const { shouldTransform } = options;
10867
+ options.shouldTransform = (id) => shouldTransform.includes(id);
10868
+ }
10867
10869
  return {
10868
10870
  include: options.include || [/\.[cm]?[jt]sx?$/],
10869
10871
  exclude: options.exclude || [/node_modules/, /\.d\.[cm]?ts$/],
10870
10872
  order: "order" in options ? options.order : "pre",
10871
- shouldTransform
10873
+ shouldTransform: options.shouldTransform
10872
10874
  };
10873
10875
  }
10874
10876
 
@@ -10898,6 +10900,7 @@ function RequireCJS(userOptions = {}) {
10898
10900
  const { body } = parseAst(code, { lang: void 0 }, id);
10899
10901
  const s = new MagicStringAST(code);
10900
10902
  for (const stmt of body) if (stmt.type === "ImportDeclaration") {
10903
+ if (stmt.importKind === "type") continue;
10901
10904
  const source = stmt.source.value;
10902
10905
  const resolution = await this.resolve(source, id);
10903
10906
  if (resolution && resolution.external === false) continue;
@@ -10906,12 +10909,16 @@ function RequireCJS(userOptions = {}) {
10906
10909
  else {
10907
10910
  const mapping = {};
10908
10911
  let namespaceId;
10912
+ let defaultId;
10909
10913
  for (const specifier of stmt.specifiers) if (specifier.type === "ImportNamespaceSpecifier") namespaceId = s.sliceNode(specifier.local);
10910
- 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);
10911
10916
  const requireCode = `require(${s.sliceNode(stmt.source)})`;
10912
10917
  let str = "";
10913
- if (namespaceId) str += `const ${namespaceId} = ${requireCode};`;
10914
- 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};`;
10915
10922
  s.overwriteNode(stmt, str);
10916
10923
  }
10917
10924
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-require-cjs",
3
- "version": "0.1.1",
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",