@@ -15,11 +15,15 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
1515const CaseSensitivePathsPlugin = require ( 'case-sensitive-paths-webpack-plugin' ) ;
1616const InterpolateHtmlPlugin = require ( 'react-dev-utils/InterpolateHtmlPlugin' ) ;
1717const WatchMissingNodeModulesPlugin = require ( 'react-dev-utils/WatchMissingNodeModulesPlugin' ) ;
18+ // const eslintFormatter = require('react-dev-utils/eslintFormatter');
1819const ModuleScopePlugin = require ( 'react-dev-utils/ModuleScopePlugin' ) ;
20+ const getCSSModuleLocalIdent = require ( 'react-dev-utils/getCSSModuleLocalIdent' ) ;
1921const getClientEnvironment = require ( './env' ) ;
2022const paths = require ( './paths' ) ;
23+
2124const HardSourceWebpackPlugin = require ( 'hard-source-webpack-plugin' ) ;
2225const TsCheckerWebpackPlugin = require ( 'ts-checker-webpack-plugin' ) ;
26+ const TsconfigPathsPlugin = require ( 'tsconfig-paths-webpack-plugin' ) ;
2327
2428// Webpack uses `publicPath` to determine where the app is being served from.
2529// In development, we always serve from the root. This makes config easier.
@@ -31,10 +35,50 @@ const publicUrl = '';
3135// Get environment variables to inject into our app.
3236const env = getClientEnvironment ( publicUrl ) ;
3337
38+
39+ // style files regexes
40+ const cssRegex = / \. c s s $ / ;
41+ const cssModuleRegex = / \. m o d u l e \. c s s $ / ;
42+ const sassRegex = / \. ( s c s s | s a s s ) $ / ;
43+ const sassModuleRegex = / \. m o d u l e \. ( s c s s | s a s s ) $ / ;
44+
45+ // common function to get style loaders
46+ const getStyleLoaders = ( cssOptions , preProcessor ) => {
47+ const loaders = [
48+ require . resolve ( 'style-loader' ) ,
49+ {
50+ loader : require . resolve ( 'css-loader' ) ,
51+ options : cssOptions ,
52+ } ,
53+ {
54+ // Options for PostCSS as we reference these options twice
55+ // Adds vendor prefixing based on your specified browser support in
56+ // package.json
57+ loader : require . resolve ( 'postcss-loader' ) ,
58+ options : {
59+ // Necessary for external CSS imports to work
60+ // https://github.com/facebook/create-react-app/issues/2677
61+ ident : 'postcss' ,
62+ plugins : ( ) => [
63+ require ( 'postcss-flexbugs-fixes' ) ,
64+ autoprefixer ( {
65+ flexbox : 'no-2009' ,
66+ } ) ,
67+ ] ,
68+ } ,
69+ } ,
70+ ] ;
71+ if ( preProcessor ) {
72+ loaders . push ( require . resolve ( preProcessor ) ) ;
73+ }
74+ return loaders ;
75+ } ;
76+
3477// This is the development configuration.
3578// It is focused on developer experience and fast rebuilds.
3679// The production configuration is different and lives in a separate file.
3780module . exports = {
81+ mode : 'development' ,
3882 // You may want 'eval' instead if you prefer to see the compiled output in DevTools.
3983 // See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
4084 devtool : 'cheap-module-source-map' ,
@@ -125,21 +169,36 @@ module.exports = {
125169 // please link the files into your node_modules/ and let module-resolution kick in.
126170 // Make sure your source files are compiled, as they will not be processed in any way.
127171 new ModuleScopePlugin ( paths . appSrc , [ paths . appPackageJson ] ) ,
128- // new TsconfigPathsPlugin({ configFile: paths.appTsConfig }),
172+ new TsconfigPathsPlugin ( { configFile : paths . appTsConfig } ) ,
129173 ] ,
130174 } ,
131175 module : {
132176 strictExportPresence : true ,
133177 rules : [
134- // TODO: Disable require.ensure as it's not a standard language feature.
135- // We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
136- // { parser: { requireEnsure: false } },
178+ // Disable require.ensure as it's not a standard language feature.
179+ { parser : { requireEnsure : false } } ,
137180
138181 {
139182 test : / \. ( j s | j s x | m j s ) $ / ,
140- loader : require . resolve ( 'source-map-loader' ) ,
141183 enforce : 'pre' ,
142- include : paths . appSrc ,
184+ use : [
185+ // {
186+ // options: {
187+ // formatter: eslintFormatter,
188+ // eslintPath: require.resolve('eslint'),
189+ // baseConfig: {
190+ // extends: [require.resolve('eslint-config-react-app')],
191+ // },
192+ // // @remove -on-eject-begin
193+ // ignore: false,
194+ // useEslintrc: false,
195+ // // @remove -on-eject-end
196+ // },
197+ // loader: require.resolve('eslint-loader'),
198+ // },
199+ ] ,
200+ include : paths . srcPaths ,
201+ exclude : [ / [ / \\ \\ ] n o d e _ m o d u l e s [ / \\ \\ ] / ] ,
143202 } ,
144203 {
145204 // "oneOf" will traverse all following loaders until one will
@@ -159,18 +218,57 @@ module.exports = {
159218 } ,
160219 {
161220 test : / \. ( j s | j s x | m j s ) $ / ,
162- include : paths . appSrc ,
163- loader : require . resolve ( 'babel-loader' ) ,
164- options : {
165- // @remove -on-eject-begin
166- babelrc : false ,
167- presets : [ require . resolve ( 'babel-preset-react-app' ) ] ,
168- // @remove -on-eject-end
169- compact : true ,
170- } ,
221+ include : paths . srcPaths ,
222+ exclude : [ / [ / \\ \\ ] n o d e _ m o d u l e s [ / \\ \\ ] / ] ,
223+ use : [
224+ // This loader parallelizes code compilation, it is optional but
225+ // improves compile time on larger projects
226+ require . resolve ( 'thread-loader' ) ,
227+ {
228+ loader : require . resolve ( 'babel-loader' ) ,
229+ options : {
230+ // @remove -on-eject-begin
231+ babelrc : false ,
232+ // @remove -on-eject-end
233+ presets : [ require . resolve ( '@babel/preset-react' ) ] ,
234+ plugins : [
235+ [
236+ require . resolve ( 'babel-plugin-named-asset-import' ) ,
237+ ] ,
238+ ] ,
239+ // This is a feature of `babel-loader` for webpack (not Babel itself).
240+ // It enables caching results in ./node_modules/.cache/babel-loader/
241+ // directory for faster rebuilds.
242+ cacheDirectory : true ,
243+ highlightCode : true ,
244+ } ,
245+ } ,
246+ ] ,
247+ } ,
248+ // Process any JS outside of the app with Babel.
249+ // Unlike the application JS, we only compile the standard ES features.
250+ {
251+ test : / \. j s $ / ,
252+ use : [
253+ // This loader parallelizes code compilation, it is optional but
254+ // improves compile time on larger projects
255+ require . resolve ( 'thread-loader' ) ,
256+ {
257+ loader : require . resolve ( 'babel-loader' ) ,
258+ options : {
259+ babelrc : false ,
260+ compact : false ,
261+ presets : [
262+ require . resolve ( '@babel/preset-react' ) ,
263+ ] ,
264+ cacheDirectory : true ,
265+ highlightCode : true ,
266+ } ,
267+ } ,
268+ ] ,
171269 } ,
172270
173- // Compile .tsx?
271+ // Compile .tsx
174272 {
175273 test : / \. ( t s | t s x ) $ / ,
176274 include : paths . appSrc ,
@@ -181,7 +279,7 @@ module.exports = {
181279 // disable type checker - we will use it in fork plugin
182280 useCache : true ,
183281 reportFiles : [
184- "src /**/*.{ts,tsx}"
282+ paths . appSrc + ' /**/*.{ts,tsx}'
185283 ] ,
186284 forceIsolatedModules : true
187285 } ,
@@ -193,83 +291,54 @@ module.exports = {
193291 // "style" loader turns CSS into JS modules that inject <style> tags.
194292 // In production, we use a plugin to extract that CSS to a file, but
195293 // in development "style" loader enables hot editing of CSS.
294+ // By default we support CSS Modules with the extension .module.css
196295 {
197- test : / \. c s s $ / ,
198- use : [
199- require . resolve ( 'style-loader' ) ,
200- {
201- loader : require . resolve ( 'css-loader' ) ,
202- options : {
203- importLoaders : 1 ,
204- } ,
205- } ,
206- {
207- loader : require . resolve ( 'postcss-loader' ) ,
208- options : {
209- // Necessary for external CSS imports to work
210- // https://github.com/facebookincubator/create-react-app/issues/2677
211- ident : 'postcss' ,
212- plugins : ( ) => [
213- require ( 'postcss-flexbugs-fixes' ) ,
214- require ( 'autoprefixer' ) ( {
215- flexbox : 'no-2009' ,
216- } ) ,
217- ] ,
218- } ,
219- } ,
220- ] ,
296+ test : cssRegex ,
297+ exclude : cssModuleRegex ,
298+ use : getStyleLoaders ( {
299+ importLoaders : 1 ,
300+ } ) ,
221301 } ,
302+ // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
303+ // using the extension .module.css
222304 {
223- test : / \. s c s s $ / ,
224- use : [
225- require . resolve ( 'style-loader' ) ,
226- {
227- loader : require . resolve ( 'css-loader' ) ,
228- options : {
229- importLoaders : 1
230- } ,
231- } ,
305+ test : cssModuleRegex ,
306+ use : getStyleLoaders ( {
307+ importLoaders : 1 ,
308+ modules : true ,
309+ getLocalIdent : getCSSModuleLocalIdent ,
310+ } ) ,
311+ } ,
312+ // Opt-in support for SASS (using .scss or .sass extensions).
313+ // Chains the sass-loader with the css-loader and the style-loader
314+ // to immediately apply all styles to the DOM.
315+ // By default we support SASS Modules with the
316+ // extensions .module.scss or .module.sass
317+ {
318+ test : sassRegex ,
319+ exclude : sassModuleRegex ,
320+ use : getStyleLoaders ( { importLoaders : 2 } , 'sass-loader' ) ,
321+ } ,
322+ // Adds support for CSS Modules, but using SASS
323+ // using the extension .module.scss or .module.sass
324+ {
325+ test : sassModuleRegex ,
326+ use : getStyleLoaders (
232327 {
233- loader : require . resolve ( 'postcss-loader' ) ,
234- options : {
235- // Necessary for external CSS imports to work
236- // https://github.com/facebookincubator/create-react-app/issues/2677
237- ident : 'postcss' ,
238- plugins : ( ) => [
239- require ( 'postcss-flexbugs-fixes' ) ,
240- autoprefixer ( {
241- browsers : [
242- '>1%' ,
243- 'last 4 versions' ,
244- 'Firefox ESR' ,
245- 'not ie < 9' , // React doesn't support IE8 anyway
246- ] ,
247- flexbox : 'no-2009' ,
248- } ) ,
249- ] ,
250- } ,
328+ importLoaders : 2 ,
329+ modules : true ,
330+ getLocalIdent : getCSSModuleLocalIdent ,
251331 } ,
252- {
253- loader : 'sass-loader' ,
254- options : {
255- includePaths : [ paths . appSrc ]
256- }
257- }
258- ] ,
332+ 'sass-loader'
333+ ) ,
259334 } ,
260335 // Svgs
261336 {
262337 test : / \. s v g $ / ,
263338 issuer : / \. t s x ? $ / ,
264- use : [
265- require . resolve ( 'babel-loader' ) ,
266- {
267- loader : require . resolve ( 'react-svg-loader' ) ,
268- options : {
269- jsx : true // true outputs JSX tags
270- }
271- }
272- ]
339+ use : [ {
340+ loader : require . resolve ( '@svgr/webpack' )
341+ } ]
273342 } ,
274343 // "file" loader makes sure those assets get served by WebpackDevServer.
275344 // When you `import` an asset, you get its (virtual) filename.
@@ -294,31 +363,29 @@ module.exports = {
294363 ] ,
295364 } ,
296365 plugins : [
297- // Makes some environment variables available in index.html.
298- // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
299- // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
300- // In development, this will be an empty string.
301- new InterpolateHtmlPlugin ( env . raw ) ,
302366 // Generates an `index.html` file with the <script> injected.
303367 new HtmlWebpackPlugin ( {
304368 inject : true ,
305369 template : paths . appHtml ,
306370 } ) ,
307- // Add module names to factory functions so they appear in browser profiler.
308- new webpack . NamedModulesPlugin ( ) ,
371+ // Makes some environment variables available in index.html.
372+ // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
373+ // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
374+ // In development, this will be an empty string.
375+ new InterpolateHtmlPlugin ( env . raw ) ,
309376 // Makes some environment variables available to the JS code, for example:
310377 // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
311378 new webpack . DefinePlugin ( env . stringified ) ,
312379 // This is necessary to emit hot updates (currently CSS only):
313380 new webpack . HotModuleReplacementPlugin ( ) ,
314381 // Watcher doesn't work well if you mistype casing in a path so we use
315382 // a plugin that prints an error when you attempt to do this.
316- // See https://github.com/facebookincubator /create-react-app/issues/240
383+ // See https://github.com/facebook /create-react-app/issues/240
317384 new CaseSensitivePathsPlugin ( ) ,
318385 // If you require a missing module and then `npm install` it, you still have
319386 // to restart the development server for Webpack to discover it. This plugin
320387 // makes the discovery automatic so you don't have to restart.
321- // See https://github.com/facebookincubator /create-react-app/issues/186
388+ // See https://github.com/facebook /create-react-app/issues/186
322389 new WatchMissingNodeModulesPlugin ( paths . appNodeModules ) ,
323390 // Moment.js is an extremely popular library that bundles large locale files
324391 // by default due to how Webpack interprets its code. This is a practical
@@ -341,10 +408,7 @@ module.exports = {
341408 tls : 'empty' ,
342409 child_process : 'empty' ,
343410 } ,
344- // Turn off performance hints during development because we don't do any
345- // splitting or minification in interest of speed. These warnings become
346- // cumbersome.
347- performance : {
348- hints : false ,
349- } ,
411+ // Turn off performance processing because we utilize
412+ // our own hints via the FileSizeReporter
413+ performance : false ,
350414} ;
0 commit comments