@modern-js/node-bundle-require 2.4.1-beta.0 → 2.6.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,11 +1,28 @@
1
1
  # @modern-js/node-bundle-require
2
2
 
3
- ## 2.4.1-beta.0
3
+ ## 2.6.0
4
4
 
5
5
  ### Patch Changes
6
6
 
7
+ - Updated dependencies [e1f799e]
8
+ - Updated dependencies [7915ab3]
9
+ - Updated dependencies [0fe658a]
10
+ - @modern-js/utils@2.6.0
11
+
12
+ ## 2.5.0
13
+
14
+ ### Patch Changes
15
+
16
+ - 89ca6cc: refactor: merge build-config into scripts/build
17
+
18
+ refactor: 把 build-config 合并进 scripts/build
19
+
20
+ - 30614fa: chore: modify package.json entry fields and build config
21
+ chore: 更改 package.json entry 字段以及构建配置
22
+ - Updated dependencies [30614fa]
23
+ - Updated dependencies [1b0ce87]
7
24
  - Updated dependencies [11c053b]
8
- - @modern-js/utils@2.4.1-beta.0
25
+ - @modern-js/utils@2.5.0
9
26
 
10
27
  ## 2.4.0
11
28
 
@@ -0,0 +1,161 @@
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) => {
8
+ 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, {
30
+ EXTERNAL_REGEXP: () => EXTERNAL_REGEXP,
31
+ bundle: () => bundle,
32
+ defaultGetOutputFile: () => defaultGetOutputFile
33
+ });
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");
39
+ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
40
+ const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
41
+ const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
42
+ function inferLoader(ext) {
43
+ if (ext === ".mjs" || ext === ".cjs") {
44
+ return "js";
45
+ }
46
+ return ext.slice(1);
47
+ }
48
+ async function isTypeModulePkg(cwd) {
49
+ const pkgJsonPath = await (0, import_utils.pkgUp)({ cwd });
50
+ if (pkgJsonPath) {
51
+ const pkgJson = await import_utils.fs.readJSON(pkgJsonPath);
52
+ return pkgJson.type === "module";
53
+ }
54
+ return false;
55
+ }
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
+ );
60
+ async function bundle(filepath, options) {
61
+ if (!JS_EXT_RE.test(filepath)) {
62
+ throw new Error(`${filepath} is not a valid JS file`);
63
+ }
64
+ 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],
69
+ outfile,
70
+ format: "cjs",
71
+ platform: "node",
72
+ bundle: true,
73
+ // fix transforming error when the project's tsconfig.json
74
+ // sets `target: "es5"`
75
+ // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
76
+ target: "esnext",
77
+ ...options == null ? void 0 : options.esbuildOptions,
78
+ plugins: [
79
+ ...(options == null ? void 0 : options.esbuildPlugins) || [],
80
+ // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
81
+ {
82
+ name: "native-node-modules",
83
+ setup(build2) {
84
+ build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
85
+ path: require.resolve(args.path, { paths: [args.resolveDir] }),
86
+ namespace: "node-file"
87
+ }));
88
+ build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
89
+ contents: `
90
+ import path from ${JSON.stringify(args.path)}
91
+ try { module.exports = require(path) }
92
+ catch {}
93
+ `
94
+ }));
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;
103
+ opts.loader = opts.loader || {};
104
+ opts.loader[".node"] = "file";
105
+ }
106
+ },
107
+ {
108
+ name: "replace-path",
109
+ setup(ctx) {
110
+ ctx.onLoad({ filter: JS_EXT_RE }, async (args) => {
111
+ const contents = import_utils.fs.readFileSync(args.path, "utf-8");
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))
121
+ };
122
+ });
123
+ }
124
+ },
125
+ // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
126
+ {
127
+ name: "make-all-packages-external",
128
+ setup(_build) {
129
+ _build.onResolve({ filter: EXTERNAL_REGEXP }, async (args) => {
130
+ let external = true;
131
+ if (args.kind === "entry-point") {
132
+ external = false;
133
+ }
134
+ try {
135
+ const resolvedPath = require.resolve(args.path, {
136
+ paths: [args.resolveDir]
137
+ });
138
+ if (BUNDLED_EXT_RE.test(resolvedPath) || await isTypeModulePkg(resolvedPath)) {
139
+ return {
140
+ external: false
141
+ };
142
+ }
143
+ } catch (err) {
144
+ }
145
+ return {
146
+ path: args.path,
147
+ external
148
+ };
149
+ });
150
+ }
151
+ }
152
+ ]
153
+ });
154
+ return outfile;
155
+ }
156
+ // Annotate the CommonJS export names for ESM import in node:
157
+ 0 && (module.exports = {
158
+ EXTERNAL_REGEXP,
159
+ bundle,
160
+ defaultGetOutputFile
161
+ });
@@ -15,26 +15,6 @@ var __copyProps = (to, from, except, desc) => {
15
15
  return to;
16
16
  };
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var __async = (__this, __arguments, generator) => {
19
- return new Promise((resolve, reject) => {
20
- var fulfilled = (value) => {
21
- try {
22
- step(generator.next(value));
23
- } catch (e) {
24
- reject(e);
25
- }
26
- };
27
- var rejected = (value) => {
28
- try {
29
- step(generator.throw(value));
30
- } catch (e) {
31
- reject(e);
32
- }
33
- };
34
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
35
- step((generator = generator.apply(__this, __arguments)).next());
36
- });
37
- };
38
18
  var src_exports = {};
39
19
  __export(src_exports, {
40
20
  bundle: () => import_bundle.bundle,
@@ -44,21 +24,19 @@ __export(src_exports, {
44
24
  module.exports = __toCommonJS(src_exports);
45
25
  var import_utils = require("@modern-js/utils");
46
26
  var import_bundle = require("./bundle");
47
- function bundleRequire(filepath, options) {
48
- return __async(this, null, function* () {
49
- const configFile = yield (0, import_bundle.bundle)(filepath, options);
50
- let mod;
51
- const req = (options == null ? void 0 : options.require) || require;
52
- try {
53
- mod = yield req(configFile);
54
- (0, import_utils.deleteRequireCache)(configFile);
55
- } finally {
56
- if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
57
- import_utils.fs.unlinkSync(configFile);
58
- }
27
+ async function bundleRequire(filepath, options) {
28
+ const configFile = await (0, import_bundle.bundle)(filepath, options);
29
+ let mod;
30
+ const req = (options == null ? void 0 : options.require) || require;
31
+ try {
32
+ mod = await req(configFile);
33
+ (0, import_utils.deleteRequireCache)(configFile);
34
+ } finally {
35
+ if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
36
+ import_utils.fs.unlinkSync(configFile);
59
37
  }
60
- return mod;
61
- });
38
+ }
39
+ return mod;
62
40
  }
63
41
  // Annotate the CommonJS export names for ESM import in node:
64
42
  0 && (module.exports = {
@@ -0,0 +1,132 @@
1
+ import path from "path";
2
+ import {
3
+ fs,
4
+ pkgUp,
5
+ nanoid,
6
+ CONFIG_CACHE_DIR,
7
+ createDebugger
8
+ } from "@modern-js/utils";
9
+ import { build } from "esbuild";
10
+ const debug = createDebugger("node-bundle");
11
+ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
12
+ const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
13
+ const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
14
+ function inferLoader(ext) {
15
+ if (ext === ".mjs" || ext === ".cjs") {
16
+ return "js";
17
+ }
18
+ return ext.slice(1);
19
+ }
20
+ async function isTypeModulePkg(cwd) {
21
+ const pkgJsonPath = await pkgUp({ cwd });
22
+ if (pkgJsonPath) {
23
+ const pkgJson = await fs.readJSON(pkgJsonPath);
24
+ return pkgJson.type === "module";
25
+ }
26
+ return false;
27
+ }
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) {
33
+ if (!JS_EXT_RE.test(filepath)) {
34
+ throw new Error(`${filepath} is not a valid JS file`);
35
+ }
36
+ debug("bundle", filepath, options);
37
+ const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
38
+ const outfile = await getOutputFile(path.basename(filepath));
39
+ await build({
40
+ entryPoints: [filepath],
41
+ outfile,
42
+ format: "cjs",
43
+ platform: "node",
44
+ bundle: true,
45
+ // fix transforming error when the project's tsconfig.json
46
+ // sets `target: "es5"`
47
+ // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
48
+ target: "esnext",
49
+ ...options == null ? void 0 : options.esbuildOptions,
50
+ plugins: [
51
+ ...(options == null ? void 0 : options.esbuildPlugins) || [],
52
+ // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
53
+ {
54
+ name: "native-node-modules",
55
+ setup(build2) {
56
+ build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
57
+ path: require.resolve(args.path, { paths: [args.resolveDir] }),
58
+ namespace: "node-file"
59
+ }));
60
+ build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
61
+ contents: `
62
+ import path from ${JSON.stringify(args.path)}
63
+ try { module.exports = require(path) }
64
+ catch {}
65
+ `
66
+ }));
67
+ build2.onResolve(
68
+ { filter: /\.node$/, namespace: "node-file" },
69
+ (args) => ({
70
+ path: args.path,
71
+ namespace: "file"
72
+ })
73
+ );
74
+ const opts = build2.initialOptions;
75
+ opts.loader = opts.loader || {};
76
+ opts.loader[".node"] = "file";
77
+ }
78
+ },
79
+ {
80
+ name: "replace-path",
81
+ setup(ctx) {
82
+ ctx.onLoad({ filter: JS_EXT_RE }, async (args) => {
83
+ const contents = fs.readFileSync(args.path, "utf-8");
84
+ 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
+ loader: inferLoader(path.extname(args.path))
93
+ };
94
+ });
95
+ }
96
+ },
97
+ // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
98
+ {
99
+ name: "make-all-packages-external",
100
+ setup(_build) {
101
+ _build.onResolve({ filter: EXTERNAL_REGEXP }, async (args) => {
102
+ let external = true;
103
+ if (args.kind === "entry-point") {
104
+ external = false;
105
+ }
106
+ try {
107
+ const resolvedPath = require.resolve(args.path, {
108
+ paths: [args.resolveDir]
109
+ });
110
+ if (BUNDLED_EXT_RE.test(resolvedPath) || await isTypeModulePkg(resolvedPath)) {
111
+ return {
112
+ external: false
113
+ };
114
+ }
115
+ } catch (err) {
116
+ }
117
+ return {
118
+ path: args.path,
119
+ external
120
+ };
121
+ });
122
+ }
123
+ }
124
+ ]
125
+ });
126
+ return outfile;
127
+ }
128
+ export {
129
+ EXTERNAL_REGEXP,
130
+ bundle,
131
+ defaultGetOutputFile
132
+ };
@@ -0,0 +1,21 @@
1
+ import { fs, deleteRequireCache } from "@modern-js/utils";
2
+ import { bundle, defaultGetOutputFile } from "./bundle";
3
+ async function bundleRequire(filepath, options) {
4
+ const configFile = await bundle(filepath, options);
5
+ let mod;
6
+ const req = (options == null ? void 0 : options.require) || require;
7
+ try {
8
+ mod = await req(configFile);
9
+ deleteRequireCache(configFile);
10
+ } finally {
11
+ if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
12
+ fs.unlinkSync(configFile);
13
+ }
14
+ }
15
+ return mod;
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.4.1-beta.0",
3
+ "version": "2.6.0",
4
4
  "description": "A Progressive React Framework for modern web development.",
5
5
  "homepage": "https://modernjs.dev",
6
6
  "bugs": "https://github.com/modern-js-dev/modern.js/issues",
@@ -14,18 +14,17 @@
14
14
  ],
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
- "main": "./dist/js/node/index.js",
18
- "jsnext:modern": "./dist/js/modern/index.js",
17
+ "main": "./dist/cjs/index.js",
19
18
  "exports": {
20
19
  ".": {
21
20
  "node": {
22
21
  "jsnext:source": "./src/index.ts",
23
- "import": "./dist/js/modern/index.js",
24
- "require": "./dist/js/node/index.js"
22
+ "import": "./dist/esm/index.js",
23
+ "require": "./dist/cjs/index.js"
25
24
  },
26
- "default": "./dist/js/node/index.js"
25
+ "default": "./dist/cjs/index.js"
27
26
  },
28
- "./bundle": "./dist/js/node/bundle.js"
27
+ "./bundle": "./dist/cjs/bundle.js"
29
28
  },
30
29
  "typesVersions": {
31
30
  "*": {
@@ -40,15 +39,15 @@
40
39
  "dependencies": {
41
40
  "@babel/runtime": "^7.18.0",
42
41
  "esbuild": "0.15.7",
43
- "@modern-js/utils": "2.4.1-beta.0"
42
+ "@modern-js/utils": "2.6.0"
44
43
  },
45
44
  "devDependencies": {
46
45
  "@types/jest": "^27",
47
46
  "@types/node": "^14",
48
47
  "jest": "^27",
49
48
  "typescript": "^4",
50
- "@scripts/jest-config": "2.4.0",
51
- "@scripts/build": "2.4.0"
49
+ "@scripts/build": "2.6.0",
50
+ "@scripts/jest-config": "2.6.0"
52
51
  },
53
52
  "sideEffects": false,
54
53
  "publishConfig": {
@@ -1,172 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- var __async = (__this, __arguments, generator) => {
21
- return new Promise((resolve, reject) => {
22
- var fulfilled = (value) => {
23
- try {
24
- step(generator.next(value));
25
- } catch (e) {
26
- reject(e);
27
- }
28
- };
29
- var rejected = (value) => {
30
- try {
31
- step(generator.throw(value));
32
- } catch (e) {
33
- reject(e);
34
- }
35
- };
36
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
- step((generator = generator.apply(__this, __arguments)).next());
38
- });
39
- };
40
- import path from "path";
41
- import {
42
- fs,
43
- pkgUp,
44
- nanoid,
45
- CONFIG_CACHE_DIR,
46
- createDebugger
47
- } from "@modern-js/utils";
48
- import { build } from "esbuild";
49
- const debug = createDebugger("node-bundle");
50
- const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
51
- const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
52
- const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
53
- function inferLoader(ext) {
54
- if (ext === ".mjs" || ext === ".cjs") {
55
- return "js";
56
- }
57
- return ext.slice(1);
58
- }
59
- function isTypeModulePkg(cwd) {
60
- return __async(this, null, function* () {
61
- const pkgJsonPath = yield pkgUp({ cwd });
62
- if (pkgJsonPath) {
63
- const pkgJson = yield fs.readJSON(pkgJsonPath);
64
- return pkgJson.type === "module";
65
- }
66
- return false;
67
- });
68
- }
69
- const defaultGetOutputFile = (filepath) => __async(void 0, null, function* () {
70
- return path.resolve(
71
- CONFIG_CACHE_DIR,
72
- `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`
73
- );
74
- });
75
- function bundle(filepath, options) {
76
- return __async(this, null, function* () {
77
- if (!JS_EXT_RE.test(filepath)) {
78
- throw new Error(`${filepath} is not a valid JS file`);
79
- }
80
- debug("bundle", filepath, options);
81
- const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
82
- const outfile = yield getOutputFile(path.basename(filepath));
83
- yield build(__spreadProps(__spreadValues({
84
- entryPoints: [filepath],
85
- outfile,
86
- format: "cjs",
87
- platform: "node",
88
- bundle: true,
89
- target: "esnext"
90
- }, options == null ? void 0 : options.esbuildOptions), {
91
- plugins: [
92
- ...(options == null ? void 0 : options.esbuildPlugins) || [],
93
- {
94
- name: "native-node-modules",
95
- setup(build2) {
96
- build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
97
- path: require.resolve(args.path, { paths: [args.resolveDir] }),
98
- namespace: "node-file"
99
- }));
100
- build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
101
- contents: `
102
- import path from ${JSON.stringify(args.path)}
103
- try { module.exports = require(path) }
104
- catch {}
105
- `
106
- }));
107
- build2.onResolve(
108
- { filter: /\.node$/, namespace: "node-file" },
109
- (args) => ({
110
- path: args.path,
111
- namespace: "file"
112
- })
113
- );
114
- const opts = build2.initialOptions;
115
- opts.loader = opts.loader || {};
116
- opts.loader[".node"] = "file";
117
- }
118
- },
119
- {
120
- name: "replace-path",
121
- setup(ctx) {
122
- ctx.onLoad({ filter: JS_EXT_RE }, (args) => __async(this, null, function* () {
123
- const contents = fs.readFileSync(args.path, "utf-8");
124
- return {
125
- contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
126
- /\b__dirname\b/g,
127
- JSON.stringify(path.dirname(args.path))
128
- ).replace(
129
- /\bimport\.meta\.url\b/g,
130
- JSON.stringify(`file://${args.path}`)
131
- ),
132
- loader: inferLoader(path.extname(args.path))
133
- };
134
- }));
135
- }
136
- },
137
- {
138
- name: "make-all-packages-external",
139
- setup(_build) {
140
- _build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => __async(this, null, function* () {
141
- let external = true;
142
- if (args.kind === "entry-point") {
143
- external = false;
144
- }
145
- try {
146
- const resolvedPath = require.resolve(args.path, {
147
- paths: [args.resolveDir]
148
- });
149
- if (BUNDLED_EXT_RE.test(resolvedPath) || (yield isTypeModulePkg(resolvedPath))) {
150
- return {
151
- external: false
152
- };
153
- }
154
- } catch (err) {
155
- }
156
- return {
157
- path: args.path,
158
- external
159
- };
160
- }));
161
- }
162
- }
163
- ]
164
- }));
165
- return outfile;
166
- });
167
- }
168
- export {
169
- EXTERNAL_REGEXP,
170
- bundle,
171
- defaultGetOutputFile
172
- };
@@ -1,43 +0,0 @@
1
- var __async = (__this, __arguments, generator) => {
2
- return new Promise((resolve, reject) => {
3
- var fulfilled = (value) => {
4
- try {
5
- step(generator.next(value));
6
- } catch (e) {
7
- reject(e);
8
- }
9
- };
10
- var rejected = (value) => {
11
- try {
12
- step(generator.throw(value));
13
- } catch (e) {
14
- reject(e);
15
- }
16
- };
17
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
- step((generator = generator.apply(__this, __arguments)).next());
19
- });
20
- };
21
- import { fs, deleteRequireCache } from "@modern-js/utils";
22
- import { bundle, defaultGetOutputFile } from "./bundle";
23
- function bundleRequire(filepath, options) {
24
- return __async(this, null, function* () {
25
- const configFile = yield bundle(filepath, options);
26
- let mod;
27
- const req = (options == null ? void 0 : options.require) || require;
28
- try {
29
- mod = yield req(configFile);
30
- deleteRequireCache(configFile);
31
- } finally {
32
- if ((options == null ? void 0 : options.autoClear) === void 0 || options.autoClear) {
33
- fs.unlinkSync(configFile);
34
- }
35
- }
36
- return mod;
37
- });
38
- }
39
- export {
40
- bundle,
41
- bundleRequire,
42
- defaultGetOutputFile
43
- };
@@ -1,195 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __defProps = Object.defineProperties;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
- var __export = (target, all) => {
25
- for (var name in all)
26
- __defProp(target, name, { get: all[name], enumerable: true });
27
- };
28
- var __copyProps = (to, from, except, desc) => {
29
- if (from && typeof from === "object" || typeof from === "function") {
30
- for (let key of __getOwnPropNames(from))
31
- if (!__hasOwnProp.call(to, key) && key !== except)
32
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
33
- }
34
- return to;
35
- };
36
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
37
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
38
- mod
39
- ));
40
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
41
- var __async = (__this, __arguments, generator) => {
42
- return new Promise((resolve, reject) => {
43
- var fulfilled = (value) => {
44
- try {
45
- step(generator.next(value));
46
- } catch (e) {
47
- reject(e);
48
- }
49
- };
50
- var rejected = (value) => {
51
- try {
52
- step(generator.throw(value));
53
- } catch (e) {
54
- reject(e);
55
- }
56
- };
57
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
58
- step((generator = generator.apply(__this, __arguments)).next());
59
- });
60
- };
61
- var bundle_exports = {};
62
- __export(bundle_exports, {
63
- EXTERNAL_REGEXP: () => EXTERNAL_REGEXP,
64
- bundle: () => bundle,
65
- defaultGetOutputFile: () => defaultGetOutputFile
66
- });
67
- module.exports = __toCommonJS(bundle_exports);
68
- var import_path = __toESM(require("path"));
69
- var import_utils = require("@modern-js/utils");
70
- var import_esbuild = require("esbuild");
71
- const debug = (0, import_utils.createDebugger)("node-bundle");
72
- const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
73
- const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
74
- const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
75
- function inferLoader(ext) {
76
- if (ext === ".mjs" || ext === ".cjs") {
77
- return "js";
78
- }
79
- return ext.slice(1);
80
- }
81
- function isTypeModulePkg(cwd) {
82
- return __async(this, null, function* () {
83
- const pkgJsonPath = yield (0, import_utils.pkgUp)({ cwd });
84
- if (pkgJsonPath) {
85
- const pkgJson = yield import_utils.fs.readJSON(pkgJsonPath);
86
- return pkgJson.type === "module";
87
- }
88
- return false;
89
- });
90
- }
91
- const defaultGetOutputFile = (filepath) => __async(void 0, null, function* () {
92
- return import_path.default.resolve(
93
- import_utils.CONFIG_CACHE_DIR,
94
- `${filepath}-${Date.now()}.${(0, import_utils.nanoid)()}.bundled.cjs`
95
- );
96
- });
97
- function bundle(filepath, options) {
98
- return __async(this, null, function* () {
99
- if (!JS_EXT_RE.test(filepath)) {
100
- throw new Error(`${filepath} is not a valid JS file`);
101
- }
102
- debug("bundle", filepath, options);
103
- const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
104
- const outfile = yield getOutputFile(import_path.default.basename(filepath));
105
- yield (0, import_esbuild.build)(__spreadProps(__spreadValues({
106
- entryPoints: [filepath],
107
- outfile,
108
- format: "cjs",
109
- platform: "node",
110
- bundle: true,
111
- target: "esnext"
112
- }, options == null ? void 0 : options.esbuildOptions), {
113
- plugins: [
114
- ...(options == null ? void 0 : options.esbuildPlugins) || [],
115
- {
116
- name: "native-node-modules",
117
- setup(build2) {
118
- build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
119
- path: require.resolve(args.path, { paths: [args.resolveDir] }),
120
- namespace: "node-file"
121
- }));
122
- build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
123
- contents: `
124
- import path from ${JSON.stringify(args.path)}
125
- try { module.exports = require(path) }
126
- catch {}
127
- `
128
- }));
129
- build2.onResolve(
130
- { filter: /\.node$/, namespace: "node-file" },
131
- (args) => ({
132
- path: args.path,
133
- namespace: "file"
134
- })
135
- );
136
- const opts = build2.initialOptions;
137
- opts.loader = opts.loader || {};
138
- opts.loader[".node"] = "file";
139
- }
140
- },
141
- {
142
- name: "replace-path",
143
- setup(ctx) {
144
- ctx.onLoad({ filter: JS_EXT_RE }, (args) => __async(this, null, function* () {
145
- const contents = import_utils.fs.readFileSync(args.path, "utf-8");
146
- return {
147
- contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
148
- /\b__dirname\b/g,
149
- JSON.stringify(import_path.default.dirname(args.path))
150
- ).replace(
151
- /\bimport\.meta\.url\b/g,
152
- JSON.stringify(`file://${args.path}`)
153
- ),
154
- loader: inferLoader(import_path.default.extname(args.path))
155
- };
156
- }));
157
- }
158
- },
159
- {
160
- name: "make-all-packages-external",
161
- setup(_build) {
162
- _build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => __async(this, null, function* () {
163
- let external = true;
164
- if (args.kind === "entry-point") {
165
- external = false;
166
- }
167
- try {
168
- const resolvedPath = require.resolve(args.path, {
169
- paths: [args.resolveDir]
170
- });
171
- if (BUNDLED_EXT_RE.test(resolvedPath) || (yield isTypeModulePkg(resolvedPath))) {
172
- return {
173
- external: false
174
- };
175
- }
176
- } catch (err) {
177
- }
178
- return {
179
- path: args.path,
180
- external
181
- };
182
- }));
183
- }
184
- }
185
- ]
186
- }));
187
- return outfile;
188
- });
189
- }
190
- // Annotate the CommonJS export names for ESM import in node:
191
- 0 && (module.exports = {
192
- EXTERNAL_REGEXP,
193
- bundle,
194
- defaultGetOutputFile
195
- });