diff --git a/src/loader.ts b/src/loader.ts index 38fd3c1..c8453b3 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -90,6 +90,14 @@ async function ESBuildLoader( } } + /** + * Enable dynamic import by default to support code splitting in Webpack + */ + transformOptions.supported = { + 'dynamic-import': true, + ...transformOptions.supported, + }; + try { const { code, map } = await transform(source, transformOptions); done(null, code, map && JSON.parse(map)); diff --git a/tests/specs/loader.ts b/tests/specs/loader.ts index 6807fd6..3329fcb 100644 --- a/tests/specs/loader.ts +++ b/tests/specs/loader.ts @@ -407,5 +407,56 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac const code = built.fs.readFileSync('/dist/index.js', 'utf8'); expect(code).toContain('div{color:red}'); }); + + test('Keeps dynamic imports by default', async () => { + const built = await build( + { + '/src/index.js': 'export default async () => (await import("./test2.js")).default', + '/src/test2.js': 'export default "test2"', + }, + (config) => { + configureEsbuildLoader(config, { options: { target: 'chrome52' } }); + }, + webpack, + ); + + expect(built.stats.hasWarnings()).toBe(false); + expect(built.stats.hasErrors()).toBe(false); + + const { assets } = built.stats.compilation; + expect(assets).toHaveProperty(['index.js']); + + // Chunk split because esbuild preserved the dynamic import + expect(Object.keys(assets).length).toBe(2); + expect(await built.require('/dist')()).toBe('test2'); + }); + + test('Dynamic imports can be disabled', async () => { + const built = await build( + { + '/src/index.js': 'export default async () => (await import("./test2.js")).default', + '/src/test2.js': 'export default "test2"', + }, + (config) => { + configureEsbuildLoader(config, { + options: { + target: 'chrome52', + supported: { 'dynamic-import': false }, + }, + }); + }, + webpack, + ); + + expect(built.stats.hasWarnings()).toBe(false); + expect(built.stats.hasErrors()).toBe(false); + + const { assets } = built.stats.compilation; + expect(assets).toHaveProperty(['index.js']); + + // No chunk split because esbuild removed the dynamic import + expect(Object.keys(assets).length).toBe(1); + expect(await built.require('/dist')()).toBe('test2'); + }); }); });