-
Notifications
You must be signed in to change notification settings - Fork 8
/
bundler.test.ts
334 lines (277 loc) · 10 KB
/
bundler.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { promises as fs } from 'fs'
import { join, resolve } from 'path'
import process from 'process'
import { deleteAsync } from 'del'
import tmp from 'tmp-promise'
import { test, expect } from 'vitest'
import { importMapSpecifier } from '../shared/consts.js'
import { useFixture } from '../test/util.js'
import { BundleError } from './bundle_error.js'
import { bundle, BundleOptions } from './bundler.js'
import { isNodeError } from './utils/error.js'
import { validateManifest } from './validation/manifest/index.js'
test('Produces an ESZIP bundle', async () => {
const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const sourceDirectory = join(basePath, 'functions')
const result = await bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
})
const generatedFiles = await fs.readdir(distPath)
expect(result.functions.length).toBe(1)
expect(generatedFiles.length).toBe(2)
const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
expect(() => validateManifest(manifest)).not.toThrowError()
const { bundles, import_map: importMapURL } = manifest
expect(bundles.length).toBe(1)
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)
expect(importMapURL).toBe(importMapSpecifier)
await cleanup()
})
test('Uses the vendored eszip module instead of fetching it from deno.land', async () => {
const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const sourceDirectory = join(basePath, 'functions')
const result = await bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
})
const generatedFiles = await fs.readdir(distPath)
expect(result.functions.length).toBe(1)
expect(generatedFiles.length).toBe(2)
const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
const { bundles } = manifest
expect(bundles.length).toBe(1)
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)
await cleanup()
})
test('Adds a custom error property to user errors during bundling', async () => {
expect.assertions(2)
const { basePath, cleanup, distPath } = await useFixture('invalid_functions')
const sourceDirectory = join(basePath, 'functions')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
try {
await bundle([sourceDirectory], distPath, declarations, { basePath })
} catch (error) {
expect(error).toBeInstanceOf(BundleError)
expect((error as BundleError).customErrorInfo).toEqual({
location: {
format: 'eszip',
runtime: 'deno',
},
type: 'functionsBundling',
})
} finally {
await cleanup()
}
})
test('Prints a nice error message when user tries importing NPM module', async () => {
expect.assertions(2)
const { basePath, cleanup, distPath } = await useFixture('imports_npm_module')
const sourceDirectory = join(basePath, 'functions')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
try {
await bundle([sourceDirectory], distPath, declarations, { basePath })
} catch (error) {
expect(error).toBeInstanceOf(BundleError)
expect((error as BundleError).message).toEqual(
`It seems like you're trying to import an npm module. This is only supported in Deno via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/p-retry"'?`,
)
} finally {
await cleanup()
}
})
test('Does not add a custom error property to system errors during bundling', async () => {
expect.assertions(1)
try {
// @ts-expect-error Sending bad input to `bundle` to force a system error.
await bundle([123, 321], '/some/directory', declarations)
} catch (error) {
expect(error).not.toBeInstanceOf(BundleError)
}
})
test('Uses the cache directory as the `DENO_DIR` value if the `edge_functions_cache_deno_dir` feature flag is set', async () => {
expect.assertions(6)
const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
const sourceDirectory = join(basePath, 'functions')
const cacheDir = await tmp.dir()
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const options: BundleOptions = {
basePath,
cacheDirectory: cacheDir.path,
configPath: join(sourceDirectory, 'config.json'),
}
// Run #1, feature flag off: The directory should not be populated.
const result1 = await bundle([sourceDirectory], distPath, declarations, options)
const outFiles1 = await fs.readdir(distPath)
expect(result1.functions.length).toBe(1)
expect(outFiles1.length).toBe(2)
try {
await fs.readdir(join(cacheDir.path, 'deno_dir'))
} catch (error) {
expect(error).toBeInstanceOf(Error)
}
// Run #2, feature flag on: The directory should be populated.
const result2 = await bundle([sourceDirectory], distPath, declarations, {
...options,
featureFlags: {
edge_functions_cache_deno_dir: true,
},
})
const outFiles2 = await fs.readdir(distPath)
expect(result2.functions.length).toBe(1)
expect(outFiles2.length).toBe(2)
const denoDir2 = await fs.readdir(join(cacheDir.path, 'deno_dir'))
expect(denoDir2.includes('gen')).toBe(true)
await cleanup()
})
test('Supports import maps with relative paths', async () => {
const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
const sourceDirectory = join(basePath, 'functions')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const result = await bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
})
const generatedFiles = await fs.readdir(distPath)
expect(result.functions.length).toBe(1)
expect(generatedFiles.length).toBe(2)
const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
const { bundles } = manifest
expect(bundles.length).toBe(1)
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)
await cleanup()
})
test('Ignores any user-defined `deno.json` files', async () => {
const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
const sourceDirectory = join(basePath, 'functions')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
// Creating an import map file that rewires the URL of the Deno registry to
// an invalid location.
const importMapFile = await tmp.file()
const importMap = {
imports: {
'https://deno.land/': 'https://black.hole/',
},
}
await fs.writeFile(importMapFile.path, JSON.stringify(importMap))
// Deno configuration files need to be in the current working directory.
// There's not a great way for us to set the working directory of the `deno`
// process that we'll run, so our best bet is to write the file to whatever
// is the current working directory now and then clean it up.
const denoConfigPath = join(process.cwd(), 'deno.json')
const denoConfig = {
importMap: importMapFile.path,
}
try {
await fs.access(denoConfigPath)
throw new Error(
`The file at '${denoConfigPath} would be overwritten by this test. Please move the file to a different location and try again.'`,
)
} catch (error) {
if (isNodeError(error) && error.code !== 'ENOENT') {
throw error
}
}
await fs.writeFile(denoConfigPath, JSON.stringify(denoConfig))
expect(() =>
bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
}),
).not.toThrow()
await cleanup()
await deleteAsync([denoConfigPath, importMapFile.path], { force: true })
})
test('Processes a function that imports a custom layer', async () => {
const { basePath, cleanup, distPath } = await useFixture('with_layers')
const sourceDirectory = join(basePath, 'functions')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const layer = { name: 'layer:test', flag: 'edge-functions-layer-test' }
const result = await bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
})
const generatedFiles = await fs.readdir(distPath)
expect(result.functions.length).toBe(1)
expect(generatedFiles.length).toBe(2)
const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
const { bundles, layers } = manifest
expect(bundles.length).toBe(1)
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)
expect(layers).toEqual([layer])
await cleanup()
})
test('Loads declarations and import maps from the deploy configuration', async () => {
const { basePath, cleanup, distPath } = await useFixture('with_deploy_config')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const directories = [join(basePath, 'netlify', 'edge-functions'), join(basePath, '.netlify', 'edge-functions')]
const result = await bundle(directories, distPath, declarations, {
basePath,
configPath: join(basePath, '.netlify', 'edge-functions', 'config.json'),
})
const generatedFiles = await fs.readdir(distPath)
expect(result.functions.length).toBe(2)
expect(generatedFiles.length).toBe(2)
const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
const { bundles } = manifest
expect(bundles.length).toBe(1)
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)
await cleanup()
})