@hirarijs/loader-cjs-interop 1.0.0
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 +68 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +48 -0
- package/package.json +31 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
default: () => index_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_module = require("module");
|
|
27
|
+
var import_loader = require("@hirarijs/loader");
|
|
28
|
+
var import_meta = {};
|
|
29
|
+
var extensions = [".js", ".cjs"];
|
|
30
|
+
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
31
|
+
function runTransform(code, filename, ctx) {
|
|
32
|
+
let mod;
|
|
33
|
+
try {
|
|
34
|
+
const req = (0, import_module.createRequire)(import_meta.url);
|
|
35
|
+
mod = req(filename);
|
|
36
|
+
} catch {
|
|
37
|
+
return { code };
|
|
38
|
+
}
|
|
39
|
+
const keys = Object.keys(mod || {}).filter((k) => k !== "default");
|
|
40
|
+
const lines = [];
|
|
41
|
+
lines.push(`import { createRequire } from 'module';`);
|
|
42
|
+
lines.push(
|
|
43
|
+
`const ${import_loader.IMPORT_META_URL_VARIABLE} = import.meta.url; const _cjs = createRequire(${import_loader.IMPORT_META_URL_VARIABLE})(${JSON.stringify(filename)});`
|
|
44
|
+
);
|
|
45
|
+
lines.push(`export default _cjs;`);
|
|
46
|
+
lines.push(`export const __esModule = true;`);
|
|
47
|
+
for (const key of keys) {
|
|
48
|
+
if (!isValidIdent(key)) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
code: lines.join("\n"),
|
|
55
|
+
map: void 0,
|
|
56
|
+
format: ctx.format
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
var plugin = {
|
|
60
|
+
name: "@hirarijs/loader-cjs-interop",
|
|
61
|
+
extensions,
|
|
62
|
+
match: (filename) => {
|
|
63
|
+
if (!extensions.some((ext) => filename.endsWith(ext))) return false;
|
|
64
|
+
return !filename.includes("node_modules");
|
|
65
|
+
},
|
|
66
|
+
transform: runTransform
|
|
67
|
+
};
|
|
68
|
+
var index_default = plugin;
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import {
|
|
4
|
+
IMPORT_META_URL_VARIABLE
|
|
5
|
+
} from "@hirarijs/loader";
|
|
6
|
+
var extensions = [".js", ".cjs"];
|
|
7
|
+
var isValidIdent = (name) => /^[A-Za-z_$][0-9A-Za-z_$]*$/.test(name);
|
|
8
|
+
function runTransform(code, filename, ctx) {
|
|
9
|
+
let mod;
|
|
10
|
+
try {
|
|
11
|
+
const req = createRequire(import.meta.url);
|
|
12
|
+
mod = req(filename);
|
|
13
|
+
} catch {
|
|
14
|
+
return { code };
|
|
15
|
+
}
|
|
16
|
+
const keys = Object.keys(mod || {}).filter((k) => k !== "default");
|
|
17
|
+
const lines = [];
|
|
18
|
+
lines.push(`import { createRequire } from 'module';`);
|
|
19
|
+
lines.push(
|
|
20
|
+
`const ${IMPORT_META_URL_VARIABLE} = import.meta.url; const _cjs = createRequire(${IMPORT_META_URL_VARIABLE})(${JSON.stringify(filename)});`
|
|
21
|
+
);
|
|
22
|
+
lines.push(`export default _cjs;`);
|
|
23
|
+
lines.push(`export const __esModule = true;`);
|
|
24
|
+
for (const key of keys) {
|
|
25
|
+
if (!isValidIdent(key)) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
lines.push(`export const ${key} = _cjs[${JSON.stringify(key)}];`);
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
code: lines.join("\n"),
|
|
32
|
+
map: void 0,
|
|
33
|
+
format: ctx.format
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
var plugin = {
|
|
37
|
+
name: "@hirarijs/loader-cjs-interop",
|
|
38
|
+
extensions,
|
|
39
|
+
match: (filename) => {
|
|
40
|
+
if (!extensions.some((ext) => filename.endsWith(ext))) return false;
|
|
41
|
+
return !filename.includes("node_modules");
|
|
42
|
+
},
|
|
43
|
+
transform: runTransform
|
|
44
|
+
};
|
|
45
|
+
var index_default = plugin;
|
|
46
|
+
export {
|
|
47
|
+
index_default as default
|
|
48
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hirarijs/loader-cjs-interop",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CJS interop loader plugin that exposes named exports for CommonJS modules",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
21
|
+
"test": "node --test"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@hirarijs/loader": "*"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@hirarijs/loader": "workspace:*",
|
|
28
|
+
"tsup": "^8.0.1",
|
|
29
|
+
"typescript": "^5.3.3"
|
|
30
|
+
}
|
|
31
|
+
}
|