@@ -40,6 +40,7 @@ const {
40
40
zipCheckFunctions,
41
41
FIXTURES_DIR ,
42
42
BINARY_PATH ,
43
+ importFunctionFile,
43
44
} = require ( './helpers/main' )
44
45
const { computeSha1 } = require ( './helpers/sha' )
45
46
const { makeTestMany } = require ( './helpers/test_many' )
@@ -201,7 +202,7 @@ testMany(
201
202
opts : options ,
202
203
} )
203
204
204
- const func = require ( `${ tmpDir } /function.js` )
205
+ const func = await importFunctionFile ( `${ tmpDir } /function.js` )
205
206
206
207
t . deepEqual ( func , { mock : { stack : 'jam' } , stack : 'jam' } )
207
208
} ,
@@ -254,7 +255,7 @@ testMany(
254
255
t . false ( await pathExists ( `${ tmpDir } /node_modules/aws-sdk` ) )
255
256
256
257
try {
257
- const func = require ( `${ tmpDir } /function.js` )
258
+ const func = await importFunctionFile ( `${ tmpDir } /function.js` )
258
259
259
260
func ( )
260
261
@@ -374,7 +375,7 @@ testMany(
374
375
375
376
await unzipFiles ( [ result ] )
376
377
377
- const func = require ( join ( tmpDir , 'function.js' ) )
378
+ const func = await importFunctionFile ( join ( tmpDir , 'function.js' ) )
378
379
379
380
t . true ( func )
380
381
} ,
@@ -456,10 +457,10 @@ testMany(
456
457
join ( tmpDir , 'function_export_only.zip_out' , 'function_export_only.js' ) ,
457
458
join ( tmpDir , 'function_import_only.zip_out' , 'function_import_only.js' ) ,
458
459
]
459
- const func1 = ( ) => require ( functionPaths [ 0 ] )
460
- const func2 = ( ) => require ( functionPaths [ 1 ] )
461
- const func3 = ( ) => require ( functionPaths [ 2 ] )
462
- const func4 = ( ) => require ( functionPaths [ 3 ] )
460
+ const func1 = ( ) => importFunctionFile ( functionPaths [ 0 ] )
461
+ const func2 = ( ) => importFunctionFile ( functionPaths [ 1 ] )
462
+ const func3 = ( ) => importFunctionFile ( functionPaths [ 2 ] )
463
+ const func4 = ( ) => importFunctionFile ( functionPaths [ 3 ] )
463
464
464
465
const functionsAreESM = await Promise . all (
465
466
functionPaths . map ( ( functionPath ) => detectEsModule ( { mainFile : functionPath } ) ) ,
@@ -470,12 +471,15 @@ testMany(
470
471
471
472
// Dynamic imports are not supported in Node <13.2.0.
472
473
if ( semver . gte ( nodeVersion , '13.2.0' ) ) {
473
- t . is ( await func2 ( ) ( ) , 0 )
474
+ const func2Default = await func2 ( )
475
+ t . is ( await func2Default ( ) , 0 )
474
476
}
475
477
476
- t . is ( func1 ( ) . ZERO , 0 )
477
- t . is ( typeof func3 ( ) . howdy , 'string' )
478
- t . deepEqual ( func4 ( ) , { } )
478
+ const { ZERO } = await func1 ( )
479
+ t . is ( ZERO , 0 )
480
+ const { howdy } = await func3 ( )
481
+ t . is ( typeof howdy , 'string' )
482
+ t . deepEqual ( await func4 ( ) , { } )
479
483
} ,
480
484
)
481
485
@@ -506,10 +510,10 @@ testMany(
506
510
join ( tmpDir , 'function_export_only' , 'function_export_only.js' ) ,
507
511
join ( tmpDir , 'function_import_only' , 'function_import_only.js' ) ,
508
512
]
509
- const func1 = ( ) => require ( functionPaths [ 0 ] )
510
- const func2 = ( ) => require ( functionPaths [ 1 ] )
511
- const func3 = ( ) => require ( functionPaths [ 2 ] )
512
- const func4 = ( ) => require ( functionPaths [ 3 ] )
513
+ const func1 = ( ) => importFunctionFile ( functionPaths [ 0 ] )
514
+ const func2 = ( ) => importFunctionFile ( functionPaths [ 1 ] )
515
+ const func3 = ( ) => importFunctionFile ( functionPaths [ 2 ] )
516
+ const func4 = ( ) => importFunctionFile ( functionPaths [ 3 ] )
513
517
514
518
const functionsAreESM = await Promise . all (
515
519
functionPaths . map ( ( functionPath ) => detectEsModule ( { mainFile : functionPath } ) ) ,
@@ -520,12 +524,15 @@ testMany(
520
524
521
525
// Dynamic imports are not supported in Node <13.2.0.
522
526
if ( semver . gte ( nodeVersion , '13.2.0' ) ) {
523
- t . is ( await func2 ( ) ( ) , 0 )
527
+ const func2Default = await func2 ( )
528
+ t . is ( await func2Default ( ) , 0 )
524
529
}
525
530
526
- t . is ( func1 ( ) . ZERO , 0 )
527
- t . is ( typeof func3 ( ) . howdy , 'string' )
528
- t . deepEqual ( func4 ( ) , { } )
531
+ const { ZERO } = await func1 ( )
532
+ t . is ( ZERO , 0 )
533
+ const { howdy } = await func3 ( )
534
+ t . is ( typeof howdy , 'string' )
535
+ t . deepEqual ( await func4 ( ) , { } )
529
536
} ,
530
537
)
531
538
@@ -540,7 +547,7 @@ testMany(
540
547
541
548
await unzipFiles ( files )
542
549
543
- const func = require ( join ( tmpDir , 'function.js' ) )
550
+ const func = await importFunctionFile ( join ( tmpDir , 'function.js' ) )
544
551
545
552
// Dynamic imports were added in Node v13.2.0.
546
553
if ( semver . gte ( nodeVersion , '13.2.0' ) ) {
@@ -718,7 +725,8 @@ testMany(
718
725
opts : options ,
719
726
} )
720
727
await unzipFiles ( files )
721
- t . true ( require ( `${ tmpDir } /function.js` ) )
728
+ const returnValue = await importFunctionFile ( `${ tmpDir } /function.js` )
729
+ t . true ( returnValue )
722
730
t . is ( files [ 0 ] . mainFile , join ( FIXTURES_DIR , fixtureName , 'function' , 'index.js' ) )
723
731
} ,
724
732
)
@@ -1333,7 +1341,8 @@ testMany(
1333
1341
async ( options , t ) => {
1334
1342
const { files, tmpDir } = await zipFixture ( t , 'node-fetch' , { opts : options } )
1335
1343
await unzipFiles ( files )
1336
- t . true ( typeof require ( `${ tmpDir } /function.js` ) === 'function' )
1344
+ const returnValue = await importFunctionFile ( `${ tmpDir } /function.js` )
1345
+ t . true ( typeof returnValue === 'function' )
1337
1346
} ,
1338
1347
)
1339
1348
@@ -1345,7 +1354,8 @@ testMany(
1345
1354
opts : options ,
1346
1355
} )
1347
1356
await unzipFiles ( files )
1348
- t . is ( require ( `${ tmpDir } /function.js` ) , 'function-js-file-in-directory' )
1357
+ const returnValue = await importFunctionFile ( `${ tmpDir } /function.js` )
1358
+ t . is ( returnValue , 'function-js-file-in-directory' )
1349
1359
} ,
1350
1360
)
1351
1361
@@ -1357,7 +1367,8 @@ testMany(
1357
1367
opts : options ,
1358
1368
} )
1359
1369
await unzipFiles ( files )
1360
- t . is ( require ( `${ tmpDir } /function.js` ) , 'index-js-file-in-directory' )
1370
+ const returnValue = await importFunctionFile ( `${ tmpDir } /function.js` )
1371
+ t . is ( returnValue , 'index-js-file-in-directory' )
1361
1372
} ,
1362
1373
)
1363
1374
@@ -1369,7 +1380,8 @@ testMany(
1369
1380
opts : options ,
1370
1381
} )
1371
1382
await unzipFiles ( files )
1372
- t . is ( require ( `${ tmpDir } /function.js` ) . type , 'index-js-file-in-directory' )
1383
+ const { type } = await importFunctionFile ( `${ tmpDir } /function.js` )
1384
+ t . is ( type , 'index-js-file-in-directory' )
1373
1385
} ,
1374
1386
)
1375
1387
@@ -1381,7 +1393,8 @@ testMany(
1381
1393
opts : options ,
1382
1394
} )
1383
1395
await unzipFiles ( files )
1384
- t . is ( require ( `${ tmpDir } /function.js` ) . type , 'function-js-file' )
1396
+ const { type } = await importFunctionFile ( `${ tmpDir } /function.js` )
1397
+ t . is ( type , 'function-js-file' )
1385
1398
} ,
1386
1399
)
1387
1400
@@ -1393,7 +1406,8 @@ testMany(
1393
1406
opts : options ,
1394
1407
} )
1395
1408
await unzipFiles ( files )
1396
- t . is ( require ( `${ tmpDir } /function.js` ) . type , 'function-js-file' )
1409
+ const { type } = await importFunctionFile ( `${ tmpDir } /function.js` )
1410
+ t . is ( type , 'function-js-file' )
1397
1411
} ,
1398
1412
)
1399
1413
@@ -1405,7 +1419,8 @@ testMany(
1405
1419
opts : options ,
1406
1420
} )
1407
1421
await unzipFiles ( files )
1408
- t . true ( typeof require ( `${ tmpDir } /function.js` ) . type === 'string' )
1422
+ const { type } = await importFunctionFile ( `${ tmpDir } /function.js` )
1423
+ t . true ( typeof type === 'string' )
1409
1424
} ,
1410
1425
)
1411
1426
@@ -1417,7 +1432,8 @@ testMany(
1417
1432
opts : options ,
1418
1433
} )
1419
1434
await unzipFiles ( files )
1420
- t . true ( typeof require ( `${ tmpDir } /function.js` ) . type === 'string' )
1435
+ const { type } = await importFunctionFile ( `${ tmpDir } /function.js` )
1436
+ t . true ( typeof type === 'string' )
1421
1437
} ,
1422
1438
)
1423
1439
@@ -1429,7 +1445,8 @@ testMany(
1429
1445
opts : options ,
1430
1446
} )
1431
1447
await unzipFiles ( files )
1432
- t . true ( typeof require ( `${ tmpDir } /function.js` ) . type === 'string' )
1448
+ const { type } = await importFunctionFile ( `${ tmpDir } /function.js` )
1449
+ t . true ( typeof type === 'string' )
1433
1450
} ,
1434
1451
)
1435
1452
@@ -1441,7 +1458,8 @@ testMany(
1441
1458
opts : options ,
1442
1459
} )
1443
1460
await unzipFiles ( files )
1444
- t . true ( typeof require ( `${ tmpDir } /function.js` ) . type === 'string' )
1461
+ const { type } = await importFunctionFile ( `${ tmpDir } /function.js` )
1462
+ t . true ( typeof type === 'string' )
1445
1463
} ,
1446
1464
)
1447
1465
@@ -1461,9 +1479,12 @@ testMany(
1461
1479
t . is ( file . bundler , 'esbuild' )
1462
1480
} )
1463
1481
1464
- t . true ( require ( `${ tmpDir } /func1.js` ) . handler ( ) )
1465
- t . true ( require ( `${ tmpDir } /func2.js` ) . handler ( ) )
1466
- t . true ( require ( `${ tmpDir } /func3.js` ) . handler ( ) )
1482
+ const { handler : handler1 } = await importFunctionFile ( `${ tmpDir } /func1.js` )
1483
+ t . true ( handler1 ( ) )
1484
+ const { handler : handler2 } = await importFunctionFile ( `${ tmpDir } /func2.js` )
1485
+ t . true ( handler2 ( ) )
1486
+ const { handler : handler3 } = await importFunctionFile ( `${ tmpDir } /func3.js` )
1487
+ t . true ( handler3 ( ) )
1467
1488
} ,
1468
1489
)
1469
1490
@@ -1475,7 +1496,8 @@ testMany(
1475
1496
opts : options ,
1476
1497
} )
1477
1498
await unzipFiles ( files )
1478
- t . true ( require ( `${ tmpDir } /function.js` ) . value )
1499
+ const { value } = await importFunctionFile ( `${ tmpDir } /function.js` )
1500
+ t . true ( value )
1479
1501
} ,
1480
1502
)
1481
1503
@@ -1487,7 +1509,8 @@ testMany(
1487
1509
opts : options ,
1488
1510
} )
1489
1511
await unzipFiles ( files )
1490
- t . true ( require ( `${ tmpDir } /function.js` ) . value )
1512
+ const { value } = await importFunctionFile ( `${ tmpDir } /function.js` )
1513
+ t . true ( value )
1491
1514
} ,
1492
1515
)
1493
1516
@@ -1500,7 +1523,7 @@ testMany(
1500
1523
} )
1501
1524
await unzipFiles ( files )
1502
1525
1503
- const result = require ( `${ tmpDir } /function.js` )
1526
+ const result = await importFunctionFile ( `${ tmpDir } /function.js` )
1504
1527
1505
1528
// We want to assert that the `target` specified in the tsconfig file (es5)
1506
1529
// was overridden by our own target. It's not easy to assert that without
@@ -1625,7 +1648,7 @@ testMany(
1625
1648
opts,
1626
1649
} )
1627
1650
1628
- const functionEntry = require ( `${ files [ 0 ] . path } /function.js` )
1651
+ const functionEntry = await importFunctionFile ( `${ files [ 0 ] . path } /function.js` )
1629
1652
1630
1653
t . true ( functionEntry )
1631
1654
} ,
@@ -1648,7 +1671,7 @@ testMany(
1648
1671
opts,
1649
1672
} )
1650
1673
1651
- const func = require ( `${ tmpDir } /func1.js` )
1674
+ const func = await importFunctionFile ( `${ tmpDir } /func1.js` )
1652
1675
1653
1676
const { body : body1 } = await func . handler ( { queryStringParameters : { name : 'post1' } } )
1654
1677
const { body : body2 } = await func . handler ( { queryStringParameters : { name : 'post2' } } )
@@ -1710,7 +1733,7 @@ testMany(
1710
1733
t . false ( files [ 0 ] . inputs . includes ( join ( FIXTURES_DIR , fixtureName , 'node_modules' , 'test-child' , 'unused_file.js' ) ) )
1711
1734
}
1712
1735
1713
- const functionEntry = require ( `${ tmpDir } /function.js` )
1736
+ const functionEntry = await importFunctionFile ( `${ tmpDir } /function.js` )
1714
1737
1715
1738
t . true ( functionEntry )
1716
1739
} ,
@@ -1733,7 +1756,7 @@ testMany(
1733
1756
opts,
1734
1757
} )
1735
1758
1736
- const function1Entry = require ( `${ tmpDir } /func1.js` )
1759
+ const function1Entry = await importFunctionFile ( `${ tmpDir } /func1.js` )
1737
1760
1738
1761
// The function should not be on a `src/` namespace.
1739
1762
t . false ( unixify ( function1Entry [ 0 ] ) . includes ( '/src/' ) )
@@ -1764,7 +1787,7 @@ testMany(
1764
1787
opts,
1765
1788
} )
1766
1789
1767
- const function2Entry = require ( `${ tmpDir } /func2.js` )
1790
+ const function2Entry = await importFunctionFile ( `${ tmpDir } /func2.js` )
1768
1791
1769
1792
// The function should be on a `src/` namespace because there's a conflict
1770
1793
// with the /func2.js path present in `includedFiles`.
@@ -1794,9 +1817,9 @@ testMany(
1794
1817
opts,
1795
1818
} )
1796
1819
1797
- const functionCommon = require ( `${ tmpDir } /function.js` )
1798
- const functionInternal = require ( `${ tmpDir } /function_internal.js` )
1799
- const functionUser = require ( `${ tmpDir } /function_user.js` )
1820
+ const functionCommon = await importFunctionFile ( `${ tmpDir } /function.js` )
1821
+ const functionInternal = await importFunctionFile ( `${ tmpDir } /function_internal.js` )
1822
+ const functionUser = await importFunctionFile ( `${ tmpDir } /function_user.js` )
1800
1823
1801
1824
// Functions from rightmost directories in the array take precedence.
1802
1825
t . is ( functionCommon , 'user' )
@@ -1831,7 +1854,7 @@ test('When generating a directory for a function with `archiveFormat: "none"`, i
1831
1854
archiveFormat : 'none' ,
1832
1855
} )
1833
1856
1834
- const functionEntry = require ( `${ functionDirectory } /function.js` )
1857
+ const functionEntry = await importFunctionFile ( `${ functionDirectory } /function.js` )
1835
1858
1836
1859
t . true ( functionEntry )
1837
1860
@@ -1917,7 +1940,7 @@ test('Adds a runtime shim and includes the files needed for dynamic imports usin
1917
1940
} ,
1918
1941
} )
1919
1942
1920
- const func = require ( `${ tmpDir } /function.js` )
1943
+ const func = await importFunctionFile ( `${ tmpDir } /function.js` )
1921
1944
const values = func ( 'one' )
1922
1945
const expectedLength = 5
1923
1946
@@ -1952,7 +1975,7 @@ test('Adds a runtime shim and includes the files needed for dynamic imports usin
1952
1975
} ,
1953
1976
} )
1954
1977
1955
- const func = require ( `${ tmpDir } /function.js` )
1978
+ const func = await importFunctionFile ( `${ tmpDir } /function.js` )
1956
1979
1957
1980
t . deepEqual ( func ( 'en' ) [ 0 ] , [ 'yes' , 'no' ] )
1958
1981
t . deepEqual ( func ( 'en' ) [ 1 ] , [ 'yes' , 'no' ] )
@@ -1970,7 +1993,7 @@ test('The dynamic import runtime shim handles files in nested directories', asyn
1970
1993
} ,
1971
1994
} )
1972
1995
1973
- const func = require ( `${ tmpDir } /function.js` )
1996
+ const func = await importFunctionFile ( `${ tmpDir } /function.js` )
1974
1997
1975
1998
t . deepEqual ( func ( 'en' ) [ 0 ] , [ 'yes' , 'no' ] )
1976
1999
t . deepEqual ( func ( 'en' ) [ 1 ] , [ 'yes' , 'no' ] )
@@ -1991,7 +2014,7 @@ test('The dynamic import runtime shim handles files in nested directories when u
1991
2014
} ,
1992
2015
} )
1993
2016
1994
- const func = require ( `${ tmpDir } /function/function.js` )
2017
+ const func = await importFunctionFile ( `${ tmpDir } /function/function.js` )
1995
2018
1996
2019
t . deepEqual ( func ( 'en' ) [ 0 ] , [ 'yes' , 'no' ] )
1997
2020
t . deepEqual ( func ( 'en' ) [ 1 ] , [ 'yes' , 'no' ] )
@@ -2011,7 +2034,7 @@ test('Negated files in `included_files` are excluded from the bundle even if the
2011
2034
} ,
2012
2035
} )
2013
2036
2014
- const func = require ( `${ tmpDir } /function.js` )
2037
+ const func = await importFunctionFile ( `${ tmpDir } /function.js` )
2015
2038
2016
2039
t . deepEqual ( func ( 'pt' ) [ 0 ] , [ 'sim' , 'não' ] )
2017
2040
t . deepEqual ( func ( 'pt' ) [ 1 ] , [ 'sim' , 'não' ] )
@@ -2052,7 +2075,7 @@ test('Creates dynamic import shims for functions with the same name and same shi
2052
2075
} )
2053
2076
2054
2077
for ( let ind = 1 ; ind <= FUNCTION_COUNT ; ind ++ ) {
2055
- const func = require ( `${ tmpDir } /function${ ind } .js` )
2078
+ const func = await importFunctionFile ( `${ tmpDir } /function${ ind } .js` )
2056
2079
2057
2080
t . deepEqual ( func ( 'en' ) [ 0 ] , [ 'yes' , 'no' ] )
2058
2081
t . deepEqual ( func ( 'en' ) [ 1 ] , [ 'yes' , 'no' ] )
@@ -2072,7 +2095,7 @@ test('Creates dynamic import shims for functions using `zipFunction`', async (t)
2072
2095
2073
2096
await unzipFiles ( [ result ] )
2074
2097
2075
- const func = require ( `${ tmpDir } /function.js` )
2098
+ const func = await importFunctionFile ( `${ tmpDir } /function.js` )
2076
2099
2077
2100
t . deepEqual ( func ( 'en' ) [ 0 ] , [ 'yes' , 'no' ] )
2078
2101
t . deepEqual ( func ( 'en' ) [ 1 ] , [ 'yes' , 'no' ] )
@@ -2438,6 +2461,7 @@ test('Creates a manifest file with the list of created functions if the `manifes
2438
2461
} ,
2439
2462
} )
2440
2463
2464
+ // eslint-disable-next-line import/no-dynamic-require, node/global-require
2441
2465
const manifest = require ( manifestPath )
2442
2466
2443
2467
t . is ( manifest . version , 1 )
@@ -2469,7 +2493,7 @@ testMany(
2469
2493
opts,
2470
2494
} )
2471
2495
2472
- const isEven = require ( `${ tmpDir } /function` )
2496
+ const isEven = await importFunctionFile ( `${ tmpDir } /function.js ` )
2473
2497
t . is ( isEven ( 2 ) , '2 is even' )
2474
2498
} ,
2475
2499
)
@@ -2494,7 +2518,7 @@ testMany(
2494
2518
2495
2519
await unzipFiles ( [ result ] )
2496
2520
2497
- const { mock1, mock2 } = require ( `${ tmpDir } /function-1.js` )
2521
+ const { mock1, mock2 } = await importFunctionFile ( `${ tmpDir } /function-1.js` )
2498
2522
2499
2523
t . true ( mock1 )
2500
2524
t . true ( mock2 )
@@ -2560,6 +2584,7 @@ testMany(
2560
2584
2561
2585
files . every ( ( file ) => t . is ( file . schedule , schedule ) )
2562
2586
2587
+ // eslint-disable-next-line import/no-dynamic-require, node/global-require
2563
2588
const manifest = require ( manifestPath )
2564
2589
2565
2590
manifest . functions . forEach ( ( fn ) => {
@@ -2579,7 +2604,7 @@ test('Generates a sourcemap for any transpiled files when `nodeSourcemap: true`'
2579
2604
featureFlags : { nftTranspile : true } ,
2580
2605
} ,
2581
2606
} )
2582
- const func = require ( join ( files [ 0 ] . path , 'function.js' ) )
2607
+ const func = await importFunctionFile ( join ( files [ 0 ] . path , 'function.js' ) )
2583
2608
2584
2609
try {
2585
2610
func . handler ( )
1 commit comments
github-actions[bot] commentedon Feb 8, 2022
⏱ Benchmark results
largeDepsEsbuild: 7s
largeDepsZisi: 57.6s