@hirarijs/loader-cjs-interop 1.0.15 → 1.0.17

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
@@ -33,18 +33,44 @@ __export(index_exports, {
33
33
  default: () => index_default
34
34
  });
35
35
  module.exports = __toCommonJS(index_exports);
36
- var import_module = require("module");
37
36
  var import_path = __toESM(require("path"), 1);
38
37
  var import_loader = require("@hirarijs/loader");
39
- var import_meta = {};
40
38
  var extensions = [".js", ".cjs"];
41
- var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
42
39
  var WRAP_MARK = "__HIRARI_CJS_INTEROP_WRAPPED__";
43
40
  var BYPASS_KEY = import_loader.BYPASS_FLAG || "__hirari_loader_bypass__";
41
+ var CJS_MARKERS = [/\bmodule\.exports\b/, /\bexports\.[A-Za-z_$]/, /\bexports\[/];
42
+ var HAS_ESM_SYNTAX = /\b(import|export)\s+(?:[\w*{]|default)/;
43
+ var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
44
+ var isLikelyCjs = (code) => !HAS_ESM_SYNTAX.test(code) && CJS_MARKERS.some((re) => re.test(code));
45
+ function collectNamedExports(code) {
46
+ const keys = /* @__PURE__ */ new Set();
47
+ const directRe = /(?:module\.exports|exports)\.(\w+)\s*=/g;
48
+ let m;
49
+ while (m = directRe.exec(code)) {
50
+ if (m[1] !== "default" && isValidIdent(m[1])) keys.add(m[1]);
51
+ }
52
+ const objectRe = /exports\s*=\s*module\.exports\s*=\s*{([^}]*)}|module\.exports\s*=\s*{([^}]*)}/g;
53
+ while (m = objectRe.exec(code)) {
54
+ const body = m[1] || m[2];
55
+ if (!body) continue;
56
+ for (const chunk of body.split(",")) {
57
+ const [raw] = chunk.split(":").map((s) => s.trim());
58
+ if (!raw) continue;
59
+ const ident = raw.replace(/^['"`]|['"`]$/g, "");
60
+ if (ident && ident !== "default" && isValidIdent(ident)) {
61
+ keys.add(ident);
62
+ }
63
+ }
64
+ }
65
+ return Array.from(keys);
66
+ }
44
67
  function runTransform(code, filename, ctx) {
45
68
  if (code.includes(WRAP_MARK)) {
46
69
  return { code, continue: true };
47
70
  }
71
+ if (!isLikelyCjs(code)) {
72
+ return { code, continue: true };
73
+ }
48
74
  const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
49
75
  const ignoreNm = opts.ignoreNodeModules !== false && opts.allowNodeModules !== true;
50
76
  if (ignoreNm && filename.includes("node_modules")) {
@@ -58,41 +84,30 @@ function runTransform(code, filename, ctx) {
58
84
  }
59
85
  }
60
86
  }
61
- let exportKeys = [];
62
- try {
63
- ;
64
- globalThis[BYPASS_KEY] = true;
65
- const req = (0, import_module.createRequire)(import_meta.url);
66
- const mod = req(filename);
67
- if (mod && typeof mod === "object") {
68
- exportKeys = Object.keys(mod).filter((k) => k !== "default" && isValidIdent(k));
69
- }
70
- if (ctx.loaderConfig.debug) {
71
- console.log(`[loader-cjs-interop] required ${filename}`);
72
- }
73
- } catch {
74
- if (ctx.loaderConfig.debug) {
75
- console.log(`[loader-cjs-interop] imported ${filename}`);
76
- }
77
- return { code, continue: true };
78
- } finally {
79
- try {
80
- delete globalThis[BYPASS_KEY];
81
- } catch {
82
- }
83
- }
87
+ const exportKeys = collectNamedExports(code);
84
88
  const lines = [];
85
89
  lines.push(`// ${WRAP_MARK}`);
86
90
  lines.push(`import { createRequire } from 'module';`);
87
- lines.push(`const _req = createRequire(${import_loader.IMPORT_META_URL_VARIABLE});`);
88
- lines.push(`const _cjs = _req(${JSON.stringify(filename)});`);
91
+ lines.push(`const __req = createRequire(${import_loader.IMPORT_META_URL_VARIABLE});`);
92
+ lines.push(`const __g = globalThis;`);
93
+ lines.push(`const __bypassKey = ${JSON.stringify(BYPASS_KEY)};`);
94
+ lines.push(`const __prev = typeof __g[__bypassKey] === 'number' ? __g[__bypassKey] : 0;`);
95
+ lines.push(`__g[__bypassKey] = __prev + 1;`);
96
+ lines.push(`const module = { exports: {} };`);
97
+ lines.push(`const exports = module.exports;`);
98
+ lines.push(`const require = __req;`);
99
+ lines.push(`;(() => {`);
100
+ lines.push(code);
101
+ lines.push(`})();`);
102
+ lines.push(`__g[__bypassKey] = __prev;`);
103
+ lines.push(`const __cjs = module.exports;`);
89
104
  lines.push(
90
- `const _default = (_cjs && _cjs.__esModule && 'default' in _cjs) ? _cjs.default : _cjs;`
105
+ `const __default = (__cjs && __cjs.__esModule && 'default' in __cjs) ? __cjs.default : __cjs;`
91
106
  );
92
- lines.push(`export default _default;`);
107
+ lines.push(`export default __default;`);
93
108
  lines.push(`export const __esModule = true;`);
94
109
  for (const key of exportKeys) {
95
- lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
110
+ lines.push(`export const ${key} = __cjs[${JSON.stringify(key)}];`);
96
111
  }
97
112
  return {
98
113
  code: lines.join("\n"),
package/dist/index.js CHANGED
@@ -1,18 +1,45 @@
1
1
  // src/index.ts
2
- import { createRequire } from "module";
3
2
  import path from "path";
4
3
  import {
5
4
  IMPORT_META_URL_VARIABLE,
6
5
  BYPASS_FLAG
7
6
  } from "@hirarijs/loader";
8
7
  var extensions = [".js", ".cjs"];
9
- var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
10
8
  var WRAP_MARK = "__HIRARI_CJS_INTEROP_WRAPPED__";
11
9
  var BYPASS_KEY = BYPASS_FLAG || "__hirari_loader_bypass__";
10
+ var CJS_MARKERS = [/\bmodule\.exports\b/, /\bexports\.[A-Za-z_$]/, /\bexports\[/];
11
+ var HAS_ESM_SYNTAX = /\b(import|export)\s+(?:[\w*{]|default)/;
12
+ var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
13
+ var isLikelyCjs = (code) => !HAS_ESM_SYNTAX.test(code) && CJS_MARKERS.some((re) => re.test(code));
14
+ function collectNamedExports(code) {
15
+ const keys = /* @__PURE__ */ new Set();
16
+ const directRe = /(?:module\.exports|exports)\.(\w+)\s*=/g;
17
+ let m;
18
+ while (m = directRe.exec(code)) {
19
+ if (m[1] !== "default" && isValidIdent(m[1])) keys.add(m[1]);
20
+ }
21
+ const objectRe = /exports\s*=\s*module\.exports\s*=\s*{([^}]*)}|module\.exports\s*=\s*{([^}]*)}/g;
22
+ while (m = objectRe.exec(code)) {
23
+ const body = m[1] || m[2];
24
+ if (!body) continue;
25
+ for (const chunk of body.split(",")) {
26
+ const [raw] = chunk.split(":").map((s) => s.trim());
27
+ if (!raw) continue;
28
+ const ident = raw.replace(/^['"`]|['"`]$/g, "");
29
+ if (ident && ident !== "default" && isValidIdent(ident)) {
30
+ keys.add(ident);
31
+ }
32
+ }
33
+ }
34
+ return Array.from(keys);
35
+ }
12
36
  function runTransform(code, filename, ctx) {
13
37
  if (code.includes(WRAP_MARK)) {
14
38
  return { code, continue: true };
15
39
  }
40
+ if (!isLikelyCjs(code)) {
41
+ return { code, continue: true };
42
+ }
16
43
  const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
17
44
  const ignoreNm = opts.ignoreNodeModules !== false && opts.allowNodeModules !== true;
18
45
  if (ignoreNm && filename.includes("node_modules")) {
@@ -26,41 +53,30 @@ function runTransform(code, filename, ctx) {
26
53
  }
27
54
  }
28
55
  }
29
- let exportKeys = [];
30
- try {
31
- ;
32
- globalThis[BYPASS_KEY] = true;
33
- const req = createRequire(import.meta.url);
34
- const mod = req(filename);
35
- if (mod && typeof mod === "object") {
36
- exportKeys = Object.keys(mod).filter((k) => k !== "default" && isValidIdent(k));
37
- }
38
- if (ctx.loaderConfig.debug) {
39
- console.log(`[loader-cjs-interop] required ${filename}`);
40
- }
41
- } catch {
42
- if (ctx.loaderConfig.debug) {
43
- console.log(`[loader-cjs-interop] imported ${filename}`);
44
- }
45
- return { code, continue: true };
46
- } finally {
47
- try {
48
- delete globalThis[BYPASS_KEY];
49
- } catch {
50
- }
51
- }
56
+ const exportKeys = collectNamedExports(code);
52
57
  const lines = [];
53
58
  lines.push(`// ${WRAP_MARK}`);
54
59
  lines.push(`import { createRequire } from 'module';`);
55
- lines.push(`const _req = createRequire(${IMPORT_META_URL_VARIABLE});`);
56
- lines.push(`const _cjs = _req(${JSON.stringify(filename)});`);
60
+ lines.push(`const __req = createRequire(${IMPORT_META_URL_VARIABLE});`);
61
+ lines.push(`const __g = globalThis;`);
62
+ lines.push(`const __bypassKey = ${JSON.stringify(BYPASS_KEY)};`);
63
+ lines.push(`const __prev = typeof __g[__bypassKey] === 'number' ? __g[__bypassKey] : 0;`);
64
+ lines.push(`__g[__bypassKey] = __prev + 1;`);
65
+ lines.push(`const module = { exports: {} };`);
66
+ lines.push(`const exports = module.exports;`);
67
+ lines.push(`const require = __req;`);
68
+ lines.push(`;(() => {`);
69
+ lines.push(code);
70
+ lines.push(`})();`);
71
+ lines.push(`__g[__bypassKey] = __prev;`);
72
+ lines.push(`const __cjs = module.exports;`);
57
73
  lines.push(
58
- `const _default = (_cjs && _cjs.__esModule && 'default' in _cjs) ? _cjs.default : _cjs;`
74
+ `const __default = (__cjs && __cjs.__esModule && 'default' in __cjs) ? __cjs.default : __cjs;`
59
75
  );
60
- lines.push(`export default _default;`);
76
+ lines.push(`export default __default;`);
61
77
  lines.push(`export const __esModule = true;`);
62
78
  for (const key of exportKeys) {
63
- lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
79
+ lines.push(`export const ${key} = __cjs[${JSON.stringify(key)}];`);
64
80
  }
65
81
  return {
66
82
  code: lines.join("\n"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hirarijs/loader-cjs-interop",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "CJS interop loader plugin that exposes named exports for CommonJS modules",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "devDependencies": {
30
30
  "@hirarijs/loader": "workspace:*",
31
- "tsup": "^8.0.1",
32
- "typescript": "^5.3.3"
33
- }
31
+ "tsup": "^8.0.1"
32
+ },
33
+ "dependencies": {}
34
34
  }