11import { Buffer } from 'buffer'
2- import { execSync } from 'node:child_process'
32import { access , readdir , readFile , rm , writeFile } from 'fs/promises'
43import { join , resolve } from 'path'
54import process from 'process'
65import { pathToFileURL } from 'url'
76
8- import { lt } from 'semver'
7+ import { gte , lt } from 'semver'
98import tmp from 'tmp-promise'
109import { test , expect , vi , describe } from 'vitest'
1110
1211import { 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
1514import { BundleError } from './bundle_error.js'
1615import { 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+
629667test ( '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-
699735describe . skipIf ( lt ( denoVersion , '2.4.2' ) ) (
700736 'Produces a tarball bundle' ,
701737 ( ) => {
0 commit comments