Skip to content

Commit

Permalink
fix(plugin): only minimize when used as minimizer (#333)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: No longer minifies when used in "plugin". Use in "minimizer" to enable minification by default.
  • Loading branch information
privatenumber committed Aug 7, 2023
1 parent 1de5d2f commit c27387d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 13 deletions.
31 changes: 20 additions & 11 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 133 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / Test

Method 'apply' has a complexity of 13. Maximum allowed is 10
if (!('format' in options)) {
const { target } = compiler.options;
Expand All @@ -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',
Expand Down Expand Up @@ -230,4 +237,6 @@ export default function EsbuildPlugin(
});
},
};

return pluginInstance;
}
69 changes: 67 additions & 2 deletions tests/specs/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,}/);
Expand All @@ -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,
Expand Down

0 comments on commit c27387d

Please sign in to comment.