Skip to content

Commit

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

* Ignore terser from linting
  • Loading branch information
timneutkens authored Feb 10, 2019
1 parent 693ab43 commit 45f5663
Show file tree
Hide file tree
Showing 10 changed files with 937 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"packages/**/*.ts",
"**/*.d.ts",
"**/node_modules/**",
"packages/next/build/webpack/plugins/terser-webpack-plugin/**",
"examples/with-ioc/**",
"examples/with-kea/**",
"examples/with-mobx/**"
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
import {SERVER_DIRECTORY, REACT_LOADABLE_MANIFEST, CLIENT_STATIC_FILES_RUNTIME_WEBPACK, CLIENT_STATIC_FILES_RUNTIME_MAIN} from 'next-server/constants'
import {NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_NODE_MODULES, NEXT_PROJECT_ROOT_DIST_CLIENT, PAGES_DIR_ALIAS, DOT_NEXT_ALIAS} 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'
import {ServerlessPlugin} from './webpack/plugins/serverless-plugin'

// The externals config makes sure that
Expand Down
20 changes: 20 additions & 0 deletions packages/next/build/webpack/plugins/terser-webpack-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const plugin = require('./index');

module.exports = plugin.default;
Loading

0 comments on commit 45f5663

Please sign in to comment.