Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin): only minimize when used as minimizer #333

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@
? new SourceMapSource(
result.code,
asset.name,
result.map as any,

Check warning on line 100 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
sourceAsString,
map as any,

Check warning on line 102 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
true,
)
: new RawSource(result.code)
) as any,

Check warning on line 106 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
{
...asset.info,
minimized: true,
Expand All @@ -129,18 +129,8 @@

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;
const isWebTarget = (
Expand All @@ -162,6 +152,23 @@
}
}

/**
* 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 @@
});
},
};

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
Loading