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

Use same hook for both webpack 4 and 5 #246

Merged
merged 5 commits into from
Jul 27, 2021
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: 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.