Skip to content

Commit

Permalink
feat: respect SourceMapDevToolPlugin plugin for sourceMap option (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 21, 2019
1 parent f4c47aa commit d01c1b5
Show file tree
Hide file tree
Showing 12 changed files with 873 additions and 442 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ module.exports = {
### `sourceMap`

Type: `Boolean`
Default: `false` (see below for details around `devtool`)
Default: `false` (see below for details around `devtool` value and `SourceMapDevToolPlugin` plugin)

**Works only with `source-map`, `inline-source-map`, `hidden-source-map` and `nosources-source-map` values for the [`devtool`](https://webpack.js.org/configuration/devtool/) option.**

Expand All @@ -289,8 +289,9 @@ Why?
- `eval` wraps modules in `eval("string")` and the minimizer does not handle strings.
- `cheap` has not column information and minimizer generate only a single line, which leave only a single mapping.

The plugin respect the [`devtool`](https://webpack.js.org/configuration/devtool/).
The plugin respect the [`devtool`](https://webpack.js.org/configuration/devtool/) and using the `SourceMapDevToolPlugin` plugin.
Using supported `devtool` values enable source map generation.
Using `SourceMapDevToolPlugin` with enabled the `columns` option enables source map generation.

Use source maps to map error message locations to modules (this slows down the compilation).
If you use your own `minify` function please read the `minify` section for handling source maps correctly.
Expand Down
204 changes: 109 additions & 95 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
"cacache": "^13.0.1",
"find-cache-dir": "^3.0.0",
"jest-worker": "^24.9.0",
"schema-utils": "^2.4.1",
"schema-utils": "^2.5.0",
"serialize-javascript": "^2.1.0",
"source-map": "^0.6.1",
"terser": "^4.3.8",
"terser": "^4.3.9",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
"@babel/cli": "^7.6.3",
"@babel/core": "^7.6.3",
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
Expand All @@ -62,15 +62,15 @@
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-import": "^2.18.2",
"husky": "^3.0.8",
"husky": "^3.0.9",
"jest": "^24.9.0",
"jest-junit": "^8.0.0",
"lint-staged": "^9.4.2",
"memory-fs": "^0.5.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"standard-version": "^7.0.0",
"uglify-js": "^3.6.1",
"uglify-js": "^3.6.3",
"webpack": "^4.41.0"
},
"keywords": [
Expand Down
23 changes: 15 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import { SourceMapConsumer } from 'source-map';
import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources';
import RequestShortener from 'webpack/lib/RequestShortener';
import { ModuleFilenameHelpers } from 'webpack';
import { ModuleFilenameHelpers, SourceMapDevToolPlugin } from 'webpack';
import validateOptions from 'schema-utils';
import serialize from 'serialize-javascript';
import terserPackageJson from 'terser/package.json';
Expand Down Expand Up @@ -171,16 +171,23 @@ class TerserPlugin {
}

apply(compiler) {
const { devtool, output } = compiler.options;
const { devtool, output, plugins } = compiler.options;

this.options.sourceMap =
typeof this.options.sourceMap === 'undefined'
? devtool &&
!devtool.includes('eval') &&
!devtool.includes('cheap') &&
(devtool.includes('source-map') ||
// Todo remove when `webpack@5` support will be dropped
devtool.includes('sourcemap'))
? (devtool &&
!devtool.includes('eval') &&
!devtool.includes('cheap') &&
(devtool.includes('source-map') ||
// Todo remove when `webpack@5` support will be dropped
devtool.includes('sourcemap'))) ||
(plugins &&
plugins.some(
(plugin) =>
plugin instanceof SourceMapDevToolPlugin &&
plugin.options &&
plugin.options.columns
))
: Boolean(this.options.sourceMap);

if (
Expand Down
109 changes: 109 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TerserPlugin from '../src/index';
import {
cleanErrorStack,
compile,
countPlugins,
createCompiler,
getAssets,
removeCache,
Expand Down Expand Up @@ -47,6 +48,114 @@ describe('TerserPlugin', () => {
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should work in multi compiler mode', async () => {
const multiCompiler = createCompiler([
{
mode: 'production',
bail: true,
cache: false,
entry: `${__dirname}/fixtures/entry.js`,
output: {
path: `${__dirname}/dist`,
filename: '[name]-1.js',
chunkFilename: '[id]-1.[name].js',
},
optimization: {
minimize: false,
},
},
{
mode: 'production',
bail: true,
cache: false,
entry: `${__dirname}/fixtures/entry.js`,
output: {
path: `${__dirname}/dist`,
filename: '[name]-2.js',
chunkFilename: '[id]-2.[name].js',
},
optimization: {
minimize: false,
},
plugins: [new TerserPlugin()],
},
{
mode: 'production',
bail: true,
cache: false,
entry: `${__dirname}/fixtures/import-export/entry.js`,
output: {
path: `${__dirname}/dist-MultiCompiler`,
filename: '[name]-3.js',
chunkFilename: '[id]-3.[name].js',
},
optimization: {
minimize: false,
},
plugins: [new TerserPlugin()],
},
]);

const emptyPluginCount = countPlugins(multiCompiler.compilers[0]);
const expectedPluginCount = countPlugins(multiCompiler.compilers[1]);

expect(emptyPluginCount).not.toEqual(expectedPluginCount);

multiCompiler.compilers.slice(2).forEach((compiler) => {
const pluginCount = countPlugins(compiler);

expect(pluginCount).not.toEqual(emptyPluginCount);
expect(pluginCount).toEqual(expectedPluginCount);
expect(pluginCount).toMatchSnapshot('compiler plugin count');
});

const multiStats = await compile(multiCompiler);

multiStats.stats.forEach((stats, index) => {
const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, multiCompiler.compilers[index])).toMatchSnapshot(
'assets'
);
});
});

it('should work as a plugin', async () => {
const compiler = createCompiler({
plugins: [new TerserPlugin()],
});

const stats = await compile(compiler);

const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should work as a minimizer', async () => {
const compiler = createCompiler({
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},
});

const stats = await compile(compiler);

const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should regenerate hash', async () => {
const originalMainTemplateUpdateHashForChunk =
MainTemplate.prototype.updateHashForChunk;
Expand Down
Loading

0 comments on commit d01c1b5

Please sign in to comment.