Skip to content

Commit

Permalink
feat: support for configuring common externals (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Apr 25, 2024
1 parent 96a5045 commit 8ec88a8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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<string | DependencyConfig>) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/prebundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>,
) {
if (pkgName && task.depName !== pkgName) {
return;
}
Expand All @@ -163,6 +166,7 @@ export async function prebundle(task: ParsedTask) {
target: 'es2019',
externals: {
...DEFAULT_EXTERNALS,
...commonExternals,
...task.externals,
},
assetBuilds: false,
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export type DependencyConfig = {
};

export type Config = {
/**
* Configure externals for all packages,
* will be merged with dependencies[i].externals.
*/
externals: Record<string, string>;
dependencies: Array<string | DependencyConfig>;
};

Expand Down

0 comments on commit 8ec88a8

Please sign in to comment.