Skip to content

Commit

Permalink
fix: get rid deprecation warnings for webpack@5 (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Oct 21, 2019
1 parent a11e66b commit 504ea8b
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import path from 'path';
import { SourceMapConsumer } from 'source-map';
import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources';
import RequestShortener from 'webpack/lib/RequestShortener';
import { ModuleFilenameHelpers, SourceMapDevToolPlugin } from 'webpack';
import {
ModuleFilenameHelpers,
SourceMapDevToolPlugin,
JavascriptModulesPlugin,
} from 'webpack';
import validateOptions from 'schema-utils';
import serialize from 'serialize-javascript';
import terserPackageJson from 'terser/package.json';
Expand Down Expand Up @@ -518,19 +522,32 @@ class TerserPlugin {
});
}

const { mainTemplate, chunkTemplate } = compilation;

// Regenerate `contenthash` for minified assets
for (const template of [mainTemplate, chunkTemplate]) {
template.hooks.hashForChunk.tap(plugin, (hash) => {
const data = serialize({
terser: terserPackageJson.version,
terserOptions: this.options.terserOptions,
});
if (JavascriptModulesPlugin) {
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
const data = serialize({
terser: terserPackageJson.version,
terserOptions: this.options.terserOptions,
});

hooks.chunkHash.tap(plugin, (chunk, hash) => {
hash.update('TerserPlugin');
hash.update(data);
});
} else {
// Todo remove after drop `webpack@4` compatibility
const { mainTemplate, chunkTemplate } = compilation;
const data = serialize({
terser: terserPackageJson.version,
terserOptions: this.options.terserOptions,
});

// Regenerate `contenthash` for minified assets
for (const template of [mainTemplate, chunkTemplate]) {
template.hooks.hashForChunk.tap(plugin, (hash) => {
hash.update('TerserPlugin');
hash.update(data);
});
}
}

compilation.hooks.optimizeChunkAssets.tapPromise(
Expand Down

0 comments on commit 504ea8b

Please sign in to comment.