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

feat: resolve modules from pluginsModulesPath in NFT #762

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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