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): mark assets as minimized only when minified #338

Merged
merged 2 commits into from
Aug 29, 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
9 changes: 8 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
...transformOptions
} = options;

const minimized = (
transformOptions.minify
|| transformOptions.minifyWhitespace
|| transformOptions.minifyIdentifiers
|| transformOptions.minifySyntax
);

const assets = (compilation.getAssets() as Asset[]).filter(asset => (

// Filter out already minimized
Expand Down Expand Up @@ -97,16 +104,16 @@
? new SourceMapSource(
result.code,
asset.name,
result.map as any,

Check warning on line 107 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 109 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 113 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
{
...asset.info,
minimized: true,
minimized,
},
);
}));
Expand All @@ -131,7 +138,7 @@
this.options = options;
}

apply(compiler: Compiler) {

Check warning on line 141 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / Test

Method 'apply' has a complexity of 14. Maximum allowed is 10
const {
implementation,
...options
Expand Down
18 changes: 18 additions & 0 deletions tests/specs/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': `
Expand Down
Loading