feat(v2): add ability to create unminimized bundles (#2474)

This commit is contained in:
Alexey Pyltsyn 2020-03-29 18:32:26 +03:00 committed by GitHub
parent a869d5d705
commit fa2e263ce8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 73 additions and 54 deletions

View file

@ -64,6 +64,7 @@ export interface StartCLIOptions {
export interface BuildCLIOptions { export interface BuildCLIOptions {
bundleAnalyzer: boolean; bundleAnalyzer: boolean;
outDir: string; outDir: string;
minify: boolean;
} }
export interface LoadContext { export interface LoadContext {

View file

@ -45,10 +45,15 @@ cli
'--out-dir <dir>', '--out-dir <dir>',
'The full path for the new output directory, relative to the current workspace (default = build).', 'The full path for the new output directory, relative to the current workspace (default = build).',
) )
.action((siteDir = '.', {bundleAnalyzer, outDir}) => { .option(
'--no-minify',
'Build website without minimizing JS bundles (default = false)',
)
.action((siteDir = '.', {bundleAnalyzer, outDir, minify}) => {
wrapCommand(build)(path.resolve(siteDir), { wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer, bundleAnalyzer,
outDir, outDir,
minify,
}); });
}); });

View file

@ -61,18 +61,21 @@ export async function build(
generatedFilesDir, generatedFilesDir,
'client-manifest.json', 'client-manifest.json',
); );
let clientConfig: Configuration = merge(createClientConfig(props), { let clientConfig: Configuration = merge(
plugins: [ createClientConfig(props, cliOptions.minify),
// Remove/clean build folders before building bundles. {
new CleanWebpackPlugin({verbose: false}), plugins: [
// Visualize size of webpack output files with an interactive zoomable treemap. // Remove/clean build folders before building bundles.
cliOptions.bundleAnalyzer && new BundleAnalyzerPlugin(), new CleanWebpackPlugin({verbose: false}),
// Generate client manifests file that will be used for server bundle. // Visualize size of webpack output files with an interactive zoomable treemap.
new ReactLoadableSSRAddon({ cliOptions.bundleAnalyzer && new BundleAnalyzerPlugin(),
filename: clientManifestPath, // Generate client manifests file that will be used for server bundle.
}), new ReactLoadableSSRAddon({
].filter(Boolean) as Plugin[], filename: clientManifestPath,
}); }),
].filter(Boolean) as Plugin[],
},
);
let serverConfig: Configuration = createServerConfig(props); let serverConfig: Configuration = createServerConfig(props);

View file

@ -34,11 +34,13 @@ export function excludeJS(modulePath: string) {
export function createBaseConfig( export function createBaseConfig(
props: Props, props: Props,
isServer: boolean, isServer: boolean,
minify: boolean,
): Configuration { ): Configuration {
const {outDir, siteDir, baseUrl, generatedFilesDir, routesPaths} = props; const {outDir, siteDir, baseUrl, generatedFilesDir, routesPaths} = props;
const totalPages = routesPaths.length; const totalPages = routesPaths.length;
const isProd = process.env.NODE_ENV === 'production'; const isProd = process.env.NODE_ENV === 'production';
return { return {
mode: isProd ? 'production' : 'development', mode: isProd ? 'production' : 'development',
output: { output: {
@ -78,45 +80,46 @@ export function createBaseConfig(
optimization: { optimization: {
removeAvailableModules: false, removeAvailableModules: false,
// Only minimize client bundle in production because server bundle is only used for static site generation // Only minimize client bundle in production because server bundle is only used for static site generation
minimize: isProd && !isServer, minimize: minify && isProd && !isServer,
minimizer: isProd minimizer:
? [ minify && isProd
new TerserPlugin({ ? [
cache: true, new TerserPlugin({
parallel: true, cache: true,
sourceMap: false, parallel: true,
terserOptions: { sourceMap: false,
parse: { terserOptions: {
// we want uglify-js to parse ecma 8 code. However, we don't want it parse: {
// to apply any minfication steps that turns valid ecma 5 code // we want uglify-js to parse ecma 8 code. However, we don't want it
// into invalid ecma 5 code. This is why the 'compress' and 'output' // to apply any minfication steps that turns valid ecma 5 code
// sections only apply transformations that are ecma 5 safe // into invalid ecma 5 code. This is why the 'compress' and 'output'
// https://github.com/facebook/create-react-app/pull/4234 // sections only apply transformations that are ecma 5 safe
ecma: 8, // https://github.com/facebook/create-react-app/pull/4234
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
}, },
compress: { }),
ecma: 5, new OptimizeCSSAssetsPlugin({
warnings: false, cssProcessorPluginOptions: {
preset: 'default',
}, },
mangle: { }),
safari10: true, ]
}, : undefined,
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
},
}),
new OptimizeCSSAssetsPlugin({
cssProcessorPluginOptions: {
preset: 'default',
},
}),
]
: undefined,
splitChunks: isServer splitChunks: isServer
? false ? false
: { : {

View file

@ -15,10 +15,13 @@ import {createBaseConfig} from './base';
import ChunkAssetPlugin from './plugins/ChunkAssetPlugin'; import ChunkAssetPlugin from './plugins/ChunkAssetPlugin';
import LogPlugin from './plugins/LogPlugin'; import LogPlugin from './plugins/LogPlugin';
export function createClientConfig(props: Props): Configuration { export function createClientConfig(
props: Props,
minify: boolean = true,
): Configuration {
const isProd = process.env.NODE_ENV === 'production'; const isProd = process.env.NODE_ENV === 'production';
const isBuilding = process.argv[2] === 'build'; const isBuilding = process.argv[2] === 'build';
const config = createBaseConfig(props, false); const config = createBaseConfig(props, false, minify);
const clientConfig = merge(config, { const clientConfig = merge(config, {
entry: [ entry: [

View file

@ -15,7 +15,10 @@ import {createBaseConfig} from './base';
import WaitPlugin from './plugins/WaitPlugin'; import WaitPlugin from './plugins/WaitPlugin';
import LogPlugin from './plugins/LogPlugin'; import LogPlugin from './plugins/LogPlugin';
export function createServerConfig(props: Props): Configuration { export function createServerConfig(
props: Props,
minify: boolean = true,
): Configuration {
const { const {
baseUrl, baseUrl,
routesPaths, routesPaths,
@ -24,7 +27,7 @@ export function createServerConfig(props: Props): Configuration {
preBodyTags, preBodyTags,
postBodyTags, postBodyTags,
} = props; } = props;
const config = createBaseConfig(props, true); const config = createBaseConfig(props, true, minify);
const routesLocation = {}; const routesLocation = {};
// Array of paths to be rendered. Relative to output directory // Array of paths to be rendered. Relative to output directory

View file

@ -65,6 +65,7 @@ Compiles your site for production.
| --- | --- | --- | | --- | --- | --- |
| `--bundle-analyzer` | | Analyze your bundle with [bundle analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) | | `--bundle-analyzer` | | Analyze your bundle with [bundle analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) |
| `--out-dir` | `build` | The full path for the new output directory, relative to the current workspace. | | `--out-dir` | `build` | The full path for the new output directory, relative to the current workspace. |
| `--no-minify` | `false` | Build website without minimizing JS/CSS bundles. |
### `docusaurus swizzle` ### `docusaurus swizzle`