@modern-js/node-bundle-require 1.20.1 → 1.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @modern-js/node-bundle-require
2
2
 
3
+ ## 1.21.1
4
+
5
+ ### Patch Changes
6
+
7
+ - @modern-js/utils@1.21.1
8
+
9
+ ## 1.21.0
10
+
11
+ ### Patch Changes
12
+
13
+ - 7b3a482: fix(bundle-require): should bundle ts or esm files inside node_modules
14
+
15
+ fix(bundle-require): 修复无法打包 node_modules 下的 ts 和 esm 文件的问题
16
+
17
+ - @modern-js/utils@1.21.0
18
+
3
19
  ## 1.20.1
4
20
 
5
21
  ### Patch Changes
@@ -5,10 +5,11 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
7
7
  import path from 'path';
8
- import { fs, nanoid, CONFIG_CACHE_DIR, createDebugger } from '@modern-js/utils';
8
+ import { fs, pkgUp, nanoid, CONFIG_CACHE_DIR, createDebugger } from '@modern-js/utils';
9
9
  import { build } from 'esbuild';
10
10
  const debug = createDebugger('node-bundle');
11
- const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or "./" or "../"
11
+ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
12
+ const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/; // Must not start with "/" or "./" or "../"
12
13
  // "/test/node_modules/foo"
13
14
  // "c:/node_modules/foo"
14
15
 
@@ -22,6 +23,19 @@ function inferLoader(ext) {
22
23
  return ext.slice(1);
23
24
  }
24
25
 
26
+ async function isTypeModulePkg(cwd) {
27
+ const pkgJsonPath = await pkgUp({
28
+ cwd
29
+ });
30
+
31
+ if (pkgJsonPath) {
32
+ const pkgJson = await fs.readJSON(pkgJsonPath);
33
+ return pkgJson.type === 'module';
34
+ }
35
+
36
+ return false;
37
+ }
38
+
25
39
  export const defaultGetOutputFile = async filepath => path.resolve(CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`);
26
40
  export async function bundle(filepath, options) {
27
41
  if (!JS_EXT_RE.test(filepath)) {
@@ -109,13 +123,27 @@ export async function bundle(filepath, options) {
109
123
  setup(_build) {
110
124
  _build.onResolve({
111
125
  filter: EXTERNAL_REGEXP
112
- }, args => {
126
+ }, async args => {
113
127
  let external = true; // FIXME: windows external entrypoint
114
128
 
115
129
  if (args.kind === 'entry-point') {
116
130
  external = false;
117
131
  }
118
132
 
133
+ try {
134
+ const resolvedPath = require.resolve(args.path, {
135
+ paths: [args.resolveDir]
136
+ }); // If it is a typescript or esm package, we should bundle it.
137
+
138
+
139
+ if (BUNDLED_EXT_RE.test(resolvedPath) || (await isTypeModulePkg(resolvedPath))) {
140
+ return {
141
+ external: false
142
+ };
143
+ }
144
+ } catch (err) {// If the package can not be resolved, do nothing.
145
+ }
146
+
119
147
  return {
120
148
  path: args.path,
121
149
  external
@@ -22,7 +22,8 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
22
22
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
23
23
 
24
24
  const debug = (0, _utils.createDebugger)('node-bundle');
25
- const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or "./" or "../"
25
+ const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
26
+ const BUNDLED_EXT_RE = /\.(ts|mts|cts|tsx|mjs)$/; // Must not start with "/" or "./" or "../"
26
27
  // "/test/node_modules/foo"
27
28
  // "c:/node_modules/foo"
28
29
 
@@ -37,6 +38,19 @@ function inferLoader(ext) {
37
38
  return ext.slice(1);
38
39
  }
39
40
 
41
+ async function isTypeModulePkg(cwd) {
42
+ const pkgJsonPath = await (0, _utils.pkgUp)({
43
+ cwd
44
+ });
45
+
46
+ if (pkgJsonPath) {
47
+ const pkgJson = await _utils.fs.readJSON(pkgJsonPath);
48
+ return pkgJson.type === 'module';
49
+ }
50
+
51
+ return false;
52
+ }
53
+
40
54
  const defaultGetOutputFile = async filepath => _path.default.resolve(_utils.CONFIG_CACHE_DIR, `${filepath}-${Date.now()}.${(0, _utils.nanoid)()}.bundled.cjs`);
41
55
 
42
56
  exports.defaultGetOutputFile = defaultGetOutputFile;
@@ -128,13 +142,27 @@ async function bundle(filepath, options) {
128
142
  setup(_build) {
129
143
  _build.onResolve({
130
144
  filter: EXTERNAL_REGEXP
131
- }, args => {
145
+ }, async args => {
132
146
  let external = true; // FIXME: windows external entrypoint
133
147
 
134
148
  if (args.kind === 'entry-point') {
135
149
  external = false;
136
150
  }
137
151
 
152
+ try {
153
+ const resolvedPath = require.resolve(args.path, {
154
+ paths: [args.resolveDir]
155
+ }); // If it is a typescript or esm package, we should bundle it.
156
+
157
+
158
+ if (BUNDLED_EXT_RE.test(resolvedPath) || (await isTypeModulePkg(resolvedPath))) {
159
+ return {
160
+ external: false
161
+ };
162
+ }
163
+ } catch (err) {// If the package can not be resolved, do nothing.
164
+ }
165
+
138
166
  return {
139
167
  path: args.path,
140
168
  external
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/node-bundle-require",
3
- "version": "1.20.1",
3
+ "version": "1.21.1",
4
4
  "description": "The meta-framework suite designed from scratch for frontend-focused modern web development.",
5
5
  "homepage": "https://modernjs.dev",
6
6
  "bugs": "https://github.com/modern-js-dev/modern.js/issues",
@@ -39,12 +39,12 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@babel/runtime": "^7.18.0",
42
- "@modern-js/utils": "1.20.1",
42
+ "@modern-js/utils": "1.21.1",
43
43
  "esbuild": "^0.14.38"
44
44
  },
45
45
  "devDependencies": {
46
- "@scripts/build": "1.20.1",
47
- "@scripts/jest-config": "1.20.1",
46
+ "@scripts/build": "1.21.1",
47
+ "@scripts/jest-config": "1.21.1",
48
48
  "@types/jest": "^27",
49
49
  "@types/node": "^14",
50
50
  "jest": "^27",