@modern-js/node-bundle-require 1.0.0-rc.20
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 +6 -0
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/js/modern/index.js +123 -0
- package/dist/js/node/index.js +136 -0
- package/dist/types/index.d.ts +26 -0
- package/modern.config.js +10 -0
- package/package.json +42 -0
- package/src/.eslintrc.json +3 -0
- package/src/index.ts +138 -0
- package/src/modern-app-env.d.ts +2 -0
- package/tests/.eslintrc.js +6 -0
- package/tests/fixture/a.ts +1 -0
- package/tests/fixture/input.ts +5 -0
- package/tests/index.test.ts +7 -0
- package/tests/modern-app-env.d.ts +2 -0
- package/tests/tsconfig.json +11 -0
- package/tsconfig.json +13 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Modern.js
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
<p align="center">
|
|
3
|
+
<a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">
|
|
7
|
+
现代 Web 工程体系
|
|
8
|
+
<br/>
|
|
9
|
+
<a href="https://modernjs.dev" target="blank">
|
|
10
|
+
modernjs.dev
|
|
11
|
+
</a>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## 参考链接
|
|
16
|
+
- [迈入现代 Web 开发](https://zhuanlan.zhihu.com/p/386607009)
|
|
17
|
+
- [现代 Web 开发者问卷调查报告](https://zhuanlan.zhihu.com/p/403206195)
|
|
18
|
+
- [字节跳动是如何落地微前端的](https://mp.weixin.qq.com/s/L9wbfNG5fTXF5bx7dcgj4Q)
|
|
19
|
+
|
|
20
|
+
## 近期计划
|
|
21
|
+
|
|
22
|
+
Modern.js 的 1.0.0.rc 版已经发到 npm,目前在做测试改进和文档建设。
|
|
23
|
+
|
|
24
|
+
README、完整的文档站、介绍等,都计划在 10 月 27 日上线。
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 fs from 'fs/promises';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import { build } from 'esbuild';
|
|
10
|
+
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
11
|
+
const CACHE_DIR = path.relative(process.cwd(), './node_modules/.node-bundle-require');
|
|
12
|
+
|
|
13
|
+
function inferLoader(ext) {
|
|
14
|
+
if (ext === '.mjs' || ext === '.cjs') return 'js';
|
|
15
|
+
return ext.slice(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const defaultGetOutputFile = filepath => path.resolve(CACHE_DIR, `${filepath}-${Date.now()}.bundled.cjs`);
|
|
19
|
+
|
|
20
|
+
export async function bundleRequire(filepath, options) {
|
|
21
|
+
if (!JS_EXT_RE.test(filepath)) {
|
|
22
|
+
throw new Error(`${filepath} is not a valid JS file`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
26
|
+
const outfile = getOutputFile(filepath);
|
|
27
|
+
await build(_objectSpread(_objectSpread({
|
|
28
|
+
entryPoints: [filepath],
|
|
29
|
+
outfile,
|
|
30
|
+
format: 'cjs',
|
|
31
|
+
platform: 'node',
|
|
32
|
+
bundle: true
|
|
33
|
+
}, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
|
|
34
|
+
plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
35
|
+
{
|
|
36
|
+
name: 'native-node-modules',
|
|
37
|
+
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
39
|
+
setup(build) {
|
|
40
|
+
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
41
|
+
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
42
|
+
build.onResolve({
|
|
43
|
+
filter: /\.node$/,
|
|
44
|
+
namespace: 'file'
|
|
45
|
+
}, args => ({
|
|
46
|
+
path: require.resolve(args.path, {
|
|
47
|
+
paths: [args.resolveDir]
|
|
48
|
+
}),
|
|
49
|
+
namespace: 'node-file'
|
|
50
|
+
})); // Files in the "node-file" virtual namespace call "require()" on the
|
|
51
|
+
// path from esbuild of the ".node" file in the output directory.
|
|
52
|
+
|
|
53
|
+
build.onLoad({
|
|
54
|
+
filter: /.*/,
|
|
55
|
+
namespace: 'node-file'
|
|
56
|
+
}, args => ({
|
|
57
|
+
contents: `
|
|
58
|
+
import path from ${JSON.stringify(args.path)}
|
|
59
|
+
try { module.exports = require(path) }
|
|
60
|
+
catch {}
|
|
61
|
+
`
|
|
62
|
+
})); // If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
63
|
+
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
64
|
+
// it. It is already an absolute path since we resolved it to one above.
|
|
65
|
+
|
|
66
|
+
build.onResolve({
|
|
67
|
+
filter: /\.node$/,
|
|
68
|
+
namespace: 'node-file'
|
|
69
|
+
}, args => ({
|
|
70
|
+
path: args.path,
|
|
71
|
+
namespace: 'file'
|
|
72
|
+
})); // Tell esbuild's default loading behavior to use the "file" loader for
|
|
73
|
+
// these ".node" files.
|
|
74
|
+
|
|
75
|
+
const opts = build.initialOptions;
|
|
76
|
+
opts.loader = opts.loader || {};
|
|
77
|
+
opts.loader['.node'] = 'file';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
}, {
|
|
81
|
+
name: 'replace-path',
|
|
82
|
+
|
|
83
|
+
setup(ctx) {
|
|
84
|
+
ctx.onLoad({
|
|
85
|
+
filter: JS_EXT_RE
|
|
86
|
+
}, async args => {
|
|
87
|
+
const contents = await fs.readFile(args.path, 'utf-8');
|
|
88
|
+
return {
|
|
89
|
+
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}`)),
|
|
90
|
+
loader: inferLoader(path.extname(args.path))
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
}, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
96
|
+
{
|
|
97
|
+
name: 'make-all-packages-external',
|
|
98
|
+
|
|
99
|
+
setup(build) {
|
|
100
|
+
let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
|
|
101
|
+
|
|
102
|
+
build.onResolve({
|
|
103
|
+
filter
|
|
104
|
+
}, args => ({
|
|
105
|
+
path: args.path,
|
|
106
|
+
external: true
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
}]
|
|
111
|
+
}));
|
|
112
|
+
let mod;
|
|
113
|
+
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
mod = await req(outfile);
|
|
117
|
+
} finally {
|
|
118
|
+
// Remove the outfile after executed
|
|
119
|
+
await fs.unlink(outfile);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return mod;
|
|
123
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.bundleRequire = bundleRequire;
|
|
7
|
+
|
|
8
|
+
var _promises = _interopRequireDefault(require("fs/promises"));
|
|
9
|
+
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
|
|
12
|
+
var _esbuild = require("esbuild");
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
+
|
|
18
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
19
|
+
|
|
20
|
+
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; }
|
|
21
|
+
|
|
22
|
+
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
23
|
+
|
|
24
|
+
const CACHE_DIR = _path.default.relative(process.cwd(), './node_modules/.node-bundle-require');
|
|
25
|
+
|
|
26
|
+
function inferLoader(ext) {
|
|
27
|
+
if (ext === '.mjs' || ext === '.cjs') return 'js';
|
|
28
|
+
return ext.slice(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const defaultGetOutputFile = filepath => _path.default.resolve(CACHE_DIR, `${filepath}-${Date.now()}.bundled.cjs`);
|
|
32
|
+
|
|
33
|
+
async function bundleRequire(filepath, options) {
|
|
34
|
+
if (!JS_EXT_RE.test(filepath)) {
|
|
35
|
+
throw new Error(`${filepath} is not a valid JS file`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
|
|
39
|
+
const outfile = getOutputFile(filepath);
|
|
40
|
+
await (0, _esbuild.build)(_objectSpread(_objectSpread({
|
|
41
|
+
entryPoints: [filepath],
|
|
42
|
+
outfile,
|
|
43
|
+
format: 'cjs',
|
|
44
|
+
platform: 'node',
|
|
45
|
+
bundle: true
|
|
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
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
52
|
+
setup(build) {
|
|
53
|
+
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
54
|
+
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
55
|
+
build.onResolve({
|
|
56
|
+
filter: /\.node$/,
|
|
57
|
+
namespace: 'file'
|
|
58
|
+
}, args => ({
|
|
59
|
+
path: require.resolve(args.path, {
|
|
60
|
+
paths: [args.resolveDir]
|
|
61
|
+
}),
|
|
62
|
+
namespace: 'node-file'
|
|
63
|
+
})); // Files in the "node-file" virtual namespace call "require()" on the
|
|
64
|
+
// path from esbuild of the ".node" file in the output directory.
|
|
65
|
+
|
|
66
|
+
build.onLoad({
|
|
67
|
+
filter: /.*/,
|
|
68
|
+
namespace: 'node-file'
|
|
69
|
+
}, args => ({
|
|
70
|
+
contents: `
|
|
71
|
+
import path from ${JSON.stringify(args.path)}
|
|
72
|
+
try { module.exports = require(path) }
|
|
73
|
+
catch {}
|
|
74
|
+
`
|
|
75
|
+
})); // If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
76
|
+
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
77
|
+
// it. It is already an absolute path since we resolved it to one above.
|
|
78
|
+
|
|
79
|
+
build.onResolve({
|
|
80
|
+
filter: /\.node$/,
|
|
81
|
+
namespace: 'node-file'
|
|
82
|
+
}, args => ({
|
|
83
|
+
path: args.path,
|
|
84
|
+
namespace: 'file'
|
|
85
|
+
})); // Tell esbuild's default loading behavior to use the "file" loader for
|
|
86
|
+
// these ".node" files.
|
|
87
|
+
|
|
88
|
+
const opts = build.initialOptions;
|
|
89
|
+
opts.loader = opts.loader || {};
|
|
90
|
+
opts.loader['.node'] = 'file';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
}, {
|
|
94
|
+
name: 'replace-path',
|
|
95
|
+
|
|
96
|
+
setup(ctx) {
|
|
97
|
+
ctx.onLoad({
|
|
98
|
+
filter: JS_EXT_RE
|
|
99
|
+
}, async args => {
|
|
100
|
+
const contents = await _promises.default.readFile(args.path, 'utf-8');
|
|
101
|
+
return {
|
|
102
|
+
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}`)),
|
|
103
|
+
loader: inferLoader(_path.default.extname(args.path))
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
}, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
109
|
+
{
|
|
110
|
+
name: 'make-all-packages-external',
|
|
111
|
+
|
|
112
|
+
setup(build) {
|
|
113
|
+
let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
|
|
114
|
+
|
|
115
|
+
build.onResolve({
|
|
116
|
+
filter
|
|
117
|
+
}, args => ({
|
|
118
|
+
path: args.path,
|
|
119
|
+
external: true
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
}]
|
|
124
|
+
}));
|
|
125
|
+
let mod;
|
|
126
|
+
const req = (options === null || options === void 0 ? void 0 : options.require) || require;
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
mod = await req(outfile);
|
|
130
|
+
} finally {
|
|
131
|
+
// Remove the outfile after executed
|
|
132
|
+
await _promises.default.unlink(outfile);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return mod;
|
|
136
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Plugin, BuildOptions } from 'esbuild';
|
|
2
|
+
export interface Options {
|
|
3
|
+
/**
|
|
4
|
+
* The `require` function that is used to load the output file
|
|
5
|
+
* Default to the global `require` function
|
|
6
|
+
* This function can be asynchronous, i.e. returns a Promise
|
|
7
|
+
*/
|
|
8
|
+
require?: (outfile: string) => any;
|
|
9
|
+
/**
|
|
10
|
+
* esbuild options
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
esbuildOptions?: BuildOptions;
|
|
14
|
+
/**
|
|
15
|
+
* esbuild plugin
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
esbuildPlugins?: Plugin[];
|
|
19
|
+
/**
|
|
20
|
+
* Get the path to the output file
|
|
21
|
+
* By default we simply replace the extension with `.bundled.cjs`
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
getOutputFile?: (filepath: string) => string;
|
|
25
|
+
}
|
|
26
|
+
export declare function bundleRequire(filepath: string, options?: Options): Promise<any>;
|
package/modern.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@modern-js/node-bundle-require",
|
|
3
|
+
"version": "1.0.0-rc.20",
|
|
4
|
+
"jsnext:source": "./src/index.ts",
|
|
5
|
+
"types": "./dist/types/index.d.ts",
|
|
6
|
+
"main": "./dist/js/node/index.js",
|
|
7
|
+
"module": "./dist/js/treeshaking/index.js",
|
|
8
|
+
"jsnext:modern": "./dist/js/modern/index.js",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@babel/runtime": "^7",
|
|
11
|
+
"esbuild": "^0.13.9"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react": "^17"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@modern-js/plugin-testing": "^1.0.0-rc.20",
|
|
18
|
+
"@modern-js/module-tools": "^1.0.0-rc.20",
|
|
19
|
+
"@types/jest": "^26.0.9",
|
|
20
|
+
"@types/node": "^14",
|
|
21
|
+
"@types/react": "^17",
|
|
22
|
+
"@types/react-dom": "^17",
|
|
23
|
+
"react": "^17",
|
|
24
|
+
"typescript": "^4"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"modernConfig": {
|
|
28
|
+
"output": {
|
|
29
|
+
"packageMode": "node-js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public",
|
|
34
|
+
"registry": "https://registry.npmjs.org/"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"dev": "modern dev",
|
|
38
|
+
"build": "modern build",
|
|
39
|
+
"new": "modern new",
|
|
40
|
+
"test": "modern test --passWithNoTests"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { build, Loader, Plugin, BuildOptions } from 'esbuild';
|
|
4
|
+
|
|
5
|
+
const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
6
|
+
|
|
7
|
+
const CACHE_DIR = path.relative(process.cwd(), './node_modules/.node-bundle-require');
|
|
8
|
+
|
|
9
|
+
function inferLoader(ext: string): Loader {
|
|
10
|
+
if (ext === '.mjs' || ext === '.cjs') return 'js'
|
|
11
|
+
return ext.slice(1) as Loader
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Options {
|
|
15
|
+
/**
|
|
16
|
+
* The `require` function that is used to load the output file
|
|
17
|
+
* Default to the global `require` function
|
|
18
|
+
* This function can be asynchronous, i.e. returns a Promise
|
|
19
|
+
*/
|
|
20
|
+
require?: (outfile: string) => any;
|
|
21
|
+
/**
|
|
22
|
+
* esbuild options
|
|
23
|
+
*/
|
|
24
|
+
esbuildOptions?: BuildOptions;
|
|
25
|
+
/**
|
|
26
|
+
* esbuild plugin
|
|
27
|
+
*/
|
|
28
|
+
esbuildPlugins?: Plugin[];
|
|
29
|
+
/**
|
|
30
|
+
* Get the path to the output file
|
|
31
|
+
* By default we simply replace the extension with `.bundled.cjs`
|
|
32
|
+
*/
|
|
33
|
+
getOutputFile?: (filepath: string) => string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const defaultGetOutputFile = (filepath: string) =>
|
|
37
|
+
path.resolve(CACHE_DIR, `${filepath}-${Date.now()}.bundled.cjs`);
|
|
38
|
+
|
|
39
|
+
export async function bundleRequire(filepath: string, options?: Options) {
|
|
40
|
+
if (!JS_EXT_RE.test(filepath)) {
|
|
41
|
+
throw new Error(`${filepath} is not a valid JS file`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const getOutputFile = options?.getOutputFile || defaultGetOutputFile;
|
|
45
|
+
const outfile = getOutputFile(filepath);
|
|
46
|
+
|
|
47
|
+
await build({
|
|
48
|
+
entryPoints: [filepath],
|
|
49
|
+
outfile,
|
|
50
|
+
format: 'cjs',
|
|
51
|
+
platform: 'node',
|
|
52
|
+
bundle: true,
|
|
53
|
+
...options?.esbuildOptions,
|
|
54
|
+
plugins: [
|
|
55
|
+
...(options?.esbuildPlugins || []),
|
|
56
|
+
// https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
|
|
57
|
+
{
|
|
58
|
+
name: 'native-node-modules',
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
60
|
+
setup(build) {
|
|
61
|
+
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
62
|
+
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
63
|
+
build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => ({
|
|
64
|
+
path: require.resolve(args.path, { paths: [args.resolveDir] }),
|
|
65
|
+
namespace: 'node-file',
|
|
66
|
+
}));
|
|
67
|
+
|
|
68
|
+
// Files in the "node-file" virtual namespace call "require()" on the
|
|
69
|
+
// path from esbuild of the ".node" file in the output directory.
|
|
70
|
+
build.onLoad({ filter: /.*/, namespace: 'node-file' }, args => ({
|
|
71
|
+
contents: `
|
|
72
|
+
import path from ${JSON.stringify(args.path)}
|
|
73
|
+
try { module.exports = require(path) }
|
|
74
|
+
catch {}
|
|
75
|
+
`,
|
|
76
|
+
}));
|
|
77
|
+
|
|
78
|
+
// If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
79
|
+
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
80
|
+
// it. It is already an absolute path since we resolved it to one above.
|
|
81
|
+
build.onResolve(
|
|
82
|
+
{ filter: /\.node$/, namespace: 'node-file' },
|
|
83
|
+
args => ({
|
|
84
|
+
path: args.path,
|
|
85
|
+
namespace: 'file',
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// Tell esbuild's default loading behavior to use the "file" loader for
|
|
90
|
+
// these ".node" files.
|
|
91
|
+
const opts = build.initialOptions;
|
|
92
|
+
opts.loader = opts.loader || {};
|
|
93
|
+
opts.loader['.node'] = 'file';
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: 'replace-path',
|
|
98
|
+
setup(ctx) {
|
|
99
|
+
ctx.onLoad({ filter: JS_EXT_RE }, async (args) => {
|
|
100
|
+
const contents = await fs.readFile(args.path, 'utf-8')
|
|
101
|
+
return {
|
|
102
|
+
contents: contents
|
|
103
|
+
.replace(/\b__filename\b/g, JSON.stringify(args.path))
|
|
104
|
+
.replace(
|
|
105
|
+
/\b__dirname\b/g,
|
|
106
|
+
JSON.stringify(path.dirname(args.path)),
|
|
107
|
+
)
|
|
108
|
+
.replace(
|
|
109
|
+
/\bimport\.meta\.url\b/g,
|
|
110
|
+
JSON.stringify(`file://${args.path}`),
|
|
111
|
+
),
|
|
112
|
+
loader: inferLoader(path.extname(args.path)),
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
118
|
+
{
|
|
119
|
+
name: 'make-all-packages-external',
|
|
120
|
+
setup(build) {
|
|
121
|
+
let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ // Must not start with "/" or "./" or "../"
|
|
122
|
+
build.onResolve({ filter }, args => ({ path: args.path, external: true }))
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
let mod: any;
|
|
129
|
+
const req = options?.require || require;
|
|
130
|
+
try {
|
|
131
|
+
mod = await req(outfile);
|
|
132
|
+
} finally {
|
|
133
|
+
// Remove the outfile after executed
|
|
134
|
+
await fs.unlink(outfile);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return mod;
|
|
138
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const filename: string = __filename
|
package/tsconfig.json
ADDED