From 3bddb9de90a850ededadb6352e704c2d4a810cd1 Mon Sep 17 00:00:00 2001 From: Ivan Pantic Date: Tue, 23 Jan 2024 03:03:10 +0100 Subject: [PATCH] fix: handle empty build (#357) Co-authored-by: Ivan Pantic Co-authored-by: Hiroki Osame --- src/plugin.ts | 10 +++++++++- tests/specs/plugin.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/plugin.ts b/src/plugin.ts index eecadfd..9a2cc56 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -201,6 +201,12 @@ export default class EsbuildPlugin { * Webpack 5: https://github.com/webpack/webpack/blob/v5.75.0/lib/SourceMapDevToolModuleOptionsPlugin.js#LL27 */ let useSourceMap = false; + + /** + * `finishModules` hook is called after all the `buildModule` hooks are called, + * which is where the `useSourceMap` flag is set + * https://webpack.js.org/api/compilation-hooks/#finishmodules + */ compilation.hooks.finishModules.tap( pluginName, (modules) => { @@ -209,7 +215,9 @@ export default class EsbuildPlugin { ? modules[0] : (modules as Set).values().next().value as webpack5.Module ); - useSourceMap = firstModule.useSourceMap; + if (firstModule) { + useSourceMap = firstModule.useSourceMap; + } }, ); diff --git a/tests/specs/plugin.ts b/tests/specs/plugin.ts index 6b49d77..f1ee00e 100644 --- a/tests/specs/plugin.ts +++ b/tests/specs/plugin.ts @@ -738,5 +738,17 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac expect(exportedFunction('hello world')).toBe('hello world'); assertMinified(exportedFunction.toString()); }); + + // https://github.com/privatenumber/esbuild-loader/issues/356 + test('can handle empty modules set', async () => { + await expect(build( + fixtures.blank, + (config) => { + config.entry = 'not-there.js'; + configureEsbuildMinifyPlugin(config); + }, + webpack, + )).resolves.toBeTruthy(); + }); }); });