@modern-js/node-bundle-require 2.35.1 → 2.37.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/cjs/bundle.js +56 -38
- package/dist/cjs/index.js +32 -23
- package/dist/esm/bundle.js +8 -3
- package/dist/esm/index.js +6 -2
- package/package.json +4 -4
package/dist/cjs/bundle.js
CHANGED
|
@@ -1,30 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
6
9
|
for (var name in all)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return EXTERNAL_REGEXP;
|
|
15
|
-
},
|
|
16
|
-
defaultGetOutputFile: function() {
|
|
17
|
-
return defaultGetOutputFile;
|
|
18
|
-
},
|
|
19
|
-
bundle: function() {
|
|
20
|
-
return bundle;
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
17
|
}
|
|
18
|
+
return to;
|
|
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
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var bundle_exports = {};
|
|
30
|
+
__export(bundle_exports, {
|
|
31
|
+
EXTERNAL_REGEXP: () => EXTERNAL_REGEXP,
|
|
32
|
+
bundle: () => bundle,
|
|
33
|
+
defaultGetOutputFile: () => defaultGetOutputFile
|
|
22
34
|
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const debug = (0,
|
|
35
|
+
module.exports = __toCommonJS(bundle_exports);
|
|
36
|
+
var import_path = __toESM(require("path"));
|
|
37
|
+
var import_utils = require("@modern-js/utils");
|
|
38
|
+
var import_esbuild = require("esbuild");
|
|
39
|
+
const debug = (0, import_utils.createDebugger)("node-bundle");
|
|
28
40
|
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
29
41
|
const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
|
|
30
42
|
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
@@ -35,25 +47,25 @@ function inferLoader(ext) {
|
|
|
35
47
|
return ext.slice(1);
|
|
36
48
|
}
|
|
37
49
|
async function isTypeModulePkg(cwd) {
|
|
38
|
-
const pkgJsonPath = await (0,
|
|
50
|
+
const pkgJsonPath = await (0, import_utils.pkgUp)({
|
|
39
51
|
cwd
|
|
40
52
|
});
|
|
41
53
|
if (pkgJsonPath) {
|
|
42
54
|
var _pkgJson_main;
|
|
43
|
-
const pkgJson = await
|
|
44
|
-
const ext =
|
|
55
|
+
const pkgJson = await import_utils.fs.readJSON(pkgJsonPath);
|
|
56
|
+
const ext = import_path.default.extname(cwd);
|
|
45
57
|
return pkgJson.type === "module" && ext !== ".cjs" && !((_pkgJson_main = pkgJson.main) === null || _pkgJson_main === void 0 ? void 0 : _pkgJson_main.endsWith(".cjs"));
|
|
46
58
|
}
|
|
47
59
|
return false;
|
|
48
60
|
}
|
|
49
|
-
const defaultGetOutputFile = async (filepath) =>
|
|
61
|
+
const defaultGetOutputFile = async (filepath) => import_path.default.resolve(import_utils.CONFIG_CACHE_DIR, `${filepath.replace(/\.(js|ts)/, "")}.${(0, import_utils.nanoid)(8)}.cjs`);
|
|
50
62
|
async function bundle(filepath, options) {
|
|
51
63
|
if (!JS_EXT_RE.test(filepath)) {
|
|
52
64
|
throw new Error(`${filepath} is not a valid JS file`);
|
|
53
65
|
}
|
|
54
66
|
debug("bundle", filepath, options);
|
|
55
67
|
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
56
|
-
const outfile = await getOutputFile(
|
|
68
|
+
const outfile = await getOutputFile(import_path.default.basename(filepath));
|
|
57
69
|
const esbuildOptions = {
|
|
58
70
|
entryPoints: [
|
|
59
71
|
filepath
|
|
@@ -72,8 +84,8 @@ async function bundle(filepath, options) {
|
|
|
72
84
|
// https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
73
85
|
{
|
|
74
86
|
name: "native-node-modules",
|
|
75
|
-
setup(
|
|
76
|
-
|
|
87
|
+
setup(build2) {
|
|
88
|
+
build2.onResolve({
|
|
77
89
|
filter: /\.node$/,
|
|
78
90
|
namespace: "file"
|
|
79
91
|
}, (args) => ({
|
|
@@ -84,7 +96,7 @@ async function bundle(filepath, options) {
|
|
|
84
96
|
}),
|
|
85
97
|
namespace: "node-file"
|
|
86
98
|
}));
|
|
87
|
-
|
|
99
|
+
build2.onLoad({
|
|
88
100
|
filter: /.*/,
|
|
89
101
|
namespace: "node-file"
|
|
90
102
|
}, (args) => ({
|
|
@@ -94,14 +106,14 @@ async function bundle(filepath, options) {
|
|
|
94
106
|
catch {}
|
|
95
107
|
`
|
|
96
108
|
}));
|
|
97
|
-
|
|
109
|
+
build2.onResolve({
|
|
98
110
|
filter: /\.node$/,
|
|
99
111
|
namespace: "node-file"
|
|
100
112
|
}, (args) => ({
|
|
101
113
|
path: args.path,
|
|
102
114
|
namespace: "file"
|
|
103
115
|
}));
|
|
104
|
-
const opts =
|
|
116
|
+
const opts = build2.initialOptions;
|
|
105
117
|
opts.loader = opts.loader || {};
|
|
106
118
|
opts.loader[".node"] = "file";
|
|
107
119
|
}
|
|
@@ -112,10 +124,10 @@ async function bundle(filepath, options) {
|
|
|
112
124
|
ctx.onLoad({
|
|
113
125
|
filter: JS_EXT_RE
|
|
114
126
|
}, async (args) => {
|
|
115
|
-
const contents =
|
|
127
|
+
const contents = import_utils.fs.readFileSync(args.path, "utf-8");
|
|
116
128
|
return {
|
|
117
|
-
contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(
|
|
118
|
-
loader: inferLoader(
|
|
129
|
+
contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(import_path.default.dirname(args.path))).replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`)),
|
|
130
|
+
loader: inferLoader(import_path.default.extname(args.path))
|
|
119
131
|
};
|
|
120
132
|
});
|
|
121
133
|
}
|
|
@@ -154,11 +166,17 @@ async function bundle(filepath, options) {
|
|
|
154
166
|
]
|
|
155
167
|
};
|
|
156
168
|
if (options === null || options === void 0 ? void 0 : options.watch) {
|
|
157
|
-
const ctx = await (0,
|
|
169
|
+
const ctx = await (0, import_esbuild.context)(esbuildOptions);
|
|
158
170
|
await ctx.rebuild();
|
|
159
171
|
await ctx.watch();
|
|
160
172
|
} else {
|
|
161
|
-
await (0,
|
|
173
|
+
await (0, import_esbuild.build)(esbuildOptions);
|
|
162
174
|
}
|
|
163
175
|
return outfile;
|
|
164
176
|
}
|
|
177
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
+
0 && (module.exports = {
|
|
179
|
+
EXTERNAL_REGEXP,
|
|
180
|
+
bundle,
|
|
181
|
+
defaultGetOutputFile
|
|
182
|
+
});
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,38 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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) => {
|
|
6
7
|
for (var name in all)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return _bundle.bundle;
|
|
15
|
-
},
|
|
16
|
-
defaultGetOutputFile: function() {
|
|
17
|
-
return _bundle.defaultGetOutputFile;
|
|
18
|
-
},
|
|
19
|
-
bundleRequire: function() {
|
|
20
|
-
return bundleRequire;
|
|
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 });
|
|
21
15
|
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var src_exports = {};
|
|
20
|
+
__export(src_exports, {
|
|
21
|
+
bundle: () => import_bundle.bundle,
|
|
22
|
+
bundleRequire: () => bundleRequire,
|
|
23
|
+
defaultGetOutputFile: () => import_bundle.defaultGetOutputFile
|
|
22
24
|
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var import_utils = require("@modern-js/utils");
|
|
27
|
+
var import_bundle = require("./bundle");
|
|
25
28
|
async function bundleRequire(filepath, options) {
|
|
26
|
-
const configFile = await (0,
|
|
29
|
+
const configFile = await (0, import_bundle.bundle)(filepath, options);
|
|
27
30
|
let mod;
|
|
28
31
|
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
29
32
|
try {
|
|
30
33
|
mod = await req(configFile);
|
|
31
|
-
(0,
|
|
34
|
+
(0, import_utils.deleteRequireCache)(configFile);
|
|
32
35
|
} finally {
|
|
33
36
|
if ((options === null || options === void 0 ? void 0 : options.autoClear) === void 0 || options.autoClear) {
|
|
34
|
-
|
|
37
|
+
import_utils.fs.unlinkSync(configFile);
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
return mod;
|
|
38
41
|
}
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
bundle,
|
|
45
|
+
bundleRequire,
|
|
46
|
+
defaultGetOutputFile
|
|
47
|
+
});
|
package/dist/esm/bundle.js
CHANGED
|
@@ -4,7 +4,7 @@ import { build, context } from "esbuild";
|
|
|
4
4
|
const debug = createDebugger("node-bundle");
|
|
5
5
|
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
6
6
|
const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
|
|
7
|
-
|
|
7
|
+
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
8
8
|
function inferLoader(ext) {
|
|
9
9
|
if (ext === ".mjs" || ext === ".cjs") {
|
|
10
10
|
return "js";
|
|
@@ -23,8 +23,8 @@ async function isTypeModulePkg(cwd) {
|
|
|
23
23
|
}
|
|
24
24
|
return false;
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const defaultGetOutputFile = async (filepath) => path.resolve(CONFIG_CACHE_DIR, `${filepath.replace(/\.(js|ts)/, "")}.${nanoid(8)}.cjs`);
|
|
27
|
+
async function bundle(filepath, options) {
|
|
28
28
|
if (!JS_EXT_RE.test(filepath)) {
|
|
29
29
|
throw new Error(`${filepath} is not a valid JS file`);
|
|
30
30
|
}
|
|
@@ -139,3 +139,8 @@ export async function bundle(filepath, options) {
|
|
|
139
139
|
}
|
|
140
140
|
return outfile;
|
|
141
141
|
}
|
|
142
|
+
export {
|
|
143
|
+
EXTERNAL_REGEXP,
|
|
144
|
+
bundle,
|
|
145
|
+
defaultGetOutputFile
|
|
146
|
+
};
|
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { fs, deleteRequireCache } from "@modern-js/utils";
|
|
2
2
|
import { bundle, defaultGetOutputFile } from "./bundle";
|
|
3
|
-
|
|
4
|
-
export async function bundleRequire(filepath, options) {
|
|
3
|
+
async function bundleRequire(filepath, options) {
|
|
5
4
|
const configFile = await bundle(filepath, options);
|
|
6
5
|
let mod;
|
|
7
6
|
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
@@ -15,3 +14,8 @@ export async function bundleRequire(filepath, options) {
|
|
|
15
14
|
}
|
|
16
15
|
return mod;
|
|
17
16
|
}
|
|
17
|
+
export {
|
|
18
|
+
bundle,
|
|
19
|
+
bundleRequire,
|
|
20
|
+
defaultGetOutputFile
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modern-js/node-bundle-require",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.37.0",
|
|
4
4
|
"description": "A Progressive React Framework for modern web development.",
|
|
5
5
|
"homepage": "https://modernjs.dev",
|
|
6
6
|
"bugs": "https://github.com/web-infra-dev/modern.js/issues",
|
|
@@ -47,15 +47,15 @@
|
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"esbuild": "0.17.19",
|
|
49
49
|
"@swc/helpers": "0.5.1",
|
|
50
|
-
"@modern-js/utils": "2.
|
|
50
|
+
"@modern-js/utils": "2.37.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/jest": "^29",
|
|
54
54
|
"@types/node": "^14",
|
|
55
55
|
"jest": "^29",
|
|
56
56
|
"typescript": "^5",
|
|
57
|
-
"@scripts/build": "2.
|
|
58
|
-
"@scripts/jest-config": "2.
|
|
57
|
+
"@scripts/build": "2.37.0",
|
|
58
|
+
"@scripts/jest-config": "2.37.0"
|
|
59
59
|
},
|
|
60
60
|
"sideEffects": false,
|
|
61
61
|
"publishConfig": {
|