@modern-js/node-bundle-require 2.35.0 → 2.36.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.
@@ -1,30 +1,42 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
6
9
  for (var name in all)
7
- Object.defineProperty(target, name, {
8
- enumerable: true,
9
- get: all[name]
10
- });
11
- }
12
- _export(exports, {
13
- EXTERNAL_REGEXP: function() {
14
- return EXTERNAL_REGEXP;
15
- },
16
- defaultGetOutputFile: function() {
17
- return defaultGetOutputFile;
18
- },
19
- bundle: function() {
20
- return bundle;
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
17
  }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var bundle_exports = {};
30
+ __export(bundle_exports, {
31
+ EXTERNAL_REGEXP: () => EXTERNAL_REGEXP,
32
+ bundle: () => bundle,
33
+ defaultGetOutputFile: () => defaultGetOutputFile
22
34
  });
23
- const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
24
- const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
25
- const _utils = require("@modern-js/utils");
26
- const _esbuild = require("esbuild");
27
- const debug = (0, _utils.createDebugger)("node-bundle");
35
+ module.exports = __toCommonJS(bundle_exports);
36
+ var import_path = __toESM(require("path"));
37
+ var import_utils = require("@modern-js/utils");
38
+ var import_esbuild = require("esbuild");
39
+ const debug = (0, import_utils.createDebugger)("node-bundle");
28
40
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
29
41
  const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
30
42
  const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
@@ -35,26 +47,25 @@ function inferLoader(ext) {
35
47
  return ext.slice(1);
36
48
  }
37
49
  async function isTypeModulePkg(cwd) {
38
- const pkgJsonPath = await (0, _utils.pkgUp)({
50
+ const pkgJsonPath = await (0, import_utils.pkgUp)({
39
51
  cwd
40
52
  });
41
53
  if (pkgJsonPath) {
42
54
  var _pkgJson_main;
43
- const pkgJson = await _utils.fs.readJSON(pkgJsonPath);
44
- const ext = _path.default.extname(cwd);
55
+ const pkgJson = await import_utils.fs.readJSON(pkgJsonPath);
56
+ const ext = import_path.default.extname(cwd);
45
57
  return pkgJson.type === "module" && ext !== ".cjs" && !((_pkgJson_main = pkgJson.main) === null || _pkgJson_main === void 0 ? void 0 : _pkgJson_main.endsWith(".cjs"));
46
58
  }
47
59
  return false;
48
60
  }
49
- const defaultGetOutputFile = async (filepath) => _path.default.resolve(_utils.CONFIG_CACHE_DIR, `${filepath.replace(/\.(js|ts)/, "")}.${(0, _utils.nanoid)(8)}.cjs`);
61
+ const defaultGetOutputFile = async (filepath) => import_path.default.resolve(import_utils.CONFIG_CACHE_DIR, `${filepath.replace(/\.(js|ts)/, "")}.${(0, import_utils.nanoid)(8)}.cjs`);
50
62
  async function bundle(filepath, options) {
51
- var _options, _options1, _options2, _options3;
52
63
  if (!JS_EXT_RE.test(filepath)) {
53
64
  throw new Error(`${filepath} is not a valid JS file`);
54
65
  }
55
66
  debug("bundle", filepath, options);
56
- const getOutputFile = ((_options = options) === null || _options === void 0 ? void 0 : _options.getOutputFile) || defaultGetOutputFile;
57
- const outfile = await getOutputFile(_path.default.basename(filepath));
67
+ const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
68
+ const outfile = await getOutputFile(import_path.default.basename(filepath));
58
69
  const esbuildOptions = {
59
70
  entryPoints: [
60
71
  filepath
@@ -67,14 +78,14 @@ async function bundle(filepath, options) {
67
78
  // sets `target: "es5"`
68
79
  // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
69
80
  target: "esnext",
70
- ...(_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.esbuildOptions,
81
+ ...options === null || options === void 0 ? void 0 : options.esbuildOptions,
71
82
  plugins: [
72
- ...((_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.esbuildPlugins) || [],
83
+ ...(options === null || options === void 0 ? void 0 : options.esbuildPlugins) || [],
73
84
  // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
74
85
  {
75
86
  name: "native-node-modules",
76
- setup(build) {
77
- build.onResolve({
87
+ setup(build2) {
88
+ build2.onResolve({
78
89
  filter: /\.node$/,
79
90
  namespace: "file"
80
91
  }, (args) => ({
@@ -85,7 +96,7 @@ async function bundle(filepath, options) {
85
96
  }),
86
97
  namespace: "node-file"
87
98
  }));
88
- build.onLoad({
99
+ build2.onLoad({
89
100
  filter: /.*/,
90
101
  namespace: "node-file"
91
102
  }, (args) => ({
@@ -95,14 +106,14 @@ async function bundle(filepath, options) {
95
106
  catch {}
96
107
  `
97
108
  }));
98
- build.onResolve({
109
+ build2.onResolve({
99
110
  filter: /\.node$/,
100
111
  namespace: "node-file"
101
112
  }, (args) => ({
102
113
  path: args.path,
103
114
  namespace: "file"
104
115
  }));
105
- const opts = build.initialOptions;
116
+ const opts = build2.initialOptions;
106
117
  opts.loader = opts.loader || {};
107
118
  opts.loader[".node"] = "file";
108
119
  }
@@ -113,10 +124,10 @@ async function bundle(filepath, options) {
113
124
  ctx.onLoad({
114
125
  filter: JS_EXT_RE
115
126
  }, async (args) => {
116
- const contents = _utils.fs.readFileSync(args.path, "utf-8");
127
+ const contents = import_utils.fs.readFileSync(args.path, "utf-8");
117
128
  return {
118
- 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}`)),
119
- loader: inferLoader(_path.default.extname(args.path))
129
+ contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(import_path.default.dirname(args.path))).replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`)),
130
+ loader: inferLoader(import_path.default.extname(args.path))
120
131
  };
121
132
  });
122
133
  }
@@ -154,12 +165,18 @@ async function bundle(filepath, options) {
154
165
  }
155
166
  ]
156
167
  };
157
- if ((_options3 = options) === null || _options3 === void 0 ? void 0 : _options3.watch) {
158
- const ctx = await (0, _esbuild.context)(esbuildOptions);
168
+ if (options === null || options === void 0 ? void 0 : options.watch) {
169
+ const ctx = await (0, import_esbuild.context)(esbuildOptions);
159
170
  await ctx.rebuild();
160
171
  await ctx.watch();
161
172
  } else {
162
- await (0, _esbuild.build)(esbuildOptions);
173
+ await (0, import_esbuild.build)(esbuildOptions);
163
174
  }
164
175
  return outfile;
165
176
  }
177
+ // Annotate the CommonJS export names for ESM import in node:
178
+ 0 && (module.exports = {
179
+ EXTERNAL_REGEXP,
180
+ bundle,
181
+ defaultGetOutputFile
182
+ });
package/dist/cjs/index.js CHANGED
@@ -1,40 +1,47 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
6
7
  for (var name in all)
7
- Object.defineProperty(target, name, {
8
- enumerable: true,
9
- get: all[name]
10
- });
11
- }
12
- _export(exports, {
13
- bundle: function() {
14
- return _bundle.bundle;
15
- },
16
- defaultGetOutputFile: function() {
17
- return _bundle.defaultGetOutputFile;
18
- },
19
- bundleRequire: function() {
20
- return bundleRequire;
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
15
  }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var src_exports = {};
20
+ __export(src_exports, {
21
+ bundle: () => import_bundle.bundle,
22
+ bundleRequire: () => bundleRequire,
23
+ defaultGetOutputFile: () => import_bundle.defaultGetOutputFile
22
24
  });
23
- const _utils = require("@modern-js/utils");
24
- const _bundle = require("./bundle");
25
+ module.exports = __toCommonJS(src_exports);
26
+ var import_utils = require("@modern-js/utils");
27
+ var import_bundle = require("./bundle");
25
28
  async function bundleRequire(filepath, options) {
26
- var _options;
27
- const configFile = await (0, _bundle.bundle)(filepath, options);
29
+ const configFile = await (0, import_bundle.bundle)(filepath, options);
28
30
  let mod;
29
- const req = ((_options = options) === null || _options === void 0 ? void 0 : _options.require) || require;
31
+ const req = (options === null || options === void 0 ? void 0 : options.require) || require;
30
32
  try {
31
33
  mod = await req(configFile);
32
- (0, _utils.deleteRequireCache)(configFile);
34
+ (0, import_utils.deleteRequireCache)(configFile);
33
35
  } finally {
34
- var _options1;
35
- if (((_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.autoClear) === void 0 || options.autoClear) {
36
- _utils.fs.unlinkSync(configFile);
36
+ if ((options === null || options === void 0 ? void 0 : options.autoClear) === void 0 || options.autoClear) {
37
+ import_utils.fs.unlinkSync(configFile);
37
38
  }
38
39
  }
39
40
  return mod;
40
41
  }
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {
44
+ bundle,
45
+ bundleRequire,
46
+ defaultGetOutputFile
47
+ });
@@ -4,7 +4,7 @@ import { build, context } from "esbuild";
4
4
  const debug = createDebugger("node-bundle");
5
5
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
6
6
  const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
7
- export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
7
+ const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
8
8
  function inferLoader(ext) {
9
9
  if (ext === ".mjs" || ext === ".cjs") {
10
10
  return "js";
@@ -23,14 +23,13 @@ async function isTypeModulePkg(cwd) {
23
23
  }
24
24
  return false;
25
25
  }
26
- export const defaultGetOutputFile = async (filepath) => path.resolve(CONFIG_CACHE_DIR, `${filepath.replace(/\.(js|ts)/, "")}.${nanoid(8)}.cjs`);
27
- export async function bundle(filepath, options) {
28
- var _options, _options1, _options2, _options3;
26
+ const defaultGetOutputFile = async (filepath) => path.resolve(CONFIG_CACHE_DIR, `${filepath.replace(/\.(js|ts)/, "")}.${nanoid(8)}.cjs`);
27
+ async function bundle(filepath, options) {
29
28
  if (!JS_EXT_RE.test(filepath)) {
30
29
  throw new Error(`${filepath} is not a valid JS file`);
31
30
  }
32
31
  debug("bundle", filepath, options);
33
- const getOutputFile = ((_options = options) === null || _options === void 0 ? void 0 : _options.getOutputFile) || defaultGetOutputFile;
32
+ const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
34
33
  const outfile = await getOutputFile(path.basename(filepath));
35
34
  const esbuildOptions = {
36
35
  entryPoints: [
@@ -44,9 +43,9 @@ export async function bundle(filepath, options) {
44
43
  // sets `target: "es5"`
45
44
  // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
46
45
  target: "esnext",
47
- ...(_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.esbuildOptions,
46
+ ...options === null || options === void 0 ? void 0 : options.esbuildOptions,
48
47
  plugins: [
49
- ...((_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.esbuildPlugins) || [],
48
+ ...(options === null || options === void 0 ? void 0 : options.esbuildPlugins) || [],
50
49
  // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
51
50
  {
52
51
  name: "native-node-modules",
@@ -131,7 +130,7 @@ export async function bundle(filepath, options) {
131
130
  }
132
131
  ]
133
132
  };
134
- if ((_options3 = options) === null || _options3 === void 0 ? void 0 : _options3.watch) {
133
+ if (options === null || options === void 0 ? void 0 : options.watch) {
135
134
  const ctx = await context(esbuildOptions);
136
135
  await ctx.rebuild();
137
136
  await ctx.watch();
@@ -140,3 +139,8 @@ export async function bundle(filepath, options) {
140
139
  }
141
140
  return outfile;
142
141
  }
142
+ export {
143
+ EXTERNAL_REGEXP,
144
+ bundle,
145
+ defaultGetOutputFile
146
+ };
package/dist/esm/index.js CHANGED
@@ -1,19 +1,21 @@
1
1
  import { fs, deleteRequireCache } from "@modern-js/utils";
2
2
  import { bundle, defaultGetOutputFile } from "./bundle";
3
- export { bundle, defaultGetOutputFile };
4
- export async function bundleRequire(filepath, options) {
5
- var _options;
3
+ async function bundleRequire(filepath, options) {
6
4
  const configFile = await bundle(filepath, options);
7
5
  let mod;
8
- const req = ((_options = options) === null || _options === void 0 ? void 0 : _options.require) || require;
6
+ const req = (options === null || options === void 0 ? void 0 : options.require) || require;
9
7
  try {
10
8
  mod = await req(configFile);
11
9
  deleteRequireCache(configFile);
12
10
  } finally {
13
- var _options1;
14
- if (((_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.autoClear) === void 0 || options.autoClear) {
11
+ if ((options === null || options === void 0 ? void 0 : options.autoClear) === void 0 || options.autoClear) {
15
12
  fs.unlinkSync(configFile);
16
13
  }
17
14
  }
18
15
  return mod;
19
16
  }
17
+ export {
18
+ bundle,
19
+ bundleRequire,
20
+ defaultGetOutputFile
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/node-bundle-require",
3
- "version": "2.35.0",
3
+ "version": "2.36.0",
4
4
  "description": "A Progressive React Framework for modern web development.",
5
5
  "homepage": "https://modernjs.dev",
6
6
  "bugs": "https://github.com/web-infra-dev/modern.js/issues",
@@ -47,15 +47,15 @@
47
47
  "dependencies": {
48
48
  "esbuild": "0.17.19",
49
49
  "@swc/helpers": "0.5.1",
50
- "@modern-js/utils": "2.35.0"
50
+ "@modern-js/utils": "2.36.0"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/jest": "^29",
54
54
  "@types/node": "^14",
55
55
  "jest": "^29",
56
56
  "typescript": "^5",
57
- "@scripts/build": "2.35.0",
58
- "@scripts/jest-config": "2.35.0"
57
+ "@scripts/build": "2.36.0",
58
+ "@scripts/jest-config": "2.36.0"
59
59
  },
60
60
  "sideEffects": false,
61
61
  "publishConfig": {