Skip to content

Commit

Permalink
feat: useLegacyEmit option (#246)
Browse files Browse the repository at this point in the history
* Use same hook for both webpack 4 and 5

* Add useLegacyEmit option

This option is used as compat option for older plugins.

* Add Unit test for useLegacyEmit option

This commit adds a new unit test in the options section that tests the new useLegacyEmit option.
The test runs DependencyExtractionWebpackPlugin that is locked on version 3.1.0. This version emits the main.asset.json on the emit hook.
If useLegacyEmit is set to false the test fails on the webpack v5 tests.
  • Loading branch information
Levdbas authored Jul 27, 2021
1 parent f31f4ed commit cf2a5a1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 32 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ Default: `false`

If `true`, the keys specified in the `entry` property will be used as keys in the manifest. No file extension will be added (unless specified as part of an `entry` property key).

### `useLegacyEmit`

Type: `Boolean`<br>
Default: `false`

If `true`, the manifest will be written on the deprecated webpack `emit` hook to be compatible with not yet updated webpack plugins.

A lot of webpack plugins are not yet updated to match the new webpack 5 API. This is a problem when other plugins use the deprecated `emit` hook. The manifest will be written before these other plugins and thus files are missing on the manifest.

### `writeToFileEmit`

Type: `Boolean`<br>
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const defaults = {
sort: null,
transformExtensions: /^(gz|map)$/i,
useEntryKeys: false,
useLegacyEmit: false,
writeToFileEmit: false
};

Expand Down Expand Up @@ -57,7 +58,7 @@ class WebpackManifestPlugin {
hook.tap(hookOptions, normalModuleLoader);
});

if (webpack.version.startsWith('4')) {
if (webpack.version.startsWith('4') || this.options.useLegacyEmit === true) {
compiler.hooks.emit.tap(hookOptions, emit);
} else {
compiler.hooks.thisCompilation.tap(hookOptions, (compilation) => {
Expand Down
78 changes: 47 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"devDependencies": {
"@ava/babel": "^1.0.1",
"@svgr/webpack": "^5.4.0",
"@wordpress/dependency-extraction-webpack-plugin": "^3.1.0",
"ava": "^3.13.0",
"codecov": "^3.1.0",
"copy-webpack-plugin": "^6.2.1",
Expand Down
22 changes: 22 additions & 0 deletions test/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { join } = require('path');

const test = require('ava');
const CopyPlugin = require('copy-webpack-plugin');
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
const del = require('del');

const { compile } = require('../helpers/unit');
Expand Down Expand Up @@ -119,3 +120,24 @@ test('useEntryKeys, exclude sourcemap', async (t) => {

t.snapshot(manifest);
});

test('useLegacyEmit', async (t) => {
const config = {
context: __dirname,
entry: {
main: '../fixtures/file.js'
},
output: {
filename: '[name].js',
path: join(outputPath, 'useLegacyEmit')
},
plugins: [
new DependencyExtractionWebpackPlugin({
outputFormat: 'json'
})
]
};
const { manifest } = await compile(config, t, { useLegacyEmit: true });

t.snapshot(manifest);
});
9 changes: 9 additions & 0 deletions test/unit/snapshots/options.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ Generated by [AVA](https://avajs.dev).
main: 'main.js',
'main.js.map': 'main.js.map',
}

## useLegacyEmit

> Snapshot 1
{
'main.js': 'main.js',
'main.json': 'main.asset.json',
}
Binary file modified test/unit/snapshots/options.js.snap
Binary file not shown.

0 comments on commit cf2a5a1

Please sign in to comment.