1
1
import { Buffer } from 'buffer'
2
- import { execSync } from 'node:child_process'
3
2
import { access , readdir , readFile , rm , writeFile } from 'fs/promises'
4
3
import { join , resolve } from 'path'
5
4
import process from 'process'
6
5
import { pathToFileURL } from 'url'
7
6
8
- import { lt } from 'semver'
7
+ import { gte , lt } from 'semver'
9
8
import tmp from 'tmp-promise'
10
9
import { test , expect , vi , describe } from 'vitest'
11
10
12
11
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'
14
13
15
14
import { BundleError } from './bundle_error.js'
16
15
import { bundle , BundleOptions } from './bundler.js'
@@ -599,7 +598,7 @@ test('Loads npm modules in a monorepo setup', async () => {
599
598
await rm ( vendorDirectory . path , { force : true , recursive : true } )
600
599
} )
601
600
602
- test ( 'Loads JSON modules' , async ( ) => {
601
+ test ( 'Loads JSON modules with `with` attribute ' , async ( ) => {
603
602
const { basePath, cleanup, distPath } = await useFixture ( 'imports_json' )
604
603
const sourceDirectory = join ( basePath , 'functions' )
605
604
const declarations : Declaration [ ] = [
@@ -626,6 +625,45 @@ test('Loads JSON modules', async () => {
626
625
await rm ( vendorDirectory . path , { force : true , recursive : true } )
627
626
} )
628
627
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
+
629
667
test ( 'Supports TSX and process.env' , async ( ) => {
630
668
const { basePath, cleanup, distPath } = await useFixture ( 'tsx' )
631
669
const sourceDirectory = join ( basePath , 'functions' )
@@ -694,8 +732,6 @@ test('Loads edge functions from the Frameworks API', async () => {
694
732
await cleanup ( )
695
733
} )
696
734
697
- const denoVersion = execSync ( 'deno eval --no-lock "console.log(Deno.version.deno)"' ) . toString ( )
698
-
699
735
describe . skipIf ( lt ( denoVersion , '2.4.2' ) ) (
700
736
'Produces a tarball bundle' ,
701
737
( ) => {
0 commit comments