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

Commit

Permalink
refactor: use serialize-javascript package instead own implementati…
Browse files Browse the repository at this point in the history
…on (#183)
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Dec 8, 2017
1 parent d97ab11 commit 8d7ebcd
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 125 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"cacache": "^10.0.0",
"find-cache-dir": "^1.0.0",
"serialize-javascript": "^1.4.0",
"schema-utils": "^0.3.0",
"source-map": "^0.6.1",
"uglify-es": "^3.2.0",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources';
import RequestShortener from 'webpack/lib/RequestShortener';
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
import validateOptions from 'schema-utils';
import serialize from 'serialize-javascript';
import schema from './options.json';
import Uglify from './uglify';
import { encode } from './uglify/serialization';
import versions from './uglify/versions';

/* eslint-disable
Expand Down Expand Up @@ -155,13 +155,13 @@ class UglifyJsPlugin {
};

if (this.options.cache) {
task.cacheKey = JSON.stringify({
task.cacheKey = serialize({
'uglify-es': versions.uglify,
'uglifyjs-webpack-plugin': versions.plugin,
'uglifyjs-webpack-plugin-options': this.options,
path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file,
input,
}, encode);
});
}

tasks.push(task);
Expand Down
4 changes: 2 additions & 2 deletions src/uglify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import os from 'os';
import cacache from 'cacache';
import findCacheDir from 'find-cache-dir';
import workerFarm from 'worker-farm';
import serialize from 'serialize-javascript';
import minify from './minify';
import { encode } from './serialization';

let workerFile = require.resolve('./worker');

Expand All @@ -28,7 +28,7 @@ export default class {
if (this.maxConcurrentWorkers > 0) {
const workerOptions = process.platform === 'win32' ? { maxConcurrentWorkers: this.maxConcurrentWorkers, maxConcurrentCallsPerWorker: 1 } : { maxConcurrentWorkers: this.maxConcurrentWorkers };
this.workers = workerFarm(workerOptions, workerFile);
this.boundWorkers = (options, cb) => this.workers(JSON.stringify(options, encode), cb);
this.boundWorkers = (options, cb) => this.workers(serialize(options), cb);
} else {
this.boundWorkers = (options, cb) => {
try {
Expand Down
34 changes: 0 additions & 34 deletions src/uglify/serialization.js

This file was deleted.

8 changes: 6 additions & 2 deletions src/uglify/worker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import minify from './minify';
import { decode } from './serialization';

module.exports = (options, callback) => {
try {
callback(null, minify(JSON.parse(options, decode)));
// 'use strict' => this === undefined (Clean Scope)
// Safer for possible security issues, albeit not critical at all here
// eslint-disable-next-line no-new-func, no-param-reassign
options = new Function(`'use strict'\nreturn ${options}`)();

callback(null, minify(options));
} catch (errors) {
callback(errors);
}
Expand Down
56 changes: 28 additions & 28 deletions test/__snapshots__/cache-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,6 @@ exports[`cache \`string\`: asset main.0c220ec66316af2c1b24.js 1`] = `"webpackJso

exports[`cache \`string\`: asset manifest.6afe1bc6685e9ab36c1c.js 1`] = `"!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,a){for(var i,u,f,l=0,s=[];l<t.length;l++)u=t[l],o[u]&&s.push(o[u][0]),o[u]=0;for(i in c)Object.prototype.hasOwnProperty.call(c,i)&&(e[i]=c[i]);for(r&&r(t,c,a);s.length;)s.shift()();if(a)for(l=0;l<a.length;l++)f=n(n.s=a[l]);return f};var t={},o={1:0};n.e=function(e){function r(){i.onerror=i.onload=null,clearTimeout(u);var n=o[e];0!==n&&(n&&n[1](new Error(\\"Loading chunk \\"+e+\\" failed.\\")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var a=document.getElementsByTagName(\\"head\\")[0],i=document.createElement(\\"script\\");i.type=\\"text/javascript\\",i.charset=\\"utf-8\\",i.async=!0,i.timeout=12e4,n.nc&&i.setAttribute(\\"nonce\\",n.nc),i.src=n.p+\\"\\"+e+\\".\\"+({0:\\"main\\"}[e]||e)+\\".\\"+{0:\\"0c220ec66316af2c1b24\\"}[e]+\\".js\\";var u=setTimeout(r,12e4);return i.onerror=i.onload=r,a.appendChild(i),c},n.m=e,n.c=t,n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,\\"a\\",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p=\\"\\",n.oe=function(e){throw console.error(e),e}}([]);"`;

exports[`cache \`string\`: cached entry test.js 1`] = `
Array [
"test.js",
"function test(foo) { foo = 1; }",
]
`;

exports[`cache \`string\`: cached entry test1.js 1`] = `
Array [
"test1.js",
"function test1(foo) { foo = 1; }",
]
`;

exports[`cache \`string\`: cached entry test2.js 1`] = `
Array [
"test2.js",
"function test2(foo) { foo = 1; }",
]
`;

exports[`cache \`string\`: cached entry test3.js 1`] = `
Array [
"test3.js",
"function test3(foo) { foo = 1; }",
]
`;

exports[`cache \`string\`: errors 1`] = `Array []`;

exports[`cache \`string\`: warnings 1`] = `Array []`;
Expand All @@ -55,27 +27,55 @@ Array [
]
`;

exports[`cache \`true\`: cached entry test.js 2`] = `
Array [
"test.js",
"function test(foo) { foo = 1; }",
]
`;

exports[`cache \`true\`: cached entry test1.js 1`] = `
Array [
"test1.js",
"function test1(foo) { foo = 1; }",
]
`;

exports[`cache \`true\`: cached entry test1.js 2`] = `
Array [
"test1.js",
"function test1(foo) { foo = 1; }",
]
`;

exports[`cache \`true\`: cached entry test2.js 1`] = `
Array [
"test2.js",
"function test2(foo) { foo = 1; }",
]
`;

exports[`cache \`true\`: cached entry test2.js 2`] = `
Array [
"test2.js",
"function test2(foo) { foo = 1; }",
]
`;

exports[`cache \`true\`: cached entry test3.js 1`] = `
Array [
"test3.js",
"function test3(foo) { foo = 1; }",
]
`;

exports[`cache \`true\`: cached entry test3.js 2`] = `
Array [
"test3.js",
"function test3(foo) { foo = 1; }",
]
`;

exports[`cache \`true\`: errors 1`] = `Array []`;

exports[`cache \`true\`: warnings 1`] = `Array []`;
30 changes: 16 additions & 14 deletions test/cache-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ describe('when options.cache', () => {
cacache
.ls(cacheDir)
.then((cacheEntriesList) => {
const cacheEntriesListKeys = Object.keys(cacheEntriesList);
const cacheKeys = Object.keys(cacheEntriesList);

expect(cacheEntriesListKeys.length).toBe(0);
expect(cacheKeys.length).toBe(0);
done();
});
});
Expand Down Expand Up @@ -231,15 +231,16 @@ describe('when options.cache', () => {
cacache
.ls(cacheDir)
.then((cacheEntriesList) => {
const cacheEntriesListKeys = Object.keys(cacheEntriesList);
const cacheKeys = Object.keys(cacheEntriesList);

// Make sure that we cached files
expect(cacheEntriesListKeys.length).toBe(files.length);
cacheEntriesListKeys.forEach((cacheJSONEntry) => {
const cacheEntry = JSON.parse(cacheJSONEntry);
expect(cacheKeys.length).toBe(files.length);
cacheKeys.forEach((cacheEntry) => {
// eslint-disable-next-line no-new-func
const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)();

expect([cacheEntry.path, cacheEntry.input])
.toMatchSnapshot(`cache \`true\`: cached entry ${cacheEntry.path}`);
expect([cacheEntryOptions.path, cacheEntryOptions.input])
.toMatchSnapshot(`cache \`true\`: cached entry ${cacheEntryOptions.path}`);
});

// Reset compilation assets and mocks
Expand Down Expand Up @@ -374,15 +375,16 @@ describe('when options.cache', () => {
cacache
.ls(othercacheDir)
.then((cacheEntriesList) => {
const cacheEntriesListKeys = Object.keys(cacheEntriesList);
const cacheKeys = Object.keys(cacheEntriesList);

// Make sure that we cached files
expect(cacheEntriesListKeys.length).toBe(files.length);
cacheEntriesListKeys.forEach((cacheJSONEntry) => {
const cacheEntry = JSON.parse(cacheJSONEntry);
expect(cacheKeys.length).toBe(files.length);
cacheKeys.forEach((cacheEntry) => {
// eslint-disable-next-line no-new-func
const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)();

expect([cacheEntry.path, cacheEntry.input])
.toMatchSnapshot(`cache \`string\`: cached entry ${cacheEntry.path}`);
expect([cacheEntryOptions.path, cacheEntryOptions.input])
.toMatchSnapshot(`cache \`true\`: cached entry ${cacheEntryOptions.path}`);
});

// Reset compilation assets and mocks
Expand Down
35 changes: 0 additions & 35 deletions test/uglify/serialization.test.js

This file was deleted.

14 changes: 7 additions & 7 deletions test/uglify/worker.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import serialize from 'serialize-javascript';
import worker from '../../src/uglify/worker';
import { encode } from '../../src/uglify/serialization';

describe('matches snapshot', () => {
it('normalizes when options.extractComments is regex', () => {
Expand All @@ -8,7 +8,7 @@ describe('matches snapshot', () => {
input: 'var foo = 1;/* hello */',
extractComments: /foo/,
};
worker(JSON.stringify(options, encode), (error, data) => {
worker(serialize(options), (error, data) => {
if (error) {
throw error;
}
Expand All @@ -26,7 +26,7 @@ describe('matches snapshot', () => {
},
},
};
worker(JSON.stringify(options, encode), (error, data) => {
worker(serialize(options), (error, data) => {
if (error) {
throw error;
}
Expand All @@ -44,7 +44,7 @@ describe('matches snapshot', () => {
},
},
};
worker(JSON.stringify(options, encode), (error, data) => {
worker(serialize(options), (error, data) => {
if (error) {
throw error;
}
Expand All @@ -63,7 +63,7 @@ describe('matches snapshot', () => {
},
extractComments: 1,
};
worker(JSON.stringify(options, encode), (error, data) => {
worker(serialize(options), (error, data) => {
if (error) {
throw error;
}
Expand All @@ -90,7 +90,7 @@ describe('matches snapshot', () => {
},
},
};
worker(JSON.stringify(options, encode), (error, data) => {
worker(serialize(options), (error, data) => {
if (error) {
throw error;
}
Expand All @@ -109,7 +109,7 @@ describe('matches snapshot', () => {
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
},
};
worker(JSON.stringify(options, encode), (error, data) => {
worker(serialize(options), (error, data) => {
if (error) {
throw error;
}
Expand Down

0 comments on commit 8d7ebcd

Please sign in to comment.