@modern-js/node-bundle-require 2.14.0 → 2.16.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 CHANGED
@@ -1,5 +1,24 @@
1
1
  # @modern-js/node-bundle-require
2
2
 
3
+ ## 2.16.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 4e876ab: chore: package.json include the monorepo-relative directory
8
+
9
+ chore: 在 package.json 中声明 monorepo 的子路径
10
+
11
+ - Updated dependencies [5954330]
12
+ - Updated dependencies [7596520]
13
+ - Updated dependencies [4e876ab]
14
+ - @modern-js/utils@2.16.0
15
+
16
+ ## 2.15.0
17
+
18
+ ### Patch Changes
19
+
20
+ - @modern-js/utils@2.15.0
21
+
3
22
  ## 2.14.0
4
23
 
5
24
  ### Patch Changes
@@ -1,41 +1,28 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
8
6
  for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
- var bundle_exports = {};
29
- __export(bundle_exports, {
7
+ Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
30
13
  EXTERNAL_REGEXP: () => EXTERNAL_REGEXP,
31
- bundle: () => bundle,
32
- defaultGetOutputFile: () => defaultGetOutputFile
14
+ defaultGetOutputFile: () => defaultGetOutputFile,
15
+ bundle: () => bundle
33
16
  });
34
- module.exports = __toCommonJS(bundle_exports);
35
- var import_path = __toESM(require("path"));
36
- var import_utils = require("@modern-js/utils");
37
- var import_esbuild = require("esbuild");
38
- const debug = (0, import_utils.createDebugger)("node-bundle");
17
+ const _path = /* @__PURE__ */ _interop_require_default(require("path"));
18
+ const _utils = require("@modern-js/utils");
19
+ const _esbuild = require("esbuild");
20
+ function _interop_require_default(obj) {
21
+ return obj && obj.__esModule ? obj : {
22
+ default: obj
23
+ };
24
+ }
25
+ const debug = (0, _utils.createDebugger)("node-bundle");
39
26
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
40
27
  const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
41
28
  const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
@@ -46,26 +33,27 @@ function inferLoader(ext) {
46
33
  return ext.slice(1);
47
34
  }
48
35
  async function isTypeModulePkg(cwd) {
49
- const pkgJsonPath = await (0, import_utils.pkgUp)({ cwd });
36
+ const pkgJsonPath = await (0, _utils.pkgUp)({
37
+ cwd
38
+ });
50
39
  if (pkgJsonPath) {
51
- const pkgJson = await import_utils.fs.readJSON(pkgJsonPath);
40
+ const pkgJson = await _utils.fs.readJSON(pkgJsonPath);
52
41
  return pkgJson.type === "module";
53
42
  }
54
43
  return false;
55
44
  }
56
- const defaultGetOutputFile = async (filepath) => import_path.default.resolve(
57
- import_utils.CONFIG_CACHE_DIR,
58
- `${filepath}-${Date.now()}.${(0, import_utils.nanoid)()}.bundled.cjs`
59
- );
45
+ const defaultGetOutputFile = async (filepath) => _path.default.resolve(_utils.CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${(0, _utils.nanoid)()}.bundled.cjs`);
60
46
  async function bundle(filepath, options) {
61
47
  if (!JS_EXT_RE.test(filepath)) {
62
48
  throw new Error(`${filepath} is not a valid JS file`);
63
49
  }
64
50
  debug("bundle", filepath, options);
65
- const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
66
- const outfile = await getOutputFile(import_path.default.basename(filepath));
67
- await (0, import_esbuild.build)({
68
- entryPoints: [filepath],
51
+ const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
52
+ const outfile = await getOutputFile(_path.default.basename(filepath));
53
+ await (0, _esbuild.build)({
54
+ entryPoints: [
55
+ filepath
56
+ ],
69
57
  outfile,
70
58
  format: "cjs",
71
59
  platform: "node",
@@ -74,32 +62,42 @@ async function bundle(filepath, options) {
74
62
  // sets `target: "es5"`
75
63
  // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
76
64
  target: "esnext",
77
- ...options == null ? void 0 : options.esbuildOptions,
65
+ ...options === null || options === void 0 ? void 0 : options.esbuildOptions,
78
66
  plugins: [
79
- ...(options == null ? void 0 : options.esbuildPlugins) || [],
67
+ ...(options === null || options === void 0 ? void 0 : options.esbuildPlugins) || [],
80
68
  // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
81
69
  {
82
70
  name: "native-node-modules",
83
- setup(build2) {
84
- build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
85
- path: require.resolve(args.path, { paths: [args.resolveDir] }),
71
+ setup(build) {
72
+ build.onResolve({
73
+ filter: /\.node$/,
74
+ namespace: "file"
75
+ }, (args) => ({
76
+ path: require.resolve(args.path, {
77
+ paths: [
78
+ args.resolveDir
79
+ ]
80
+ }),
86
81
  namespace: "node-file"
87
82
  }));
88
- build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
83
+ build.onLoad({
84
+ filter: /.*/,
85
+ namespace: "node-file"
86
+ }, (args) => ({
89
87
  contents: `
90
88
  import path from ${JSON.stringify(args.path)}
91
89
  try { module.exports = require(path) }
92
90
  catch {}
93
91
  `
94
92
  }));
95
- build2.onResolve(
96
- { filter: /\.node$/, namespace: "node-file" },
97
- (args) => ({
98
- path: args.path,
99
- namespace: "file"
100
- })
101
- );
102
- const opts = build2.initialOptions;
93
+ build.onResolve({
94
+ filter: /\.node$/,
95
+ namespace: "node-file"
96
+ }, (args) => ({
97
+ path: args.path,
98
+ namespace: "file"
99
+ }));
100
+ const opts = build.initialOptions;
103
101
  opts.loader = opts.loader || {};
104
102
  opts.loader[".node"] = "file";
105
103
  }
@@ -107,17 +105,13 @@ async function bundle(filepath, options) {
107
105
  {
108
106
  name: "replace-path",
109
107
  setup(ctx) {
110
- ctx.onLoad({ filter: JS_EXT_RE }, async (args) => {
111
- const contents = import_utils.fs.readFileSync(args.path, "utf-8");
108
+ ctx.onLoad({
109
+ filter: JS_EXT_RE
110
+ }, async (args) => {
111
+ const contents = _utils.fs.readFileSync(args.path, "utf-8");
112
112
  return {
113
- contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
114
- /\b__dirname\b/g,
115
- JSON.stringify(import_path.default.dirname(args.path))
116
- ).replace(
117
- /\bimport\.meta\.url\b/g,
118
- JSON.stringify(`file://${args.path}`)
119
- ),
120
- loader: inferLoader(import_path.default.extname(args.path))
113
+ 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}`)),
114
+ loader: inferLoader(_path.default.extname(args.path))
121
115
  };
122
116
  });
123
117
  }
@@ -126,14 +120,18 @@ async function bundle(filepath, options) {
126
120
  {
127
121
  name: "make-all-packages-external",
128
122
  setup(_build) {
129
- _build.onResolve({ filter: EXTERNAL_REGEXP }, async (args) => {
123
+ _build.onResolve({
124
+ filter: EXTERNAL_REGEXP
125
+ }, async (args) => {
130
126
  let external = true;
131
127
  if (args.kind === "entry-point") {
132
128
  external = false;
133
129
  }
134
130
  try {
135
131
  const resolvedPath = require.resolve(args.path, {
136
- paths: [args.resolveDir]
132
+ paths: [
133
+ args.resolveDir
134
+ ]
137
135
  });
138
136
  if (BUNDLED_EXT_RE.test(resolvedPath) || await isTypeModulePkg(resolvedPath)) {
139
137
  return {
@@ -153,9 +151,3 @@ async function bundle(filepath, options) {
153
151
  });
154
152
  return outfile;
155
153
  }
156
- // Annotate the CommonJS export names for ESM import in node:
157
- 0 && (module.exports = {
158
- EXTERNAL_REGEXP,
159
- bundle,
160
- defaultGetOutputFile
161
- });
package/dist/cjs/index.js CHANGED
@@ -1,46 +1,32 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
6
  for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var src_exports = {};
19
- __export(src_exports, {
20
- bundle: () => import_bundle.bundle,
21
- bundleRequire: () => bundleRequire,
22
- defaultGetOutputFile: () => import_bundle.defaultGetOutputFile
7
+ Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ bundle: () => _bundle.bundle,
14
+ defaultGetOutputFile: () => _bundle.defaultGetOutputFile,
15
+ bundleRequire: () => bundleRequire
23
16
  });
24
- module.exports = __toCommonJS(src_exports);
25
- var import_utils = require("@modern-js/utils");
26
- var import_bundle = require("./bundle");
17
+ const _utils = require("@modern-js/utils");
18
+ const _bundle = require("./bundle");
27
19
  async function bundleRequire(filepath, options) {
28
- const configFile = await (0, import_bundle.bundle)(filepath, options);
20
+ const configFile = await (0, _bundle.bundle)(filepath, options);
29
21
  let mod;
30
- const req = (options == null ? void 0 : options.require) || require;
22
+ const req = (options === null || options === void 0 ? void 0 : options.require) || require;
31
23
  try {
32
24
  mod = await req(configFile);
33
- (0, import_utils.deleteRequireCache)(configFile);
25
+ (0, _utils.deleteRequireCache)(configFile);
34
26
  } finally {
35
- if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
36
- import_utils.fs.unlinkSync(configFile);
27
+ if ((options === null || options === void 0 ? void 0 : options.autoClear) === void 0 || options.autoClear) {
28
+ _utils.fs.unlinkSync(configFile);
37
29
  }
38
30
  }
39
31
  return mod;
40
32
  }
41
- // Annotate the CommonJS export names for ESM import in node:
42
- 0 && (module.exports = {
43
- bundle,
44
- bundleRequire,
45
- defaultGetOutputFile
46
- });
@@ -1,16 +1,10 @@
1
1
  import path from "path";
2
- import {
3
- fs,
4
- pkgUp,
5
- nanoid,
6
- CONFIG_CACHE_DIR,
7
- createDebugger
8
- } from "@modern-js/utils";
2
+ import { fs, pkgUp, nanoid, CONFIG_CACHE_DIR, createDebugger } from "@modern-js/utils";
9
3
  import { build } from "esbuild";
10
4
  const debug = createDebugger("node-bundle");
11
5
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
12
6
  const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
13
- const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
7
+ export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
14
8
  function inferLoader(ext) {
15
9
  if (ext === ".mjs" || ext === ".cjs") {
16
10
  return "js";
@@ -18,26 +12,27 @@ function inferLoader(ext) {
18
12
  return ext.slice(1);
19
13
  }
20
14
  async function isTypeModulePkg(cwd) {
21
- const pkgJsonPath = await pkgUp({ cwd });
15
+ const pkgJsonPath = await pkgUp({
16
+ cwd
17
+ });
22
18
  if (pkgJsonPath) {
23
19
  const pkgJson = await fs.readJSON(pkgJsonPath);
24
20
  return pkgJson.type === "module";
25
21
  }
26
22
  return false;
27
23
  }
28
- const defaultGetOutputFile = async (filepath) => path.resolve(
29
- CONFIG_CACHE_DIR,
30
- `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`
31
- );
32
- async function bundle(filepath, options) {
24
+ export const defaultGetOutputFile = async (filepath) => path.resolve(CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`);
25
+ export async function bundle(filepath, options) {
33
26
  if (!JS_EXT_RE.test(filepath)) {
34
27
  throw new Error(`${filepath} is not a valid JS file`);
35
28
  }
36
29
  debug("bundle", filepath, options);
37
- const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
30
+ const getOutputFile = (options === null || options === void 0 ? void 0 : options.getOutputFile) || defaultGetOutputFile;
38
31
  const outfile = await getOutputFile(path.basename(filepath));
39
32
  await build({
40
- entryPoints: [filepath],
33
+ entryPoints: [
34
+ filepath
35
+ ],
41
36
  outfile,
42
37
  format: "cjs",
43
38
  platform: "node",
@@ -46,31 +41,41 @@ async function bundle(filepath, options) {
46
41
  // sets `target: "es5"`
47
42
  // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
48
43
  target: "esnext",
49
- ...options == null ? void 0 : options.esbuildOptions,
44
+ ...options === null || options === void 0 ? void 0 : options.esbuildOptions,
50
45
  plugins: [
51
- ...(options == null ? void 0 : options.esbuildPlugins) || [],
46
+ ...(options === null || options === void 0 ? void 0 : options.esbuildPlugins) || [],
52
47
  // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
53
48
  {
54
49
  name: "native-node-modules",
55
50
  setup(build2) {
56
- build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
57
- path: require.resolve(args.path, { paths: [args.resolveDir] }),
51
+ build2.onResolve({
52
+ filter: /\.node$/,
53
+ namespace: "file"
54
+ }, (args) => ({
55
+ path: require.resolve(args.path, {
56
+ paths: [
57
+ args.resolveDir
58
+ ]
59
+ }),
58
60
  namespace: "node-file"
59
61
  }));
60
- build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
62
+ build2.onLoad({
63
+ filter: /.*/,
64
+ namespace: "node-file"
65
+ }, (args) => ({
61
66
  contents: `
62
67
  import path from ${JSON.stringify(args.path)}
63
68
  try { module.exports = require(path) }
64
69
  catch {}
65
70
  `
66
71
  }));
67
- build2.onResolve(
68
- { filter: /\.node$/, namespace: "node-file" },
69
- (args) => ({
70
- path: args.path,
71
- namespace: "file"
72
- })
73
- );
72
+ build2.onResolve({
73
+ filter: /\.node$/,
74
+ namespace: "node-file"
75
+ }, (args) => ({
76
+ path: args.path,
77
+ namespace: "file"
78
+ }));
74
79
  const opts = build2.initialOptions;
75
80
  opts.loader = opts.loader || {};
76
81
  opts.loader[".node"] = "file";
@@ -79,16 +84,12 @@ async function bundle(filepath, options) {
79
84
  {
80
85
  name: "replace-path",
81
86
  setup(ctx) {
82
- ctx.onLoad({ filter: JS_EXT_RE }, async (args) => {
87
+ ctx.onLoad({
88
+ filter: JS_EXT_RE
89
+ }, async (args) => {
83
90
  const contents = fs.readFileSync(args.path, "utf-8");
84
91
  return {
85
- contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
86
- /\b__dirname\b/g,
87
- JSON.stringify(path.dirname(args.path))
88
- ).replace(
89
- /\bimport\.meta\.url\b/g,
90
- JSON.stringify(`file://${args.path}`)
91
- ),
92
+ 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}`)),
92
93
  loader: inferLoader(path.extname(args.path))
93
94
  };
94
95
  });
@@ -98,14 +99,18 @@ async function bundle(filepath, options) {
98
99
  {
99
100
  name: "make-all-packages-external",
100
101
  setup(_build) {
101
- _build.onResolve({ filter: EXTERNAL_REGEXP }, async (args) => {
102
+ _build.onResolve({
103
+ filter: EXTERNAL_REGEXP
104
+ }, async (args) => {
102
105
  let external = true;
103
106
  if (args.kind === "entry-point") {
104
107
  external = false;
105
108
  }
106
109
  try {
107
110
  const resolvedPath = require.resolve(args.path, {
108
- paths: [args.resolveDir]
111
+ paths: [
112
+ args.resolveDir
113
+ ]
109
114
  });
110
115
  if (BUNDLED_EXT_RE.test(resolvedPath) || await isTypeModulePkg(resolvedPath)) {
111
116
  return {
@@ -125,8 +130,3 @@ async function bundle(filepath, options) {
125
130
  });
126
131
  return outfile;
127
132
  }
128
- export {
129
- EXTERNAL_REGEXP,
130
- bundle,
131
- defaultGetOutputFile
132
- };
package/dist/esm/index.js CHANGED
@@ -1,21 +1,17 @@
1
1
  import { fs, deleteRequireCache } from "@modern-js/utils";
2
2
  import { bundle, defaultGetOutputFile } from "./bundle";
3
- async function bundleRequire(filepath, options) {
3
+ export { bundle, defaultGetOutputFile };
4
+ export async function bundleRequire(filepath, options) {
4
5
  const configFile = await bundle(filepath, options);
5
6
  let mod;
6
- const req = (options == null ? void 0 : options.require) || require;
7
+ const req = (options === null || options === void 0 ? void 0 : options.require) || require;
7
8
  try {
8
9
  mod = await req(configFile);
9
10
  deleteRequireCache(configFile);
10
11
  } finally {
11
- if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
12
+ if ((options === null || options === void 0 ? void 0 : options.autoClear) === void 0 || options.autoClear) {
12
13
  fs.unlinkSync(configFile);
13
14
  }
14
15
  }
15
16
  return mod;
16
17
  }
17
- export {
18
- bundle,
19
- bundleRequire,
20
- defaultGetOutputFile
21
- };
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@modern-js/node-bundle-require",
3
- "version": "2.14.0",
3
+ "version": "2.16.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",
7
- "repository": "web-infra-dev/modern.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/web-infra-dev/modern.js",
10
+ "directory": "packages/toolkit/node-bundle-require"
11
+ },
8
12
  "license": "MIT",
9
13
  "keywords": [
10
14
  "react",
@@ -43,15 +47,15 @@
43
47
  "dependencies": {
44
48
  "@babel/runtime": "^7.18.0",
45
49
  "esbuild": "0.15.7",
46
- "@modern-js/utils": "2.14.0"
50
+ "@modern-js/utils": "2.16.0"
47
51
  },
48
52
  "devDependencies": {
49
53
  "@types/jest": "^29",
50
54
  "@types/node": "^14",
51
55
  "jest": "^29",
52
56
  "typescript": "^4",
53
- "@scripts/build": "2.14.0",
54
- "@scripts/jest-config": "2.14.0"
57
+ "@scripts/build": "2.16.0",
58
+ "@scripts/jest-config": "2.16.0"
55
59
  },
56
60
  "sideEffects": false,
57
61
  "publishConfig": {