@modern-js/node-bundle-require 2.4.0 → 2.5.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,20 @@
1
1
  # @modern-js/node-bundle-require
2
2
 
3
+ ## 2.5.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 89ca6cc: refactor: merge build-config into scripts/build
8
+
9
+ refactor: 把 build-config 合并进 scripts/build
10
+
11
+ - 30614fa: chore: modify package.json entry fields and build config
12
+ chore: 更改 package.json entry 字段以及构建配置
13
+ - Updated dependencies [30614fa]
14
+ - Updated dependencies [1b0ce87]
15
+ - Updated dependencies [11c053b]
16
+ - @modern-js/utils@2.5.0
17
+
3
18
  ## 2.4.0
4
19
 
5
20
  ### Patch Changes
@@ -0,0 +1,152 @@
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
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+ var bundle_exports = {};
25
+ __export(bundle_exports, {
26
+ EXTERNAL_REGEXP: () => EXTERNAL_REGEXP,
27
+ bundle: () => bundle,
28
+ defaultGetOutputFile: () => defaultGetOutputFile
29
+ });
30
+ module.exports = __toCommonJS(bundle_exports);
31
+ var import_path = __toESM(require("path"));
32
+ var import_utils = require("@modern-js/utils");
33
+ var import_esbuild = require("esbuild");
34
+ const debug = (0, import_utils.createDebugger)("node-bundle");
35
+ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
36
+ const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/;
37
+ const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
38
+ function inferLoader(ext) {
39
+ if (ext === ".mjs" || ext === ".cjs") {
40
+ return "js";
41
+ }
42
+ return ext.slice(1);
43
+ }
44
+ async function isTypeModulePkg(cwd) {
45
+ const pkgJsonPath = await (0, import_utils.pkgUp)({ cwd });
46
+ if (pkgJsonPath) {
47
+ const pkgJson = await import_utils.fs.readJSON(pkgJsonPath);
48
+ return pkgJson.type === "module";
49
+ }
50
+ return false;
51
+ }
52
+ const defaultGetOutputFile = async (filepath) => import_path.default.resolve(
53
+ import_utils.CONFIG_CACHE_DIR,
54
+ `${filepath}-${Date.now()}.${(0, import_utils.nanoid)()}.bundled.cjs`
55
+ );
56
+ async function bundle(filepath, options) {
57
+ if (!JS_EXT_RE.test(filepath)) {
58
+ throw new Error(`${filepath} is not a valid JS file`);
59
+ }
60
+ debug("bundle", filepath, options);
61
+ const getOutputFile = (options == null ? void 0 : options.getOutputFile) || defaultGetOutputFile;
62
+ const outfile = await getOutputFile(import_path.default.basename(filepath));
63
+ await (0, import_esbuild.build)({
64
+ entryPoints: [filepath],
65
+ outfile,
66
+ format: "cjs",
67
+ platform: "node",
68
+ bundle: true,
69
+ target: "esnext",
70
+ ...options == null ? void 0 : options.esbuildOptions,
71
+ plugins: [
72
+ ...(options == null ? void 0 : options.esbuildPlugins) || [],
73
+ {
74
+ name: "native-node-modules",
75
+ setup(build2) {
76
+ build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
77
+ path: require.resolve(args.path, { paths: [args.resolveDir] }),
78
+ namespace: "node-file"
79
+ }));
80
+ build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
81
+ contents: `
82
+ import path from ${JSON.stringify(args.path)}
83
+ try { module.exports = require(path) }
84
+ catch {}
85
+ `
86
+ }));
87
+ build2.onResolve(
88
+ { filter: /\.node$/, namespace: "node-file" },
89
+ (args) => ({
90
+ path: args.path,
91
+ namespace: "file"
92
+ })
93
+ );
94
+ const opts = build2.initialOptions;
95
+ opts.loader = opts.loader || {};
96
+ opts.loader[".node"] = "file";
97
+ }
98
+ },
99
+ {
100
+ name: "replace-path",
101
+ setup(ctx) {
102
+ ctx.onLoad({ filter: JS_EXT_RE }, async (args) => {
103
+ const contents = import_utils.fs.readFileSync(args.path, "utf-8");
104
+ return {
105
+ contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
106
+ /\b__dirname\b/g,
107
+ JSON.stringify(import_path.default.dirname(args.path))
108
+ ).replace(
109
+ /\bimport\.meta\.url\b/g,
110
+ JSON.stringify(`file://${args.path}`)
111
+ ),
112
+ loader: inferLoader(import_path.default.extname(args.path))
113
+ };
114
+ });
115
+ }
116
+ },
117
+ {
118
+ name: "make-all-packages-external",
119
+ setup(_build) {
120
+ _build.onResolve({ filter: EXTERNAL_REGEXP }, async (args) => {
121
+ let external = true;
122
+ if (args.kind === "entry-point") {
123
+ external = false;
124
+ }
125
+ try {
126
+ const resolvedPath = require.resolve(args.path, {
127
+ paths: [args.resolveDir]
128
+ });
129
+ if (BUNDLED_EXT_RE.test(resolvedPath) || await isTypeModulePkg(resolvedPath)) {
130
+ return {
131
+ external: false
132
+ };
133
+ }
134
+ } catch (err) {
135
+ }
136
+ return {
137
+ path: args.path,
138
+ external
139
+ };
140
+ });
141
+ }
142
+ }
143
+ ]
144
+ });
145
+ return outfile;
146
+ }
147
+ // Annotate the CommonJS export names for ESM import in node:
148
+ 0 && (module.exports = {
149
+ EXTERNAL_REGEXP,
150
+ bundle,
151
+ defaultGetOutputFile
152
+ });
@@ -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,127 @@
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
+ target: "esnext",
46
+ ...options == null ? void 0 : options.esbuildOptions,
47
+ plugins: [
48
+ ...(options == null ? void 0 : options.esbuildPlugins) || [],
49
+ {
50
+ name: "native-node-modules",
51
+ setup(build2) {
52
+ build2.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
53
+ path: require.resolve(args.path, { paths: [args.resolveDir] }),
54
+ namespace: "node-file"
55
+ }));
56
+ build2.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
57
+ contents: `
58
+ import path from ${JSON.stringify(args.path)}
59
+ try { module.exports = require(path) }
60
+ catch {}
61
+ `
62
+ }));
63
+ build2.onResolve(
64
+ { filter: /\.node$/, namespace: "node-file" },
65
+ (args) => ({
66
+ path: args.path,
67
+ namespace: "file"
68
+ })
69
+ );
70
+ const opts = build2.initialOptions;
71
+ opts.loader = opts.loader || {};
72
+ opts.loader[".node"] = "file";
73
+ }
74
+ },
75
+ {
76
+ name: "replace-path",
77
+ setup(ctx) {
78
+ ctx.onLoad({ filter: JS_EXT_RE }, async (args) => {
79
+ const contents = fs.readFileSync(args.path, "utf-8");
80
+ return {
81
+ contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(
82
+ /\b__dirname\b/g,
83
+ JSON.stringify(path.dirname(args.path))
84
+ ).replace(
85
+ /\bimport\.meta\.url\b/g,
86
+ JSON.stringify(`file://${args.path}`)
87
+ ),
88
+ loader: inferLoader(path.extname(args.path))
89
+ };
90
+ });
91
+ }
92
+ },
93
+ {
94
+ name: "make-all-packages-external",
95
+ setup(_build) {
96
+ _build.onResolve({ filter: EXTERNAL_REGEXP }, async (args) => {
97
+ let external = true;
98
+ if (args.kind === "entry-point") {
99
+ external = false;
100
+ }
101
+ try {
102
+ const resolvedPath = require.resolve(args.path, {
103
+ paths: [args.resolveDir]
104
+ });
105
+ if (BUNDLED_EXT_RE.test(resolvedPath) || await isTypeModulePkg(resolvedPath)) {
106
+ return {
107
+ external: false
108
+ };
109
+ }
110
+ } catch (err) {
111
+ }
112
+ return {
113
+ path: args.path,
114
+ external
115
+ };
116
+ });
117
+ }
118
+ }
119
+ ]
120
+ });
121
+ return outfile;
122
+ }
123
+ export {
124
+ EXTERNAL_REGEXP,
125
+ bundle,
126
+ defaultGetOutputFile
127
+ };
@@ -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.0",
3
+ "version": "2.5.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.0"
42
+ "@modern-js/utils": "2.5.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/build": "2.4.0",
51
- "@scripts/jest-config": "2.4.0"
49
+ "@scripts/build": "2.5.0",
50
+ "@scripts/jest-config": "2.5.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
- });