prebundle 1.3.3 → 1.4.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/README.md CHANGED
@@ -213,6 +213,22 @@ export default {
213
213
  };
214
214
  ```
215
215
 
216
+ ### dtsOnly
217
+
218
+ Only bundle the dts files, default `false`.
219
+
220
+ ```ts
221
+ // prebundle.config.mjs
222
+ export default {
223
+ dependencies: [
224
+ {
225
+ name: 'foo',
226
+ dtsOnly: 'true',
227
+ },
228
+ ],
229
+ };
230
+ ```
231
+
216
232
  ### prettier
217
233
 
218
234
  Whether to prettier the code and strip comments, default `false`.
package/dist/index.js CHANGED
@@ -1,15 +1,15 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
2
- import * as __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__ from "../compiled/fs-extra/index.js";
3
- import * as __WEBPACK_EXTERNAL_MODULE_node_module_ab9f2194__ from "node:module";
4
- import * as __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__ from "node:url";
5
- import * as __WEBPACK_EXTERNAL_MODULE__vercel_ncc_57e68fb3__ from "@vercel/ncc";
6
- import * as __WEBPACK_EXTERNAL_MODULE__compiled_fast_glob_index_js_8d514d69__ from "../compiled/fast-glob/index.js";
7
- import * as __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js_c302f6e3__ from "../compiled/rslog/index.js";
8
- import * as __WEBPACK_EXTERNAL_MODULE_rollup_plugin_dts_1c9650fc__ from "rollup-plugin-dts";
9
- import * as __WEBPACK_EXTERNAL_MODULE_rollup__ from "rollup";
10
- import * as __WEBPACK_EXTERNAL_MODULE_terser__ from "terser";
11
- import * as __WEBPACK_EXTERNAL_MODULE_prettier__ from "prettier";
12
- import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
1
+ import { dirname, extname, join } from "node:path";
2
+ import fs_extra from "../compiled/fs-extra/index.js";
3
+ import { createRequire } from "node:module";
4
+ import { pathToFileURL } from "node:url";
5
+ import ncc from "@vercel/ncc";
6
+ import fast_glob from "../compiled/fast-glob/index.js";
7
+ import rslog from "../compiled/rslog/index.js";
8
+ import { dts } from "rollup-plugin-dts";
9
+ import { rollup } from "rollup";
10
+ import { minify } from "terser";
11
+ import { format } from "prettier";
12
+ import { existsSync } from "node:fs";
13
13
  const DIST_DIR = 'compiled';
14
14
  const DEFAULT_EXTERNALS = {
15
15
  './package.json': './package.json',
@@ -57,32 +57,32 @@ const NODE_BUILTINS = [
57
57
  'zlib'
58
58
  ];
59
59
  const cwd = process.cwd();
60
- const helper_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module_ab9f2194__.createRequire)(import.meta.url);
60
+ const helper_require = createRequire(import.meta.url);
61
61
  function findDepPath(name) {
62
62
  try {
63
- let entry = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.dirname)(helper_require.resolve((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(name), {
63
+ let entry = dirname(helper_require.resolve(join(name), {
64
64
  paths: [
65
65
  cwd
66
66
  ]
67
67
  }));
68
- while(!(0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.dirname)(entry).endsWith('node_modules'))entry = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.dirname)(entry);
69
- if (name.includes('/')) return (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.dirname)(entry), name);
68
+ while(!dirname(entry).endsWith('node_modules'))entry = dirname(entry);
69
+ if (name.includes('/')) return join(dirname(entry), name);
70
70
  return entry;
71
71
  } catch (err) {
72
72
  return null;
73
73
  }
74
74
  }
75
75
  const resolveConfig = async ()=>{
76
- const configPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(cwd, 'prebundle.config.mjs');
77
- const config = await import((0, __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__.pathToFileURL)(configPath).href);
76
+ const configPath = join(cwd, 'prebundle.config.mjs');
77
+ const config = await import(pathToFileURL(configPath).href);
78
78
  return config.default;
79
79
  };
80
80
  function parseTasks(dependencies, globalPrettier) {
81
81
  const result = [];
82
82
  for (const dep of dependencies){
83
83
  const depName = 'string' == typeof dep ? dep : dep.name;
84
- const importPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(cwd, DIST_DIR, depName);
85
- const distPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(cwd, DIST_DIR, depName);
84
+ const importPath = join(cwd, DIST_DIR, depName);
85
+ const distPath = join(cwd, DIST_DIR, depName);
86
86
  const depPath = findDepPath(depName);
87
87
  if (!depPath) throw new Error(`Failed to resolve dependency: ${depName}`);
88
88
  const depEntry = helper_require.resolve(depName, {
@@ -113,6 +113,7 @@ function parseTasks(dependencies, globalPrettier) {
113
113
  ignoreDts: dep.ignoreDts,
114
114
  copyDts: dep.copyDts,
115
115
  externals: dep.externals ?? {},
116
+ dtsOnly: dep.dtsOnly ?? false,
116
117
  dtsExternals: dep.dtsExternals ?? [],
117
118
  emitFiles: dep.emitFiles ?? [],
118
119
  prettier: dep.prettier ?? globalPrettier,
@@ -136,7 +137,7 @@ function pkgNameToAtTypes(name) {
136
137
  }
137
138
  function findDirectTypeFile(filepath) {
138
139
  if (/\.d\.[cm]?ts/.test(filepath)) return filepath;
139
- const ext = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.extname)(filepath);
140
+ const ext = extname(filepath);
140
141
  const base = filepath.slice(0, -ext.length);
141
142
  const _find = (list)=>{
142
143
  for (const f of list)try {
@@ -168,66 +169,60 @@ function findDirectTypeFile(filepath) {
168
169
  default:
169
170
  }
170
171
  }
171
- const { logger } = __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js_c302f6e3__["default"];
172
+ const { logger } = rslog;
172
173
  function emitAssets(assets, distPath) {
173
174
  for (const key of Object.keys(assets)){
174
175
  const asset = assets[key];
175
- __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].outputFileSync((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(distPath, key), asset.source);
176
+ fs_extra.outputFileSync(join(distPath, key), asset.source);
176
177
  }
177
178
  }
178
179
  async function emitIndex(code, distPath, prettier) {
179
- const distIndex = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(distPath, 'index.js');
180
+ const distIndex = join(distPath, 'index.js');
180
181
  if (prettier) {
181
- const minimized = await (0, __WEBPACK_EXTERNAL_MODULE_terser__.minify)(code, {
182
+ const minimized = await minify(code, {
182
183
  compress: false,
183
184
  mangle: false,
184
185
  ecma: 2019
185
186
  });
186
187
  if (!minimized.code) throw new Error('terser minify failed');
187
- const formatted = await (0, __WEBPACK_EXTERNAL_MODULE_prettier__.format)(minimized.code, {
188
+ const formatted = await format(minimized.code, {
188
189
  filepath: distIndex
189
190
  });
190
- await __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].outputFile(distIndex, formatted);
191
- } else await __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].outputFile(distIndex, code);
191
+ await fs_extra.outputFile(distIndex, formatted);
192
+ } else await fs_extra.outputFile(distIndex, code);
192
193
  }
193
194
  const getTypes = (json)=>{
194
195
  var _json_exports;
195
- return json && (json.types || json.typing || json.typings || getTypes(null === (_json_exports = json.exports) || void 0 === _json_exports ? void 0 : _json_exports['.'])) || null;
196
+ return json && (json.types || json.typing || json.typings || getTypes(null == (_json_exports = json.exports) ? void 0 : _json_exports['.'])) || null;
196
197
  };
197
198
  async function emitDts(task, externals) {
198
199
  const outputDefaultDts = ()=>{
199
- __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].outputFileSync((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, 'index.d.ts'), 'export = any;\n');
200
+ fs_extra.outputFileSync(join(task.distPath, 'index.d.ts'), 'export = any;\n');
200
201
  };
201
- if (task.ignoreDts) {
202
- outputDefaultDts();
203
- return;
204
- }
202
+ if (task.ignoreDts) return void outputDefaultDts();
205
203
  if (task.copyDts) {
206
- const dtsFiles = __WEBPACK_EXTERNAL_MODULE__compiled_fast_glob_index_js_8d514d69__["default"].sync('**/*.d.ts', {
204
+ const dtsFiles = fast_glob.sync('**/*.d.ts', {
207
205
  cwd: task.depPath,
208
206
  absolute: false
209
207
  });
210
- for (const dtsFile of dtsFiles)__WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].copySync((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.depPath, dtsFile), (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, dtsFile));
208
+ for (const dtsFile of dtsFiles)fs_extra.copySync(join(task.depPath, dtsFile), join(task.distPath, dtsFile));
211
209
  return;
212
210
  }
213
211
  const getInput = ()=>{
214
- const pkgPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.depPath, 'package.json');
215
- const pkgJson = __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].readJsonSync(pkgPath, 'utf-8');
212
+ const pkgPath = join(task.depPath, 'package.json');
213
+ const pkgJson = fs_extra.readJsonSync(pkgPath, 'utf-8');
216
214
  const types = getTypes(pkgJson);
217
- if (types) return (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.depPath, types);
215
+ if (types) return join(task.depPath, types);
218
216
  const directTypeFile = findDirectTypeFile(task.depEntry);
219
217
  if (directTypeFile) return directTypeFile;
220
218
  const depTypesPath = findDepPath(`${pkgNameToAtTypes(task.depName)}/package.json`);
221
219
  if (!depTypesPath) return null;
222
- const depTypesPkg = __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].readJsonSync(depTypesPath, 'utf-8');
220
+ const depTypesPkg = fs_extra.readJsonSync(depTypesPath, 'utf-8');
223
221
  const depTypes = getTypes(depTypesPkg);
224
- return depTypes ? (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.dirname)(depTypesPath), depTypes) : null;
222
+ return depTypes ? join(dirname(depTypesPath), depTypes) : null;
225
223
  };
226
224
  const input = getInput();
227
- if (!input) {
228
- outputDefaultDts();
229
- return;
230
- }
225
+ if (!input) return void outputDefaultDts();
231
226
  try {
232
227
  const inputConfig = {
233
228
  input,
@@ -237,7 +232,7 @@ async function emitDts(task, externals) {
237
232
  ...NODE_BUILTINS
238
233
  ],
239
234
  plugins: [
240
- (0, __WEBPACK_EXTERNAL_MODULE_rollup_plugin_dts_1c9650fc__.dts)({
235
+ dts({
241
236
  respectExternal: true,
242
237
  compilerOptions: {
243
238
  skipLibCheck: true,
@@ -260,7 +255,7 @@ async function emitDts(task, externals) {
260
255
  exports: 'named',
261
256
  entryFileNames: 'index.d.ts'
262
257
  };
263
- const bundle = await (0, __WEBPACK_EXTERNAL_MODULE_rollup__.rollup)(inputConfig);
258
+ const bundle = await rollup(inputConfig);
264
259
  await bundle.write(outputConfig);
265
260
  } catch (error) {
266
261
  logger.error(`rollup-plugin-dts failed: ${task.depName}`);
@@ -268,9 +263,9 @@ async function emitDts(task, externals) {
268
263
  }
269
264
  }
270
265
  function emitPackageJson(task, assets) {
271
- const packageJsonPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.depPath, 'package.json');
272
- const packageJson = __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].readJsonSync(packageJsonPath, 'utf-8');
273
- const outputPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, 'package.json');
266
+ const packageJsonPath = join(task.depPath, 'package.json');
267
+ const packageJson = fs_extra.readJsonSync(packageJsonPath, 'utf-8');
268
+ const outputPath = join(task.distPath, 'package.json');
274
269
  const pickedPackageJson = pick(packageJson, [
275
270
  'name',
276
271
  'author',
@@ -287,64 +282,66 @@ function emitPackageJson(task, assets) {
287
282
  'type'
288
283
  ]));
289
284
  } catch {}
290
- __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].writeJSONSync(outputPath, pickedPackageJson);
285
+ fs_extra.writeJSONSync(outputPath, pickedPackageJson);
291
286
  }
292
287
  function emitLicense(task) {
293
- const licensePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.depPath, 'LICENSE');
294
- if (__WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].existsSync(licensePath)) __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].copySync(licensePath, (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, 'license'));
288
+ const licensePath = join(task.depPath, 'LICENSE');
289
+ if (fs_extra.existsSync(licensePath)) fs_extra.copySync(licensePath, join(task.distPath, 'license'));
295
290
  }
296
291
  function emitExtraFiles(task) {
297
292
  const { emitFiles } = task;
298
293
  for (const item of emitFiles){
299
- const path = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, item.path);
300
- __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].outputFileSync(path, item.content);
294
+ const path = join(task.distPath, item.path);
295
+ fs_extra.outputFileSync(path, item.content);
301
296
  }
302
297
  }
303
298
  function removeSourceMap(task) {
304
- const maps = __WEBPACK_EXTERNAL_MODULE__compiled_fast_glob_index_js_8d514d69__["default"].sync((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, '**/*.map'));
305
- for (const mapPath of maps)__WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].removeSync(mapPath);
299
+ const maps = fast_glob.sync(join(task.distPath, '**/*.map'));
300
+ for (const mapPath of maps)fs_extra.removeSync(mapPath);
306
301
  }
307
302
  function renameDistFolder(task) {
308
- const pkgPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, 'package.json');
309
- const pkgJson = __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].readJsonSync(pkgPath, 'utf-8');
303
+ const pkgPath = join(task.distPath, 'package.json');
304
+ const pkgJson = fs_extra.readJsonSync(pkgPath, 'utf-8');
310
305
  for (const key of [
311
306
  'types',
312
307
  'typing',
313
308
  'typings'
314
309
  ]){
315
310
  var _pkgJson_key;
316
- if (null === (_pkgJson_key = pkgJson[key]) || void 0 === _pkgJson_key ? void 0 : _pkgJson_key.startsWith('dist/')) {
311
+ if (null == (_pkgJson_key = pkgJson[key]) ? void 0 : _pkgJson_key.startsWith('dist/')) {
317
312
  pkgJson[key] = pkgJson[key].replace('dist/', 'types/');
318
- const distFolder = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, 'dist');
319
- const typesFolder = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(task.distPath, 'types');
320
- if (__WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].existsSync(distFolder)) __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].renameSync(distFolder, typesFolder);
313
+ const distFolder = join(task.distPath, 'dist');
314
+ const typesFolder = join(task.distPath, 'types');
315
+ if (fs_extra.existsSync(distFolder)) fs_extra.renameSync(distFolder, typesFolder);
321
316
  }
322
317
  }
323
- __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].writeJSONSync(pkgPath, pkgJson);
318
+ fs_extra.writeJSONSync(pkgPath, pkgJson);
324
319
  }
325
320
  const pkgName = process.argv[2];
326
321
  async function prebundle(task, commonExternals = {}) {
327
322
  if (pkgName && task.depName !== pkgName) return;
328
323
  logger.start(`prebundle: ${task.depName}`);
329
- __WEBPACK_EXTERNAL_MODULE__compiled_fs_extra_index_js_a27e5f9c__["default"].removeSync(task.distPath);
324
+ fs_extra.removeSync(task.distPath);
330
325
  if (task.beforeBundle) await task.beforeBundle(task);
331
326
  const mergedExternals = {
332
327
  ...DEFAULT_EXTERNALS,
333
328
  ...commonExternals,
334
329
  ...task.externals
335
330
  };
336
- const nodeModulesPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(process.cwd(), 'node_modules');
337
- const hasNodeModules = (0, __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__.existsSync)(nodeModulesPath);
331
+ const nodeModulesPath = join(process.cwd(), 'node_modules');
332
+ const hasNodeModules = existsSync(nodeModulesPath);
338
333
  const enableCache = !process.env.CI && hasNodeModules;
339
- const { code, assets } = await (0, __WEBPACK_EXTERNAL_MODULE__vercel_ncc_57e68fb3__["default"])(task.depEntry, {
334
+ const { code, assets } = await ncc(task.depEntry, {
340
335
  minify: task.minify,
341
336
  target: task.target,
342
337
  externals: mergedExternals,
343
338
  assetBuilds: false,
344
- cache: enableCache ? (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(nodeModulesPath, '.cache', 'ncc-cache') : false
339
+ cache: enableCache ? join(nodeModulesPath, '.cache', 'ncc-cache') : false
345
340
  });
346
- await emitIndex(code, task.distPath, task.prettier);
347
- emitAssets(assets, task.distPath);
341
+ if (!task.dtsOnly) {
342
+ await emitIndex(code, task.distPath, task.prettier && !task.minify);
343
+ emitAssets(assets, task.distPath);
344
+ }
348
345
  await emitDts(task, mergedExternals);
349
346
  emitLicense(task);
350
347
  emitPackageJson(task, assets);
package/dist/types.d.ts CHANGED
@@ -9,6 +9,8 @@ export type DependencyConfig = {
9
9
  minify?: boolean;
10
10
  /** Externals to leave as requires of the build. */
11
11
  externals?: Record<string, string>;
12
+ /** Only bundle the dts files */
13
+ dtsOnly?: boolean;
12
14
  /** Externals types */
13
15
  dtsExternals?: Array<string | RegExp>;
14
16
  /** Whether to prettier the code and strip comments */
@@ -42,6 +44,7 @@ export type ParsedTask = {
42
44
  distPath: string;
43
45
  importPath: string;
44
46
  ignoreDts?: boolean;
47
+ dtsOnly?: boolean;
45
48
  copyDts?: boolean;
46
49
  prettier?: boolean;
47
50
  target: NonNullable<DependencyConfig['target']>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prebundle",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/rspack-contrib/prebundle"
@@ -24,21 +24,21 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@vercel/ncc": "0.38.3",
27
- "prettier": "^3.5.3",
28
- "rollup": "^4.38.0",
27
+ "prettier": "^3.6.2",
28
+ "rollup": "^4.44.1",
29
29
  "rollup-plugin-dts": "^6.2.1",
30
- "terser": "^5.39.0"
30
+ "terser": "^5.43.1"
31
31
  },
32
32
  "devDependencies": {
33
- "@rslib/core": "0.6.1",
33
+ "@rslib/core": "0.10.4",
34
34
  "@types/fs-extra": "^11.0.4",
35
- "@types/node": "22.13.14",
35
+ "@types/node": "22.15.34",
36
36
  "fast-glob": "^3.3.3",
37
37
  "fs-extra": "^11.3.0",
38
- "rslog": "^1.2.3",
39
- "typescript": "^5.8.2"
38
+ "rslog": "^1.2.9",
39
+ "typescript": "^5.8.3"
40
40
  },
41
- "packageManager": "pnpm@10.7.0",
41
+ "packageManager": "pnpm@10.12.4",
42
42
  "publishConfig": {
43
43
  "access": "public",
44
44
  "registry": "https://registry.npmjs.org/",