Skip to content

Commit

Permalink
Bring in terser-webpack-plugin (backport #6231 to master) (#6232)
Browse files Browse the repository at this point in the history
* Bring in terser-webpack-plugin (backport #6231 to master)

* Use correct path for ignore

* Comment out schema
  • Loading branch information
timneutkens committed Feb 10, 2019
1 parent ab46c45 commit 155423f
Show file tree
Hide file tree
Showing 9 changed files with 937 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/webpack.js
Expand Up @@ -19,7 +19,7 @@ import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
import {SERVER_DIRECTORY, NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_NODE_MODULES, NEXT_PROJECT_ROOT_DIST, DEFAULT_PAGES_DIR, REACT_LOADABLE_MANIFEST, CLIENT_STATIC_FILES_RUNTIME_WEBPACK, CLIENT_STATIC_FILES_RUNTIME_MAIN} from '../lib/constants'
import AutoDllPlugin from 'autodll-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin'
import TerserPlugin from './webpack/plugins/terser-webpack-plugin/src/cjs.js'

// The externals config makes sure that
// on the server side when modules are
Expand Down
20 changes: 20 additions & 0 deletions build/webpack/plugins/terser-webpack-plugin/LICENSE
@@ -0,0 +1,20 @@
Copyright JS Foundation and other contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
106 changes: 106 additions & 0 deletions build/webpack/plugins/terser-webpack-plugin/src/TaskRunner.js
@@ -0,0 +1,106 @@
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';

const worker = require.resolve('./worker');

export default class TaskRunner {
constructor(options = {}) {
const { cache, parallel } = options;
this.cacheDir =
cache === true ? findCacheDir({ name: 'terser-webpack-plugin' }) : cache;
// In some cases cpus() returns undefined
// https://github.com/nodejs/node/issues/19022
const cpus = os.cpus() || { length: 1 };
this.maxConcurrentWorkers =
parallel === true
? cpus.length - 1
: Math.min(Number(parallel) || 0, cpus.length - 1);
}

run(tasks, callback) {
/* istanbul ignore if */
if (!tasks.length) {
callback(null, []);
return;
}

if (this.maxConcurrentWorkers > 1) {
const workerOptions =
process.platform === 'win32'
? {
maxConcurrentWorkers: this.maxConcurrentWorkers,
maxConcurrentCallsPerWorker: 1,
}
: { maxConcurrentWorkers: this.maxConcurrentWorkers };
this.workers = workerFarm(workerOptions, worker);
this.boundWorkers = (options, cb) => {
try {
this.workers(serialize(options), cb);
} catch (error) {
// worker-farm can fail with ENOMEM or something else
cb(error);
}
};
} else {
this.boundWorkers = (options, cb) => {
try {
cb(null, minify(options));
} catch (error) {
cb(error);
}
};
}

let toRun = tasks.length;
const results = [];
const step = (index, data) => {
toRun -= 1;
results[index] = data;

if (!toRun) {
callback(null, results);
}
};

tasks.forEach((task, index) => {
const enqueue = () => {
this.boundWorkers(task, (error, data) => {
const result = error ? { error } : data;
const done = () => step(index, result);

if (this.cacheDir && !result.error) {
cacache
.put(
this.cacheDir,
serialize(task.cacheKeys),
JSON.stringify(data)
)
.then(done, done);
} else {
done();
}
});
};

if (this.cacheDir) {
cacache
.get(this.cacheDir, serialize(task.cacheKeys))
.then(({ data }) => step(index, JSON.parse(data)), enqueue);
} else {
enqueue();
}
});
}

exit() {
if (this.workers) {
workerFarm.end(this.workers);
}
}
}
3 changes: 3 additions & 0 deletions build/webpack/plugins/terser-webpack-plugin/src/cjs.js
@@ -0,0 +1,3 @@
const plugin = require('./index');

module.exports = plugin.default;

0 comments on commit 155423f

Please sign in to comment.