@hirarijs/loader-cjs-interop 1.0.5 → 1.0.6

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.cjs CHANGED
@@ -34,7 +34,6 @@ __export(index_exports, {
34
34
  });
35
35
  module.exports = __toCommonJS(index_exports);
36
36
  var import_module = require("module");
37
- var import_url = require("url");
38
37
  var import_path = __toESM(require("path"), 1);
39
38
  var import_loader = require("@hirarijs/loader");
40
39
  var import_meta = {};
@@ -65,38 +64,29 @@ function runTransform(code, filename, ctx) {
65
64
  isCjs = false;
66
65
  }
67
66
  if (isCjs) {
68
- const keys = Object.keys(mod || {}).filter((k) => k !== "default");
69
- const lines2 = [];
70
- lines2.push(`import { createRequire } from 'module';`);
71
- lines2.push(
67
+ const exportKeys = mod && typeof mod === "object" ? Object.keys(mod).filter((k) => k !== "default") : [];
68
+ if (!exportKeys.length && typeof mod !== "function") {
69
+ return { code, continue: true };
70
+ }
71
+ const lines = [];
72
+ lines.push(`import { createRequire } from 'module';`);
73
+ lines.push(
72
74
  `const ${import_loader.IMPORT_META_URL_VARIABLE} = import.meta.url; const _cjs = createRequire(${import_loader.IMPORT_META_URL_VARIABLE})(${JSON.stringify(filename)});`
73
75
  );
74
- lines2.push(`export default _cjs;`);
75
- lines2.push(`export const __esModule = true;`);
76
- for (const key of keys) {
76
+ lines.push(`export default _cjs;`);
77
+ lines.push(`export const __esModule = true;`);
78
+ for (const key of exportKeys) {
77
79
  if (!isValidIdent(key)) continue;
78
- lines2.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
80
+ lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
79
81
  }
80
82
  return {
81
- code: lines2.join("\n"),
83
+ code: lines.join("\n"),
82
84
  map: void 0,
83
85
  format: ctx.format,
84
86
  continue: opts.continue === true
85
87
  };
86
88
  }
87
- const importUrl = (0, import_url.pathToFileURL)(filename).href;
88
- const lines = [];
89
- lines.push(`import * as _mod from ${JSON.stringify(importUrl)};`);
90
- lines.push(`const _default = _mod.default ?? _mod;`);
91
- lines.push(`export default _default;`);
92
- lines.push(`export const __esModule = true;`);
93
- lines.push(`export * from ${JSON.stringify(importUrl)};`);
94
- return {
95
- code: lines.join("\n"),
96
- map: void 0,
97
- format: ctx.format,
98
- continue: opts.continue === true
99
- };
89
+ return { code, continue: true };
100
90
  }
101
91
  var plugin = {
102
92
  name: "@hirarijs/loader-cjs-interop",
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  // src/index.ts
2
2
  import { createRequire } from "module";
3
- import { pathToFileURL } from "url";
4
3
  import path from "path";
5
4
  import {
6
5
  IMPORT_META_URL_VARIABLE
@@ -32,38 +31,29 @@ function runTransform(code, filename, ctx) {
32
31
  isCjs = false;
33
32
  }
34
33
  if (isCjs) {
35
- const keys = Object.keys(mod || {}).filter((k) => k !== "default");
36
- const lines2 = [];
37
- lines2.push(`import { createRequire } from 'module';`);
38
- lines2.push(
34
+ const exportKeys = mod && typeof mod === "object" ? Object.keys(mod).filter((k) => k !== "default") : [];
35
+ if (!exportKeys.length && typeof mod !== "function") {
36
+ return { code, continue: true };
37
+ }
38
+ const lines = [];
39
+ lines.push(`import { createRequire } from 'module';`);
40
+ lines.push(
39
41
  `const ${IMPORT_META_URL_VARIABLE} = import.meta.url; const _cjs = createRequire(${IMPORT_META_URL_VARIABLE})(${JSON.stringify(filename)});`
40
42
  );
41
- lines2.push(`export default _cjs;`);
42
- lines2.push(`export const __esModule = true;`);
43
- for (const key of keys) {
43
+ lines.push(`export default _cjs;`);
44
+ lines.push(`export const __esModule = true;`);
45
+ for (const key of exportKeys) {
44
46
  if (!isValidIdent(key)) continue;
45
- lines2.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
47
+ lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
46
48
  }
47
49
  return {
48
- code: lines2.join("\n"),
50
+ code: lines.join("\n"),
49
51
  map: void 0,
50
52
  format: ctx.format,
51
53
  continue: opts.continue === true
52
54
  };
53
55
  }
54
- const importUrl = pathToFileURL(filename).href;
55
- const lines = [];
56
- lines.push(`import * as _mod from ${JSON.stringify(importUrl)};`);
57
- lines.push(`const _default = _mod.default ?? _mod;`);
58
- lines.push(`export default _default;`);
59
- lines.push(`export const __esModule = true;`);
60
- lines.push(`export * from ${JSON.stringify(importUrl)};`);
61
- return {
62
- code: lines.join("\n"),
63
- map: void 0,
64
- format: ctx.format,
65
- continue: opts.continue === true
66
- };
56
+ return { code, continue: true };
67
57
  }
68
58
  var plugin = {
69
59
  name: "@hirarijs/loader-cjs-interop",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hirarijs/loader-cjs-interop",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "CJS interop loader plugin that exposes named exports for CommonJS modules",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",