Skip to content

Commit

Permalink
feat(v2): add ability to create unminimized bundles (#2474)
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 authored Mar 29, 2020
1 parent a869d5d commit fa2e263
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 54 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface StartCLIOptions {
export interface BuildCLIOptions {
bundleAnalyzer: boolean;
outDir: string;
minify: boolean;
}

export interface LoadContext {
Expand Down
7 changes: 6 additions & 1 deletion packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ cli
'--out-dir <dir>',
'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), {
bundleAnalyzer,
outDir,
minify,
});
});

Expand Down
27 changes: 15 additions & 12 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,21 @@ export async function build(
generatedFilesDir,
'client-manifest.json',
);
let clientConfig: Configuration = merge(createClientConfig(props), {
plugins: [
// Remove/clean build folders before building bundles.
new CleanWebpackPlugin({verbose: false}),
// Visualize size of webpack output files with an interactive zoomable treemap.
cliOptions.bundleAnalyzer && new BundleAnalyzerPlugin(),
// Generate client manifests file that will be used for server bundle.
new ReactLoadableSSRAddon({
filename: clientManifestPath,
}),
].filter(Boolean) as Plugin[],
});
let clientConfig: Configuration = merge(
createClientConfig(props, cliOptions.minify),
{
plugins: [
// Remove/clean build folders before building bundles.
new CleanWebpackPlugin({verbose: false}),
// Visualize size of webpack output files with an interactive zoomable treemap.
cliOptions.bundleAnalyzer && new BundleAnalyzerPlugin(),
// Generate client manifests file that will be used for server bundle.
new ReactLoadableSSRAddon({
filename: clientManifestPath,
}),
].filter(Boolean) as Plugin[],
},
);

let serverConfig: Configuration = createServerConfig(props);

Expand Down
77 changes: 40 additions & 37 deletions packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export function excludeJS(modulePath: string) {
export function createBaseConfig(
props: Props,
isServer: boolean,
minify: boolean,
): Configuration {
const {outDir, siteDir, baseUrl, generatedFilesDir, routesPaths} = props;

const totalPages = routesPaths.length;
const isProd = process.env.NODE_ENV === 'production';

return {
mode: isProd ? 'production' : 'development',
output: {
Expand Down Expand Up @@ -78,45 +80,46 @@ export function createBaseConfig(
optimization: {
removeAvailableModules: false,
// Only minimize client bundle in production because server bundle is only used for static site generation
minimize: isProd && !isServer,
minimizer: isProd
? [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: false,
terserOptions: {
parse: {
// we want uglify-js to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
},
mangle: {
safari10: true,
minimize: minify && isProd && !isServer,
minimizer:
minify && isProd
? [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: false,
terserOptions: {
parse: {
// we want uglify-js to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// 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,
},
},
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',
},
},
}),
new OptimizeCSSAssetsPlugin({
cssProcessorPluginOptions: {
preset: 'default',
},
}),
]
: undefined,
}),
]
: undefined,
splitChunks: isServer
? false
: {
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus/src/webpack/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import {createBaseConfig} from './base';
import ChunkAssetPlugin from './plugins/ChunkAssetPlugin';
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 isBuilding = process.argv[2] === 'build';
const config = createBaseConfig(props, false);
const config = createBaseConfig(props, false, minify);

const clientConfig = merge(config, {
entry: [
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import {createBaseConfig} from './base';
import WaitPlugin from './plugins/WaitPlugin';
import LogPlugin from './plugins/LogPlugin';

export function createServerConfig(props: Props): Configuration {
export function createServerConfig(
props: Props,
minify: boolean = true,
): Configuration {
const {
baseUrl,
routesPaths,
Expand All @@ -24,7 +27,7 @@ export function createServerConfig(props: Props): Configuration {
preBodyTags,
postBodyTags,
} = props;
const config = createBaseConfig(props, true);
const config = createBaseConfig(props, true, minify);

const routesLocation = {};
// Array of paths to be rendered. Relative to output directory
Expand Down
1 change: 1 addition & 0 deletions website/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
| `--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`

Expand Down

0 comments on commit fa2e263

Please sign in to comment.