Skip to content

Commit

Permalink
feat: add external dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Nov 15, 2024
1 parent 24f5386 commit 7571f1d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/findDependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 10 additions & 2 deletions src/findDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const findDependencies = (args: {
sourceFilePath: string
imports?: string[]
visited?: string[]
packages?: Set<string>
tsConfigFilePath?: string
importsSubpathPatterns?: Record<string, string>
}): {
Expand All @@ -25,13 +26,18 @@ export const findDependencies = (args: {
* @see https://nodejs.org/api/packages.html#subpath-patterns
*/
importsSubpathPatterns: Record<string, string>
/**
* The external packages that the source file depends on
*/
packages: Set<string>
} => {
const sourceFilePath = args.sourceFilePath
const visited = args.visited ?? []
const dependencies = args.imports ?? []
const packages = args.packages ?? new Set<string>()
let importsSubpathPatterns = args.importsSubpathPatterns ?? {}
if (visited.includes(sourceFilePath))
return { dependencies, importsSubpathPatterns }
return { dependencies, importsSubpathPatterns, packages }
const tsConfigFilePath = args.tsConfigFilePath
const tsConfig =

Check warning on line 42 in src/findDependencies.ts

View workflow job for this annotation

GitHub Actions / tests

Unsafe assignment of an `any` value
tsConfigFilePath !== undefined
Expand Down Expand Up @@ -71,6 +77,7 @@ export const findDependencies = (args: {
} catch {
// Module or file not found
visited.push(file)
packages.add(moduleSpecifier)
}
}
ts.forEachChild(fileNode, parseChild)
Expand All @@ -83,10 +90,11 @@ export const findDependencies = (args: {
visited,
tsConfigFilePath,
importsSubpathPatterns,
packages,
})
}

return { dependencies, importsSubpathPatterns }
return { dependencies, importsSubpathPatterns, packages }
}

const resolve = ({
Expand Down
10 changes: 9 additions & 1 deletion src/packLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -104,6 +109,9 @@ export const packLambda = async ({
JSON.stringify({
type: 'module',
imports: importsSubpathPatterns,
dependencies: Object.fromEntries(
packages.values().map((pkg) => [pkg, '*']),
),
}),
'utf-8',
),
Expand Down

0 comments on commit 7571f1d

Please sign in to comment.