@hirarijs/loader-cjs-interop 1.0.0 → 1.0.2
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 +27 -4
- package/dist/index.js +17 -4
- package/package.json +4 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -24,17 +34,31 @@ __export(index_exports, {
|
|
|
24
34
|
});
|
|
25
35
|
module.exports = __toCommonJS(index_exports);
|
|
26
36
|
var import_module = require("module");
|
|
37
|
+
var import_path = __toESM(require("path"), 1);
|
|
27
38
|
var import_loader = require("@hirarijs/loader");
|
|
28
39
|
var import_meta = {};
|
|
29
40
|
var extensions = [".js", ".cjs"];
|
|
30
41
|
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
31
42
|
function runTransform(code, filename, ctx) {
|
|
43
|
+
const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
|
|
44
|
+
if (filename.includes("node_modules") && opts.allowNodeModules !== true) {
|
|
45
|
+
return { code, continue: true };
|
|
46
|
+
}
|
|
47
|
+
if (opts.aliases) {
|
|
48
|
+
for (const [key, target] of Object.entries(opts.aliases)) {
|
|
49
|
+
if (filename.includes(`node_modules/${key}`) || filename.includes(`node_modules\\${key}`)) {
|
|
50
|
+
const aliased = import_path.default.isAbsolute(target) ? target : import_path.default.resolve(process.cwd(), target);
|
|
51
|
+
filename = aliased;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
32
56
|
let mod;
|
|
33
57
|
try {
|
|
34
58
|
const req = (0, import_module.createRequire)(import_meta.url);
|
|
35
59
|
mod = req(filename);
|
|
36
60
|
} catch {
|
|
37
|
-
return { code };
|
|
61
|
+
return { code, continue: true };
|
|
38
62
|
}
|
|
39
63
|
const keys = Object.keys(mod || {}).filter((k) => k !== "default");
|
|
40
64
|
const lines = [];
|
|
@@ -59,9 +83,8 @@ function runTransform(code, filename, ctx) {
|
|
|
59
83
|
var plugin = {
|
|
60
84
|
name: "@hirarijs/loader-cjs-interop",
|
|
61
85
|
extensions,
|
|
62
|
-
match
|
|
63
|
-
|
|
64
|
-
return !filename.includes("node_modules");
|
|
86
|
+
match(filename) {
|
|
87
|
+
return extensions.some((ext) => filename.endsWith(ext));
|
|
65
88
|
},
|
|
66
89
|
transform: runTransform
|
|
67
90
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { createRequire } from "module";
|
|
3
|
+
import path from "path";
|
|
3
4
|
import {
|
|
4
5
|
IMPORT_META_URL_VARIABLE
|
|
5
6
|
} from "@hirarijs/loader";
|
|
6
7
|
var extensions = [".js", ".cjs"];
|
|
7
8
|
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
8
9
|
function runTransform(code, filename, ctx) {
|
|
10
|
+
const opts = ctx.loaderConfig.pluginOptions?.["@hirarijs/loader-cjs-interop"] || {};
|
|
11
|
+
if (filename.includes("node_modules") && opts.allowNodeModules !== true) {
|
|
12
|
+
return { code, continue: true };
|
|
13
|
+
}
|
|
14
|
+
if (opts.aliases) {
|
|
15
|
+
for (const [key, target] of Object.entries(opts.aliases)) {
|
|
16
|
+
if (filename.includes(`node_modules/${key}`) || filename.includes(`node_modules\\${key}`)) {
|
|
17
|
+
const aliased = path.isAbsolute(target) ? target : path.resolve(process.cwd(), target);
|
|
18
|
+
filename = aliased;
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
9
23
|
let mod;
|
|
10
24
|
try {
|
|
11
25
|
const req = createRequire(import.meta.url);
|
|
12
26
|
mod = req(filename);
|
|
13
27
|
} catch {
|
|
14
|
-
return { code };
|
|
28
|
+
return { code, continue: true };
|
|
15
29
|
}
|
|
16
30
|
const keys = Object.keys(mod || {}).filter((k) => k !== "default");
|
|
17
31
|
const lines = [];
|
|
@@ -36,9 +50,8 @@ function runTransform(code, filename, ctx) {
|
|
|
36
50
|
var plugin = {
|
|
37
51
|
name: "@hirarijs/loader-cjs-interop",
|
|
38
52
|
extensions,
|
|
39
|
-
match
|
|
40
|
-
|
|
41
|
-
return !filename.includes("node_modules");
|
|
53
|
+
match(filename) {
|
|
54
|
+
return extensions.some((ext) => filename.endsWith(ext));
|
|
42
55
|
},
|
|
43
56
|
transform: runTransform
|
|
44
57
|
};
|
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.2",
|
|
4
4
|
"description": "CJS interop loader plugin that exposes named exports for CommonJS modules",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
"types": "./dist/index.d.ts"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
16
19
|
"files": [
|
|
17
20
|
"dist"
|
|
18
21
|
],
|