@hirarijs/loader-cjs-interop 1.0.9 → 1.0.11
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 +15 -11
- package/dist/index.js +17 -11
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -35,10 +35,15 @@ __export(index_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
36
|
var import_module = require("module");
|
|
37
37
|
var import_path = __toESM(require("path"), 1);
|
|
38
|
+
var import_loader = require("@hirarijs/loader");
|
|
38
39
|
var import_meta = {};
|
|
39
40
|
var extensions = [".js", ".cjs"];
|
|
40
41
|
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
42
|
+
var WRAP_MARK = "__HIRARI_CJS_INTEROP_WRAPPED__";
|
|
41
43
|
function runTransform(code, filename, ctx) {
|
|
44
|
+
if (code.includes(WRAP_MARK)) {
|
|
45
|
+
return { code, continue: true };
|
|
46
|
+
}
|
|
42
47
|
const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
|
|
43
48
|
const ignoreNm = opts.ignoreNodeModules !== false && opts.allowNodeModules !== true;
|
|
44
49
|
if (ignoreNm && filename.includes("node_modules")) {
|
|
@@ -47,35 +52,34 @@ function runTransform(code, filename, ctx) {
|
|
|
47
52
|
if (opts.aliases) {
|
|
48
53
|
for (const [key, target] of Object.entries(opts.aliases)) {
|
|
49
54
|
if (filename.includes(`node_modules/${key}`) || filename.includes(`node_modules\\${key}`)) {
|
|
50
|
-
|
|
51
|
-
filename = aliased;
|
|
55
|
+
filename = import_path.default.isAbsolute(target) ? target : import_path.default.resolve(process.cwd(), target);
|
|
52
56
|
break;
|
|
53
57
|
}
|
|
54
58
|
}
|
|
55
59
|
}
|
|
56
|
-
let
|
|
60
|
+
let exportKeys = [];
|
|
57
61
|
try {
|
|
58
62
|
const req = (0, import_module.createRequire)(import_meta.url);
|
|
59
|
-
mod = req(filename);
|
|
63
|
+
const mod = req(filename);
|
|
64
|
+
if (mod && typeof mod === "object") {
|
|
65
|
+
exportKeys = Object.keys(mod).filter((k) => k !== "default" && isValidIdent(k));
|
|
66
|
+
}
|
|
60
67
|
} catch {
|
|
61
68
|
return { code, continue: true };
|
|
62
69
|
}
|
|
63
|
-
const exportKeys = mod && typeof mod === "object" ? Object.keys(mod).filter((k) => k !== "default" && isValidIdent(k)) : [];
|
|
64
70
|
const lines = [];
|
|
71
|
+
lines.push(`// ${WRAP_MARK}`);
|
|
65
72
|
lines.push(`import { createRequire } from 'module';`);
|
|
66
|
-
lines.push(`const _req = createRequire(
|
|
73
|
+
lines.push(`const _req = createRequire(${import_loader.IMPORT_META_URL_VARIABLE});`);
|
|
67
74
|
lines.push(`const _cjs = _req(${JSON.stringify(filename)});`);
|
|
68
75
|
lines.push(
|
|
69
76
|
`const _default = (_cjs && _cjs.__esModule && 'default' in _cjs) ? _cjs.default : _cjs;`
|
|
70
77
|
);
|
|
71
78
|
lines.push(`export default _default;`);
|
|
72
79
|
lines.push(`export const __esModule = true;`);
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
76
|
-
}
|
|
80
|
+
for (const key of exportKeys) {
|
|
81
|
+
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
77
82
|
}
|
|
78
|
-
console.log(lines.join("\n"));
|
|
79
83
|
return {
|
|
80
84
|
code: lines.join("\n"),
|
|
81
85
|
map: void 0,
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { createRequire } from "module";
|
|
3
3
|
import path from "path";
|
|
4
|
+
import {
|
|
5
|
+
IMPORT_META_URL_VARIABLE
|
|
6
|
+
} from "@hirarijs/loader";
|
|
4
7
|
var extensions = [".js", ".cjs"];
|
|
5
8
|
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
9
|
+
var WRAP_MARK = "__HIRARI_CJS_INTEROP_WRAPPED__";
|
|
6
10
|
function runTransform(code, filename, ctx) {
|
|
11
|
+
if (code.includes(WRAP_MARK)) {
|
|
12
|
+
return { code, continue: true };
|
|
13
|
+
}
|
|
7
14
|
const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
|
|
8
15
|
const ignoreNm = opts.ignoreNodeModules !== false && opts.allowNodeModules !== true;
|
|
9
16
|
if (ignoreNm && filename.includes("node_modules")) {
|
|
@@ -12,35 +19,34 @@ function runTransform(code, filename, ctx) {
|
|
|
12
19
|
if (opts.aliases) {
|
|
13
20
|
for (const [key, target] of Object.entries(opts.aliases)) {
|
|
14
21
|
if (filename.includes(`node_modules/${key}`) || filename.includes(`node_modules\\${key}`)) {
|
|
15
|
-
|
|
16
|
-
filename = aliased;
|
|
22
|
+
filename = path.isAbsolute(target) ? target : path.resolve(process.cwd(), target);
|
|
17
23
|
break;
|
|
18
24
|
}
|
|
19
25
|
}
|
|
20
26
|
}
|
|
21
|
-
let
|
|
27
|
+
let exportKeys = [];
|
|
22
28
|
try {
|
|
23
29
|
const req = createRequire(import.meta.url);
|
|
24
|
-
mod = req(filename);
|
|
30
|
+
const mod = req(filename);
|
|
31
|
+
if (mod && typeof mod === "object") {
|
|
32
|
+
exportKeys = Object.keys(mod).filter((k) => k !== "default" && isValidIdent(k));
|
|
33
|
+
}
|
|
25
34
|
} catch {
|
|
26
35
|
return { code, continue: true };
|
|
27
36
|
}
|
|
28
|
-
const exportKeys = mod && typeof mod === "object" ? Object.keys(mod).filter((k) => k !== "default" && isValidIdent(k)) : [];
|
|
29
37
|
const lines = [];
|
|
38
|
+
lines.push(`// ${WRAP_MARK}`);
|
|
30
39
|
lines.push(`import { createRequire } from 'module';`);
|
|
31
|
-
lines.push(`const _req = createRequire(
|
|
40
|
+
lines.push(`const _req = createRequire(${IMPORT_META_URL_VARIABLE});`);
|
|
32
41
|
lines.push(`const _cjs = _req(${JSON.stringify(filename)});`);
|
|
33
42
|
lines.push(
|
|
34
43
|
`const _default = (_cjs && _cjs.__esModule && 'default' in _cjs) ? _cjs.default : _cjs;`
|
|
35
44
|
);
|
|
36
45
|
lines.push(`export default _default;`);
|
|
37
46
|
lines.push(`export const __esModule = true;`);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
41
|
-
}
|
|
47
|
+
for (const key of exportKeys) {
|
|
48
|
+
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
42
49
|
}
|
|
43
|
-
console.log(lines.join("\n"));
|
|
44
50
|
return {
|
|
45
51
|
code: lines.join("\n"),
|
|
46
52
|
map: void 0,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hirarijs/loader-cjs-interop",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "CJS interop loader plugin that exposes named exports for CommonJS modules",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"dist"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
23
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --target es2020 --tsconfig tsconfig.json",
|
|
24
24
|
"test": "node --test"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|