prebundle 1.5.0 → 1.6.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/bin.js CHANGED
@@ -1,4 +1,28 @@
1
1
  #!/usr/bin/env node
2
+ import cac from 'cac';
3
+ import { createRequire } from 'node:module';
2
4
  import { run } from './dist/index.js';
3
5
 
4
- run();
6
+ const require = createRequire(import.meta.url);
7
+ const pkg = require('./package.json');
8
+
9
+ const cli = cac('prebundle');
10
+
11
+ cli
12
+ .command('[...packages]', 'Prebundle configured dependencies')
13
+ .option('--config <path>', 'Path to a custom config file')
14
+ .action(async (packages = [], options) => {
15
+ try {
16
+ await run({
17
+ config: options.config,
18
+ packages,
19
+ });
20
+ } catch (error) {
21
+ console.error(error);
22
+ process.exitCode = 1;
23
+ }
24
+ });
25
+
26
+ cli.help();
27
+ cli.version(pkg.version);
28
+ cli.parse();
package/dist/helper.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Config, DependencyConfig, ParsedTask } from './types.js';
2
2
  export declare function findDepPath(name: string): string | null;
3
- export declare const resolveConfig: () => Promise<Config>;
3
+ export declare const resolveConfig: (configFile?: string) => Promise<Config>;
4
4
  export declare function parseTasks(dependencies: Array<string | DependencyConfig>, globalPrettier?: boolean): ParsedTask[];
5
5
  export declare function pick<T, U extends keyof T>(obj: T, keys: ReadonlyArray<U>): Pick<T, U>;
6
6
  export declare function replaceFileContent(filePath: string, replaceFn: (content: string) => string): void;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- export declare function run(): Promise<void>;
1
+ export interface RunOptions {
2
+ config?: string;
3
+ packages?: string[];
4
+ }
5
+ export declare function run(options?: RunOptions): Promise<void>;
2
6
  export type { Config } from './types.js';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { dirname, extname, join } from "node:path";
1
+ import { dirname, extname, join, resolve } from "node:path";
2
2
  import fs_extra from "../compiled/fs-extra/index.js";
3
3
  import { createRequire } from "node:module";
4
4
  import { pathToFileURL } from "node:url";
@@ -60,7 +60,7 @@ const cwd = process.cwd();
60
60
  const helper_require = createRequire(import.meta.url);
61
61
  function findDepPath(name) {
62
62
  try {
63
- let entry = dirname(helper_require.resolve(join(name), {
63
+ let entry = dirname(helper_require.resolve(name, {
64
64
  paths: [
65
65
  cwd
66
66
  ]
@@ -72,20 +72,20 @@ function findDepPath(name) {
72
72
  return null;
73
73
  }
74
74
  }
75
- const resolveConfig = async ()=>{
76
- const configFiles = [
75
+ const resolveConfig = async (configFile)=>{
76
+ const configFiles = configFile ? [
77
+ resolve(cwd, configFile)
78
+ ] : [
77
79
  'prebundle.config.ts',
78
80
  'prebundle.config.mts',
79
81
  'prebundle.config.mjs',
80
82
  'prebundle.config.js'
81
- ];
82
- for (const filename of configFiles){
83
- const configPath = join(cwd, filename);
84
- if (fs_extra.existsSync(configPath)) {
85
- const config = await import(pathToFileURL(configPath).href);
86
- return config.default;
87
- }
83
+ ].map((filename)=>join(cwd, filename));
84
+ for (const filename of configFiles)if (fs_extra.existsSync(filename)) {
85
+ const config = await import(pathToFileURL(filename).href);
86
+ return config.default ?? config;
88
87
  }
88
+ if (configFile) throw new Error(`Unable to locate prebundle config file at: ${configFile}`);
89
89
  throw new Error('Unable to locate prebundle config file.');
90
90
  };
91
91
  function parseTasks(dependencies, globalPrettier) {
@@ -202,10 +202,7 @@ async function emitIndex(code, distPath, prettier) {
202
202
  await fs_extra.outputFile(distIndex, formatted);
203
203
  } else await fs_extra.outputFile(distIndex, code);
204
204
  }
205
- const getTypes = (json)=>{
206
- var _json_exports;
207
- return json && (json.types || json.typing || json.typings || getTypes(null == (_json_exports = json.exports) ? void 0 : _json_exports['.'])) || null;
208
- };
205
+ const getTypes = (json)=>json && (json.types || json.typing || json.typings || getTypes(json.exports?.['.'])) || null;
209
206
  async function emitDts(task, externals) {
210
207
  const outputDefaultDts = ()=>{
211
208
  fs_extra.outputFileSync(join(task.distPath, 'index.d.ts'), 'export = any;\n');
@@ -287,7 +284,8 @@ function emitPackageJson(task, assets) {
287
284
  ]);
288
285
  if (task.depName !== pickedPackageJson.name) pickedPackageJson.name = task.depName;
289
286
  pickedPackageJson.types = task.copyDts ? getTypes(packageJson) : 'index.d.ts';
290
- pickedPackageJson.type = 'commonjs';
287
+ if (task.dtsOnly) pickedPackageJson.type = packageJson.type || 'commonjs';
288
+ else pickedPackageJson.type = 'commonjs';
291
289
  if (assets['package.json']) try {
292
290
  Object.assign(pickedPackageJson, pick(JSON.parse(assets['package.json'].source), [
293
291
  'type'
@@ -317,20 +315,15 @@ function renameDistFolder(task) {
317
315
  'types',
318
316
  'typing',
319
317
  'typings'
320
- ]){
321
- var _pkgJson_key;
322
- if (null == (_pkgJson_key = pkgJson[key]) ? void 0 : _pkgJson_key.startsWith('dist/')) {
323
- pkgJson[key] = pkgJson[key].replace('dist/', 'types/');
324
- const distFolder = join(task.distPath, 'dist');
325
- const typesFolder = join(task.distPath, 'types');
326
- if (fs_extra.existsSync(distFolder)) fs_extra.renameSync(distFolder, typesFolder);
327
- }
318
+ ])if (pkgJson[key]?.startsWith('dist/')) {
319
+ pkgJson[key] = pkgJson[key].replace('dist/', 'types/');
320
+ const distFolder = join(task.distPath, 'dist');
321
+ const typesFolder = join(task.distPath, 'types');
322
+ if (fs_extra.existsSync(distFolder)) fs_extra.renameSync(distFolder, typesFolder);
328
323
  }
329
324
  fs_extra.writeJSONSync(pkgPath, pkgJson);
330
325
  }
331
- const pkgName = process.argv[2];
332
326
  async function prebundle(task, commonExternals = {}) {
333
- if (pkgName && task.depName !== pkgName) return;
334
327
  logger.start(`prebundle: ${task.depName}`);
335
328
  fs_extra.removeSync(task.distPath);
336
329
  if (task.beforeBundle) await task.beforeBundle(task);
@@ -342,29 +335,31 @@ async function prebundle(task, commonExternals = {}) {
342
335
  const nodeModulesPath = join(process.cwd(), 'node_modules');
343
336
  const hasNodeModules = existsSync(nodeModulesPath);
344
337
  const enableCache = !process.env.CI && hasNodeModules;
345
- const { code, assets } = await ncc(task.depEntry, {
346
- minify: task.minify,
347
- target: task.target,
348
- externals: mergedExternals,
349
- assetBuilds: false,
350
- cache: enableCache ? join(nodeModulesPath, '.cache', 'ncc-cache') : false
351
- });
352
- if (!task.dtsOnly) {
338
+ if (task.dtsOnly) emitPackageJson(task, {});
339
+ else {
340
+ const { code, assets } = await ncc(task.depEntry, {
341
+ minify: task.minify,
342
+ target: task.target,
343
+ externals: mergedExternals,
344
+ assetBuilds: false,
345
+ cache: enableCache ? join(nodeModulesPath, '.cache', 'ncc-cache') : false
346
+ });
353
347
  await emitIndex(code, task.distPath, task.prettier && !task.minify);
354
348
  emitAssets(assets, task.distPath);
349
+ emitPackageJson(task, assets);
355
350
  }
356
351
  await emitDts(task, mergedExternals);
357
352
  emitLicense(task);
358
- emitPackageJson(task, assets);
359
353
  removeSourceMap(task);
360
354
  renameDistFolder(task);
361
355
  emitExtraFiles(task);
362
356
  if (task.afterBundle) await task.afterBundle(task);
363
357
  logger.success(`prebundle: ${task.depName}\n\n`);
364
358
  }
365
- async function run() {
366
- const config = await resolveConfig();
359
+ async function run(options = {}) {
360
+ const config = await resolveConfig(options.config);
367
361
  const parsedTasks = parseTasks(config.dependencies, config.prettier);
368
- for (const task of parsedTasks)await prebundle(task, config.externals);
362
+ const filters = options.packages?.length ? new Set(options.packages) : null;
363
+ for (const task of parsedTasks)if (!filters || filters.has(task.depName)) await prebundle(task, config.externals);
369
364
  }
370
365
  export { run };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "prebundle",
3
- "version": "1.5.0",
3
+ "version": "1.6.1",
4
4
  "repository": {
5
5
  "type": "git",
6
- "url": "https://github.com/rspack-contrib/prebundle"
6
+ "url": "https://github.com/rstackjs/prebundle"
7
7
  },
8
8
  "type": "module",
9
9
  "main": "./dist/index.js",
@@ -20,25 +20,30 @@
20
20
  "dev": "rslib build --watch",
21
21
  "prebundle": "node ./bin.js",
22
22
  "prepare": "npm run build",
23
- "bump": "npx bumpp"
23
+ "bump": "npx bumpp",
24
+ "test": "rstest"
24
25
  },
25
26
  "dependencies": {
27
+ "cac": "^6.7.14",
26
28
  "@vercel/ncc": "0.38.4",
27
- "prettier": "^3.6.2",
28
- "rollup": "^4.52.3",
29
- "rollup-plugin-dts": "^6.2.3",
30
- "terser": "^5.44.0"
29
+ "prettier": "^3.7.3",
30
+ "rollup": "^4.53.3",
31
+ "rollup-plugin-dts": "^6.3.0",
32
+ "terser": "^5.44.1"
31
33
  },
32
34
  "devDependencies": {
33
- "@rslib/core": "0.15.0",
35
+ "@astrojs/sitemap": "^3.6.0",
36
+ "@rslib/core": "0.18.2",
37
+ "@rstest/core": "^0.6.8",
34
38
  "@types/fs-extra": "^11.0.4",
35
- "@types/node": "22.18.8",
39
+ "@types/node": "22.19.1",
40
+ "chalk": "^5.6.2",
36
41
  "fast-glob": "^3.3.3",
37
42
  "fs-extra": "^11.3.2",
38
- "rslog": "^1.2.11",
43
+ "rslog": "^1.3.2",
39
44
  "typescript": "^5.9.3"
40
45
  },
41
- "packageManager": "pnpm@10.17.1",
46
+ "packageManager": "pnpm@10.24.0",
42
47
  "publishConfig": {
43
48
  "access": "public",
44
49
  "registry": "https://registry.npmjs.org/",