From 84c3e0a045a5c165766ad1b44a68cf8f4075b22b Mon Sep 17 00:00:00 2001 From: Endilie Yacop Sucipto Date: Mon, 8 Apr 2019 01:40:38 +0700 Subject: [PATCH] refactor(v2): simplify webpack stuff (#1342) * refactor(v2): webpack stuff * nits * nits test --- packages/docusaurus/lib/commands/build.js | 19 +- packages/docusaurus/lib/commands/start.js | 35 +-- packages/docusaurus/lib/webpack/base.js | 231 ++++++++---------- packages/docusaurus/lib/webpack/client.js | 57 ++--- packages/docusaurus/lib/webpack/server.js | 73 +++--- packages/docusaurus/lib/webpack/utils.js | 105 ++++---- packages/docusaurus/package.json | 1 - packages/docusaurus/test/webpack/base.test.js | 4 +- .../docusaurus/test/webpack/client.test.js | 4 +- .../docusaurus/test/webpack/server.test.js | 4 +- .../docusaurus/test/webpack/utils.test.js | 37 +-- yarn.lock | 18 -- 12 files changed, 254 insertions(+), 334 deletions(-) diff --git a/packages/docusaurus/lib/commands/build.js b/packages/docusaurus/lib/commands/build.js index 68208b8c07..6035f5b08e 100644 --- a/packages/docusaurus/lib/commands/build.js +++ b/packages/docusaurus/lib/commands/build.js @@ -6,6 +6,7 @@ */ const webpack = require('webpack'); +const merge = require('webpack-merge'); const CleanWebpackPlugin = require('clean-webpack-plugin'); const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer'); const path = require('path'); @@ -48,16 +49,16 @@ module.exports = async function build(siteDir, cliOptions = {}) { // Apply user webpack config. const {outDir, plugins} = props; - const clientConfigObj = createClientConfig(props); - // Remove/clean build folders before building bundles. - clientConfigObj.plugin('clean').use(CleanWebpackPlugin, [{verbose: false}]); - // Visualize size of webpack output files with an interactive zoomable treemap. - if (cliOptions.bundleAnalyzer) { - clientConfigObj.plugin('bundleAnalyzer').use(BundleAnalyzerPlugin); - } + let clientConfig = 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(), + ].filter(Boolean), + }); - let clientConfig = clientConfigObj.toConfig(); - let serverConfig = createServerConfig(props).toConfig(); + let serverConfig = createServerConfig(props); // Plugin lifecycle - configureWebpack plugins.forEach(plugin => { diff --git a/packages/docusaurus/lib/commands/start.js b/packages/docusaurus/lib/commands/start.js index fd9ae65b89..12b5c573d9 100644 --- a/packages/docusaurus/lib/commands/start.js +++ b/packages/docusaurus/lib/commands/start.js @@ -17,6 +17,7 @@ const {prepareUrls} = require('react-dev-utils/WebpackDevServerUtils'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin'); const WebpackDevServer = require('webpack-dev-server'); +const merge = require('webpack-merge'); const {normalizeUrl} = require('@docusaurus/utils'); const load = require('../load'); const loadConfig = require('../load/config'); @@ -83,24 +84,24 @@ module.exports = async function start(siteDir, cliOptions = {}) { const openUrl = normalizeUrl([urls.localUrlForBrowser, baseUrl]); // Create compiler from generated webpack config. - let config = createClientConfig(props); - const {siteConfig, plugins = []} = props; - config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [ - { - inject: false, - hash: true, - template: path.resolve( - __dirname, - '../core/templates/index.html.template.ejs', - ), - filename: 'index.html', - title: siteConfig.title, - }, - ]); - // Needed for hot reload. - config.plugin('hmr').use(HotModuleReplacementPlugin); - config = config.toConfig(); + let config = merge(createClientConfig(props), { + plugins: [ + // Generates an `index.html` file with the