Unverified Commit ca565fc6 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Adjust flying shuttle minify (#6980)

* Disable Terser plugin when using flying shuttle

* Add a new minify loader

* Downgrade terser

* Turn on minify loader

* Only compress source / mangle output
parent f04bd8bf
......@@ -8,7 +8,7 @@ import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
import { SERVER_DIRECTORY, REACT_LOADABLE_MANIFEST, CHUNK_GRAPH_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 { NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_DIST_CLIENT, PAGES_DIR_ALIAS, DOT_NEXT_ALIAS } from '../lib/constants'
import {TerserPlugin} from './webpack/plugins/terser-webpack-plugin/src/index'
import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
import { AllModulesIdentifiedPlugin } from './webpack/plugins/all-modules-identified-plugin'
......@@ -174,7 +174,8 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, debug =
minimizer: !(dev || debug) ? [
new TerserPlugin({...terserPluginConfig,
terserOptions: {
safari10: true
safari10: true,
...(__selectivePageBuilding ? { compress: false, mangle: true } : undefined)
}
})
] : undefined,
......@@ -223,6 +224,13 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, debug =
// @ts-ignore this is filtered
module: {
rules: [
__selectivePageBuilding && !isServer && {
test: /\.(js|mjs|jsx)$/,
use: {
loader: 'next-minify-loader',
options: { terserOptions: { safari10: true, compress: true, mangle: false } }
}
},
config.experimental.ampBindInitData && !isServer && {
test: /\.(js|mjs|jsx)$/,
include: [path.join(dir, 'data')],
......
import loaderUtils from 'loader-utils'
import { loader } from 'webpack'
import minify from '../plugins/terser-webpack-plugin/src/minify'
const nextMiniferLoader: loader.Loader = function(source) {
this.cacheable()
const options = loaderUtils.getOptions(this) || {}
const { error, code } = minify({
file: 'noop',
input: source,
terserOptions: { ...options.terserOptions, sourceMap: false },
})
if (error) {
this.callback(new Error(`Error from Terser: ${error.message}`))
return
}
this.callback(undefined, code)
return
}
export default nextMiniferLoader
......@@ -5,6 +5,7 @@ import workerFarm from 'worker-farm';
import { writeFile, readFile } from 'fs';
import findCacheDir from 'find-cache-dir';
import serialize from 'serialize-javascript';
import Sema from 'async-sema';
const worker = require.resolve('./worker');
const writeFileP = promisify(writeFile)
......@@ -16,6 +17,7 @@ export default class TaskRunner {
// In some cases cpus() returns undefined
// https://github.com/nodejs/node/issues/19022
this.maxConcurrentWorkers = cpus
this.sema = new Sema(cpus * 3)
}
run(tasks, callback) {
......@@ -55,6 +57,7 @@ export default class TaskRunner {
let toRun = tasks.length;
const results = [];
const step = (index, data) => {
this.sema.release();
toRun -= 1;
results[index] = data;
......@@ -65,7 +68,7 @@ export default class TaskRunner {
tasks.forEach((task, index) => {
const cachePath = join(
this.cacheDir,
this.cacheDir,
task.cacheKey
)
......@@ -84,13 +87,15 @@ export default class TaskRunner {
});
};
if (this.cacheDir) {
readFileP(cachePath, 'utf8')
.then(data => step(index, JSON.parse(data)))
.catch(() => enqueue())
} else {
enqueue();
}
this.sema.acquire().then(() => {
if (this.cacheDir) {
readFileP(cachePath, 'utf8')
.then(data => step(index, JSON.parse(data)))
.catch(() => enqueue())
} else {
enqueue();
}
})
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment