Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: resolve modules from pluginsModulesPath in NFT (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Oct 22, 2021
1 parent 2d7900f commit e421576
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
32 changes: 30 additions & 2 deletions src/runtimes/node/bundlers/nft/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { dirname, normalize, resolve } from 'path'
import { basename, dirname, join, normalize, resolve } from 'path'

import { nodeFileTrace } from '@vercel/nft'
import resolveDependency from '@vercel/nft/out/resolve-dependency'

import type { BundleFunction } from '..'
import type { FunctionConfig } from '../../../../config'
Expand All @@ -16,7 +17,15 @@ interface NftCache {
[key: string]: unknown
}

const bundle: BundleFunction = async ({ basePath, config, mainFile, repositoryRoot = basePath }) => {
const appearsToBeModuleName = (name: string) => !name.startsWith('.')

const bundle: BundleFunction = async ({
basePath,
config,
mainFile,
pluginsModulesPath,
repositoryRoot = basePath,
}) => {
const { includedFiles = [], includedFilesBasePath } = config
const { exclude: excludedPaths, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
Expand All @@ -30,6 +39,7 @@ const bundle: BundleFunction = async ({ basePath, config, mainFile, repositoryRo
basePath: repositoryRoot,
config,
mainFile,
pluginsModulesPath,
})
const filteredIncludedPaths = filterExcludedPaths([...dependencyPaths, ...includedFilePaths], excludedPaths)
const dirnames = filteredIncludedPaths.map((filePath) => normalize(dirname(filePath))).sort()
Expand All @@ -48,10 +58,12 @@ const traceFilesAndTranspile = async function ({
basePath,
config,
mainFile,
pluginsModulesPath,
}: {
basePath?: string
config: FunctionConfig
mainFile: string
pluginsModulesPath?: string
}) {
const fsCache: FsCache = {}
const cache: NftCache = {}
Expand All @@ -68,6 +80,22 @@ const traceFilesAndTranspile = async function ({
return null
}

throw error
}
},
resolve: async (specifier, parent, ...args) => {
try {
return await resolveDependency(specifier, parent, ...args)
} catch (error) {
// If we get a `MODULE_NOT_FOUND` error for what appears to be a module
// name, we try to resolve it a second time using `pluginsModulesPath`
// as the base directory.
if (error.code === 'MODULE_NOT_FOUND' && pluginsModulesPath && appearsToBeModuleName(specifier)) {
const newParent = join(pluginsModulesPath, basename(parent))

return await resolveDependency(specifier, newParent, ...args)
}

throw error
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ testMany(

testMany(
'Resolves dependencies from .netlify/plugins/node_modules',
['bundler_default', 'bundler_esbuild', 'bundler_esbuild_zisi', 'bundler_default_parse_esbuild', 'todo:bundler_nft'],
['bundler_default', 'bundler_esbuild', 'bundler_esbuild_zisi', 'bundler_default_parse_esbuild', 'bundler_nft'],
async (options, t) => {
await zipNode(t, 'node-module-next-image', { opts: options })
},
Expand Down

1 comment on commit e421576

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 7.9s

largeDepsNft: 51s

largeDepsZisi: 1m 3.4s

Please sign in to comment.