@modern-js/node-bundle-require 2.0.0-beta.0 → 2.0.0-beta.1

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 CHANGED
@@ -1,5 +1,31 @@
1
1
  # @modern-js/node-bundle-require
2
2
 
3
+ ## 2.0.0-beta.1
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9: 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 [92f0ead]
16
+ - Updated dependencies [edd1cfb1af]
17
+ - Updated dependencies [cc971eabfc]
18
+ - Updated dependencies [5b9049f]
19
+ - Updated dependencies [92004d1]
20
+ - Updated dependencies [b8bbe036c7]
21
+ - Updated dependencies [d5a31df781]
22
+ - Updated dependencies [dda38c9]
23
+ - Updated dependencies [3bbea92b2a]
24
+ - Updated dependencies [abf3421]
25
+ - Updated dependencies [543be9558e]
26
+ - Updated dependencies [14b712d]
27
+ - @modern-js/utils@2.0.0-beta.1
28
+
3
29
  ## 2.0.0-beta.0
4
30
 
5
31
  ### Major Changes
@@ -1,47 +1,38 @@
1
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
2
  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
3
  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
4
  import path from 'path';
8
5
  import { fs, pkgUp, nanoid, CONFIG_CACHE_DIR, createDebugger } from '@modern-js/utils';
9
6
  import { build } from 'esbuild';
10
7
  const debug = createDebugger('node-bundle');
11
8
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
12
- const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/; // Must not start with "/" or "./" or "../"
9
+ const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
10
+
11
+ // Must not start with "/" or "./" or "../"
13
12
  // "/test/node_modules/foo"
14
13
  // "c:/node_modules/foo"
15
-
16
14
  export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
17
-
18
15
  function inferLoader(ext) {
19
16
  if (ext === '.mjs' || ext === '.cjs') {
20
17
  return 'js';
21
18
  }
22
-
23
19
  return ext.slice(1);
24
20
  }
25
-
26
21
  async function isTypeModulePkg(cwd) {
27
22
  const pkgJsonPath = await pkgUp({
28
23
  cwd
29
24
  });
30
-
31
25
  if (pkgJsonPath) {
32
26
  const pkgJson = await fs.readJSON(pkgJsonPath);
33
27
  return pkgJson.type === 'module';
34
28
  }
35
-
36
29
  return false;
37
30
  }
38
-
39
31
  export const defaultGetOutputFile = async filepath => path.resolve(CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`);
40
32
  export async function bundle(filepath, options) {
41
33
  if (!JS_EXT_RE.test(filepath)) {
42
34
  throw new Error(`${filepath} is not a valid JS file`);
43
35
  }
44
-
45
36
  debug('bundle', filepath, options);
46
37
  const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
47
38
  const outfile = await getOutputFile(path.basename(filepath));
@@ -56,10 +47,10 @@ export async function bundle(filepath, options) {
56
47
  // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
57
48
  target: 'esnext'
58
49
  }, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
59
- plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
50
+ plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []),
51
+ // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
60
52
  {
61
53
  name: 'native-node-modules',
62
-
63
54
  setup(build) {
64
55
  // If a ".node" file is imported within a module in the "file" namespace, resolve
65
56
  // it to an absolute path and put it into the "node-file" virtual namespace.
@@ -71,9 +62,10 @@ export async function bundle(filepath, options) {
71
62
  paths: [args.resolveDir]
72
63
  }),
73
64
  namespace: 'node-file'
74
- })); // Files in the "node-file" virtual namespace call "require()" on the
75
- // path from esbuild of the ".node" file in the output directory.
65
+ }));
76
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.
77
69
  build.onLoad({
78
70
  filter: /.*/,
79
71
  namespace: 'node-file'
@@ -83,27 +75,27 @@ export async function bundle(filepath, options) {
83
75
  try { module.exports = require(path) }
84
76
  catch {}
85
77
  `
86
- })); // If a ".node" file is imported within a module in the "node-file" namespace, put
78
+ }));
79
+
80
+ // If a ".node" file is imported within a module in the "node-file" namespace, put
87
81
  // it in the "file" namespace where esbuild's default loading behavior will handle
88
82
  // it. It is already an absolute path since we resolved it to one above.
89
-
90
83
  build.onResolve({
91
84
  filter: /\.node$/,
92
85
  namespace: 'node-file'
93
86
  }, args => ({
94
87
  path: args.path,
95
88
  namespace: 'file'
96
- })); // Tell esbuild's default loading behavior to use the "file" loader for
97
- // these ".node" files.
89
+ }));
98
90
 
91
+ // Tell esbuild's default loading behavior to use the "file" loader for
92
+ // these ".node" files.
99
93
  const opts = build.initialOptions;
100
94
  opts.loader = opts.loader || {};
101
95
  opts.loader['.node'] = 'file';
102
96
  }
103
-
104
97
  }, {
105
98
  name: 'replace-path',
106
-
107
99
  setup(ctx) {
108
100
  ctx.onLoad({
109
101
  filter: JS_EXT_RE
@@ -115,42 +107,38 @@ export async function bundle(filepath, options) {
115
107
  };
116
108
  });
117
109
  }
118
-
119
- }, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
110
+ },
111
+ // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
120
112
  {
121
113
  name: 'make-all-packages-external',
122
-
123
114
  setup(_build) {
124
115
  _build.onResolve({
125
116
  filter: EXTERNAL_REGEXP
126
117
  }, async args => {
127
- let external = true; // FIXME: windows external entrypoint
128
-
118
+ let external = true;
119
+ // FIXME: windows external entrypoint
129
120
  if (args.kind === 'entry-point') {
130
121
  external = false;
131
122
  }
132
-
133
123
  try {
134
124
  const resolvedPath = require.resolve(args.path, {
135
125
  paths: [args.resolveDir]
136
- }); // If it is a typescript or esm package, we should bundle it.
137
-
138
-
126
+ });
127
+ // If it is a typescript or esm package, we should bundle it.
139
128
  if (BUNDLED_EXT_RE.test(resolvedPath) || (await isTypeModulePkg(resolvedPath))) {
140
129
  return {
141
130
  external: false
142
131
  };
143
132
  }
144
- } catch (err) {// If the package can not be resolved, do nothing.
133
+ } catch (err) {
134
+ // If the package can not be resolved, do nothing.
145
135
  }
146
-
147
136
  return {
148
137
  path: args.path,
149
138
  external
150
139
  };
151
140
  });
152
141
  }
153
-
154
142
  }]
155
143
  }));
156
144
  return outfile;
@@ -1,27 +1,24 @@
1
1
  import { fs } from '@modern-js/utils';
2
2
  import { bundle, defaultGetOutputFile } from "./bundle";
3
3
  export { bundle, defaultGetOutputFile };
4
-
5
4
  function deleteRequireCache(path) {
6
5
  if (require.cache[path]) {
7
6
  delete require.cache[path];
8
7
  }
9
-
10
8
  if (module.children) {
11
9
  module.children = module.children.filter(item => item.filename !== path);
12
10
  }
13
11
  }
14
-
15
12
  export async function bundleRequire(filepath, options) {
16
13
  const configFile = await bundle(filepath, options);
17
14
  let mod;
18
15
  const req = (options === null || options === void 0 ? void 0 : options.require) || require;
19
-
20
16
  try {
21
- mod = await req(configFile); // Webpack will check require history for persistent cache.
17
+ mod = await req(configFile);
18
+
19
+ // Webpack will check require history for persistent cache.
22
20
  // If webpack can not resolve the file, the previous cache pack will become invalid.
23
21
  // The bundled file is temporary, so we should clear the require history to avoid breaking the webpack cache.
24
-
25
22
  deleteRequireCache(configFile);
26
23
  } finally {
27
24
  // default auto clear configFile
@@ -29,6 +26,5 @@ export async function bundleRequire(filepath, options) {
29
26
  fs.unlinkSync(configFile);
30
27
  }
31
28
  }
32
-
33
29
  return mod;
34
30
  }
@@ -6,60 +6,44 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.EXTERNAL_REGEXP = void 0;
7
7
  exports.bundle = bundle;
8
8
  exports.defaultGetOutputFile = void 0;
9
-
10
9
  var _path = _interopRequireDefault(require("path"));
11
-
12
10
  var _utils = require("@modern-js/utils");
13
-
14
11
  var _esbuild = require("esbuild");
15
-
16
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
13
  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; }
19
-
20
14
  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; }
21
-
22
15
  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; }
23
-
24
16
  const debug = (0, _utils.createDebugger)('node-bundle');
25
17
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
26
- const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/; // Must not start with "/" or "./" or "../"
18
+ const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
19
+
20
+ // Must not start with "/" or "./" or "../"
27
21
  // "/test/node_modules/foo"
28
22
  // "c:/node_modules/foo"
29
-
30
23
  const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
31
24
  exports.EXTERNAL_REGEXP = EXTERNAL_REGEXP;
32
-
33
25
  function inferLoader(ext) {
34
26
  if (ext === '.mjs' || ext === '.cjs') {
35
27
  return 'js';
36
28
  }
37
-
38
29
  return ext.slice(1);
39
30
  }
40
-
41
31
  async function isTypeModulePkg(cwd) {
42
32
  const pkgJsonPath = await (0, _utils.pkgUp)({
43
33
  cwd
44
34
  });
45
-
46
35
  if (pkgJsonPath) {
47
36
  const pkgJson = await _utils.fs.readJSON(pkgJsonPath);
48
37
  return pkgJson.type === 'module';
49
38
  }
50
-
51
39
  return false;
52
40
  }
53
-
54
41
  const defaultGetOutputFile = async filepath => _path.default.resolve(_utils.CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${(0, _utils.nanoid)()}.bundled.cjs`);
55
-
56
42
  exports.defaultGetOutputFile = defaultGetOutputFile;
57
-
58
43
  async function bundle(filepath, options) {
59
44
  if (!JS_EXT_RE.test(filepath)) {
60
45
  throw new Error(`${filepath} is not a valid JS file`);
61
46
  }
62
-
63
47
  debug('bundle', filepath, options);
64
48
  const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
65
49
  const outfile = await getOutputFile(_path.default.basename(filepath));
@@ -74,10 +58,10 @@ async function bundle(filepath, options) {
74
58
  // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
75
59
  target: 'esnext'
76
60
  }, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
77
- plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
61
+ plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []),
62
+ // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
78
63
  {
79
64
  name: 'native-node-modules',
80
-
81
65
  setup(build) {
82
66
  // If a ".node" file is imported within a module in the "file" namespace, resolve
83
67
  // it to an absolute path and put it into the "node-file" virtual namespace.
@@ -89,9 +73,10 @@ async function bundle(filepath, options) {
89
73
  paths: [args.resolveDir]
90
74
  }),
91
75
  namespace: 'node-file'
92
- })); // Files in the "node-file" virtual namespace call "require()" on the
93
- // path from esbuild of the ".node" file in the output directory.
76
+ }));
94
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.
95
80
  build.onLoad({
96
81
  filter: /.*/,
97
82
  namespace: 'node-file'
@@ -101,75 +86,70 @@ async function bundle(filepath, options) {
101
86
  try { module.exports = require(path) }
102
87
  catch {}
103
88
  `
104
- })); // If a ".node" file is imported within a module in the "node-file" namespace, put
89
+ }));
90
+
91
+ // If a ".node" file is imported within a module in the "node-file" namespace, put
105
92
  // it in the "file" namespace where esbuild's default loading behavior will handle
106
93
  // it. It is already an absolute path since we resolved it to one above.
107
-
108
94
  build.onResolve({
109
95
  filter: /\.node$/,
110
96
  namespace: 'node-file'
111
97
  }, args => ({
112
98
  path: args.path,
113
99
  namespace: 'file'
114
- })); // Tell esbuild's default loading behavior to use the "file" loader for
115
- // these ".node" files.
100
+ }));
116
101
 
102
+ // Tell esbuild's default loading behavior to use the "file" loader for
103
+ // these ".node" files.
117
104
  const opts = build.initialOptions;
118
105
  opts.loader = opts.loader || {};
119
106
  opts.loader['.node'] = 'file';
120
107
  }
121
-
122
108
  }, {
123
109
  name: 'replace-path',
124
-
125
110
  setup(ctx) {
126
111
  ctx.onLoad({
127
112
  filter: JS_EXT_RE
128
113
  }, async args => {
129
114
  const contents = _utils.fs.readFileSync(args.path, 'utf-8');
130
-
131
115
  return {
132
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}`)),
133
117
  loader: inferLoader(_path.default.extname(args.path))
134
118
  };
135
119
  });
136
120
  }
137
-
138
- }, // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
121
+ },
122
+ // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
139
123
  {
140
124
  name: 'make-all-packages-external',
141
-
142
125
  setup(_build) {
143
126
  _build.onResolve({
144
127
  filter: EXTERNAL_REGEXP
145
128
  }, async args => {
146
- let external = true; // FIXME: windows external entrypoint
147
-
129
+ let external = true;
130
+ // FIXME: windows external entrypoint
148
131
  if (args.kind === 'entry-point') {
149
132
  external = false;
150
133
  }
151
-
152
134
  try {
153
135
  const resolvedPath = require.resolve(args.path, {
154
136
  paths: [args.resolveDir]
155
- }); // If it is a typescript or esm package, we should bundle it.
156
-
157
-
137
+ });
138
+ // If it is a typescript or esm package, we should bundle it.
158
139
  if (BUNDLED_EXT_RE.test(resolvedPath) || (await isTypeModulePkg(resolvedPath))) {
159
140
  return {
160
141
  external: false
161
142
  };
162
143
  }
163
- } catch (err) {// If the package can not be resolved, do nothing.
144
+ } catch (err) {
145
+ // If the package can not be resolved, do nothing.
164
146
  }
165
-
166
147
  return {
167
148
  path: args.path,
168
149
  external
169
150
  };
170
151
  });
171
152
  }
172
-
173
153
  }]
174
154
  }));
175
155
  return outfile;
@@ -16,31 +16,26 @@ Object.defineProperty(exports, "defaultGetOutputFile", {
16
16
  return _bundle.defaultGetOutputFile;
17
17
  }
18
18
  });
19
-
20
19
  var _utils = require("@modern-js/utils");
21
-
22
20
  var _bundle = require("./bundle");
23
-
24
21
  function deleteRequireCache(path) {
25
22
  if (require.cache[path]) {
26
23
  delete require.cache[path];
27
24
  }
28
-
29
25
  if (module.children) {
30
26
  module.children = module.children.filter(item => item.filename !== path);
31
27
  }
32
28
  }
33
-
34
29
  async function bundleRequire(filepath, options) {
35
30
  const configFile = await (0, _bundle.bundle)(filepath, options);
36
31
  let mod;
37
32
  const req = (options === null || options === void 0 ? void 0 : options.require) || require;
38
-
39
33
  try {
40
- mod = await req(configFile); // Webpack will check require history for persistent cache.
34
+ mod = await req(configFile);
35
+
36
+ // Webpack will check require history for persistent cache.
41
37
  // If webpack can not resolve the file, the previous cache pack will become invalid.
42
38
  // The bundled file is temporary, so we should clear the require history to avoid breaking the webpack cache.
43
-
44
39
  deleteRequireCache(configFile);
45
40
  } finally {
46
41
  // default auto clear configFile
@@ -48,6 +43,5 @@ async function bundleRequire(filepath, options) {
48
43
  _utils.fs.unlinkSync(configFile);
49
44
  }
50
45
  }
51
-
52
46
  return mod;
53
47
  }
@@ -10,23 +10,19 @@ export interface Options {
10
10
  /**
11
11
  * esbuild options
12
12
  */
13
-
14
13
  esbuildOptions?: BuildOptions;
15
14
  /**
16
15
  * esbuild plugin
17
16
  */
18
-
19
17
  esbuildPlugins?: Plugin[];
20
18
  /**
21
19
  * Get the path to the output file
22
20
  * By default we simply replace the extension with `.bundled.cjs`
23
21
  */
24
-
25
22
  getOutputFile?: (filepath: string) => Promise<string>;
26
23
  /**
27
24
  * auto clear bundle file
28
25
  */
29
-
30
26
  autoClear?: boolean;
31
27
  }
32
28
  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.0",
3
+ "version": "2.0.0-beta.1",
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",
@@ -39,16 +39,16 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@babel/runtime": "^7.18.0",
42
- "@modern-js/utils": "2.0.0-beta.0",
43
- "esbuild": "0.15.7"
42
+ "esbuild": "0.15.7",
43
+ "@modern-js/utils": "2.0.0-beta.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@scripts/build": "2.0.0-beta.0",
47
- "@scripts/jest-config": "2.0.0-beta.0",
48
46
  "@types/jest": "^27",
49
47
  "@types/node": "^14",
50
48
  "jest": "^27",
51
- "typescript": "^4"
49
+ "typescript": "^4",
50
+ "@scripts/build": "2.0.0-beta.1",
51
+ "@scripts/jest-config": "2.0.0-beta.1"
52
52
  },
53
53
  "sideEffects": false,
54
54
  "publishConfig": {