@modern-js/node-bundle-require 1.0.1 → 1.1.2-canary.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/CHANGELOG.md +12 -0
- package/dist/js/modern/index.js +16 -8
- package/dist/js/node/index.js +17 -8
- package/package.json +10 -9
- package/src/index.ts +15 -9
- package/tests/index.test.ts +4 -4
package/CHANGELOG.md
CHANGED
package/dist/js/modern/index.js
CHANGED
|
@@ -4,7 +4,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
4
4
|
|
|
5
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
6
|
|
|
7
|
-
import fs from 'fs
|
|
7
|
+
import fs from 'fs';
|
|
8
8
|
import path from 'path';
|
|
9
9
|
import { randomUUID } from 'crypto';
|
|
10
10
|
import { build } from 'esbuild';
|
|
@@ -12,7 +12,7 @@ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or ".
|
|
|
12
12
|
// "/test/node_modules/foo"
|
|
13
13
|
// "c:/node_modules/foo"
|
|
14
14
|
|
|
15
|
-
export const EXTERNAL_REGEXP = /^
|
|
15
|
+
export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
16
16
|
const CACHE_DIR = path.relative(process.cwd(), './node_modules/.node-bundle-require');
|
|
17
17
|
|
|
18
18
|
function inferLoader(ext) {
|
|
@@ -94,7 +94,7 @@ export async function bundleRequire(filepath, options) {
|
|
|
94
94
|
ctx.onLoad({
|
|
95
95
|
filter: JS_EXT_RE
|
|
96
96
|
}, async args => {
|
|
97
|
-
const contents =
|
|
97
|
+
const contents = fs.readFileSync(args.path, 'utf-8');
|
|
98
98
|
return {
|
|
99
99
|
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}`)),
|
|
100
100
|
loader: inferLoader(path.extname(args.path))
|
|
@@ -109,10 +109,18 @@ export async function bundleRequire(filepath, options) {
|
|
|
109
109
|
setup(_build) {
|
|
110
110
|
_build.onResolve({
|
|
111
111
|
filter: EXTERNAL_REGEXP
|
|
112
|
-
}, args =>
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
}, args => {
|
|
113
|
+
let external = true; // FIXME: windows external entrypoint
|
|
114
|
+
|
|
115
|
+
if (args.kind === 'entry-point') {
|
|
116
|
+
external = false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
path: args.path,
|
|
121
|
+
external
|
|
122
|
+
};
|
|
123
|
+
});
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
}]
|
|
@@ -124,7 +132,7 @@ export async function bundleRequire(filepath, options) {
|
|
|
124
132
|
mod = await req(outfile);
|
|
125
133
|
} finally {
|
|
126
134
|
// Remove the outfile after executed
|
|
127
|
-
|
|
135
|
+
fs.unlinkSync(outfile);
|
|
128
136
|
}
|
|
129
137
|
|
|
130
138
|
return mod;
|
package/dist/js/node/index.js
CHANGED
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.EXTERNAL_REGEXP = void 0;
|
|
7
7
|
exports.bundleRequire = bundleRequire;
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
10
10
|
|
|
11
11
|
var _path = _interopRequireDefault(require("path"));
|
|
12
12
|
|
|
@@ -26,7 +26,7 @@ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or ".
|
|
|
26
26
|
// "/test/node_modules/foo"
|
|
27
27
|
// "c:/node_modules/foo"
|
|
28
28
|
|
|
29
|
-
const EXTERNAL_REGEXP = /^
|
|
29
|
+
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
30
30
|
exports.EXTERNAL_REGEXP = EXTERNAL_REGEXP;
|
|
31
31
|
|
|
32
32
|
const CACHE_DIR = _path.default.relative(process.cwd(), './node_modules/.node-bundle-require');
|
|
@@ -110,7 +110,8 @@ async function bundleRequire(filepath, options) {
|
|
|
110
110
|
ctx.onLoad({
|
|
111
111
|
filter: JS_EXT_RE
|
|
112
112
|
}, async args => {
|
|
113
|
-
const contents =
|
|
113
|
+
const contents = _fs.default.readFileSync(args.path, 'utf-8');
|
|
114
|
+
|
|
114
115
|
return {
|
|
115
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}`)),
|
|
116
117
|
loader: inferLoader(_path.default.extname(args.path))
|
|
@@ -125,10 +126,18 @@ async function bundleRequire(filepath, options) {
|
|
|
125
126
|
setup(_build) {
|
|
126
127
|
_build.onResolve({
|
|
127
128
|
filter: EXTERNAL_REGEXP
|
|
128
|
-
}, args =>
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
}, args => {
|
|
130
|
+
let external = true; // FIXME: windows external entrypoint
|
|
131
|
+
|
|
132
|
+
if (args.kind === 'entry-point') {
|
|
133
|
+
external = false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
path: args.path,
|
|
138
|
+
external
|
|
139
|
+
};
|
|
140
|
+
});
|
|
132
141
|
}
|
|
133
142
|
|
|
134
143
|
}]
|
|
@@ -140,7 +149,7 @@ async function bundleRequire(filepath, options) {
|
|
|
140
149
|
mod = await req(outfile);
|
|
141
150
|
} finally {
|
|
142
151
|
// Remove the outfile after executed
|
|
143
|
-
|
|
152
|
+
_fs.default.unlinkSync(outfile);
|
|
144
153
|
}
|
|
145
154
|
|
|
146
155
|
return mod;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modern-js/node-bundle-require",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.2-canary.0",
|
|
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",
|
|
@@ -26,13 +26,19 @@
|
|
|
26
26
|
"default": "./dist/js/treeshaking/index.js"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"prepare": "pnpm build",
|
|
31
|
+
"new": "modern new",
|
|
32
|
+
"build": "modern build",
|
|
33
|
+
"test": "modern test --passWithNoTests"
|
|
34
|
+
},
|
|
29
35
|
"dependencies": {
|
|
30
36
|
"@babel/runtime": "^7",
|
|
31
37
|
"esbuild": "^0.13.8"
|
|
32
38
|
},
|
|
33
39
|
"devDependencies": {
|
|
34
|
-
"@modern-js/plugin-testing": "^1.
|
|
35
|
-
"@modern-js/module-tools": "^1.
|
|
40
|
+
"@modern-js/plugin-testing": "^1.1.1",
|
|
41
|
+
"@modern-js/module-tools": "^1.1.1",
|
|
36
42
|
"@types/jest": "^26.0.9",
|
|
37
43
|
"@types/node": "^14",
|
|
38
44
|
"typescript": "^4"
|
|
@@ -46,10 +52,5 @@
|
|
|
46
52
|
"publishConfig": {
|
|
47
53
|
"registry": "https://registry.npmjs.org/",
|
|
48
54
|
"access": "public"
|
|
49
|
-
},
|
|
50
|
-
"scripts": {
|
|
51
|
-
"new": "modern new",
|
|
52
|
-
"build": "modern build",
|
|
53
|
-
"test": "modern test --passWithNoTests"
|
|
54
55
|
}
|
|
55
|
-
}
|
|
56
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import fs from 'fs
|
|
1
|
+
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { randomUUID } from 'crypto';
|
|
4
4
|
import { build, Loader, Plugin, BuildOptions } from 'esbuild';
|
|
@@ -8,8 +8,7 @@ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
|
|
|
8
8
|
// Must not start with "/" or "./" or "../"
|
|
9
9
|
// "/test/node_modules/foo"
|
|
10
10
|
// "c:/node_modules/foo"
|
|
11
|
-
export const EXTERNAL_REGEXP =
|
|
12
|
-
/^([[a-zA-Z\]:].*node_modules)|^[a-zA-Z]{2,}:|^[a-zA-Z]{3,}|^(\/[a-zA-Z].*node_modules.*)/;
|
|
11
|
+
export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
13
12
|
|
|
14
13
|
const CACHE_DIR = path.relative(
|
|
15
14
|
process.cwd(),
|
|
@@ -114,7 +113,7 @@ export async function bundleRequire(filepath: string, options?: Options) {
|
|
|
114
113
|
name: 'replace-path',
|
|
115
114
|
setup(ctx) {
|
|
116
115
|
ctx.onLoad({ filter: JS_EXT_RE }, async args => {
|
|
117
|
-
const contents =
|
|
116
|
+
const contents = fs.readFileSync(args.path, 'utf-8');
|
|
118
117
|
return {
|
|
119
118
|
contents: contents
|
|
120
119
|
.replace(/\b__filename\b/g, JSON.stringify(args.path))
|
|
@@ -135,10 +134,17 @@ export async function bundleRequire(filepath: string, options?: Options) {
|
|
|
135
134
|
{
|
|
136
135
|
name: 'make-all-packages-external',
|
|
137
136
|
setup(_build) {
|
|
138
|
-
_build.onResolve({ filter: EXTERNAL_REGEXP }, args =>
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
137
|
+
_build.onResolve({ filter: EXTERNAL_REGEXP }, args => {
|
|
138
|
+
let external = true;
|
|
139
|
+
// FIXME: windows external entrypoint
|
|
140
|
+
if (args.kind === 'entry-point') {
|
|
141
|
+
external = false;
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
path: args.path,
|
|
145
|
+
external,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
142
148
|
},
|
|
143
149
|
},
|
|
144
150
|
],
|
|
@@ -150,7 +156,7 @@ export async function bundleRequire(filepath: string, options?: Options) {
|
|
|
150
156
|
mod = await req(outfile);
|
|
151
157
|
} finally {
|
|
152
158
|
// Remove the outfile after executed
|
|
153
|
-
|
|
159
|
+
fs.unlinkSync(outfile);
|
|
154
160
|
}
|
|
155
161
|
|
|
156
162
|
return mod;
|
package/tests/index.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { bundleRequire, EXTERNAL_REGEXP } from '
|
|
2
|
+
import { bundleRequire, EXTERNAL_REGEXP } from '../src';
|
|
3
3
|
|
|
4
4
|
test('require', async () => {
|
|
5
5
|
const result = await bundleRequire(
|
|
@@ -11,9 +11,9 @@ test('require', async () => {
|
|
|
11
11
|
describe('external regexp', () => {
|
|
12
12
|
expect(EXTERNAL_REGEXP.test('./test')).toBeFalsy();
|
|
13
13
|
expect(EXTERNAL_REGEXP.test('/test')).toBeFalsy();
|
|
14
|
-
expect(EXTERNAL_REGEXP.test('c:/foo')).
|
|
15
|
-
expect(EXTERNAL_REGEXP.test('C:/foo')).
|
|
14
|
+
expect(EXTERNAL_REGEXP.test('c:/foo')).toBeTruthy();
|
|
15
|
+
expect(EXTERNAL_REGEXP.test('C:/foo')).toBeTruthy();
|
|
16
16
|
expect(EXTERNAL_REGEXP.test('c:/node_modules/foo')).toBeTruthy();
|
|
17
17
|
expect(EXTERNAL_REGEXP.test('foo')).toBeTruthy();
|
|
18
|
-
expect(EXTERNAL_REGEXP.test('/test/node_modules')).
|
|
18
|
+
expect(EXTERNAL_REGEXP.test('/test/node_modules')).toBeFalsy();
|
|
19
19
|
});
|