Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Commit

Permalink
fix: don't handle invalid source map (webpack-contrib#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and whtsky committed Apr 17, 2018
1 parent 269285d commit 3b5ab02
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import serialize from 'serialize-javascript';
import schema from './options.json';
import Uglify from './uglify';
import versions from './uglify/versions';
import utils from './utils';

const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;

Expand Down Expand Up @@ -122,9 +123,18 @@ class UglifyJsPlugin {
const { source, map } = asset.sourceAndMap();

input = source;
inputSourceMap = map;

sourceMap = new SourceMapConsumer(inputSourceMap);
if (utils.isSourceMap(map)) {
inputSourceMap = map;
sourceMap = new SourceMapConsumer(inputSourceMap);
} else {
inputSourceMap = map;
sourceMap = null;

compilation.warnings.push(
new Error(`${file} contain invalid source map`),
);
}
} else {
input = asset.source();
inputSourceMap = null;
Expand Down
13 changes: 13 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function isSourceMap(input) {
// All required options for `new SourceMapConsumer(...options)`
// https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
return Boolean(input &&
input.version &&
input.sources &&
input.names &&
input.mappings);
}

export default {
isSourceMap,
};
16 changes: 16 additions & 0 deletions test/__snapshots__/source-map-options.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when options.sourceMap true and options.parallel true compilation handler when called optimize-chunk-assets handler only calls callback once: errors 1`] = `Array []`;

exports[`when options.sourceMap true and options.parallel true compilation handler when called optimize-chunk-assets handler only calls callback once: warnings 1`] = `
Array [
[Error: test4.js contain invalid source map],
]
`;

exports[`when options.sourceMap true and options.parallel true matches snapshot: asset main.c26a2e0fdba39d045bc6.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`;

exports[`when options.sourceMap true and options.parallel true matches snapshot: asset manifest.d6857f782c13a99b5917.js 1`] = `"!function(i){var p=window.webpackJsonp;window.webpackJsonp=function(r,n,e){for(var o,t,u,c=0,f=[];c<r.length;c++)t=r[c],a[t]&&f.push(a[t][0]),a[t]=0;for(o in n)Object.prototype.hasOwnProperty.call(n,o)&&(i[o]=n[o]);for(p&&p(r,n,e);f.length;)f.shift()();if(e)for(c=0;c<e.length;c++)u=l(l.s=e[c]);return u};var e={},a={1:0};function l(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return i[r].call(n.exports,n,n.exports,l),n.l=!0,n.exports}l.m=i,l.c=e,l.d=function(r,n,e){l.o(r,n)||Object.defineProperty(r,n,{configurable:!1,enumerable:!0,get:e})},l.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return l.d(n,\\"a\\",n),n},l.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},l.p=\\"\\",l.oe=function(r){throw console.error(r),r}}([]);"`;
Expand Down Expand Up @@ -192,6 +200,14 @@ Object {

exports[`when options.sourceMap true and options.parallel true matches snapshot: warnings 1`] = `Array []`;

exports[`when options.sourceMap true compilation handler when called optimize-chunk-assets handler only calls callback once: errors 1`] = `Array []`;

exports[`when options.sourceMap true compilation handler when called optimize-chunk-assets handler only calls callback once: warnings 1`] = `
Array [
[Error: test4.js contain invalid source map],
]
`;

exports[`when options.sourceMap true matches snapshot: asset main.c26a2e0fdba39d045bc6.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`;

exports[`when options.sourceMap true matches snapshot: asset manifest.d6857f782c13a99b5917.js 1`] = `"!function(i){var p=window.webpackJsonp;window.webpackJsonp=function(r,n,e){for(var o,t,u,c=0,f=[];c<r.length;c++)t=r[c],a[t]&&f.push(a[t][0]),a[t]=0;for(o in n)Object.prototype.hasOwnProperty.call(n,o)&&(i[o]=n[o]);for(p&&p(r,n,e);f.length;)f.shift()();if(e)for(c=0;c<e.length;c++)u=l(l.s=e[c]);return u};var e={},a={1:0};function l(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return i[r].call(n.exports,n,n.exports,l),n.l=!0,n.exports}l.m=i,l.c=e,l.d=function(r,n,e){l.o(r,n)||Object.defineProperty(r,n,{configurable:!1,enumerable:!0,get:e})},l.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return l.d(n,\\"a\\",n),n},l.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},l.p=\\"\\",l.oe=function(r){throw console.error(r),r}}([]);"`;
Expand Down
8 changes: 5 additions & 3 deletions test/parallel-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('when options.parallel', () => {
beforeEach(() => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = assets;
compilation.assets = Object.assign({}, assets);
compilation.errors = [];

workerFarm.mockClear();
Expand Down Expand Up @@ -183,7 +183,8 @@ describe('when options.parallel', () => {
beforeEach(() => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = assets;
compilation.assets = Object.assign({}, assets);
compilation.warnings = [];
compilation.errors = [];

workerFarm.mockClear();
Expand Down Expand Up @@ -290,7 +291,8 @@ describe('when options.parallel', () => {
beforeEach(() => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = assets;
compilation.assets = Object.assign({}, assets);
compilation.warnings = [];
compilation.errors = [];

workerFarm.mockClear();
Expand Down
22 changes: 18 additions & 4 deletions test/source-map-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ describe('when options.sourceMap', () => {
'test3.js': {
source: () => 'function test3(foo) { foo = 1; }',
},
'test4.js': {
sourceAndMap: () => {
return {
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
map: null,
};
},
},
};

describe('true', () => {
Expand Down Expand Up @@ -62,7 +70,8 @@ describe('when options.sourceMap', () => {
beforeEach(() => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = assets;
compilation.assets = Object.assign({}, assets);
compilation.warnings = [];
compilation.errors = [];

eventBinding.handler(compilation);
Expand Down Expand Up @@ -107,8 +116,10 @@ describe('when options.sourceMap', () => {
it('only calls callback once', (done) => {
callback = jest.fn();
compilationEventBinding.handler([{
files: ['test.js', 'test1.js', 'test2.js', 'test3.js'],
files: ['test.js', 'test1.js', 'test2.js', 'test3.js', 'test4.js'],
}], () => {
expect(compilation.warnings).toMatchSnapshot('warnings');
expect(compilation.errors).toMatchSnapshot('errors');
callback();
expect(callback.mock.calls.length).toBe(1);
done();
Expand Down Expand Up @@ -183,7 +194,8 @@ describe('when options.sourceMap', () => {
beforeEach(() => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = assets;
compilation.assets = Object.assign({}, assets);
compilation.warnings = [];
compilation.errors = [];

eventBinding.handler(compilation);
Expand Down Expand Up @@ -228,8 +240,10 @@ describe('when options.sourceMap', () => {
it('only calls callback once', (done) => {
callback = jest.fn();
compilationEventBinding.handler([{
files: ['test.js', 'test1.js', 'test2.js', 'test3.js'],
files: ['test.js', 'test1.js', 'test2.js', 'test3.js', 'test4.js'],
}], () => {
expect(compilation.warnings).toMatchSnapshot('warnings');
expect(compilation.errors).toMatchSnapshot('errors');
callback();
expect(callback.mock.calls.length).toBe(1);
done();
Expand Down
21 changes: 21 additions & 0 deletions test/utils/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import utils from '../../src/utils';

describe('utils', () => {
it('isSourceMap', () => {
const rawSourceMap = {
version: 3,
file: 'min.js',
names: ['bar', 'baz', 'n'],
sources: ['one.js', 'two.js'],
sourceRoot: 'http://example.com/www/js/',
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA',
};

expect(utils.isSourceMap(null)).toBe(false);
expect(utils.isSourceMap()).toBe(false);
expect(utils.isSourceMap({})).toBe(false);
expect(utils.isSourceMap([])).toBe(false);
expect(utils.isSourceMap('foo')).toBe(false);
expect(utils.isSourceMap(rawSourceMap)).toBe(true);
});
});

0 comments on commit 3b5ab02

Please sign in to comment.