Skip to content

Commit

Permalink
fix: use webpack.sources when it's available for webpack@5 (#301)
Browse files Browse the repository at this point in the history
Co-authored-by: Evilebot Tnawi <[email protected]>
  • Loading branch information
timneutkens and evilebottnawi authored Aug 20, 2020
1 parent 9487f3a commit 9d861d8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import path from 'path';
import os from 'os';

import { SourceMapConsumer } from 'source-map';
import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources';
import RequestShortener from 'webpack/lib/RequestShortener';
import {
import webpackSources from 'webpack-sources';
import webpack, {
util,
ModuleFilenameHelpers,
SourceMapDevToolPlugin,
javascript,
version as webpackVersion,
} from 'webpack';
import RequestShortener from 'webpack/lib/RequestShortener';

import validateOptions from 'schema-utils';
import serialize from 'serialize-javascript';
import terserPackageJson from 'terser/package.json';
Expand All @@ -21,6 +22,9 @@ import schema from './options.json';

import { minify as minifyFn } from './minify';

// webpack 5 exposes the sources property to ensure the right version of webpack-sources is used
const sources = webpack.sources || webpackSources;

class TerserPlugin {
constructor(options = {}) {
validateOptions(schema, options, {
Expand Down Expand Up @@ -263,7 +267,7 @@ class TerserPlugin {
}

if (map) {
outputSource = new SourceMapSource(
outputSource = new sources.SourceMapSource(
code,
name,
map,
Expand All @@ -272,7 +276,7 @@ class TerserPlugin {
true
);
} else {
outputSource = new RawSource(code);
outputSource = new sources.RawSource(code);
}

const assetInfo = { ...info, minimized: true };
Expand All @@ -296,7 +300,7 @@ class TerserPlugin {
}

if (banner) {
outputSource = new ConcatSource(
outputSource = new sources.ConcatSource(
shebang ? `${shebang}\n` : '',
`/*! ${banner} */\n`,
outputSource
Expand Down Expand Up @@ -576,7 +580,7 @@ class TerserPlugin {
TerserPlugin.emitAsset(
compilation,
commentsFilename,
new RawSource(`${extractedComments}\n`)
new sources.RawSource(`${extractedComments}\n`)
);
});

Expand Down

0 comments on commit 9d861d8

Please sign in to comment.