Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 693e45e

Browse files
authoredApr 12, 2018
fix: don't handle invalid source map (#268)
1 parent 5ee35be commit 693e45e

File tree

6 files changed

+85
-9
lines changed

6 files changed

+85
-9
lines changed
 

‎src/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import serialize from 'serialize-javascript';
1212
import schema from './options.json';
1313
import Uglify from './uglify';
1414
import versions from './uglify/versions';
15+
import utils from './utils';
1516

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

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

124125
input = source;
125-
inputSourceMap = map;
126126

127-
sourceMap = new SourceMapConsumer(inputSourceMap);
127+
if (utils.isSourceMap(map)) {
128+
inputSourceMap = map;
129+
sourceMap = new SourceMapConsumer(inputSourceMap);
130+
} else {
131+
inputSourceMap = map;
132+
sourceMap = null;
133+
134+
compilation.warnings.push(
135+
new Error(`${file} contain invalid source map`),
136+
);
137+
}
128138
} else {
129139
input = asset.source();
130140
inputSourceMap = null;

‎src/utils/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function isSourceMap(input) {
2+
// All required options for `new SourceMapConsumer(...options)`
3+
// https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
4+
return Boolean(input &&
5+
input.version &&
6+
input.sources &&
7+
input.names &&
8+
input.mappings);
9+
}
10+
11+
export default {
12+
isSourceMap,
13+
};

‎test/__snapshots__/source-map-options.test.js.snap

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`when options.sourceMap true and options.parallel true compilation handler when called optimize-chunk-assets handler only calls callback once: errors 1`] = `Array []`;
4+
5+
exports[`when options.sourceMap true and options.parallel true compilation handler when called optimize-chunk-assets handler only calls callback once: warnings 1`] = `
6+
Array [
7+
[Error: test4.js contain invalid source map],
8+
]
9+
`;
10+
311
exports[`when options.sourceMap true and options.parallel true matches snapshot: asset main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`;
412

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

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

203+
exports[`when options.sourceMap true compilation handler when called optimize-chunk-assets handler only calls callback once: errors 1`] = `Array []`;
204+
205+
exports[`when options.sourceMap true compilation handler when called optimize-chunk-assets handler only calls callback once: warnings 1`] = `
206+
Array [
207+
[Error: test4.js contain invalid source map],
208+
]
209+
`;
210+
195211
exports[`when options.sourceMap true matches snapshot: asset main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`;
196212

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

‎test/parallel-options.test.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('when options.parallel', () => {
7878
beforeEach(() => {
7979
chunkPluginEnvironment = new PluginEnvironment();
8080
compilation = chunkPluginEnvironment.getEnvironmentStub();
81-
compilation.assets = assets;
81+
compilation.assets = Object.assign({}, assets);
8282
compilation.errors = [];
8383

8484
workerFarm.mockClear();
@@ -183,7 +183,8 @@ describe('when options.parallel', () => {
183183
beforeEach(() => {
184184
chunkPluginEnvironment = new PluginEnvironment();
185185
compilation = chunkPluginEnvironment.getEnvironmentStub();
186-
compilation.assets = assets;
186+
compilation.assets = Object.assign({}, assets);
187+
compilation.warnings = [];
187188
compilation.errors = [];
188189

189190
workerFarm.mockClear();
@@ -290,7 +291,8 @@ describe('when options.parallel', () => {
290291
beforeEach(() => {
291292
chunkPluginEnvironment = new PluginEnvironment();
292293
compilation = chunkPluginEnvironment.getEnvironmentStub();
293-
compilation.assets = assets;
294+
compilation.assets = Object.assign({}, assets);
295+
compilation.warnings = [];
294296
compilation.errors = [];
295297

296298
workerFarm.mockClear();

‎test/source-map-options.test.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ describe('when options.sourceMap', () => {
2020
'test3.js': {
2121
source: () => 'function test3(foo) { foo = 1; }',
2222
},
23+
'test4.js': {
24+
sourceAndMap: () => {
25+
return {
26+
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
27+
map: null,
28+
};
29+
},
30+
},
2331
};
2432

2533
describe('true', () => {
@@ -62,7 +70,8 @@ describe('when options.sourceMap', () => {
6270
beforeEach(() => {
6371
chunkPluginEnvironment = new PluginEnvironment();
6472
compilation = chunkPluginEnvironment.getEnvironmentStub();
65-
compilation.assets = assets;
73+
compilation.assets = Object.assign({}, assets);
74+
compilation.warnings = [];
6675
compilation.errors = [];
6776

6877
eventBinding.handler(compilation);
@@ -107,8 +116,10 @@ describe('when options.sourceMap', () => {
107116
it('only calls callback once', (done) => {
108117
callback = jest.fn();
109118
compilationEventBinding.handler([{
110-
files: ['test.js', 'test1.js', 'test2.js', 'test3.js'],
119+
files: ['test.js', 'test1.js', 'test2.js', 'test3.js', 'test4.js'],
111120
}], () => {
121+
expect(compilation.warnings).toMatchSnapshot('warnings');
122+
expect(compilation.errors).toMatchSnapshot('errors');
112123
callback();
113124
expect(callback.mock.calls.length).toBe(1);
114125
done();
@@ -183,7 +194,8 @@ describe('when options.sourceMap', () => {
183194
beforeEach(() => {
184195
chunkPluginEnvironment = new PluginEnvironment();
185196
compilation = chunkPluginEnvironment.getEnvironmentStub();
186-
compilation.assets = assets;
197+
compilation.assets = Object.assign({}, assets);
198+
compilation.warnings = [];
187199
compilation.errors = [];
188200

189201
eventBinding.handler(compilation);
@@ -228,8 +240,10 @@ describe('when options.sourceMap', () => {
228240
it('only calls callback once', (done) => {
229241
callback = jest.fn();
230242
compilationEventBinding.handler([{
231-
files: ['test.js', 'test1.js', 'test2.js', 'test3.js'],
243+
files: ['test.js', 'test1.js', 'test2.js', 'test3.js', 'test4.js'],
232244
}], () => {
245+
expect(compilation.warnings).toMatchSnapshot('warnings');
246+
expect(compilation.errors).toMatchSnapshot('errors');
233247
callback();
234248
expect(callback.mock.calls.length).toBe(1);
235249
done();

‎test/utils/index.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import utils from '../../src/utils';
2+
3+
describe('utils', () => {
4+
it('isSourceMap', () => {
5+
const rawSourceMap = {
6+
version: 3,
7+
file: 'min.js',
8+
names: ['bar', 'baz', 'n'],
9+
sources: ['one.js', 'two.js'],
10+
sourceRoot: 'http://example.com/www/js/',
11+
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA',
12+
};
13+
14+
expect(utils.isSourceMap(null)).toBe(false);
15+
expect(utils.isSourceMap()).toBe(false);
16+
expect(utils.isSourceMap({})).toBe(false);
17+
expect(utils.isSourceMap([])).toBe(false);
18+
expect(utils.isSourceMap('foo')).toBe(false);
19+
expect(utils.isSourceMap(rawSourceMap)).toBe(true);
20+
});
21+
});

0 commit comments

Comments
 (0)
This repository has been archived.