From d911db4105d9f599406ef43c7b4048c67a09f678 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 16 May 2020 22:59:11 +0200 Subject: [PATCH] packages/cli: rename bundle loaders to transforms and include plugins --- packages/cli/src/lib/bundle/config.ts | 12 ++----- .../lib/bundle/{loaders.ts => transforms.ts} | 33 +++++++++++++++++-- 2 files changed, 33 insertions(+), 12 deletions(-) rename packages/cli/src/lib/bundle/{loaders.ts => transforms.ts} (70%) diff --git a/packages/cli/src/lib/bundle/config.ts b/packages/cli/src/lib/bundle/config.ts index 7fe6f2e585..cdd16fec2e 100644 --- a/packages/cli/src/lib/bundle/config.ts +++ b/packages/cli/src/lib/bundle/config.ts @@ -15,11 +15,10 @@ */ import webpack from 'webpack'; -import HtmlWebpackPlugin from 'html-webpack-plugin'; import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; import ModuleScopePlugin from 'react-dev-utils/ModuleScopePlugin'; import { BundlingPaths } from './paths'; -import { loaders } from './loaders'; +import { transforms } from './transforms'; import { optimization } from './optimization'; import { BundlingOptions } from './types'; // import checkRequiredFiles from 'react-dev-utils/checkRequiredFiles'; @@ -34,12 +33,7 @@ export function createConfig( ): webpack.Configuration { const { checksEnabled, isDev } = options; - const plugins = [ - new HtmlWebpackPlugin({ - template: paths.targetHtml, - }), - new webpack.HotModuleReplacementPlugin(), - ]; + const { plugins, loaders } = transforms(paths, options); if (checksEnabled) { plugins.push( @@ -78,7 +72,7 @@ export function createConfig( }, }, module: { - rules: loaders(), + rules: loaders, }, output: { path: paths.targetDist, diff --git a/packages/cli/src/lib/bundle/loaders.ts b/packages/cli/src/lib/bundle/transforms.ts similarity index 70% rename from packages/cli/src/lib/bundle/loaders.ts rename to packages/cli/src/lib/bundle/transforms.ts index 730ba2496a..ad7cdd4f25 100644 --- a/packages/cli/src/lib/bundle/loaders.ts +++ b/packages/cli/src/lib/bundle/transforms.ts @@ -14,10 +14,23 @@ * limitations under the License. */ -import { Module } from 'webpack'; +import webpack, { Module, Plugin } from 'webpack'; +import HtmlWebpackPlugin from 'html-webpack-plugin'; +import { BundlingOptions } from './types'; +import { BundlingPaths } from './paths'; -export const loaders = (): Module['rules'] => { - return [ +type Transforms = { + loaders: Module['rules']; + plugins: Plugin[]; +}; + +export const transforms = ( + paths: BundlingPaths, + options: BundlingOptions, +): Transforms => { + const { isDev } = options; + + const loaders = [ { test: /\.(tsx?)$/, exclude: /node_modules/, @@ -55,4 +68,18 @@ export const loaders = (): Module['rules'] => { use: [require.resolve('style-loader'), require.resolve('css-loader')], }, ]; + + const plugins = new Array(); + + plugins.push( + new HtmlWebpackPlugin({ + template: paths.targetHtml, + }), + ); + + if (isDev) { + plugins.push(new webpack.HotModuleReplacementPlugin()); + } + + return { loaders, plugins }; };