From cf2a5a11f840d096b6047ff738381939b1ab1e31 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Tue, 27 Jul 2021 02:19:38 +0200 Subject: [PATCH] feat: useLegacyEmit option (#246) * 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. --- README.md | 9 ++++ lib/index.js | 3 +- package-lock.json | 78 +++++++++++++++++----------- package.json | 1 + test/unit/options.js | 22 ++++++++ test/unit/snapshots/options.js.md | 9 ++++ test/unit/snapshots/options.js.snap | Bin 361 -> 413 bytes 7 files changed, 90 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 734c66a..42b75cf 100644 --- a/README.md +++ b/README.md @@ -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`
+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`
diff --git a/lib/index.js b/lib/index.js index 0a12f17..3da41ce 100644 --- a/lib/index.js +++ b/lib/index.js @@ -23,6 +23,7 @@ const defaults = { sort: null, transformExtensions: /^(gz|map)$/i, useEntryKeys: false, + useLegacyEmit: false, writeToFileEmit: false }; @@ -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) => { diff --git a/package-lock.json b/package-lock.json index a2e9f85..54d5ac1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1947,6 +1947,16 @@ "@xtuc/long": "4.2.2" } }, + "@wordpress/dependency-extraction-webpack-plugin": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@wordpress/dependency-extraction-webpack-plugin/-/dependency-extraction-webpack-plugin-3.1.0.tgz", + "integrity": "sha512-lxdQ6qkHFiL1y7C/tHi/pxnFMaBa4AzBI/NcQ2+yJXwUqO746KYO9A6zEueQxa3mpEH3kT4r98C/p+MEf8xAsg==", + "dev": true, + "requires": { + "json2php": "^0.0.4", + "webpack-sources": "^2.2.0" + } + }, "@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", @@ -2207,9 +2217,9 @@ }, "dependencies": { "bn.js": { - "version": "4.11.9", - "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.9.tgz", - "integrity": "sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==", + "version": "4.12.0", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz", + "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==", "dev": true } } @@ -2822,9 +2832,9 @@ "dev": true }, "bn.js": { - "version": "5.1.3", - "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.1.3.tgz", - "integrity": "sha512-GkTiFpjFtUzU9CbMeJ5iazkCzGL3jrhzerzZIuqLABjbwRaFt33I9tUdSNryIptM+RxDet6OKm2WnLXzW51KsQ==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.2.0.tgz", + "integrity": "sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==", "dev": true }, "boolbase": { @@ -4058,9 +4068,9 @@ }, "dependencies": { "bn.js": { - "version": "4.11.9", - "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.9.tgz", - "integrity": "sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==", + "version": "4.12.0", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz", + "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==", "dev": true } } @@ -4427,9 +4437,9 @@ }, "dependencies": { "bn.js": { - "version": "4.11.9", - "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.9.tgz", - "integrity": "sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==", + "version": "4.12.0", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz", + "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==", "dev": true } } @@ -4594,9 +4604,9 @@ } }, "enhanced-resolve": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-4.3.0.tgz", - "integrity": "sha512-3e87LvavsdxyoCfGusJnrZ5G8SLPOFeHSNpZI/ATL9a5leXo2k0w6MKnbqhdBad9qTobSfB20Ld7UmgoNbAZkQ==", + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-4.5.0.tgz", + "integrity": "sha512-Nv9m36S/vxpsI+Hc4/ZGRs0n9mXqSWGGq49zxb/cJfPAQMbUtttJAlNPS4AQzaBdw/pKskw5bMbekT/Y7W/Wlg==", "dev": true, "requires": { "graceful-fs": "^4.1.2", @@ -5192,9 +5202,9 @@ "dev": true }, "events": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/events/-/events-3.2.0.tgz", - "integrity": "sha512-/46HWwbfCX2xTawVfkKLGxMifJYQBWMwY1mjywRtb4c9x8l5NP3KoJtnIOiL1hfdRkIuYhETxQlo62IF8tcnlg==", + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", + "integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==", "dev": true }, "evp_bytestokey": { @@ -6873,6 +6883,12 @@ "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", "dev": true }, + "json2php": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/json2php/-/json2php-0.0.4.tgz", + "integrity": "sha1-a9haHdpqXdfpECK7JEA8wbfC7jQ=", + "dev": true + }, "json5": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", @@ -7686,9 +7702,9 @@ }, "dependencies": { "bn.js": { - "version": "4.11.9", - "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.9.tgz", - "integrity": "sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==", + "version": "4.12.0", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz", + "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==", "dev": true } } @@ -9259,9 +9275,9 @@ }, "dependencies": { "bn.js": { - "version": "4.11.9", - "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.9.tgz", - "integrity": "sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==", + "version": "4.12.0", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz", + "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==", "dev": true } } @@ -10010,9 +10026,9 @@ } }, "source-map-url": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz", - "integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.1.tgz", + "integrity": "sha512-cPiFOTLUKvJFIg4SKVScy4ilPPW6rFgMgfuZJPNoDuMs3nC1HbMUycBoJw77xFIp6z1UJQJOfx6C9GMH80DiTw==", "dev": true }, "spawn-sync": { @@ -11311,9 +11327,9 @@ } }, "webpack": { - "version": "4.44.2", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-4.44.2.tgz", - "integrity": "sha512-6KJVGlCxYdISyurpQ0IPTklv+DULv05rs2hseIXer6D7KrUicRDLFb4IUM1S6LUAKypPM/nSiVSuv8jHu1m3/Q==", + "version": "4.46.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-4.46.0.tgz", + "integrity": "sha512-6jJuJjg8znb/xRItk7bkT0+Q7AHCYjjFnvKIWQPkNIOyRqoCGvkOs0ipeQzrqz4l5FtN5ZI/ukEHroeX/o1/5Q==", "dev": true, "requires": { "@webassemblyjs/ast": "1.9.0", @@ -11324,7 +11340,7 @@ "ajv": "^6.10.2", "ajv-keywords": "^3.4.1", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^4.3.0", + "enhanced-resolve": "^4.5.0", "eslint-scope": "^4.0.3", "json-parse-better-errors": "^1.0.2", "loader-runner": "^2.4.0", diff --git a/package.json b/package.json index 7bd2f21..9495dd4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/unit/options.js b/test/unit/options.js index 0907b80..d9ed35f 100644 --- a/test/unit/options.js +++ b/test/unit/options.js @@ -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'); @@ -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); +}); diff --git a/test/unit/snapshots/options.js.md b/test/unit/snapshots/options.js.md index 086ca0a..6bac1d8 100644 --- a/test/unit/snapshots/options.js.md +++ b/test/unit/snapshots/options.js.md @@ -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', + } diff --git a/test/unit/snapshots/options.js.snap b/test/unit/snapshots/options.js.snap index 24445cbafd66e159f8d6faf9f5a6ecad0639d3f5..e7cd9753253bc56fab47a87a2e18c9a5732d1525 100644 GIT binary patch delta 230 zcmVugf-zBOjjxYn;&Er@mN;kpTp{fjHrW&4il8ar}2#?oY}}pS6z> zEczIT1t!|{cARU!cWB8g^O?yDjF`Zp#~C5`FcSmAgg37>l`iKr#9CkG`rT;KnZyJZ zUCs=_i&+>LtXjU?+;yICho}2&ck;aM*^FROV^#(RW(Ib!8NizF5orR delta 178 zcmV;j08Rg$1L*>PK~_N^Q*L2!b7*gLAa*he0s!J+TBfB5=N_4EhVk&jP;HBRX7Q(vsa$N&P}K%8*GWM00?W%?;!;M01U8A-T(jq