diff --git a/package-lock.json b/package-lock.json index f76bcb26..b2bf715d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,6 +35,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", @@ -3902,6 +3903,25 @@ "node": ">=12" } }, + "node_modules/esbuild-node-externals": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/esbuild-node-externals/-/esbuild-node-externals-1.5.0.tgz", + "integrity": "sha512-9394Ne2t2Z243BWeNBRkXEYVMOVbQuzp7XSkASZTOQs0GSXDuno5aH5OmzEXc6GMuln5zJjpkZpgwUPW0uRKgw==", + "dev": true, + "dependencies": { + "find-up": "5.0.0", + "tslib": "2.3.1" + }, + "peerDependencies": { + "esbuild": "0.12 - 0.15" + } + }, + "node_modules/esbuild-node-externals/node_modules/tslib": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", + "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==", + "dev": true + }, "node_modules/esbuild-openbsd-64": { "version": "0.15.9", "resolved": "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.15.9.tgz", @@ -14677,6 +14697,24 @@ "dev": true, "optional": true }, + "esbuild-node-externals": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/esbuild-node-externals/-/esbuild-node-externals-1.5.0.tgz", + "integrity": "sha512-9394Ne2t2Z243BWeNBRkXEYVMOVbQuzp7XSkASZTOQs0GSXDuno5aH5OmzEXc6GMuln5zJjpkZpgwUPW0uRKgw==", + "dev": true, + "requires": { + "find-up": "5.0.0", + "tslib": "2.3.1" + }, + "dependencies": { + "tslib": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", + "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==", + "dev": true + } + } + }, "esbuild-openbsd-64": { "version": "0.15.9", "resolved": "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.15.9.tgz", diff --git a/package.json b/package.json index a1ad748f..35428af0 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/pack-externals.ts b/src/pack-externals.ts index 0a368f85..9bc0f8b3 100644 --- a/src/pack-externals.ts +++ b/src/pack-externals.ts @@ -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'; @@ -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). @@ -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 = [];