@@ -407,5 +407,56 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac
407407 const code = built . fs . readFileSync ( '/dist/index.js' , 'utf8' ) ;
408408 expect ( code ) . toContain ( 'div{color:red}' ) ;
409409 } ) ;
410+
411+ test ( 'Keeps dynamic imports by default' , async ( ) => {
412+ const built = await build (
413+ {
414+ '/src/index.js' : 'export default async () => (await import("./test2.js")).default' ,
415+ '/src/test2.js' : 'export default "test2"' ,
416+ } ,
417+ ( config ) => {
418+ configureEsbuildLoader ( config , { options : { target : 'chrome52' } } ) ;
419+ } ,
420+ webpack ,
421+ ) ;
422+
423+ expect ( built . stats . hasWarnings ( ) ) . toBe ( false ) ;
424+ expect ( built . stats . hasErrors ( ) ) . toBe ( false ) ;
425+
426+ const { assets } = built . stats . compilation ;
427+ expect ( assets ) . toHaveProperty ( [ 'index.js' ] ) ;
428+
429+ // Chunk split because esbuild preserved the dynamic import
430+ expect ( Object . keys ( assets ) . length ) . toBe ( 2 ) ;
431+ expect ( await built . require ( '/dist' ) ( ) ) . toBe ( 'test2' ) ;
432+ } ) ;
433+
434+ test ( 'Dynamic imports can be disabled' , async ( ) => {
435+ const built = await build (
436+ {
437+ '/src/index.js' : 'export default async () => (await import("./test2.js")).default' ,
438+ '/src/test2.js' : 'export default "test2"' ,
439+ } ,
440+ ( config ) => {
441+ configureEsbuildLoader ( config , {
442+ options : {
443+ target : 'chrome52' ,
444+ supported : { 'dynamic-import' : false } ,
445+ } ,
446+ } ) ;
447+ } ,
448+ webpack ,
449+ ) ;
450+
451+ expect ( built . stats . hasWarnings ( ) ) . toBe ( false ) ;
452+ expect ( built . stats . hasErrors ( ) ) . toBe ( false ) ;
453+
454+ const { assets } = built . stats . compilation ;
455+ expect ( assets ) . toHaveProperty ( [ 'index.js' ] ) ;
456+
457+ // No chunk split because esbuild removed the dynamic import
458+ expect ( Object . keys ( assets ) . length ) . toBe ( 1 ) ;
459+ expect ( await built . require ( '/dist' ) ( ) ) . toBe ( 'test2' ) ;
460+ } ) ;
410461 } ) ;
411462} ) ;
0 commit comments