diff --git a/README.md b/README.md index 52270e4..fee6ef5 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,18 @@ export default { }; ``` +You can also configure `externals` for all packages like this: + +```ts +// prebundle.config.mjs +export default { + externals: { + webpack: '../webpack', + }, + dependencies: [{ name: 'foo' }, { name: 'foo' }], +}; +``` + ### minify Whether to minify the code, default `true`. diff --git a/src/helper.ts b/src/helper.ts index c933dda..ba004a5 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -1,7 +1,7 @@ import { dirname, join } from 'node:path'; import fs from 'fs-extra'; import { cwd, DIST_DIR } from './constant.js'; -import type { DependencyConfig, ParsedTask } from './types.js'; +import type { Config, DependencyConfig, ParsedTask } from './types.js'; import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); @@ -23,7 +23,7 @@ export function findDepPath(name: string) { export const resolveConfig = async () => { const configPath = join(cwd, 'prebundle.config.mjs'); const config = await import(configPath); - return config.default; + return config.default as Config; }; export function parseTasks(dependencies: Array) { diff --git a/src/index.ts b/src/index.ts index dba58b8..8e7bd47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ export async function run() { const parsedTasks = parseTasks(config.dependencies); for (const task of parsedTasks) { - await prebundle(task); + await prebundle(task, config.externals); } } diff --git a/src/prebundle.ts b/src/prebundle.ts index 741f5ef..017eff7 100644 --- a/src/prebundle.ts +++ b/src/prebundle.ts @@ -145,7 +145,10 @@ function renameDistFolder(task: ParsedTask) { const pkgName = process.argv[2]; -export async function prebundle(task: ParsedTask) { +export async function prebundle( + task: ParsedTask, + commonExternals: Record, +) { if (pkgName && task.depName !== pkgName) { return; } @@ -163,6 +166,7 @@ export async function prebundle(task: ParsedTask) { target: 'es2019', externals: { ...DEFAULT_EXTERNALS, + ...commonExternals, ...task.externals, }, assetBuilds: false, diff --git a/src/types.ts b/src/types.ts index 61968ae..fb6db55 100644 --- a/src/types.ts +++ b/src/types.ts @@ -23,6 +23,11 @@ export type DependencyConfig = { }; export type Config = { + /** + * Configure externals for all packages, + * will be merged with dependencies[i].externals. + */ + externals: Record; dependencies: Array; };