From a5fddd0a65d7845850e5be44991f84af8dbd2c12 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Tue, 29 Aug 2023 16:25:55 +0900 Subject: [PATCH] fix(plugin): mark assets as minimized only when minified (#338) --- src/plugin.ts | 9 ++++++++- tests/specs/plugin.ts | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/plugin.ts b/src/plugin.ts index f992d42..eecadfd 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -37,6 +37,13 @@ const transformAssets = async ( ...transformOptions } = options; + const minimized = ( + transformOptions.minify + || transformOptions.minifyWhitespace + || transformOptions.minifyIdentifiers + || transformOptions.minifySyntax + ); + const assets = (compilation.getAssets() as Asset[]).filter(asset => ( // Filter out already minimized @@ -106,7 +113,7 @@ const transformAssets = async ( ) as any, { ...asset.info, - minimized: true, + minimized, }, ); })); diff --git a/tests/specs/plugin.ts b/tests/specs/plugin.ts index 96dda7c..3c95d05 100644 --- a/tests/specs/plugin.ts +++ b/tests/specs/plugin.ts @@ -176,6 +176,24 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac expect(code).not.toMatch('return '); }); + test('should minify when used alongside plugin', async () => { + const built = await build( + fixtures.minification, + (config) => { + configureEsbuildMinifyPlugin(config); + config.plugins?.push(new EsbuildPlugin()); + }, + webpack, + ); + + expect(built.stats.hasWarnings()).toBe(false); + expect(built.stats.hasErrors()).toBe(false); + + const exportedFunction = built.require('/dist/'); + expect(exportedFunction('hello world')).toBe('hello world'); + assertMinified(exportedFunction.toString()); + }); + test('minify chunks & filter using include/exclude', async () => { const built = await build({ '/src/index.js': `