Skip to content

Commit 1cd7909

Browse files
feat: emit system log on import assertions (#6590)
* chore: add test * feat: emit system log for import assertions * chore: doh * chore: adjust version check
1 parent f65a081 commit 1cd7909

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

packages/edge-bundler/node/bundler.test.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { Buffer } from 'buffer'
2-
import { execSync } from 'node:child_process'
32
import { access, readdir, readFile, rm, writeFile } from 'fs/promises'
43
import { join, resolve } from 'path'
54
import process from 'process'
65
import { pathToFileURL } from 'url'
76

8-
import { lt } from 'semver'
7+
import { gte, lt } from 'semver'
98
import tmp from 'tmp-promise'
109
import { test, expect, vi, describe } from 'vitest'
1110

1211
import { importMapSpecifier } from '../shared/consts.js'
13-
import { runESZIP, runTarball, useFixture } from '../test/util.js'
12+
import { denoVersion, runESZIP, runTarball, useFixture } from '../test/util.js'
1413

1514
import { BundleError } from './bundle_error.js'
1615
import { bundle, BundleOptions } from './bundler.js'
@@ -599,7 +598,7 @@ test('Loads npm modules in a monorepo setup', async () => {
599598
await rm(vendorDirectory.path, { force: true, recursive: true })
600599
})
601600

602-
test('Loads JSON modules', async () => {
601+
test('Loads JSON modules with `with` attribute', async () => {
603602
const { basePath, cleanup, distPath } = await useFixture('imports_json')
604603
const sourceDirectory = join(basePath, 'functions')
605604
const declarations: Declaration[] = [
@@ -626,6 +625,45 @@ test('Loads JSON modules', async () => {
626625
await rm(vendorDirectory.path, { force: true, recursive: true })
627626
})
628627

628+
// We can't run this on versions above 2.0.0 because the bundling will fail
629+
// entirely, and what we're asserting here is that we emit a system log when
630+
// import assertions are detected on successful builds. Also, running it on
631+
// earlier versions won't work either, since those won't even show a warning.
632+
test.skipIf(lt(denoVersion, '1.46.3') || gte(denoVersion, '2.0.0'))(
633+
'Emits a system log when import assertions are used',
634+
async () => {
635+
const { basePath, cleanup, distPath } = await useFixture('with_import_assert')
636+
const sourceDirectory = join(basePath, 'functions')
637+
const declarations: Declaration[] = [
638+
{
639+
function: 'func1',
640+
path: '/func1',
641+
},
642+
]
643+
const vendorDirectory = await tmp.dir()
644+
const systemLogger = vi.fn()
645+
646+
await bundle([sourceDirectory], distPath, declarations, {
647+
basePath,
648+
systemLogger,
649+
vendorDirectory: vendorDirectory.path,
650+
})
651+
652+
const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8')
653+
const manifest = JSON.parse(manifestFile)
654+
const bundlePath = join(distPath, manifest.bundles[0].asset)
655+
const { func1 } = await runESZIP(bundlePath, vendorDirectory.path)
656+
657+
expect(func1).toBe(`{"foo":"bar"}`)
658+
expect(systemLogger).toHaveBeenCalledWith(
659+
`Edge function uses import assertions: ${join(sourceDirectory, 'func1.ts')}`,
660+
)
661+
662+
await cleanup()
663+
await rm(vendorDirectory.path, { force: true, recursive: true })
664+
},
665+
)
666+
629667
test('Supports TSX and process.env', async () => {
630668
const { basePath, cleanup, distPath } = await useFixture('tsx')
631669
const sourceDirectory = join(basePath, 'functions')
@@ -694,8 +732,6 @@ test('Loads edge functions from the Frameworks API', async () => {
694732
await cleanup()
695733
})
696734

697-
const denoVersion = execSync('deno eval --no-lock "console.log(Deno.version.deno)"').toString()
698-
699735
describe.skipIf(lt(denoVersion, '2.4.2'))(
700736
'Produces a tarball bundle',
701737
() => {

packages/edge-bundler/node/config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ export const getFunctionConfig = async ({
9696
// with the extractor.
9797
const collector = await tmp.file()
9898

99+
// Retrieving the version of Deno.
100+
const version = new SemVer((await deno.getBinaryVersion((await deno.getBinaryPath({ silent: true })).path)) || '')
101+
99102
// The extractor will use its exit code to signal different error scenarios,
100103
// based on the list of exit codes we send as an argument. We then capture
101104
// the exit code to know exactly what happened and guide people accordingly.
102-
const version = new SemVer((await deno.getBinaryVersion((await deno.getBinaryPath({ silent: true })).path)) || '')
103-
104105
const { exitCode, stderr, stdout } = await deno.run(
105106
[
106107
'run',
@@ -121,6 +122,10 @@ export const getFunctionConfig = async ({
121122
{ rejectOnExitCode: false },
122123
)
123124

125+
if (stderr.includes('Import assertions are deprecated')) {
126+
log.system(`Edge function uses import assertions: ${func.path}`)
127+
}
128+
124129
if (exitCode !== ConfigExitCode.Success) {
125130
handleConfigError(func, exitCode, stderr, log)
126131

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import dict from './dict.json' assert { type: "json" }
2+
3+
4+
export default async () => Response.json(dict)

packages/edge-bundler/test/util.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { promises as fs } from 'fs'
2-
import { join, resolve } from 'path'
3-
import { stderr, stdout } from 'process'
4-
import { fileURLToPath, pathToFileURL } from 'url'
1+
import { execSync } from 'node:child_process'
2+
import { promises as fs } from 'node:fs'
3+
import { join, resolve } from 'node:path'
4+
import { stderr, stdout } from 'node:process'
5+
import { fileURLToPath, pathToFileURL } from 'node:url'
56

67
import cpy from 'cpy'
78
import { execa } from 'execa'
@@ -169,3 +170,5 @@ export const runTarball = async (tarballPath: string) => {
169170

170171
return JSON.parse(result.stdout)
171172
}
173+
174+
export const denoVersion = execSync('deno eval --no-lock "console.log(Deno.version.deno)"').toString()

0 commit comments

Comments
 (0)