diff --git a/src/findDependencies.spec.ts b/src/findDependencies.spec.ts index 176c0be..e655d77 100644 --- a/src/findDependencies.spec.ts +++ b/src/findDependencies.spec.ts @@ -7,6 +7,21 @@ import { findDependencies } from './findDependencies.js' const __dirname = new URL('.', import.meta.url).pathname void describe('findDependencies()', () => { + void it('should return a list of external dependencies', () => { + const { packages } = findDependencies({ + sourceFilePath: path.join(__dirname, '..', 'cdk', 'lambda.ts'), + }) + assert.equal( + packages.has('aws-lambda'), + true, + "Should include the 'aws-lambda' package", + ) + assert.equal( + packages.has('id128'), + true, + "Should include the 'id128' package", + ) + }) void it('should honor tsconfig.json paths', () => { const { dependencies } = findDependencies({ sourceFilePath: path.join( diff --git a/src/findDependencies.ts b/src/findDependencies.ts index 4b77162..81f1ff2 100644 --- a/src/findDependencies.ts +++ b/src/findDependencies.ts @@ -16,6 +16,7 @@ export const findDependencies = (args: { sourceFilePath: string imports?: string[] visited?: string[] + packages?: Set tsConfigFilePath?: string importsSubpathPatterns?: Record }): { @@ -25,13 +26,18 @@ export const findDependencies = (args: { * @see https://nodejs.org/api/packages.html#subpath-patterns */ importsSubpathPatterns: Record + /** + * The external packages that the source file depends on + */ + packages: Set } => { const sourceFilePath = args.sourceFilePath const visited = args.visited ?? [] const dependencies = args.imports ?? [] + const packages = args.packages ?? new Set() let importsSubpathPatterns = args.importsSubpathPatterns ?? {} if (visited.includes(sourceFilePath)) - return { dependencies, importsSubpathPatterns } + return { dependencies, importsSubpathPatterns, packages } const tsConfigFilePath = args.tsConfigFilePath const tsConfig = tsConfigFilePath !== undefined @@ -71,6 +77,7 @@ export const findDependencies = (args: { } catch { // Module or file not found visited.push(file) + packages.add(moduleSpecifier) } } ts.forEachChild(fileNode, parseChild) @@ -83,10 +90,11 @@ export const findDependencies = (args: { visited, tsConfigFilePath, importsSubpathPatterns, + packages, }) } - return { dependencies, importsSubpathPatterns } + return { dependencies, importsSubpathPatterns, packages } } const resolve = ({ diff --git a/src/packLambda.ts b/src/packLambda.ts index e7d15a1..11445e4 100644 --- a/src/packLambda.ts +++ b/src/packLambda.ts @@ -49,10 +49,15 @@ export const packLambda = async ({ debug?: (label: string, info: string) => void progress?: (label: string, info: string) => void }): Promise<{ handler: string; hash: string }> => { - const { dependencies: deps, importsSubpathPatterns } = findDependencies({ + const { + dependencies: deps, + importsSubpathPatterns, + packages, + } = findDependencies({ sourceFilePath, tsConfigFilePath, }) + debug?.(`dependencies`, [...packages].join(', ')) const lambdaFiles = [sourceFilePath, ...deps] const zipfile = new yazl.ZipFile() @@ -104,6 +109,9 @@ export const packLambda = async ({ JSON.stringify({ type: 'module', imports: importsSubpathPatterns, + dependencies: Object.fromEntries( + packages.values().map((pkg) => [pkg, '*']), + ), }), 'utf-8', ),