diff --git a/src/plugin.ts b/src/plugin.ts index 41f5435..698dca8 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -129,17 +129,7 @@ export default function EsbuildPlugin( const transform = implementation?.transform ?? defaultEsbuildTransform; - const hasGranularMinificationConfig = ( - 'minifyIdentifiers' in options - || 'minifySyntax' in options - || 'minifyWhitespace' in options - ); - - if (!hasGranularMinificationConfig) { - options.minify = true; - } - - return { + const pluginInstance = { apply(compiler: Compiler) { if (!('format' in options)) { const { target } = compiler.options; @@ -162,6 +152,23 @@ export default function EsbuildPlugin( } } + /** + * Enable minification by default if used in the minimizer array + * unless further specified in the options + */ + const usedAsMinimizer = compiler.options.optimization?.minimizer?.includes?.(pluginInstance); + if ( + usedAsMinimizer + && !( + 'minify' in options + || 'minifyWhitespace' in options + || 'minifyIdentifiers' in options + || 'minifySyntax' in options + ) + ) { + options.minify = compiler.options.optimization?.minimize; + } + compiler.hooks.compilation.tap(pluginName, (compilation) => { const meta = JSON.stringify({ name: 'esbuild-loader', @@ -230,4 +237,6 @@ export default function EsbuildPlugin( }); }, }; + + return pluginInstance; } diff --git a/tests/specs/plugin.ts b/tests/specs/plugin.ts index b4cb14d..98c4b16 100644 --- a/tests/specs/plugin.ts +++ b/tests/specs/plugin.ts @@ -9,7 +9,7 @@ import { configureMiniCssExtractPlugin, } from '../utils.js'; import * as fixtures from '../fixtures.js'; -import type { EsbuildPluginOptions } from '#esbuild-loader'; +import { EsbuildPlugin, type EsbuildPluginOptions } from '#esbuild-loader'; const assertMinified = (code: string) => { expect(code).not.toMatch(/\s{2,}/); @@ -23,7 +23,72 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac const webpackIs4 = isWebpack4(webpack); describe('Plugin', ({ test, describe }) => { - describe('Minify JS', ({ test }) => { + describe('Minify JS', ({ test, describe }) => { + describe('should not minify by default', ({ test }) => { + test('minimizer', async () => { + const built = await build( + fixtures.minification, + (config) => { + config.optimization = { + minimize: false, + minimizer: [ + 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'); + expect(exportedFunction.toString()).toMatch(/\s{2,}/); + }); + + test('plugin', async () => { + const built = await build( + fixtures.minification, + (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'); + expect(exportedFunction.toString()).toMatch(/\s{2,}/); + }); + + test('plugin with minimize enabled', async () => { + const built = await build( + fixtures.minification, + (config) => { + config.optimization = { + minimize: true, + + // Remove Terser + minimizer: [], + }; + + 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'); + expect(exportedFunction.toString()).toMatch(/\s{2,}/); + }); + }); + test('minify', async () => { const built = await build( fixtures.minification,