Skip to content

Commit

Permalink
feat: handle esbuild absWorkingDir option (#52)
Browse files Browse the repository at this point in the history
* feat: handle esbuild `absWorkingDir` option

* docs: mention `options.cwd` in readme
  • Loading branch information
antongolub authored Dec 20, 2023
1 parent 83ed85a commit 8e6e809
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
4 changes: 4 additions & 0 deletions esbuild-node-externals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ An array for the externals to allow, so they will be included in the bundle. Can

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

#### `options.cwd` (default to `buildOptions.absWorkingDir || process.cwd()`)

Sets the current working directory for the plugin.

## 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
34 changes: 23 additions & 11 deletions esbuild-node-externals/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ export interface Options {
optionalDependencies?: boolean;
allowList?: AllowList;
allowWorkspaces?: boolean;
cwd?: string;
}

const foundPackagePaths: Map<string, string[]> = new Map();
const findPackagePathsMemoized = (cwd: string): string[] => {
if (foundPackagePaths.has(cwd)) {
return foundPackagePaths.get(cwd)!;
}

foundPackagePaths.set(cwd, findPackagePaths(cwd));
return findPackagePathsMemoized(cwd);
};

export const nodeExternalsPlugin = (paramsOptions: Options = {}): Plugin => {
const options = {
dependencies: true,
Expand All @@ -34,21 +45,22 @@ export const nodeExternalsPlugin = (paramsOptions: Options = {}): Plugin => {
const allowPredicate =
options.allowList && createAllowPredicate(options.allowList);

const nodeModules = findDependencies({
packagePaths: options.packagePath
? options.packagePath
: findPackagePaths(),
dependencies: options.dependencies,
devDependencies: options.devDependencies,
peerDependencies: options.peerDependencies,
optionalDependencies: options.optionalDependencies,
allowPredicate,
allowWorkspaces: options.allowWorkspaces,
});

return {
name: 'node-externals',
setup(build) {
const cwd = options.cwd || build.initialOptions.absWorkingDir || process.cwd()
const nodeModules = findDependencies({
packagePaths: options.packagePath
? options.packagePath
: findPackagePathsMemoized(cwd),
dependencies: options.dependencies,
devDependencies: options.devDependencies,
peerDependencies: options.peerDependencies,
optionalDependencies: options.optionalDependencies,
allowPredicate,
allowWorkspaces: options.allowWorkspaces,
});
// On every module resolved, we check if the module name should be an external
build.onResolve({ namespace: 'file', filter: /.*/ }, (args) => {
// To allow allowList to target sub imports
Expand Down
5 changes: 3 additions & 2 deletions esbuild-node-externals/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ const isInGitDirectory = (path: string, gitRootPath?: string): boolean => {
* files outside of the git repository will not be yielded.
* Inspired by https://github.com/Septh/rollup-plugin-node-externals/blob/f13ee95c6f1f01d8ba2276bf491aac399adc5482/src/dependencies.ts#L18
*/
export const findPackagePaths = (): string[] => {
export const findPackagePaths = (_cwd: string = process.cwd()): string[] => {
// Find git root if in git repository
const gitDirectoryPath = findUp.sync('.git', {
type: 'directory',
cwd: _cwd,
});
const gitRootPath: string | undefined =
gitDirectoryPath === undefined ? undefined : path.dirname(gitDirectoryPath);

let cwd: string = process.cwd();
let cwd: string = _cwd;
let packagePath: string | undefined;
const packagePaths: string[] = [];

Expand Down

0 comments on commit 8e6e809

Please sign in to comment.