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

fix: use webpack.sources when it's available #301

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/index.js
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