Skip to content

Commit

Permalink
feat: add flag to support workspaces dependencies (#41)
Browse files Browse the repository at this point in the history
Co-authored-by: Léo Pradel <[email protected]>
  • Loading branch information
James-Mnemosyne and pradel authored Nov 20, 2023
1 parent 372fbe5 commit 50b01b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions esbuild-node-externals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Make package.json `optionalDependencies` external.

Specify packages which are not marked as external. They will be included in the bundle.

#### `options.allowWorkspaces` (default to `false`)

Automatically exclude all packages defined as workspaces (`workspace:*`) in a monorepo.

## Inspiration

This package and the implementation are inspired by the work of @liady on [webpack-node-externals](https://github.com/liady/webpack-node-externals) for webpack and @Septh on [rollup-plugin-node-externals](https://github.com/Septh/rollup-plugin-node-externals) for rollup.
Expand Down
3 changes: 3 additions & 0 deletions esbuild-node-externals/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Options {
peerDependencies?: boolean;
optionalDependencies?: boolean;
allowList?: string[];
allowWorkspaces?: boolean;
}

export const nodeExternalsPlugin = (paramsOptions: Options = {}): Plugin => {
Expand All @@ -18,6 +19,7 @@ export const nodeExternalsPlugin = (paramsOptions: Options = {}): Plugin => {
peerDependencies: true,
optionalDependencies: true,
allowList: [] as string[],
allowWorkspaces: false,
...paramsOptions,
packagePath:
paramsOptions.packagePath && typeof paramsOptions.packagePath === 'string'
Expand All @@ -34,6 +36,7 @@ export const nodeExternalsPlugin = (paramsOptions: Options = {}): Plugin => {
peerDependencies: options.peerDependencies,
optionalDependencies: options.optionalDependencies,
allowList: options.allowList,
allowWorkspaces: options.allowWorkspaces,
});

return {
Expand Down
15 changes: 14 additions & 1 deletion esbuild-node-externals/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ export const findPackagePaths = (): string[] => {
return packagePaths;
};

function getDependencyKeys(map: Record<string, string> = {}, allowWorkspaces: boolean = false): string[] {
if (!map) {
return [];
}
if (!allowWorkspaces) {
return Object.keys(map);
}
// Filter out shared workspaces
return Object.keys(map)?.filter((depKey) => map[depKey] !== 'workspace:*');
}

/**
* Return an array of the package.json dependencies that should be excluded from the build.
*/
Expand All @@ -53,6 +64,7 @@ export const findDependencies = (options: {
peerDependencies: boolean;
optionalDependencies: boolean;
allowList: string[];
allowWorkspaces: boolean;
}): string[] => {
const packageJsonKeys = [
options.dependencies && 'dependencies',
Expand All @@ -74,7 +86,8 @@ export const findDependencies = (options: {
}

return packageJsonKeys
.map((key) => (packageJson[key] ? Object.keys(packageJson[key]) : []))
// Automatically exclude keys for interconnected yarn workspaces.
.map((key) => getDependencyKeys(packageJson[key], options.allowWorkspaces))
.flat(1)
.filter((packageName) => !options.allowList.includes(packageName));
});
Expand Down

0 comments on commit 50b01b0

Please sign in to comment.