@modern-js/node-bundle-require 2.0.0-beta.3 → 2.0.0-beta.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/CHANGELOG.md +34 -0
- package/dist/js/modern/bundle.js +156 -129
- package/dist/js/modern/index.js +42 -21
- package/dist/js/modern/modern-app-env.d.js +0 -0
- package/dist/js/node/bundle.js +173 -138
- package/dist/js/node/index.js +60 -37
- package/dist/js/node/modern-app-env.d.js +0 -0
- package/dist/types/bundle.d.ts +4 -0
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @modern-js/node-bundle-require
|
|
2
2
|
|
|
3
|
+
## 2.0.0-beta.4
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- dda38c9c3e: chore: v2
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- a2509bfbdb: feat: bump esbuild from 0.14.38 to 0.15.7
|
|
12
|
+
|
|
13
|
+
feat: 将 esbuild 从 0.14.38 版本升级至 0.15.7 版本
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [7879e8f]
|
|
16
|
+
- Updated dependencies [6aca875]
|
|
17
|
+
- Updated dependencies [2e6031955e]
|
|
18
|
+
- Updated dependencies [7b7d12c]
|
|
19
|
+
- Updated dependencies [92f0eade39]
|
|
20
|
+
- Updated dependencies [edd1cfb1af]
|
|
21
|
+
- Updated dependencies [cc971eabfc]
|
|
22
|
+
- Updated dependencies [5b9049f2e9]
|
|
23
|
+
- Updated dependencies [92004d1906]
|
|
24
|
+
- Updated dependencies [b8bbe036c7]
|
|
25
|
+
- Updated dependencies [d5a31df781]
|
|
26
|
+
- Updated dependencies [dda38c9c3e]
|
|
27
|
+
- Updated dependencies [3bbea92b2a]
|
|
28
|
+
- Updated dependencies [b710adb843]
|
|
29
|
+
- Updated dependencies [ea7cf06]
|
|
30
|
+
- Updated dependencies [bbe4c4a]
|
|
31
|
+
- Updated dependencies [e4558a0]
|
|
32
|
+
- Updated dependencies [abf3421a75]
|
|
33
|
+
- Updated dependencies [543be9558e]
|
|
34
|
+
- Updated dependencies [14b712da84]
|
|
35
|
+
- @modern-js/utils@2.0.0-beta.4
|
|
36
|
+
|
|
3
37
|
## 2.0.0-beta.3
|
|
4
38
|
|
|
5
39
|
### Major Changes
|
package/dist/js/modern/bundle.js
CHANGED
|
@@ -1,145 +1,172 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
import path from "path";
|
|
41
|
+
import {
|
|
42
|
+
fs,
|
|
43
|
+
pkgUp,
|
|
44
|
+
nanoid,
|
|
45
|
+
CONFIG_CACHE_DIR,
|
|
46
|
+
createDebugger
|
|
47
|
+
} from "@modern-js/utils";
|
|
48
|
+
import { build } from "esbuild";
|
|
49
|
+
const debug = createDebugger("node-bundle");
|
|
8
50
|
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
9
51
|
const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
|
|
10
|
-
|
|
11
|
-
// Must not start with "/" or "./" or "../"
|
|
12
|
-
// "/test/node_modules/foo"
|
|
13
|
-
// "c:/node_modules/foo"
|
|
14
|
-
export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
52
|
+
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
15
53
|
function inferLoader(ext) {
|
|
16
|
-
if (ext ===
|
|
17
|
-
return
|
|
54
|
+
if (ext === ".mjs" || ext === ".cjs") {
|
|
55
|
+
return "js";
|
|
18
56
|
}
|
|
19
57
|
return ext.slice(1);
|
|
20
58
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
cwd
|
|
59
|
+
function isTypeModulePkg(cwd) {
|
|
60
|
+
return __async(this, null, function* () {
|
|
61
|
+
const pkgJsonPath = yield pkgUp({ cwd });
|
|
62
|
+
if (pkgJsonPath) {
|
|
63
|
+
const pkgJson = yield fs.readJSON(pkgJsonPath);
|
|
64
|
+
return pkgJson.type === "module";
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
24
67
|
});
|
|
25
|
-
if (pkgJsonPath) {
|
|
26
|
-
const pkgJson = await fs.readJSON(pkgJsonPath);
|
|
27
|
-
return pkgJson.type === 'module';
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
68
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
{
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
namespace: 'node-file'
|
|
65
|
-
}));
|
|
66
|
-
|
|
67
|
-
// Files in the "node-file" virtual namespace call "require()" on the
|
|
68
|
-
// path from esbuild of the ".node" file in the output directory.
|
|
69
|
-
build.onLoad({
|
|
70
|
-
filter: /.*/,
|
|
71
|
-
namespace: 'node-file'
|
|
72
|
-
}, args => ({
|
|
73
|
-
contents: `
|
|
69
|
+
const defaultGetOutputFile = (filepath) => __async(void 0, null, function* () {
|
|
70
|
+
return path.resolve(
|
|
71
|
+
CONFIG_CACHE_DIR,
|
|
72
|
+
`${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
function bundle(filepath, options) {
|
|
76
|
+
return __async(this, null, function* () {
|
|
77
|
+
if (!JS_EXT_RE.test(filepath)) {
|
|
78
|
+
throw new Error(`${filepath} is not a valid JS file`);
|
|
79
|
+
}
|
|
80
|
+
debug("bundle", filepath, options);
|
|
81
|
+
const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
82
|
+
const outfile = yield getOutputFile(path.basename(filepath));
|
|
83
|
+
yield build(__spreadProps(__spreadValues({
|
|
84
|
+
entryPoints: [filepath],
|
|
85
|
+
outfile,
|
|
86
|
+
format: "cjs",
|
|
87
|
+
platform: "node",
|
|
88
|
+
bundle: true,
|
|
89
|
+
target: "esnext"
|
|
90
|
+
}, options == null ? void 0 : options.esbuildOptions), {
|
|
91
|
+
plugins: [
|
|
92
|
+
...(options == null ? void 0 : options.esbuildPlugins) || [],
|
|
93
|
+
{
|
|
94
|
+
name: "native-node-modules",
|
|
95
|
+
setup(build2) {
|
|
96
|
+
build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
|
|
97
|
+
path: require.resolve(args.path, { paths: [args.resolveDir] }),
|
|
98
|
+
namespace: "node-file"
|
|
99
|
+
}));
|
|
100
|
+
build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
|
|
101
|
+
contents: `
|
|
74
102
|
import path from ${JSON.stringify(args.path)}
|
|
75
103
|
try { module.exports = require(path) }
|
|
76
104
|
catch {}
|
|
77
105
|
`
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}));
|
|
90
|
-
|
|
91
|
-
// Tell esbuild's default loading behavior to use the "file" loader for
|
|
92
|
-
// these ".node" files.
|
|
93
|
-
const opts = build.initialOptions;
|
|
94
|
-
opts.loader = opts.loader || {};
|
|
95
|
-
opts.loader['.node'] = 'file';
|
|
96
|
-
}
|
|
97
|
-
}, {
|
|
98
|
-
name: 'replace-path',
|
|
99
|
-
setup(ctx) {
|
|
100
|
-
ctx.onLoad({
|
|
101
|
-
filter: JS_EXT_RE
|
|
102
|
-
}, async args => {
|
|
103
|
-
const contents = fs.readFileSync(args.path, 'utf-8');
|
|
104
|
-
return {
|
|
105
|
-
contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(path.dirname(args.path))).replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`)),
|
|
106
|
-
loader: inferLoader(path.extname(args.path))
|
|
107
|
-
};
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
112
|
-
{
|
|
113
|
-
name: 'make-all-packages-external',
|
|
114
|
-
setup(_build) {
|
|
115
|
-
_build.onResolve({
|
|
116
|
-
filter: EXTERNAL_REGEXP
|
|
117
|
-
}, async args => {
|
|
118
|
-
let external = true;
|
|
119
|
-
// FIXME: windows external entrypoint
|
|
120
|
-
if (args.kind === 'entry-point') {
|
|
121
|
-
external = false;
|
|
106
|
+
}));
|
|
107
|
+
build2.onResolve(
|
|
108
|
+
{ filter: /\.node$/, namespace: "node-file" },
|
|
109
|
+
(args) => ({
|
|
110
|
+
path: args.path,
|
|
111
|
+
namespace: "file"
|
|
112
|
+
})
|
|
113
|
+
);
|
|
114
|
+
const opts = build2.initialOptions;
|
|
115
|
+
opts.loader = opts.loader || {};
|
|
116
|
+
opts.loader[".node"] = "file";
|
|
122
117
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
name: "replace-path",
|
|
121
|
+
setup(ctx) {
|
|
122
|
+
ctx.onLoad({ filter: JS_EXT_RE }, (args) => __async(this, null, function* () {
|
|
123
|
+
const contents = fs.readFileSync(args.path, "utf-8");
|
|
129
124
|
return {
|
|
130
|
-
|
|
125
|
+
contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
|
|
126
|
+
/\b__dirname\b/g,
|
|
127
|
+
JSON.stringify(path.dirname(args.path))
|
|
128
|
+
).replace(
|
|
129
|
+
/\bimport\.meta\.url\b/g,
|
|
130
|
+
JSON.stringify(`file://${args.path}`)
|
|
131
|
+
),
|
|
132
|
+
loader: inferLoader(path.extname(args.path))
|
|
131
133
|
};
|
|
132
|
-
}
|
|
133
|
-
} catch (err) {
|
|
134
|
-
// If the package can not be resolved, do nothing.
|
|
134
|
+
}));
|
|
135
135
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: "make-all-packages-external",
|
|
139
|
+
setup(_build) {
|
|
140
|
+
_build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => __async(this, null, function* () {
|
|
141
|
+
let external = true;
|
|
142
|
+
if (args.kind === "entry-point") {
|
|
143
|
+
external = false;
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const resolvedPath = require.resolve(args.path, {
|
|
147
|
+
paths: [args.resolveDir]
|
|
148
|
+
});
|
|
149
|
+
if (BUNDLED_EXT_RE.test(resolvedPath) || (yield isTypeModulePkg(resolvedPath))) {
|
|
150
|
+
return {
|
|
151
|
+
external: false
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
} catch (err) {
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
path: args.path,
|
|
158
|
+
external
|
|
159
|
+
};
|
|
160
|
+
}));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
}));
|
|
165
|
+
return outfile;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
export {
|
|
169
|
+
EXTERNAL_REGEXP,
|
|
170
|
+
bundle,
|
|
171
|
+
defaultGetOutputFile
|
|
172
|
+
};
|
package/dist/js/modern/index.js
CHANGED
|
@@ -1,30 +1,51 @@
|
|
|
1
|
-
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { fs } from "@modern-js/utils";
|
|
2
22
|
import { bundle, defaultGetOutputFile } from "./bundle";
|
|
3
|
-
export { bundle, defaultGetOutputFile };
|
|
4
23
|
function deleteRequireCache(path) {
|
|
5
24
|
if (require.cache[path]) {
|
|
6
25
|
delete require.cache[path];
|
|
7
26
|
}
|
|
8
27
|
if (module.children) {
|
|
9
|
-
module.children = module.children.filter(item => item.filename !== path);
|
|
28
|
+
module.children = module.children.filter((item) => item.filename !== path);
|
|
10
29
|
}
|
|
11
30
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// default auto clear configFile
|
|
25
|
-
if ((options === null || options === void 0 ? void 0 : options.autoClear) === undefined || options.autoClear) {
|
|
26
|
-
fs.unlinkSync(configFile);
|
|
31
|
+
function bundleRequire(filepath, options) {
|
|
32
|
+
return __async(this, null, function* () {
|
|
33
|
+
const configFile = yield bundle(filepath, options);
|
|
34
|
+
let mod;
|
|
35
|
+
const req = (options == null ? void 0 : options.require) || require;
|
|
36
|
+
try {
|
|
37
|
+
mod = yield req(configFile);
|
|
38
|
+
deleteRequireCache(configFile);
|
|
39
|
+
} finally {
|
|
40
|
+
if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
|
|
41
|
+
fs.unlinkSync(configFile);
|
|
42
|
+
}
|
|
27
43
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
44
|
+
return mod;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
bundle,
|
|
49
|
+
bundleRequire,
|
|
50
|
+
defaultGetOutputFile
|
|
51
|
+
};
|
|
File without changes
|
package/dist/js/node/bundle.js
CHANGED
|
@@ -1,156 +1,191 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var stdin_exports = {};
|
|
25
|
+
__export(stdin_exports, {
|
|
26
|
+
EXTERNAL_REGEXP: () => EXTERNAL_REGEXP,
|
|
27
|
+
bundle: () => bundle,
|
|
28
|
+
defaultGetOutputFile: () => defaultGetOutputFile
|
|
5
29
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
30
|
+
module.exports = __toCommonJS(stdin_exports);
|
|
31
|
+
var import_path = __toESM(require("path"));
|
|
32
|
+
var import_utils = require("@modern-js/utils");
|
|
33
|
+
var import_esbuild = require("esbuild");
|
|
34
|
+
var __defProp2 = Object.defineProperty;
|
|
35
|
+
var __defProps = Object.defineProperties;
|
|
36
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
37
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
38
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
39
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
40
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
41
|
+
var __spreadValues = (a, b) => {
|
|
42
|
+
for (var prop in b || (b = {}))
|
|
43
|
+
if (__hasOwnProp2.call(b, prop))
|
|
44
|
+
__defNormalProp(a, prop, b[prop]);
|
|
45
|
+
if (__getOwnPropSymbols)
|
|
46
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
47
|
+
if (__propIsEnum.call(b, prop))
|
|
48
|
+
__defNormalProp(a, prop, b[prop]);
|
|
49
|
+
}
|
|
50
|
+
return a;
|
|
51
|
+
};
|
|
52
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
53
|
+
var __async = (__this, __arguments, generator) => {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
var fulfilled = (value) => {
|
|
56
|
+
try {
|
|
57
|
+
step(generator.next(value));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
reject(e);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var rejected = (value) => {
|
|
63
|
+
try {
|
|
64
|
+
step(generator.throw(value));
|
|
65
|
+
} catch (e) {
|
|
66
|
+
reject(e);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
70
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
const debug = (0, import_utils.createDebugger)("node-bundle");
|
|
17
74
|
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
18
75
|
const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
|
|
19
|
-
|
|
20
|
-
// Must not start with "/" or "./" or "../"
|
|
21
|
-
// "/test/node_modules/foo"
|
|
22
|
-
// "c:/node_modules/foo"
|
|
23
76
|
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
24
|
-
exports.EXTERNAL_REGEXP = EXTERNAL_REGEXP;
|
|
25
77
|
function inferLoader(ext) {
|
|
26
|
-
if (ext ===
|
|
27
|
-
return
|
|
78
|
+
if (ext === ".mjs" || ext === ".cjs") {
|
|
79
|
+
return "js";
|
|
28
80
|
}
|
|
29
81
|
return ext.slice(1);
|
|
30
82
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
cwd
|
|
83
|
+
function isTypeModulePkg(cwd) {
|
|
84
|
+
return __async(this, null, function* () {
|
|
85
|
+
const pkgJsonPath = yield (0, import_utils.pkgUp)({ cwd });
|
|
86
|
+
if (pkgJsonPath) {
|
|
87
|
+
const pkgJson = yield import_utils.fs.readJSON(pkgJsonPath);
|
|
88
|
+
return pkgJson.type === "module";
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
34
91
|
});
|
|
35
|
-
if (pkgJsonPath) {
|
|
36
|
-
const pkgJson = await _utils.fs.readJSON(pkgJsonPath);
|
|
37
|
-
return pkgJson.type === 'module';
|
|
38
|
-
}
|
|
39
|
-
return false;
|
|
40
92
|
}
|
|
41
|
-
const defaultGetOutputFile =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}),
|
|
75
|
-
namespace: 'node-file'
|
|
76
|
-
}));
|
|
77
|
-
|
|
78
|
-
// Files in the "node-file" virtual namespace call "require()" on the
|
|
79
|
-
// path from esbuild of the ".node" file in the output directory.
|
|
80
|
-
build.onLoad({
|
|
81
|
-
filter: /.*/,
|
|
82
|
-
namespace: 'node-file'
|
|
83
|
-
}, args => ({
|
|
84
|
-
contents: `
|
|
93
|
+
const defaultGetOutputFile = (filepath) => __async(void 0, null, function* () {
|
|
94
|
+
return import_path.default.resolve(
|
|
95
|
+
import_utils.CONFIG_CACHE_DIR,
|
|
96
|
+
`${filepath}-${Date.now()}.${(0, import_utils.nanoid)()}.bundled.cjs`
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
function bundle(filepath, options) {
|
|
100
|
+
return __async(this, null, function* () {
|
|
101
|
+
if (!JS_EXT_RE.test(filepath)) {
|
|
102
|
+
throw new Error(`${filepath} is not a valid JS file`);
|
|
103
|
+
}
|
|
104
|
+
debug("bundle", filepath, options);
|
|
105
|
+
const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
106
|
+
const outfile = yield getOutputFile(import_path.default.basename(filepath));
|
|
107
|
+
yield (0, import_esbuild.build)(__spreadProps(__spreadValues({
|
|
108
|
+
entryPoints: [filepath],
|
|
109
|
+
outfile,
|
|
110
|
+
format: "cjs",
|
|
111
|
+
platform: "node",
|
|
112
|
+
bundle: true,
|
|
113
|
+
target: "esnext"
|
|
114
|
+
}, options == null ? void 0 : options.esbuildOptions), {
|
|
115
|
+
plugins: [
|
|
116
|
+
...(options == null ? void 0 : options.esbuildPlugins) || [],
|
|
117
|
+
{
|
|
118
|
+
name: "native-node-modules",
|
|
119
|
+
setup(build2) {
|
|
120
|
+
build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
|
|
121
|
+
path: require.resolve(args.path, { paths: [args.resolveDir] }),
|
|
122
|
+
namespace: "node-file"
|
|
123
|
+
}));
|
|
124
|
+
build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
|
|
125
|
+
contents: `
|
|
85
126
|
import path from ${JSON.stringify(args.path)}
|
|
86
127
|
try { module.exports = require(path) }
|
|
87
128
|
catch {}
|
|
88
129
|
`
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}));
|
|
101
|
-
|
|
102
|
-
// Tell esbuild's default loading behavior to use the "file" loader for
|
|
103
|
-
// these ".node" files.
|
|
104
|
-
const opts = build.initialOptions;
|
|
105
|
-
opts.loader = opts.loader || {};
|
|
106
|
-
opts.loader['.node'] = 'file';
|
|
107
|
-
}
|
|
108
|
-
}, {
|
|
109
|
-
name: 'replace-path',
|
|
110
|
-
setup(ctx) {
|
|
111
|
-
ctx.onLoad({
|
|
112
|
-
filter: JS_EXT_RE
|
|
113
|
-
}, async args => {
|
|
114
|
-
const contents = _utils.fs.readFileSync(args.path, 'utf-8');
|
|
115
|
-
return {
|
|
116
|
-
contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(_path.default.dirname(args.path))).replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`)),
|
|
117
|
-
loader: inferLoader(_path.default.extname(args.path))
|
|
118
|
-
};
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
123
|
-
{
|
|
124
|
-
name: 'make-all-packages-external',
|
|
125
|
-
setup(_build) {
|
|
126
|
-
_build.onResolve({
|
|
127
|
-
filter: EXTERNAL_REGEXP
|
|
128
|
-
}, async args => {
|
|
129
|
-
let external = true;
|
|
130
|
-
// FIXME: windows external entrypoint
|
|
131
|
-
if (args.kind === 'entry-point') {
|
|
132
|
-
external = false;
|
|
130
|
+
}));
|
|
131
|
+
build2.onResolve(
|
|
132
|
+
{ filter: /\.node$/, namespace: "node-file" },
|
|
133
|
+
(args) => ({
|
|
134
|
+
path: args.path,
|
|
135
|
+
namespace: "file"
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
const opts = build2.initialOptions;
|
|
139
|
+
opts.loader = opts.loader || {};
|
|
140
|
+
opts.loader[".node"] = "file";
|
|
133
141
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
name: "replace-path",
|
|
145
|
+
setup(ctx) {
|
|
146
|
+
ctx.onLoad({ filter: JS_EXT_RE }, (args) => __async(this, null, function* () {
|
|
147
|
+
const contents = import_utils.fs.readFileSync(args.path, "utf-8");
|
|
140
148
|
return {
|
|
141
|
-
|
|
149
|
+
contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
|
|
150
|
+
/\b__dirname\b/g,
|
|
151
|
+
JSON.stringify(import_path.default.dirname(args.path))
|
|
152
|
+
).replace(
|
|
153
|
+
/\bimport\.meta\.url\b/g,
|
|
154
|
+
JSON.stringify(`file://${args.path}`)
|
|
155
|
+
),
|
|
156
|
+
loader: inferLoader(import_path.default.extname(args.path))
|
|
142
157
|
};
|
|
143
|
-
}
|
|
144
|
-
} catch (err) {
|
|
145
|
-
// If the package can not be resolved, do nothing.
|
|
158
|
+
}));
|
|
146
159
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: "make-all-packages-external",
|
|
163
|
+
setup(_build) {
|
|
164
|
+
_build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => __async(this, null, function* () {
|
|
165
|
+
let external = true;
|
|
166
|
+
if (args.kind === "entry-point") {
|
|
167
|
+
external = false;
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
const resolvedPath = require.resolve(args.path, {
|
|
171
|
+
paths: [args.resolveDir]
|
|
172
|
+
});
|
|
173
|
+
if (BUNDLED_EXT_RE.test(resolvedPath) || (yield isTypeModulePkg(resolvedPath))) {
|
|
174
|
+
return {
|
|
175
|
+
external: false
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
} catch (err) {
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
path: args.path,
|
|
182
|
+
external
|
|
183
|
+
};
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
]
|
|
188
|
+
}));
|
|
189
|
+
return outfile;
|
|
190
|
+
});
|
|
191
|
+
}
|
package/dist/js/node/index.js
CHANGED
|
@@ -1,47 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: function () {
|
|
16
|
-
return _bundle.defaultGetOutputFile;
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
14
|
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var stdin_exports = {};
|
|
19
|
+
__export(stdin_exports, {
|
|
20
|
+
bundle: () => import_bundle.bundle,
|
|
21
|
+
bundleRequire: () => bundleRequire,
|
|
22
|
+
defaultGetOutputFile: () => import_bundle.defaultGetOutputFile
|
|
18
23
|
});
|
|
19
|
-
|
|
20
|
-
var
|
|
24
|
+
module.exports = __toCommonJS(stdin_exports);
|
|
25
|
+
var import_utils = require("@modern-js/utils");
|
|
26
|
+
var import_bundle = require("./bundle");
|
|
27
|
+
var __async = (__this, __arguments, generator) => {
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
var fulfilled = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.next(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var rejected = (value) => {
|
|
37
|
+
try {
|
|
38
|
+
step(generator.throw(value));
|
|
39
|
+
} catch (e) {
|
|
40
|
+
reject(e);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
44
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
45
|
+
});
|
|
46
|
+
};
|
|
21
47
|
function deleteRequireCache(path) {
|
|
22
48
|
if (require.cache[path]) {
|
|
23
49
|
delete require.cache[path];
|
|
24
50
|
}
|
|
25
51
|
if (module.children) {
|
|
26
|
-
module.children = module.children.filter(item => item.filename !== path);
|
|
52
|
+
module.children = module.children.filter((item) => item.filename !== path);
|
|
27
53
|
}
|
|
28
54
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// default auto clear configFile
|
|
42
|
-
if ((options === null || options === void 0 ? void 0 : options.autoClear) === undefined || options.autoClear) {
|
|
43
|
-
_utils.fs.unlinkSync(configFile);
|
|
55
|
+
function bundleRequire(filepath, options) {
|
|
56
|
+
return __async(this, null, function* () {
|
|
57
|
+
const configFile = yield (0, import_bundle.bundle)(filepath, options);
|
|
58
|
+
let mod;
|
|
59
|
+
const req = (options == null ? void 0 : options.require) || require;
|
|
60
|
+
try {
|
|
61
|
+
mod = yield req(configFile);
|
|
62
|
+
deleteRequireCache(configFile);
|
|
63
|
+
} finally {
|
|
64
|
+
if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
|
|
65
|
+
import_utils.fs.unlinkSync(configFile);
|
|
66
|
+
}
|
|
44
67
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
68
|
+
return mod;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
File without changes
|
package/dist/types/bundle.d.ts
CHANGED
|
@@ -10,19 +10,23 @@ export interface Options {
|
|
|
10
10
|
/**
|
|
11
11
|
* esbuild options
|
|
12
12
|
*/
|
|
13
|
+
|
|
13
14
|
esbuildOptions?: BuildOptions;
|
|
14
15
|
/**
|
|
15
16
|
* esbuild plugin
|
|
16
17
|
*/
|
|
18
|
+
|
|
17
19
|
esbuildPlugins?: Plugin[];
|
|
18
20
|
/**
|
|
19
21
|
* Get the path to the output file
|
|
20
22
|
* By default we simply replace the extension with `.bundled.cjs`
|
|
21
23
|
*/
|
|
24
|
+
|
|
22
25
|
getOutputFile?: (filepath: string) => Promise<string>;
|
|
23
26
|
/**
|
|
24
27
|
* auto clear bundle file
|
|
25
28
|
*/
|
|
29
|
+
|
|
26
30
|
autoClear?: boolean;
|
|
27
31
|
}
|
|
28
32
|
export declare const defaultGetOutputFile: (filepath: string) => Promise<string>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modern-js/node-bundle-require",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.4",
|
|
4
4
|
"description": "The meta-framework suite designed from scratch for frontend-focused modern web development.",
|
|
5
5
|
"homepage": "https://modernjs.dev",
|
|
6
6
|
"bugs": "https://github.com/modern-js-dev/modern.js/issues",
|
|
@@ -40,15 +40,15 @@
|
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@babel/runtime": "^7.18.0",
|
|
42
42
|
"esbuild": "0.15.7",
|
|
43
|
-
"@modern-js/utils": "2.0.0-beta.
|
|
43
|
+
"@modern-js/utils": "2.0.0-beta.4"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/jest": "^27",
|
|
47
47
|
"@types/node": "^14",
|
|
48
48
|
"jest": "^27",
|
|
49
49
|
"typescript": "^4",
|
|
50
|
-
"@scripts/build": "2.0.0-beta.
|
|
51
|
-
"@scripts/jest-config": "2.0.0-beta.
|
|
50
|
+
"@scripts/build": "2.0.0-beta.4",
|
|
51
|
+
"@scripts/jest-config": "2.0.0-beta.4"
|
|
52
52
|
},
|
|
53
53
|
"sideEffects": false,
|
|
54
54
|
"publishConfig": {
|