@@ -17,14 +17,24 @@ const CopyPlugin = require('copy-webpack-plugin');
1717const { BundleAnalyzerPlugin } = require ( 'webpack-bundle-analyzer' ) ;
1818const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' ) ;
1919const getCSSModuleLocalIdent = require ( 'react-dev-utils/getCSSModuleLocalIdent' ) ;
20+ const dotenv = require ( 'dotenv' ) ;
21+ const dotenvExpand = require ( 'dotenv-expand' ) ;
22+ // const { TimeAnalyticsPlugin } = require('time-analytics-webpack-plugin');
23+
24+ // Load the current .env and expand it
25+ const parsedEnv = dotenvExpand . expand ( dotenv . config ( ) ) ;
2026
2127// style files regexes
2228const cssRegex = / \. c s s $ / ;
2329const cssModuleRegex = / \. m o d u l e \. c s s $ / ;
2430const sassRegex = / \. ( s c s s | s a s s ) $ / ;
2531const sassModuleRegex = / \. m o d u l e \. ( s c s s | s a s s ) $ / ;
2632
27- module . exports = ( env , argv ) => {
33+ module . exports = ( webpackEnv , argv ) => {
34+ const env = {
35+ ...( parsedEnv . parsed || { } ) ,
36+ ...( webpackEnv || { } ) ,
37+ } ;
2838 const { mode } = argv ;
2939 const isEnvDevelopment = mode === 'development' ;
3040 const isEnvProduction = mode === 'production' ;
@@ -105,6 +115,15 @@ module.exports = (env, argv) => {
105115 * }
106116 */
107117
118+ try {
119+ // If a visynWebpackOverride.js file exists in the default app, it will be used to override the visyn configuration.
120+ const visynWebpackOverride = require ( path . join ( defaultAppPath , 'visynWebpackOverride.js' ) ) ( { env } ) || { } ;
121+ console . log ( 'Using visynWebpackOverride.js file to override visyn configuration.' ) ;
122+ Object . assign ( appPkg . visyn , visynWebpackOverride ) ;
123+ } catch ( e ) {
124+ // ignore if file does not exist
125+ }
126+
108127 let {
109128 // eslint-disable-next-line prefer-const
110129 entries, registry, copyFiles, historyApiFallback,
@@ -255,6 +274,7 @@ module.exports = (env, argv) => {
255274 return loaders ;
256275 } ;
257276
277+ // Use TimeAnalyticsPlugin.wrap to anaylze the build time
258278 return {
259279 mode,
260280 // Webpack noise constrained to errors and warnings
@@ -343,42 +363,11 @@ module.exports = (env, argv) => {
343363 minimizer : [
344364 // This is only used in production mode
345365 new TerserPlugin ( {
346- terserOptions : {
347- parse : {
348- // We want terser to parse ecma 8 code. However, we don't want it
349- // to apply any minification steps that turns valid ecma 5 code
350- // into invalid ecma 5 code. This is why the 'compress' and 'output'
351- // sections only apply transformations that are ecma 5 safe
352- // https://github.com/facebook/create-react-app/pull/4234
353- // ecma: 8, TODO:
354- } ,
355- compress : {
356- ecma : 5 ,
357- // Disabled because of an issue with Uglify breaking seemingly valid code:
358- // https://github.com/facebook/create-react-app/issues/2376
359- // Pending further investigation:
360- // https://github.com/mishoo/UglifyJS2/issues/2011
361- comparisons : false ,
362- // Disabled because of an issue with Terser breaking valid code:
363- // https://github.com/facebook/create-react-app/issues/5250
364- // Pending further investigation:
365- // https://github.com/terser-js/terser/issues/120
366- inline : 2 ,
367- } ,
368- mangle : {
369- safari10 : true ,
370- } ,
371- // Added for profiling in devtools
372- keep_classnames : true ,
373- keep_fnames : false ,
374- output : {
375- ecma : 5 ,
376- comments : false ,
377- // Turned on because emoji and regex is not minified properly using default
378- // https://github.com/facebook/create-react-app/issues/2488
379- ascii_only : true ,
380- } ,
381- } ,
366+ // Use swcMinify instead of terser: https://webpack.js.org/plugins/terser-webpack-plugin/#swc
367+ minify : TerserPlugin . swcMinify ,
368+ // `terserOptions` options will be passed to `swc` (`@swc/core`)
369+ // Link to options - https://swc.rs/docs/config-js-minify
370+ terserOptions : { } ,
382371 } ) ,
383372 // This is only used in production mode
384373 // TODO: Somehow this breaks with lineup: /media/LineUpJS.d518227895a66b92cfd7.css:1:1: Unknown word [media/LineUpJS.d518227895a66b92cfd7.css:1,1]
@@ -477,37 +466,19 @@ module.exports = (env, argv) => {
477466 esModule : false ,
478467 } ,
479468 } ,
480- // Process application JS with Babel.
481- // The preset includes JSX, Flow, TypeScript, and some ESnext features.
469+ // Process application JS with swc-loader as it is much faster than babel.
482470 {
483471 test : / \. ( j s | m j s | j s x | t s | t s x ) $ / ,
484472 exclude : [ / n o d e _ m o d u l e s / , customResolveAliasRegex ] . filter ( Boolean ) ,
485- loader : 'babel -loader' ,
473+ loader : 'swc -loader' ,
486474 options : {
487- presets : [ [ '@babel/preset-env' , { targets : { browsers : 'last 2 Chrome versions' } } ] , '@babel/preset-typescript' , '@babel/preset-react' ] ,
488- plugins : [
489- // https://exerror.com/babel-referenceerror-regeneratorruntime-is-not-defined/#Solution_3_For_Babel_7_User
490- [
491- '@babel/transform-runtime' ,
492- {
493- regenerator : true ,
494- } ,
495- ] ,
496- // plugin-proposal-decorators is only needed if you're using experimental decorators in TypeScript
497- [ '@babel/plugin-proposal-decorators' , { legacy : true } ] ,
498- // ["@babel/plugin-proposal-class-properties", { loose: false }],
499- // Enable hmr for react components in dev mode
500- isEnvDevelopment && 'react-refresh/babel' ,
501- ] . filter ( Boolean ) ,
502- babelrc : false ,
503- configFile : false ,
504- // This is a feature of `babel-loader` for webpack (not Babel itself).
505- // It enables caching results in ./node_modules/.cache/babel-loader/
506- // directory for faster rebuilds.
507- cacheDirectory : true ,
508- // See create-react-app#6846 for context on why cacheCompression is disabled
509- cacheCompression : false ,
510- compact : isEnvProduction ,
475+ jsc : {
476+ parser : {
477+ syntax : 'typescript' ,
478+ decorators : true ,
479+ // TODO: Check what other settings should be supported: https://swc.rs/docs/configuration/swcrc#compilation
480+ } ,
481+ } ,
511482 } ,
512483 } ,
513484 // "postcss" loader applies autoprefixer to our CSS.
@@ -640,13 +611,14 @@ module.exports = (env, argv) => {
640611 && new ReactRefreshWebpackPlugin ( {
641612 overlay : false ,
642613 } ) ,
643- ...Object . values ( entries ) . map (
644- ( entry ) => new HtmlWebpackPlugin ( {
614+ ...Object . entries ( entries ) . map (
615+ ( [ chunkName , entry ] ) => new HtmlWebpackPlugin ( {
645616 inject : true ,
646617 template : path . join ( defaultAppPath , entry . template ) ,
647618 filename : entry . html ,
648619 title : libName ,
649- excludeChunks : entry . excludeChunks ,
620+ // By default, exclude all other chunks
621+ excludeChunks : entry . excludeChunks || Object . keys ( entries ) . filter ( ( entryKey ) => entryKey !== chunkName ) ,
650622 meta : {
651623 description : libDesc ,
652624 } ,
@@ -707,8 +679,7 @@ module.exports = (env, argv) => {
707679 syntactic : true ,
708680 } ,
709681 // Build the repo and type-check
710- build : true ,
711- // Recommended for use with babel-loader
682+ build : Object . keys ( resolveAliases ) . length === 0 ,
712683 mode : 'write-references' ,
713684 // Use the corresponding config file of the repo folder
714685 configFile : path . join ( workspacePath , repo , 'tsconfig.json' ) ,
@@ -725,7 +696,8 @@ module.exports = (env, argv) => {
725696 incremental : true ,
726697 paths : Object . assign (
727698 { } ,
728- resolveAliases ,
699+ // Map the aliases to the same path, but within an array like tsc requires it
700+ Object . fromEntries ( Object . entries ( resolveAliases ) . map ( ( [ alias , aliasPath ] ) => [ alias , [ aliasPath ] ] ) ) ,
729701 ...( ! isSingleRepoMode
730702 ? workspaceRepos . map ( ( r ) => ( {
731703 [ `${ workspaceRepoToName [ r ] } /dist` ] : [ path . join ( workspacePath , r , 'src/*' ) ] ,
0 commit comments