Skip to content

Commit

Permalink
refactor: node externals (#393)
Browse files Browse the repository at this point in the history
* chore(deps): esbuild-node-externals is an optional peer dependency

* refactor: updated detecting `esbuild-node-externals` utils

- Added `nodeExternalsPluginUtilsPath()`
  • Loading branch information
webdeveric committed Nov 26, 2022
1 parent f95a171 commit e23d1f0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@typescript-eslint/eslint-plugin": "^5.34.0",
"@typescript-eslint/parser": "^5.34.0",
"esbuild": "^0.15.9",
"esbuild-node-externals": "^1.5.0",
"eslint": "^8.20.0",
"husky": "^8.0.1",
"jest": "^29.0.3",
Expand All @@ -93,7 +94,13 @@
"typescript": "^4.8.3"
},
"peerDependencies": {
"esbuild": ">=0.8 <0.16"
"esbuild": ">=0.8 <0.16",
"esbuild-node-externals": "^1.0.0"
},
"peerDependenciesMeta": {
"esbuild-node-externals": {
"optional": true
}
},
"engines": {
"node": ">=16.0.0"
Expand Down
52 changes: 38 additions & 14 deletions src/pack-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import {
import { getPackager } from './packagers';
import { findProjectRoot, findUp } from './utils';

import type {
findDependencies as FindDependenciesFn,
findPackagePaths as FindPackagePathsFn,
} from 'esbuild-node-externals/dist/utils';

import type EsbuildServerlessPlugin from './index';
import type { JSONObject } from './types';

Expand Down Expand Up @@ -164,6 +169,18 @@ function getProdModules(externalModules: { external: string }[], packageJsonPath
return prodModules;
}

export function nodeExternalsPluginUtilsPath(): string | undefined {
try {
const resolvedPackage = require.resolve('esbuild-node-externals/dist/utils', {
paths: [process.cwd()],
});

return resolvedPackage;
} catch {
// No-op
}
}

/**
* We need a performant algorithm to install the packages for each single
* function (in case we package individually).
Expand All @@ -180,20 +197,27 @@ function getProdModules(externalModules: { external: string }[], packageJsonPath
export async function packExternalModules(this: EsbuildServerlessPlugin) {
const plugins = this.plugins;

if (
plugins &&
plugins.map((plugin) => plugin.name).includes('node-externals') &&
fse.existsSync(path.resolve(__dirname, '../../esbuild-node-externals/dist/utils.js'))
) {
const { findDependencies, findPackagePaths } = require('esbuild-node-externals/dist/utils');

const allowList = this.buildOptions?.nodeExternals?.allowList ? this.buildOptions.nodeExternals.allowList : [];

this.buildOptions.external = findDependencies({
dependencies: true,
packagePaths: findPackagePaths(),
allowList,
});
if (plugins && plugins.map((plugin) => plugin.name).includes('node-externals')) {
const utilsPath = nodeExternalsPluginUtilsPath();

if (utilsPath) {
const {
findDependencies,
findPackagePaths,
}: {
findDependencies: typeof FindDependenciesFn;
findPackagePaths: typeof FindPackagePathsFn;
} = require(utilsPath);

this.buildOptions.external = findDependencies({
packagePaths: findPackagePaths(),
dependencies: true,
devDependencies: false,
peerDependencies: false,
optionalDependencies: false,
allowList: this.buildOptions.nodeExternals?.allowList ?? [],
});
}
}

let externals = [];
Expand Down

0 comments on commit e23d1f0

Please sign in to comment.