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

fix: don't handle invalid source map #268

Merged
merged 1 commit into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw warning on invalid source map. Is it very DX.

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.0c220ec66316af2c1b24.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(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}}([]);"`;
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.0c220ec66316af2c1b24.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(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}}([]);"`;
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compilation mondify assets, we should clone 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);
});
});