@hirarijs/loader-cjs-interop 1.0.2 → 1.0.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.
- package/dist/index.cjs +24 -19
- package/dist/index.js +24 -19
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -41,7 +41,8 @@ var extensions = [".js", ".cjs"];
|
|
|
41
41
|
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
42
42
|
function runTransform(code, filename, ctx) {
|
|
43
43
|
const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
|
|
44
|
-
|
|
44
|
+
const ignoreNm = opts.ignoreNodeModules !== false && opts.allowNodeModules !== true;
|
|
45
|
+
if (ignoreNm && filename.includes("node_modules")) {
|
|
45
46
|
return { code, continue: true };
|
|
46
47
|
}
|
|
47
48
|
if (opts.aliases) {
|
|
@@ -54,31 +55,35 @@ function runTransform(code, filename, ctx) {
|
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
let mod;
|
|
58
|
+
let isCjs = false;
|
|
57
59
|
try {
|
|
58
60
|
const req = (0, import_module.createRequire)(import_meta.url);
|
|
59
61
|
mod = req(filename);
|
|
62
|
+
isCjs = true;
|
|
60
63
|
} catch {
|
|
61
|
-
|
|
64
|
+
isCjs = false;
|
|
62
65
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
continue;
|
|
66
|
+
if (isCjs) {
|
|
67
|
+
const keys = Object.keys(mod || {}).filter((k) => k !== "default");
|
|
68
|
+
const lines = [];
|
|
69
|
+
lines.push(`import { createRequire } from 'module';`);
|
|
70
|
+
lines.push(
|
|
71
|
+
`const ${import_loader.IMPORT_META_URL_VARIABLE} = import.meta.url; const _cjs = createRequire(${import_loader.IMPORT_META_URL_VARIABLE})(${JSON.stringify(filename)});`
|
|
72
|
+
);
|
|
73
|
+
lines.push(`export default _cjs;`);
|
|
74
|
+
lines.push(`export const __esModule = true;`);
|
|
75
|
+
for (const key of keys) {
|
|
76
|
+
if (!isValidIdent(key)) continue;
|
|
77
|
+
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
74
78
|
}
|
|
75
|
-
|
|
79
|
+
return {
|
|
80
|
+
code: lines.join("\n"),
|
|
81
|
+
map: void 0,
|
|
82
|
+
format: ctx.format,
|
|
83
|
+
continue: opts.continue === true
|
|
84
|
+
};
|
|
76
85
|
}
|
|
77
|
-
return {
|
|
78
|
-
code: lines.join("\n"),
|
|
79
|
-
map: void 0,
|
|
80
|
-
format: ctx.format
|
|
81
|
-
};
|
|
86
|
+
return { code, continue: true };
|
|
82
87
|
}
|
|
83
88
|
var plugin = {
|
|
84
89
|
name: "@hirarijs/loader-cjs-interop",
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,8 @@ var extensions = [".js", ".cjs"];
|
|
|
8
8
|
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
9
9
|
function runTransform(code, filename, ctx) {
|
|
10
10
|
const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
|
|
11
|
-
|
|
11
|
+
const ignoreNm = opts.ignoreNodeModules !== false && opts.allowNodeModules !== true;
|
|
12
|
+
if (ignoreNm && filename.includes("node_modules")) {
|
|
12
13
|
return { code, continue: true };
|
|
13
14
|
}
|
|
14
15
|
if (opts.aliases) {
|
|
@@ -21,31 +22,35 @@ function runTransform(code, filename, ctx) {
|
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
let mod;
|
|
25
|
+
let isCjs = false;
|
|
24
26
|
try {
|
|
25
27
|
const req = createRequire(import.meta.url);
|
|
26
28
|
mod = req(filename);
|
|
29
|
+
isCjs = true;
|
|
27
30
|
} catch {
|
|
28
|
-
|
|
31
|
+
isCjs = false;
|
|
29
32
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
continue;
|
|
33
|
+
if (isCjs) {
|
|
34
|
+
const keys = Object.keys(mod || {}).filter((k) => k !== "default");
|
|
35
|
+
const lines = [];
|
|
36
|
+
lines.push(`import { createRequire } from 'module';`);
|
|
37
|
+
lines.push(
|
|
38
|
+
`const ${IMPORT_META_URL_VARIABLE} = import.meta.url; const _cjs = createRequire(${IMPORT_META_URL_VARIABLE})(${JSON.stringify(filename)});`
|
|
39
|
+
);
|
|
40
|
+
lines.push(`export default _cjs;`);
|
|
41
|
+
lines.push(`export const __esModule = true;`);
|
|
42
|
+
for (const key of keys) {
|
|
43
|
+
if (!isValidIdent(key)) continue;
|
|
44
|
+
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
41
45
|
}
|
|
42
|
-
|
|
46
|
+
return {
|
|
47
|
+
code: lines.join("\n"),
|
|
48
|
+
map: void 0,
|
|
49
|
+
format: ctx.format,
|
|
50
|
+
continue: opts.continue === true
|
|
51
|
+
};
|
|
43
52
|
}
|
|
44
|
-
return {
|
|
45
|
-
code: lines.join("\n"),
|
|
46
|
-
map: void 0,
|
|
47
|
-
format: ctx.format
|
|
48
|
-
};
|
|
53
|
+
return { code, continue: true };
|
|
49
54
|
}
|
|
50
55
|
var plugin = {
|
|
51
56
|
name: "@hirarijs/loader-cjs-interop",
|