@modern-js/node-bundle-require 2.35.0 → 2.36.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 +60 -43
- package/dist/cjs/index.js +34 -27
- package/dist/esm/bundle.js +12 -8
- package/dist/esm/index.js +8 -6
- 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,26 +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
|
-
var _options, _options1, _options2, _options3;
|
|
52
63
|
if (!JS_EXT_RE.test(filepath)) {
|
|
53
64
|
throw new Error(`${filepath} is not a valid JS file`);
|
|
54
65
|
}
|
|
55
66
|
debug("bundle", filepath, options);
|
|
56
|
-
const getOutputFile = (
|
|
57
|
-
const outfile = await getOutputFile(
|
|
67
|
+
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
68
|
+
const outfile = await getOutputFile(import_path.default.basename(filepath));
|
|
58
69
|
const esbuildOptions = {
|
|
59
70
|
entryPoints: [
|
|
60
71
|
filepath
|
|
@@ -67,14 +78,14 @@ async function bundle(filepath, options) {
|
|
|
67
78
|
// sets `target: "es5"`
|
|
68
79
|
// reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
|
|
69
80
|
target: "esnext",
|
|
70
|
-
...
|
|
81
|
+
...options === null || options === void 0 ? void 0 : options.esbuildOptions,
|
|
71
82
|
plugins: [
|
|
72
|
-
...(
|
|
83
|
+
...(options === null || options === void 0 ? void 0 : options.esbuildPlugins) || [],
|
|
73
84
|
// https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
74
85
|
{
|
|
75
86
|
name: "native-node-modules",
|
|
76
|
-
setup(
|
|
77
|
-
|
|
87
|
+
setup(build2) {
|
|
88
|
+
build2.onResolve({
|
|
78
89
|
filter: /\.node$/,
|
|
79
90
|
namespace: "file"
|
|
80
91
|
}, (args) => ({
|
|
@@ -85,7 +96,7 @@ async function bundle(filepath, options) {
|
|
|
85
96
|
}),
|
|
86
97
|
namespace: "node-file"
|
|
87
98
|
}));
|
|
88
|
-
|
|
99
|
+
build2.onLoad({
|
|
89
100
|
filter: /.*/,
|
|
90
101
|
namespace: "node-file"
|
|
91
102
|
}, (args) => ({
|
|
@@ -95,14 +106,14 @@ async function bundle(filepath, options) {
|
|
|
95
106
|
catch {}
|
|
96
107
|
`
|
|
97
108
|
}));
|
|
98
|
-
|
|
109
|
+
build2.onResolve({
|
|
99
110
|
filter: /\.node$/,
|
|
100
111
|
namespace: "node-file"
|
|
101
112
|
}, (args) => ({
|
|
102
113
|
path: args.path,
|
|
103
114
|
namespace: "file"
|
|
104
115
|
}));
|
|
105
|
-
const opts =
|
|
116
|
+
const opts = build2.initialOptions;
|
|
106
117
|
opts.loader = opts.loader || {};
|
|
107
118
|
opts.loader[".node"] = "file";
|
|
108
119
|
}
|
|
@@ -113,10 +124,10 @@ async function bundle(filepath, options) {
|
|
|
113
124
|
ctx.onLoad({
|
|
114
125
|
filter: JS_EXT_RE
|
|
115
126
|
}, async (args) => {
|
|
116
|
-
const contents =
|
|
127
|
+
const contents = import_utils.fs.readFileSync(args.path, "utf-8");
|
|
117
128
|
return {
|
|
118
|
-
contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(
|
|
119
|
-
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))
|
|
120
131
|
};
|
|
121
132
|
});
|
|
122
133
|
}
|
|
@@ -154,12 +165,18 @@ async function bundle(filepath, options) {
|
|
|
154
165
|
}
|
|
155
166
|
]
|
|
156
167
|
};
|
|
157
|
-
if (
|
|
158
|
-
const ctx = await (0,
|
|
168
|
+
if (options === null || options === void 0 ? void 0 : options.watch) {
|
|
169
|
+
const ctx = await (0, import_esbuild.context)(esbuildOptions);
|
|
159
170
|
await ctx.rebuild();
|
|
160
171
|
await ctx.watch();
|
|
161
172
|
} else {
|
|
162
|
-
await (0,
|
|
173
|
+
await (0, import_esbuild.build)(esbuildOptions);
|
|
163
174
|
}
|
|
164
175
|
return outfile;
|
|
165
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,40 +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
|
-
|
|
27
|
-
const configFile = await (0, _bundle.bundle)(filepath, options);
|
|
29
|
+
const configFile = await (0, import_bundle.bundle)(filepath, options);
|
|
28
30
|
let mod;
|
|
29
|
-
const req = (
|
|
31
|
+
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
30
32
|
try {
|
|
31
33
|
mod = await req(configFile);
|
|
32
|
-
(0,
|
|
34
|
+
(0, import_utils.deleteRequireCache)(configFile);
|
|
33
35
|
} finally {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
_utils.fs.unlinkSync(configFile);
|
|
36
|
+
if ((options === null || options === void 0 ? void 0 : options.autoClear) === void 0 || options.autoClear) {
|
|
37
|
+
import_utils.fs.unlinkSync(configFile);
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
return mod;
|
|
40
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,14 +23,13 @@ async function isTypeModulePkg(cwd) {
|
|
|
23
23
|
}
|
|
24
24
|
return false;
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var _options, _options1, _options2, _options3;
|
|
26
|
+
const defaultGetOutputFile = async (filepath) => path.resolve(CONFIG_CACHE_DIR, `${filepath.replace(/\.(js|ts)/, "")}.${nanoid(8)}.cjs`);
|
|
27
|
+
async function bundle(filepath, options) {
|
|
29
28
|
if (!JS_EXT_RE.test(filepath)) {
|
|
30
29
|
throw new Error(`${filepath} is not a valid JS file`);
|
|
31
30
|
}
|
|
32
31
|
debug("bundle", filepath, options);
|
|
33
|
-
const getOutputFile = (
|
|
32
|
+
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
34
33
|
const outfile = await getOutputFile(path.basename(filepath));
|
|
35
34
|
const esbuildOptions = {
|
|
36
35
|
entryPoints: [
|
|
@@ -44,9 +43,9 @@ export async function bundle(filepath, options) {
|
|
|
44
43
|
// sets `target: "es5"`
|
|
45
44
|
// reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
|
|
46
45
|
target: "esnext",
|
|
47
|
-
...
|
|
46
|
+
...options === null || options === void 0 ? void 0 : options.esbuildOptions,
|
|
48
47
|
plugins: [
|
|
49
|
-
...(
|
|
48
|
+
...(options === null || options === void 0 ? void 0 : options.esbuildPlugins) || [],
|
|
50
49
|
// https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
51
50
|
{
|
|
52
51
|
name: "native-node-modules",
|
|
@@ -131,7 +130,7 @@ export async function bundle(filepath, options) {
|
|
|
131
130
|
}
|
|
132
131
|
]
|
|
133
132
|
};
|
|
134
|
-
if (
|
|
133
|
+
if (options === null || options === void 0 ? void 0 : options.watch) {
|
|
135
134
|
const ctx = await context(esbuildOptions);
|
|
136
135
|
await ctx.rebuild();
|
|
137
136
|
await ctx.watch();
|
|
@@ -140,3 +139,8 @@ export async function bundle(filepath, options) {
|
|
|
140
139
|
}
|
|
141
140
|
return outfile;
|
|
142
141
|
}
|
|
142
|
+
export {
|
|
143
|
+
EXTERNAL_REGEXP,
|
|
144
|
+
bundle,
|
|
145
|
+
defaultGetOutputFile
|
|
146
|
+
};
|
package/dist/esm/index.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { fs, deleteRequireCache } from "@modern-js/utils";
|
|
2
2
|
import { bundle, defaultGetOutputFile } from "./bundle";
|
|
3
|
-
|
|
4
|
-
export async function bundleRequire(filepath, options) {
|
|
5
|
-
var _options;
|
|
3
|
+
async function bundleRequire(filepath, options) {
|
|
6
4
|
const configFile = await bundle(filepath, options);
|
|
7
5
|
let mod;
|
|
8
|
-
const req = (
|
|
6
|
+
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
9
7
|
try {
|
|
10
8
|
mod = await req(configFile);
|
|
11
9
|
deleteRequireCache(configFile);
|
|
12
10
|
} finally {
|
|
13
|
-
|
|
14
|
-
if (((_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.autoClear) === void 0 || options.autoClear) {
|
|
11
|
+
if ((options === null || options === void 0 ? void 0 : options.autoClear) === void 0 || options.autoClear) {
|
|
15
12
|
fs.unlinkSync(configFile);
|
|
16
13
|
}
|
|
17
14
|
}
|
|
18
15
|
return mod;
|
|
19
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.36.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.36.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.36.0",
|
|
58
|
+
"@scripts/jest-config": "2.36.0"
|
|
59
59
|
},
|
|
60
60
|
"sideEffects": false,
|
|
61
61
|
"publishConfig": {
|