Skip to content

Commit 507c947

Browse files
committed
fix(plugin): only minimize when used as minimizer (#333)
BREAKING CHANGE: No longer minifies when used in "plugin". Use in "minimizer" to enable minification by default.
1 parent 221f864 commit 507c947

File tree

2 files changed

+87
-13
lines changed

2 files changed

+87
-13
lines changed

src/plugin.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,7 @@ export default function EsbuildPlugin(
129129

130130
const transform = implementation?.transform ?? defaultEsbuildTransform;
131131

132-
const hasGranularMinificationConfig = (
133-
'minifyIdentifiers' in options
134-
|| 'minifySyntax' in options
135-
|| 'minifyWhitespace' in options
136-
);
137-
138-
if (!hasGranularMinificationConfig) {
139-
options.minify = true;
140-
}
141-
142-
return {
132+
const pluginInstance = {
143133
apply(compiler: Compiler) {
144134
if (!('format' in options)) {
145135
const { target } = compiler.options;
@@ -162,6 +152,23 @@ export default function EsbuildPlugin(
162152
}
163153
}
164154

155+
/**
156+
* Enable minification by default if used in the minimizer array
157+
* unless further specified in the options
158+
*/
159+
const usedAsMinimizer = compiler.options.optimization?.minimizer?.includes?.(pluginInstance);
160+
if (
161+
usedAsMinimizer
162+
&& !(
163+
'minify' in options
164+
|| 'minifyWhitespace' in options
165+
|| 'minifyIdentifiers' in options
166+
|| 'minifySyntax' in options
167+
)
168+
) {
169+
options.minify = compiler.options.optimization?.minimize;
170+
}
171+
165172
compiler.hooks.compilation.tap(pluginName, (compilation) => {
166173
const meta = JSON.stringify({
167174
name: 'esbuild-loader',
@@ -230,4 +237,6 @@ export default function EsbuildPlugin(
230237
});
231238
},
232239
};
240+
241+
return pluginInstance;
233242
}

tests/specs/plugin.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
configureMiniCssExtractPlugin,
1010
} from '../utils.js';
1111
import * as fixtures from '../fixtures.js';
12-
import type { EsbuildPluginOptions } from '#esbuild-loader';
12+
import { EsbuildPlugin, type EsbuildPluginOptions } from '#esbuild-loader';
1313

1414
const assertMinified = (code: string) => {
1515
expect(code).not.toMatch(/\s{2,}/);
@@ -23,7 +23,72 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac
2323
const webpackIs4 = isWebpack4(webpack);
2424

2525
describe('Plugin', ({ test, describe }) => {
26-
describe('Minify JS', ({ test }) => {
26+
describe('Minify JS', ({ test, describe }) => {
27+
describe('should not minify by default', ({ test }) => {
28+
test('minimizer', async () => {
29+
const built = await build(
30+
fixtures.minification,
31+
(config) => {
32+
config.optimization = {
33+
minimize: false,
34+
minimizer: [
35+
new EsbuildPlugin(),
36+
],
37+
};
38+
},
39+
webpack,
40+
);
41+
42+
expect(built.stats.hasWarnings()).toBe(false);
43+
expect(built.stats.hasErrors()).toBe(false);
44+
45+
const exportedFunction = built.require('/dist/');
46+
expect(exportedFunction('hello world')).toBe('hello world');
47+
expect(exportedFunction.toString()).toMatch(/\s{2,}/);
48+
});
49+
50+
test('plugin', async () => {
51+
const built = await build(
52+
fixtures.minification,
53+
(config) => {
54+
config.plugins?.push(new EsbuildPlugin());
55+
},
56+
webpack,
57+
);
58+
59+
expect(built.stats.hasWarnings()).toBe(false);
60+
expect(built.stats.hasErrors()).toBe(false);
61+
62+
const exportedFunction = built.require('/dist/');
63+
expect(exportedFunction('hello world')).toBe('hello world');
64+
expect(exportedFunction.toString()).toMatch(/\s{2,}/);
65+
});
66+
67+
test('plugin with minimize enabled', async () => {
68+
const built = await build(
69+
fixtures.minification,
70+
(config) => {
71+
config.optimization = {
72+
minimize: true,
73+
74+
// Remove Terser
75+
minimizer: [],
76+
};
77+
78+
config.plugins?.push(new EsbuildPlugin());
79+
},
80+
webpack,
81+
);
82+
83+
expect(built.stats.hasWarnings()).toBe(false);
84+
expect(built.stats.hasErrors()).toBe(false);
85+
86+
const exportedFunction = built.require('/dist/');
87+
expect(exportedFunction('hello world')).toBe('hello world');
88+
expect(exportedFunction.toString()).toMatch(/\s{2,}/);
89+
});
90+
});
91+
2792
test('minify', async () => {
2893
const built = await build(
2994
fixtures.minification,

0 commit comments

Comments
 (0)