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

Commit 28a85fb

Browse files
danezeduardoboucas
andauthoredMay 18, 2022
fix: correctly annotate transpilation errors in nft bundler (#1086)
Add `customErrorInfo` to errors happening during transpilation of esm with esbuild in the nft bundler Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
1 parent 61e5b72 commit 28a85fb

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed
 

‎src/runtimes/node/bundlers/nft/es_modules.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const processESM = async ({
5656
fsCache,
5757
mainFile,
5858
reasons,
59+
name,
5960
}: {
6061
basePath: string | undefined
6162
config: FunctionConfig
@@ -64,6 +65,7 @@ export const processESM = async ({
6465
fsCache: FsCache
6566
mainFile: string
6667
reasons: NodeFileTraceReasons
68+
name: string
6769
}): Promise<{ rewrites?: Map<string, string>; moduleFormat: ModuleFormat }> => {
6870
const entrypointIsESM = isEntrypointESM({ basePath, esmPaths, mainFile })
6971

@@ -82,7 +84,7 @@ export const processESM = async ({
8284
}
8385
}
8486

85-
const rewrites = await transpileESM({ basePath, config, esmPaths, fsCache, reasons })
87+
const rewrites = await transpileESM({ basePath, config, esmPaths, fsCache, reasons, name })
8688

8789
return {
8890
moduleFormat: 'cjs',
@@ -140,12 +142,14 @@ const transpileESM = async ({
140142
esmPaths,
141143
fsCache,
142144
reasons,
145+
name,
143146
}: {
144147
basePath: string | undefined
145148
config: FunctionConfig
146149
esmPaths: Set<string>
147150
fsCache: FsCache
148151
reasons: NodeFileTraceReasons
152+
name: string
149153
}) => {
150154
const cache: Map<string, boolean> = new Map()
151155
const pathsToTranspile = [...esmPaths].filter((path) => shouldTranspile(path, cache, esmPaths, reasons))
@@ -166,7 +170,7 @@ const transpileESM = async ({
166170
await Promise.all(
167171
pathsToTranspile.map(async (path) => {
168172
const absolutePath = resolvePath(path, basePath)
169-
const transpiled = await transpile(absolutePath, config)
173+
const transpiled = await transpile(absolutePath, config, name)
170174

171175
rewrites.set(absolutePath, transpiled)
172176
}),

‎src/runtimes/node/bundlers/nft/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const bundle: BundleFunction = async ({
2525
config,
2626
featureFlags,
2727
mainFile,
28+
name,
2829
pluginsModulesPath,
2930
repositoryRoot = basePath,
3031
}) => {
@@ -43,6 +44,7 @@ const bundle: BundleFunction = async ({
4344
featureFlags,
4445
mainFile,
4546
pluginsModulesPath,
47+
name,
4648
})
4749
const filteredIncludedPaths = filterExcludedPaths([...dependencyPaths, ...includedFilePaths], excludedPaths)
4850
const dirnames = filteredIncludedPaths.map((filePath) => normalize(dirname(filePath))).sort()
@@ -73,12 +75,14 @@ const traceFilesAndTranspile = async function ({
7375
featureFlags,
7476
mainFile,
7577
pluginsModulesPath,
78+
name,
7679
}: {
7780
basePath?: string
7881
config: FunctionConfig
7982
featureFlags: FeatureFlags
8083
mainFile: string
8184
pluginsModulesPath?: string
85+
name: string
8286
}) {
8387
const fsCache: FsCache = {}
8488
const {
@@ -129,6 +133,7 @@ const traceFilesAndTranspile = async function ({
129133
fsCache,
130134
mainFile,
131135
reasons,
136+
name,
132137
})
133138

134139
return {
+27-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
import { build } from '@netlify/esbuild'
22

33
import type { FunctionConfig } from '../../../../config.js'
4+
import type { RuntimeName } from '../../../runtime.js'
45
import { getBundlerTarget } from '../esbuild/bundler_target.js'
6+
import type { NodeBundlerName } from '../index.js'
57

6-
export const transpile = async (path: string, config: FunctionConfig) => {
8+
export const transpile = async (path: string, config: FunctionConfig, functionName: string) => {
79
// The version of ECMAScript to use as the build target. This will determine
810
// whether certain features are transpiled down or left untransformed.
911
const nodeTarget = getBundlerTarget(config.nodeVersion)
10-
const transpiled = await build({
11-
bundle: false,
12-
entryPoints: [path],
13-
format: 'cjs',
14-
logLevel: 'error',
15-
platform: 'node',
16-
sourcemap: Boolean(config.nodeSourcemap),
17-
target: [nodeTarget],
18-
write: false,
19-
})
2012

21-
return transpiled.outputFiles[0].text
13+
try {
14+
const transpiled = await build({
15+
bundle: false,
16+
entryPoints: [path],
17+
format: 'cjs',
18+
logLevel: 'error',
19+
platform: 'node',
20+
sourcemap: Boolean(config.nodeSourcemap),
21+
target: [nodeTarget],
22+
write: false,
23+
})
24+
25+
return transpiled.outputFiles[0].text
26+
} catch (error) {
27+
const bundler: NodeBundlerName = 'nft'
28+
const runtime: RuntimeName = 'js'
29+
30+
error.customErrorInfo = {
31+
type: 'functionsBundling',
32+
location: { bundler, functionName, runtime },
33+
}
34+
35+
throw error
36+
}
2237
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const func = async () => {}
2+
3+
await func()
4+
5+
export { func } //makes nft detect this file as esm

‎tests/main.js

+17
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,23 @@ testMany(
18971897
},
18981898
)
18991899

1900+
test('Adds `type: "functionsBundling"` to user errors when transpiling esm in nft bundler', async (t) => {
1901+
try {
1902+
await zipNode(t, 'node-esm-top-level-await-error', {
1903+
opts: { config: { '*': { nodeBundler: 'nft' } } },
1904+
})
1905+
1906+
t.fail('Bundling should have thrown')
1907+
} catch (error) {
1908+
const { customErrorInfo } = error
1909+
1910+
t.is(customErrorInfo.type, 'functionsBundling')
1911+
t.is(customErrorInfo.location.bundler, 'nft')
1912+
t.is(customErrorInfo.location.functionName, 'function')
1913+
t.is(customErrorInfo.location.runtime, 'js')
1914+
}
1915+
})
1916+
19001917
test('Returns a list of all modules with dynamic imports in a `nodeModulesWithDynamicImports` property', async (t) => {
19011918
const fixtureName = 'node-module-dynamic-import'
19021919
const { files } = await zipNode(t, fixtureName, {

1 commit comments

Comments
 (1)

github-actions[bot] commented on May 18, 2022

@github-actions[bot]
Contributor

⏱ Benchmark results

largeDepsEsbuild: 6.3s

largeDepsNft: 29.1s

largeDepsZisi: 45.1s

This repository has been archived.