@modern-js/node-bundle-require 1.2.4 → 1.3.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/.eslintrc.js +8 -0
- package/CHANGELOG.md +25 -1
- package/dist/js/modern/bundle.js +131 -0
- package/dist/js/modern/index.js +15 -124
- package/dist/js/node/bundle.js +147 -0
- package/dist/js/node/index.js +20 -133
- package/dist/types/bundle.d.ts +27 -0
- package/dist/types/index.d.ts +2 -26
- package/modern.config.js +5 -1
- package/package.json +17 -11
- package/tests/.eslintrc.js +0 -6
- package/tests/fixture/a.ts +0 -2
- package/tests/fixture/input.ts +0 -5
- package/tests/index.test.ts +0 -26
- package/tests/modern-app-env.d.ts +0 -2
- package/tests/tsconfig.json +0 -10
package/.eslintrc.js
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
|
-
# @modern-js/
|
|
1
|
+
# @modern-js/node-bundle-require
|
|
2
|
+
|
|
3
|
+
## 1.3.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 6c1438d2: fix: missing peer deps warnings
|
|
8
|
+
- 895fa0ff: chore: using "workspace:\*" in devDependencies
|
|
9
|
+
|
|
10
|
+
## 1.3.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- b8599d09: fix: failed to generate webpack cache
|
|
15
|
+
- 6cffe99d: chore:
|
|
16
|
+
remove react eslint rules for `modern-js` rule set.
|
|
17
|
+
add .eslintrc for each package to speed up linting
|
|
18
|
+
- 04ae5262: chore: bump @modern-js/utils to v1.4.1 in dependencies
|
|
19
|
+
- 60f7d8bf: feat: add tests dir to npmignore
|
|
20
|
+
|
|
21
|
+
## 1.3.0
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- d2d1d6b2: feat: support server config
|
|
2
26
|
|
|
3
27
|
## 1.2.4
|
|
4
28
|
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
|
+
|
|
3
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
|
+
|
|
5
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
|
+
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import { fs, CONFIG_CACHE_DIR, createDebugger } from '@modern-js/utils';
|
|
9
|
+
import { nanoid } from 'nanoid';
|
|
10
|
+
import { build } from 'esbuild';
|
|
11
|
+
const debug = createDebugger('node-bundle');
|
|
12
|
+
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or "./" or "../"
|
|
13
|
+
// "/test/node_modules/foo"
|
|
14
|
+
// "c:/node_modules/foo"
|
|
15
|
+
|
|
16
|
+
export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
17
|
+
|
|
18
|
+
function inferLoader(ext) {
|
|
19
|
+
if (ext === '.mjs' || ext === '.cjs') {
|
|
20
|
+
return 'js';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return ext.slice(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const defaultGetOutputFile = filepath => path.resolve(CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`);
|
|
27
|
+
|
|
28
|
+
export async function bundle(filepath, options) {
|
|
29
|
+
if (!JS_EXT_RE.test(filepath)) {
|
|
30
|
+
throw new Error(`${filepath} is not a valid JS file`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
debug('bundle', filepath, options);
|
|
34
|
+
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
35
|
+
const outfile = getOutputFile(path.basename(filepath));
|
|
36
|
+
await build(_objectSpread(_objectSpread({
|
|
37
|
+
entryPoints: [filepath],
|
|
38
|
+
outfile,
|
|
39
|
+
format: 'cjs',
|
|
40
|
+
platform: 'node',
|
|
41
|
+
bundle: true,
|
|
42
|
+
// fix transforming error when the project's tsconfig.json
|
|
43
|
+
// sets `target: "es5"`
|
|
44
|
+
// reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
|
|
45
|
+
target: 'esnext'
|
|
46
|
+
}, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
|
|
47
|
+
plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
48
|
+
{
|
|
49
|
+
name: 'native-node-modules',
|
|
50
|
+
|
|
51
|
+
setup(build) {
|
|
52
|
+
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
53
|
+
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
54
|
+
build.onResolve({
|
|
55
|
+
filter: /\.node$/,
|
|
56
|
+
namespace: 'file'
|
|
57
|
+
}, args => ({
|
|
58
|
+
path: require.resolve(args.path, {
|
|
59
|
+
paths: [args.resolveDir]
|
|
60
|
+
}),
|
|
61
|
+
namespace: 'node-file'
|
|
62
|
+
})); // Files in the "node-file" virtual namespace call "require()" on the
|
|
63
|
+
// path from esbuild of the ".node" file in the output directory.
|
|
64
|
+
|
|
65
|
+
build.onLoad({
|
|
66
|
+
filter: /.*/,
|
|
67
|
+
namespace: 'node-file'
|
|
68
|
+
}, args => ({
|
|
69
|
+
contents: `
|
|
70
|
+
import path from ${JSON.stringify(args.path)}
|
|
71
|
+
try { module.exports = require(path) }
|
|
72
|
+
catch {}
|
|
73
|
+
`
|
|
74
|
+
})); // If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
75
|
+
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
76
|
+
// it. It is already an absolute path since we resolved it to one above.
|
|
77
|
+
|
|
78
|
+
build.onResolve({
|
|
79
|
+
filter: /\.node$/,
|
|
80
|
+
namespace: 'node-file'
|
|
81
|
+
}, args => ({
|
|
82
|
+
path: args.path,
|
|
83
|
+
namespace: 'file'
|
|
84
|
+
})); // Tell esbuild's default loading behavior to use the "file" loader for
|
|
85
|
+
// these ".node" files.
|
|
86
|
+
|
|
87
|
+
const opts = build.initialOptions;
|
|
88
|
+
opts.loader = opts.loader || {};
|
|
89
|
+
opts.loader['.node'] = 'file';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
}, {
|
|
93
|
+
name: 'replace-path',
|
|
94
|
+
|
|
95
|
+
setup(ctx) {
|
|
96
|
+
ctx.onLoad({
|
|
97
|
+
filter: JS_EXT_RE
|
|
98
|
+
}, async args => {
|
|
99
|
+
const contents = fs.readFileSync(args.path, 'utf-8');
|
|
100
|
+
return {
|
|
101
|
+
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}`)),
|
|
102
|
+
loader: inferLoader(path.extname(args.path))
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
}, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
108
|
+
{
|
|
109
|
+
name: 'make-all-packages-external',
|
|
110
|
+
|
|
111
|
+
setup(_build) {
|
|
112
|
+
_build.onResolve({
|
|
113
|
+
filter: EXTERNAL_REGEXP
|
|
114
|
+
}, args => {
|
|
115
|
+
let external = true; // FIXME: windows external entrypoint
|
|
116
|
+
|
|
117
|
+
if (args.kind === 'entry-point') {
|
|
118
|
+
external = false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
path: args.path,
|
|
123
|
+
external
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
}]
|
|
129
|
+
}));
|
|
130
|
+
return outfile;
|
|
131
|
+
}
|
package/dist/js/modern/index.js
CHANGED
|
@@ -1,139 +1,30 @@
|
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
|
-
|
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
|
-
|
|
5
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
|
-
|
|
7
|
-
import path from 'path';
|
|
8
1
|
import { fs } from '@modern-js/utils';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or "./" or "../"
|
|
12
|
-
// "/test/node_modules/foo"
|
|
13
|
-
// "c:/node_modules/foo"
|
|
2
|
+
import { bundle } from "./bundle";
|
|
3
|
+
export { bundle };
|
|
14
4
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function inferLoader(ext) {
|
|
19
|
-
if (ext === '.mjs' || ext === '.cjs') {
|
|
20
|
-
return 'js';
|
|
5
|
+
function deleteRequireCache(path) {
|
|
6
|
+
if (require.cache[path]) {
|
|
7
|
+
delete require.cache[path];
|
|
21
8
|
}
|
|
22
9
|
|
|
23
|
-
|
|
10
|
+
if (module.children) {
|
|
11
|
+
module.children = module.children.filter(item => item.filename !== path);
|
|
12
|
+
}
|
|
24
13
|
}
|
|
25
14
|
|
|
26
|
-
const defaultGetOutputFile = filepath => path.resolve(CACHE_DIR, `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`);
|
|
27
|
-
|
|
28
15
|
export async function bundleRequire(filepath, options) {
|
|
29
|
-
|
|
30
|
-
throw new Error(`${filepath} is not a valid JS file`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
34
|
-
const outfile = getOutputFile(filepath);
|
|
35
|
-
await build(_objectSpread(_objectSpread({
|
|
36
|
-
entryPoints: [filepath],
|
|
37
|
-
outfile,
|
|
38
|
-
format: 'cjs',
|
|
39
|
-
platform: 'node',
|
|
40
|
-
bundle: true,
|
|
41
|
-
// fix transforming error when the project's tsconfig.json
|
|
42
|
-
// sets `target: "es5"`
|
|
43
|
-
// reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
|
|
44
|
-
target: 'esnext'
|
|
45
|
-
}, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
|
|
46
|
-
plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
47
|
-
{
|
|
48
|
-
name: 'native-node-modules',
|
|
49
|
-
|
|
50
|
-
setup(build) {
|
|
51
|
-
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
52
|
-
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
53
|
-
build.onResolve({
|
|
54
|
-
filter: /\.node$/,
|
|
55
|
-
namespace: 'file'
|
|
56
|
-
}, args => ({
|
|
57
|
-
path: require.resolve(args.path, {
|
|
58
|
-
paths: [args.resolveDir]
|
|
59
|
-
}),
|
|
60
|
-
namespace: 'node-file'
|
|
61
|
-
})); // Files in the "node-file" virtual namespace call "require()" on the
|
|
62
|
-
// path from esbuild of the ".node" file in the output directory.
|
|
63
|
-
|
|
64
|
-
build.onLoad({
|
|
65
|
-
filter: /.*/,
|
|
66
|
-
namespace: 'node-file'
|
|
67
|
-
}, args => ({
|
|
68
|
-
contents: `
|
|
69
|
-
import path from ${JSON.stringify(args.path)}
|
|
70
|
-
try { module.exports = require(path) }
|
|
71
|
-
catch {}
|
|
72
|
-
`
|
|
73
|
-
})); // If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
74
|
-
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
75
|
-
// it. It is already an absolute path since we resolved it to one above.
|
|
76
|
-
|
|
77
|
-
build.onResolve({
|
|
78
|
-
filter: /\.node$/,
|
|
79
|
-
namespace: 'node-file'
|
|
80
|
-
}, args => ({
|
|
81
|
-
path: args.path,
|
|
82
|
-
namespace: 'file'
|
|
83
|
-
})); // Tell esbuild's default loading behavior to use the "file" loader for
|
|
84
|
-
// these ".node" files.
|
|
85
|
-
|
|
86
|
-
const opts = build.initialOptions;
|
|
87
|
-
opts.loader = opts.loader || {};
|
|
88
|
-
opts.loader['.node'] = 'file';
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
}, {
|
|
92
|
-
name: 'replace-path',
|
|
93
|
-
|
|
94
|
-
setup(ctx) {
|
|
95
|
-
ctx.onLoad({
|
|
96
|
-
filter: JS_EXT_RE
|
|
97
|
-
}, async args => {
|
|
98
|
-
const contents = fs.readFileSync(args.path, 'utf-8');
|
|
99
|
-
return {
|
|
100
|
-
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}`)),
|
|
101
|
-
loader: inferLoader(path.extname(args.path))
|
|
102
|
-
};
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
}, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
107
|
-
{
|
|
108
|
-
name: 'make-all-packages-external',
|
|
109
|
-
|
|
110
|
-
setup(_build) {
|
|
111
|
-
_build.onResolve({
|
|
112
|
-
filter: EXTERNAL_REGEXP
|
|
113
|
-
}, args => {
|
|
114
|
-
let external = true; // FIXME: windows external entrypoint
|
|
115
|
-
|
|
116
|
-
if (args.kind === 'entry-point') {
|
|
117
|
-
external = false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return {
|
|
121
|
-
path: args.path,
|
|
122
|
-
external
|
|
123
|
-
};
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
}]
|
|
128
|
-
}));
|
|
16
|
+
const configFile = await bundle(filepath, options);
|
|
129
17
|
let mod;
|
|
130
18
|
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
131
19
|
|
|
132
20
|
try {
|
|
133
|
-
mod = await req(
|
|
21
|
+
mod = await req(configFile); // Webpack will check require history for persistent cache.
|
|
22
|
+
// If webpack can not resolve the file, the previous cache pack will become invalid.
|
|
23
|
+
// The bundled file is temporary, so we should clear the require history to avoid breaking the webpack cache.
|
|
24
|
+
|
|
25
|
+
deleteRequireCache(configFile);
|
|
134
26
|
} finally {
|
|
135
|
-
|
|
136
|
-
fs.unlinkSync(outfile);
|
|
27
|
+
fs.unlinkSync(configFile);
|
|
137
28
|
}
|
|
138
29
|
|
|
139
30
|
return mod;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.EXTERNAL_REGEXP = void 0;
|
|
7
|
+
exports.bundle = bundle;
|
|
8
|
+
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
10
|
+
|
|
11
|
+
var _utils = require("@modern-js/utils");
|
|
12
|
+
|
|
13
|
+
var _nanoid = require("nanoid");
|
|
14
|
+
|
|
15
|
+
var _esbuild = require("esbuild");
|
|
16
|
+
|
|
17
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
|
+
|
|
19
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
20
|
+
|
|
21
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
22
|
+
|
|
23
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
24
|
+
|
|
25
|
+
const debug = (0, _utils.createDebugger)('node-bundle');
|
|
26
|
+
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or "./" or "../"
|
|
27
|
+
// "/test/node_modules/foo"
|
|
28
|
+
// "c:/node_modules/foo"
|
|
29
|
+
|
|
30
|
+
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
31
|
+
exports.EXTERNAL_REGEXP = EXTERNAL_REGEXP;
|
|
32
|
+
|
|
33
|
+
function inferLoader(ext) {
|
|
34
|
+
if (ext === '.mjs' || ext === '.cjs') {
|
|
35
|
+
return 'js';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return ext.slice(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const defaultGetOutputFile = filepath => _path.default.resolve(_utils.CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${(0, _nanoid.nanoid)()}.bundled.cjs`);
|
|
42
|
+
|
|
43
|
+
async function bundle(filepath, options) {
|
|
44
|
+
if (!JS_EXT_RE.test(filepath)) {
|
|
45
|
+
throw new Error(`${filepath} is not a valid JS file`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
debug('bundle', filepath, options);
|
|
49
|
+
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
50
|
+
const outfile = getOutputFile(_path.default.basename(filepath));
|
|
51
|
+
await (0, _esbuild.build)(_objectSpread(_objectSpread({
|
|
52
|
+
entryPoints: [filepath],
|
|
53
|
+
outfile,
|
|
54
|
+
format: 'cjs',
|
|
55
|
+
platform: 'node',
|
|
56
|
+
bundle: true,
|
|
57
|
+
// fix transforming error when the project's tsconfig.json
|
|
58
|
+
// sets `target: "es5"`
|
|
59
|
+
// reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
|
|
60
|
+
target: 'esnext'
|
|
61
|
+
}, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
|
|
62
|
+
plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
63
|
+
{
|
|
64
|
+
name: 'native-node-modules',
|
|
65
|
+
|
|
66
|
+
setup(build) {
|
|
67
|
+
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
68
|
+
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
69
|
+
build.onResolve({
|
|
70
|
+
filter: /\.node$/,
|
|
71
|
+
namespace: 'file'
|
|
72
|
+
}, args => ({
|
|
73
|
+
path: require.resolve(args.path, {
|
|
74
|
+
paths: [args.resolveDir]
|
|
75
|
+
}),
|
|
76
|
+
namespace: 'node-file'
|
|
77
|
+
})); // Files in the "node-file" virtual namespace call "require()" on the
|
|
78
|
+
// path from esbuild of the ".node" file in the output directory.
|
|
79
|
+
|
|
80
|
+
build.onLoad({
|
|
81
|
+
filter: /.*/,
|
|
82
|
+
namespace: 'node-file'
|
|
83
|
+
}, args => ({
|
|
84
|
+
contents: `
|
|
85
|
+
import path from ${JSON.stringify(args.path)}
|
|
86
|
+
try { module.exports = require(path) }
|
|
87
|
+
catch {}
|
|
88
|
+
`
|
|
89
|
+
})); // If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
90
|
+
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
91
|
+
// it. It is already an absolute path since we resolved it to one above.
|
|
92
|
+
|
|
93
|
+
build.onResolve({
|
|
94
|
+
filter: /\.node$/,
|
|
95
|
+
namespace: 'node-file'
|
|
96
|
+
}, args => ({
|
|
97
|
+
path: args.path,
|
|
98
|
+
namespace: 'file'
|
|
99
|
+
})); // Tell esbuild's default loading behavior to use the "file" loader for
|
|
100
|
+
// these ".node" files.
|
|
101
|
+
|
|
102
|
+
const opts = build.initialOptions;
|
|
103
|
+
opts.loader = opts.loader || {};
|
|
104
|
+
opts.loader['.node'] = 'file';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
}, {
|
|
108
|
+
name: 'replace-path',
|
|
109
|
+
|
|
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
|
+
|
|
116
|
+
return {
|
|
117
|
+
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}`)),
|
|
118
|
+
loader: inferLoader(_path.default.extname(args.path))
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
}, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
124
|
+
{
|
|
125
|
+
name: 'make-all-packages-external',
|
|
126
|
+
|
|
127
|
+
setup(_build) {
|
|
128
|
+
_build.onResolve({
|
|
129
|
+
filter: EXTERNAL_REGEXP
|
|
130
|
+
}, args => {
|
|
131
|
+
let external = true; // FIXME: windows external entrypoint
|
|
132
|
+
|
|
133
|
+
if (args.kind === 'entry-point') {
|
|
134
|
+
external = false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
path: args.path,
|
|
139
|
+
external
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
}]
|
|
145
|
+
}));
|
|
146
|
+
return outfile;
|
|
147
|
+
}
|
package/dist/js/node/index.js
CHANGED
|
@@ -3,154 +3,41 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
Object.defineProperty(exports, "bundle", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _bundle.bundle;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
7
12
|
exports.bundleRequire = bundleRequire;
|
|
8
13
|
|
|
9
|
-
var _path = _interopRequireDefault(require("path"));
|
|
10
|
-
|
|
11
14
|
var _utils = require("@modern-js/utils");
|
|
12
15
|
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
var _nanoid = require("nanoid");
|
|
16
|
-
|
|
17
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
|
-
|
|
19
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
20
|
-
|
|
21
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
16
|
+
var _bundle = require("./bundle");
|
|
22
17
|
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// "/test/node_modules/foo"
|
|
27
|
-
// "c:/node_modules/foo"
|
|
28
|
-
|
|
29
|
-
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
30
|
-
exports.EXTERNAL_REGEXP = EXTERNAL_REGEXP;
|
|
31
|
-
|
|
32
|
-
const CACHE_DIR = _path.default.relative(process.cwd(), './node_modules/.node-bundle-require');
|
|
33
|
-
|
|
34
|
-
function inferLoader(ext) {
|
|
35
|
-
if (ext === '.mjs' || ext === '.cjs') {
|
|
36
|
-
return 'js';
|
|
18
|
+
function deleteRequireCache(path) {
|
|
19
|
+
if (require.cache[path]) {
|
|
20
|
+
delete require.cache[path];
|
|
37
21
|
}
|
|
38
22
|
|
|
39
|
-
|
|
23
|
+
if (module.children) {
|
|
24
|
+
module.children = module.children.filter(item => item.filename !== path);
|
|
25
|
+
}
|
|
40
26
|
}
|
|
41
27
|
|
|
42
|
-
const defaultGetOutputFile = filepath => _path.default.resolve(CACHE_DIR, `${filepath}-${Date.now()}.${(0, _nanoid.nanoid)()}.bundled.cjs`);
|
|
43
|
-
|
|
44
28
|
async function bundleRequire(filepath, options) {
|
|
45
|
-
|
|
46
|
-
throw new Error(`${filepath} is not a valid JS file`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
50
|
-
const outfile = getOutputFile(filepath);
|
|
51
|
-
await (0, _esbuild.build)(_objectSpread(_objectSpread({
|
|
52
|
-
entryPoints: [filepath],
|
|
53
|
-
outfile,
|
|
54
|
-
format: 'cjs',
|
|
55
|
-
platform: 'node',
|
|
56
|
-
bundle: true,
|
|
57
|
-
// fix transforming error when the project's tsconfig.json
|
|
58
|
-
// sets `target: "es5"`
|
|
59
|
-
// reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
|
|
60
|
-
target: 'esnext'
|
|
61
|
-
}, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
|
|
62
|
-
plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
63
|
-
{
|
|
64
|
-
name: 'native-node-modules',
|
|
65
|
-
|
|
66
|
-
setup(build) {
|
|
67
|
-
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
68
|
-
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
69
|
-
build.onResolve({
|
|
70
|
-
filter: /\.node$/,
|
|
71
|
-
namespace: 'file'
|
|
72
|
-
}, args => ({
|
|
73
|
-
path: require.resolve(args.path, {
|
|
74
|
-
paths: [args.resolveDir]
|
|
75
|
-
}),
|
|
76
|
-
namespace: 'node-file'
|
|
77
|
-
})); // Files in the "node-file" virtual namespace call "require()" on the
|
|
78
|
-
// path from esbuild of the ".node" file in the output directory.
|
|
79
|
-
|
|
80
|
-
build.onLoad({
|
|
81
|
-
filter: /.*/,
|
|
82
|
-
namespace: 'node-file'
|
|
83
|
-
}, args => ({
|
|
84
|
-
contents: `
|
|
85
|
-
import path from ${JSON.stringify(args.path)}
|
|
86
|
-
try { module.exports = require(path) }
|
|
87
|
-
catch {}
|
|
88
|
-
`
|
|
89
|
-
})); // If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
90
|
-
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
91
|
-
// it. It is already an absolute path since we resolved it to one above.
|
|
92
|
-
|
|
93
|
-
build.onResolve({
|
|
94
|
-
filter: /\.node$/,
|
|
95
|
-
namespace: 'node-file'
|
|
96
|
-
}, args => ({
|
|
97
|
-
path: args.path,
|
|
98
|
-
namespace: 'file'
|
|
99
|
-
})); // Tell esbuild's default loading behavior to use the "file" loader for
|
|
100
|
-
// these ".node" files.
|
|
101
|
-
|
|
102
|
-
const opts = build.initialOptions;
|
|
103
|
-
opts.loader = opts.loader || {};
|
|
104
|
-
opts.loader['.node'] = 'file';
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
}, {
|
|
108
|
-
name: 'replace-path',
|
|
109
|
-
|
|
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
|
-
|
|
116
|
-
return {
|
|
117
|
-
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}`)),
|
|
118
|
-
loader: inferLoader(_path.default.extname(args.path))
|
|
119
|
-
};
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
}, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
124
|
-
{
|
|
125
|
-
name: 'make-all-packages-external',
|
|
126
|
-
|
|
127
|
-
setup(_build) {
|
|
128
|
-
_build.onResolve({
|
|
129
|
-
filter: EXTERNAL_REGEXP
|
|
130
|
-
}, args => {
|
|
131
|
-
let external = true; // FIXME: windows external entrypoint
|
|
132
|
-
|
|
133
|
-
if (args.kind === 'entry-point') {
|
|
134
|
-
external = false;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
path: args.path,
|
|
139
|
-
external
|
|
140
|
-
};
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
}]
|
|
145
|
-
}));
|
|
29
|
+
const configFile = await (0, _bundle.bundle)(filepath, options);
|
|
146
30
|
let mod;
|
|
147
31
|
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
148
32
|
|
|
149
33
|
try {
|
|
150
|
-
mod = await req(
|
|
34
|
+
mod = await req(configFile); // Webpack will check require history for persistent cache.
|
|
35
|
+
// If webpack can not resolve the file, the previous cache pack will become invalid.
|
|
36
|
+
// The bundled file is temporary, so we should clear the require history to avoid breaking the webpack cache.
|
|
37
|
+
|
|
38
|
+
deleteRequireCache(configFile);
|
|
151
39
|
} finally {
|
|
152
|
-
|
|
153
|
-
_utils.fs.unlinkSync(outfile);
|
|
40
|
+
_utils.fs.unlinkSync(configFile);
|
|
154
41
|
}
|
|
155
42
|
|
|
156
43
|
return mod;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Plugin, BuildOptions } from 'esbuild';
|
|
2
|
+
export declare const EXTERNAL_REGEXP: RegExp;
|
|
3
|
+
export interface Options {
|
|
4
|
+
/**
|
|
5
|
+
* The `require` function that is used to load the output file
|
|
6
|
+
* Default to the global `require` function
|
|
7
|
+
* This function can be asynchronous, i.e. returns a Promise
|
|
8
|
+
*/
|
|
9
|
+
require?: (outfile: string) => any;
|
|
10
|
+
/**
|
|
11
|
+
* esbuild options
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
esbuildOptions?: BuildOptions;
|
|
15
|
+
/**
|
|
16
|
+
* esbuild plugin
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
esbuildPlugins?: Plugin[];
|
|
20
|
+
/**
|
|
21
|
+
* Get the path to the output file
|
|
22
|
+
* By default we simply replace the extension with `.bundled.cjs`
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
getOutputFile?: (filepath: string) => string;
|
|
26
|
+
}
|
|
27
|
+
export declare function bundle(filepath: string, options?: Options): Promise<string>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,27 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
export interface Options {
|
|
4
|
-
/**
|
|
5
|
-
* The `require` function that is used to load the output file
|
|
6
|
-
* Default to the global `require` function
|
|
7
|
-
* This function can be asynchronous, i.e. returns a Promise
|
|
8
|
-
*/
|
|
9
|
-
require?: (outfile: string) => any;
|
|
10
|
-
/**
|
|
11
|
-
* esbuild options
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
esbuildOptions?: BuildOptions;
|
|
15
|
-
/**
|
|
16
|
-
* esbuild plugin
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
esbuildPlugins?: Plugin[];
|
|
20
|
-
/**
|
|
21
|
-
* Get the path to the output file
|
|
22
|
-
* By default we simply replace the extension with `.bundled.cjs`
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
getOutputFile?: (filepath: string) => string;
|
|
26
|
-
}
|
|
1
|
+
import { bundle, Options } from './bundle';
|
|
2
|
+
export { bundle };
|
|
27
3
|
export declare function bundleRequire(filepath: string, options?: Options): Promise<any>;
|
package/modern.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modern-js/node-bundle-require",
|
|
3
|
-
"version": "1.2
|
|
3
|
+
"version": "1.3.2",
|
|
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",
|
|
@@ -25,28 +25,34 @@
|
|
|
25
25
|
"require": "./dist/js/node/index.js"
|
|
26
26
|
},
|
|
27
27
|
"default": "./dist/js/treeshaking/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./bundle": "./dist/js/node/bundle.js"
|
|
30
|
+
},
|
|
31
|
+
"typesVersions": {
|
|
32
|
+
"*": {
|
|
33
|
+
".": [
|
|
34
|
+
"./dist/types/index.d.ts"
|
|
35
|
+
],
|
|
36
|
+
"bundle": [
|
|
37
|
+
"./dist/types/bundle.d.ts"
|
|
38
|
+
]
|
|
28
39
|
}
|
|
29
40
|
},
|
|
30
41
|
"dependencies": {
|
|
31
42
|
"@babel/runtime": "^7",
|
|
32
|
-
"esbuild": "^0.
|
|
33
|
-
"nanoid": "^3.3.
|
|
43
|
+
"esbuild": "^0.14.38",
|
|
44
|
+
"nanoid": "^3.3.2"
|
|
34
45
|
},
|
|
35
46
|
"devDependencies": {
|
|
47
|
+
"@modern-js/utils": "1.6.0",
|
|
36
48
|
"@scripts/build": "0.0.0",
|
|
37
|
-
"@
|
|
49
|
+
"@scripts/jest-config": "0.0.0",
|
|
38
50
|
"@types/jest": "^26.0.9",
|
|
39
51
|
"@types/node": "^14",
|
|
40
|
-
"typescript": "^4",
|
|
41
52
|
"jest": "^27",
|
|
42
|
-
"
|
|
53
|
+
"typescript": "^4"
|
|
43
54
|
},
|
|
44
55
|
"sideEffects": false,
|
|
45
|
-
"modernConfig": {
|
|
46
|
-
"output": {
|
|
47
|
-
"packageMode": "node-js"
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
56
|
"publishConfig": {
|
|
51
57
|
"registry": "https://registry.npmjs.org/",
|
|
52
58
|
"access": "public"
|
package/tests/.eslintrc.js
DELETED
package/tests/fixture/a.ts
DELETED
package/tests/fixture/input.ts
DELETED
package/tests/index.test.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { bundleRequire, EXTERNAL_REGEXP } from '../src';
|
|
3
|
-
|
|
4
|
-
test('require', async () => {
|
|
5
|
-
const result = await bundleRequire(
|
|
6
|
-
path.join(__dirname, './fixture/input.ts'),
|
|
7
|
-
);
|
|
8
|
-
// when tsconfig.json sets `compilerOptions.target` to `es5`
|
|
9
|
-
// normally it will met error
|
|
10
|
-
// So we need to manually set esbuild's target to esnext to avoid that
|
|
11
|
-
// These two cases above use ES6+ ability, to test whether esbuild successfuly
|
|
12
|
-
// works on non-ES5 files
|
|
13
|
-
// reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
|
|
14
|
-
expect(result.default.a.filename.endsWith('a.ts')).toEqual(true);
|
|
15
|
-
expect(result.default.a.showFileName().endsWith('a.ts')).toEqual(true);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('external regexp', () => {
|
|
19
|
-
expect(EXTERNAL_REGEXP.test('./test')).toBeFalsy();
|
|
20
|
-
expect(EXTERNAL_REGEXP.test('/test')).toBeFalsy();
|
|
21
|
-
expect(EXTERNAL_REGEXP.test('c:/foo')).toBeTruthy();
|
|
22
|
-
expect(EXTERNAL_REGEXP.test('C:/foo')).toBeTruthy();
|
|
23
|
-
expect(EXTERNAL_REGEXP.test('c:/node_modules/foo')).toBeTruthy();
|
|
24
|
-
expect(EXTERNAL_REGEXP.test('foo')).toBeTruthy();
|
|
25
|
-
expect(EXTERNAL_REGEXP.test('/test/node_modules')).toBeFalsy();
|
|
26
|
-
});
|