Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable dynamic-imports by default #361

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

const tsconfigCache = new Map<string, TsConfigResult>();

async function ESBuildLoader(

Check warning on line 18 in src/loader.ts

View workflow job for this annotation

GitHub Actions / Test

Async function 'ESBuildLoader' has a complexity of 14. Maximum allowed is 10
this: webpack.loader.LoaderContext<LoaderOptions>,
source: string,
): Promise<void> {
Expand Down Expand Up @@ -90,6 +90,14 @@
}
}

/**
* 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));
Expand Down
51 changes: 51 additions & 0 deletions tests/specs/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Loading