| |
| |
| |
| |
| |
|
|
| const debug = require('debug')('build:config-overrides'); |
| const webpack = require('webpack'); |
| const { injectBabelPlugin } = require('react-app-rewired'); |
| const rewireStyledComponents = require('react-app-rewire-styled-components'); |
| const rewireReactHotLoader = require('react-app-rewire-hot-loader'); |
| const swPrecachePlugin = require('sw-precache-webpack-plugin'); |
| const fs = require('fs'); |
| const path = require('path'); |
| const match = require('micromatch'); |
| const WriteFilePlugin = require('write-file-webpack-plugin'); |
| const { ReactLoadablePlugin } = require('react-loadable/webpack'); |
| const OfflinePlugin = require('offline-plugin'); |
| const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); |
| const BundleBuddyWebpackPlugin = require('bundle-buddy-webpack-plugin'); |
| const CircularDependencyPlugin = require('circular-dependency-plugin'); |
|
|
| |
| function walkFolder(currentDirPath, callback) { |
| fs.readdirSync(currentDirPath).forEach(name => { |
| |
| if (name.indexOf('.') === 0) return; |
|
|
| var filePath = path.join(currentDirPath, name); |
| var stat = fs.statSync(filePath); |
|
|
| if (stat.isFile()) { |
| callback(filePath, stat); |
| } else if (stat.isDirectory()) { |
| walkFolder(filePath, callback); |
| } |
| }); |
| } |
|
|
| const isServiceWorkerPlugin = plugin => plugin instanceof swPrecachePlugin; |
|
|
| const removeEslint = config => { |
| config.module.rules = config.module.rules.filter(rule => { |
| |
| if (rule.use) { |
| return rule.use.every(use => { |
| if (!use.options) return true; |
| return !use.options.eslintPath; |
| }); |
| } |
| return true; |
| }); |
| }; |
|
|
| |
| const transpileShared = config => { |
| config.module.rules.forEach(rule => { |
| if (!rule.oneOf) return; |
|
|
| rule.oneOf.forEach(loader => { |
| if (!loader.include) return; |
| if (!loader.options) return; |
| |
| |
| if (!Object.keys(loader.options).includes('babelrc')) return; |
|
|
| |
| |
| loader.include = [loader.include, path.resolve(__dirname, './shared')]; |
| }); |
| }); |
|
|
| return config; |
| }; |
|
|
| module.exports = function override(config, env) { |
| if (process.env.REACT_APP_MAINTENANCE_MODE === 'enabled') { |
| console.error('\n\n⚠️ ----MAINTENANCE MODE ENABLED----⚠️\n\n'); |
| } |
| if (process.env.NODE_ENV === 'development') { |
| config.output.path = path.join(__dirname, './build'); |
| config = rewireReactHotLoader(config, env); |
| config.plugins.push( |
| WriteFilePlugin({ |
| log: true, |
| useHashIndex: false, |
| }) |
| ); |
| } |
| config.plugins.push( |
| new ReactLoadablePlugin({ |
| filename: './build/react-loadable.json', |
| }) |
| ); |
| config = injectBabelPlugin('react-loadable/babel', config); |
| config = transpileShared(config); |
| |
| config.plugins = config.plugins.filter( |
| plugin => !isServiceWorkerPlugin(plugin) |
| ); |
| |
| let externals = []; |
| walkFolder('./public/', file => { |
| if (file.indexOf('index.html') > -1) return; |
| externals.push(file.replace(/public/, '')); |
| }); |
| config.plugins.push( |
| new OfflinePlugin({ |
| |
| caches: {}, |
| externals, |
| autoUpdate: true, |
| |
| |
| |
| |
| cacheMaps: [ |
| { |
| match: function() { |
| return false; |
| }, |
| requestTypes: ['navigate'], |
| }, |
| ], |
| rewrites: arg => arg, |
| ServiceWorker: { |
| entry: './public/push-sw.js', |
| events: true, |
| prefetchRequest: { |
| mode: 'cors', |
| credentials: 'include', |
| }, |
| }, |
| AppCache: false, |
| }) |
| ); |
| if (process.env.ANALYZE_BUNDLE === 'true') { |
| debug('Bundle analyzer enabled'); |
| config.plugins.push( |
| new BundleAnalyzerPlugin({ |
| analyzerMode: 'static', |
| openAnalyzer: false, |
| }) |
| ); |
| } |
| if (process.env.BUNDLE_BUDDY === 'true') { |
| config.plugins.push(new BundleBuddyWebpackPlugin()); |
| } |
| config.plugins.unshift( |
| new webpack.optimize.CommonsChunkPlugin({ |
| names: ['bootstrap'], |
| filename: 'static/js/[name].[hash].js', |
| minChunks: Infinity, |
| }) |
| ); |
| if (process.env.NODE_ENV === 'production') { |
| removeEslint(config); |
| config.plugins.push(new webpack.optimize.ModuleConcatenationPlugin()); |
| config.plugins.push( |
| new webpack.DefinePlugin({ |
| 'process.env': { |
| SENTRY_DSN_CLIENT: `"${process.env.SENTRY_DSN_CLIENT}"`, |
| }, |
| }) |
| ); |
| |
| config = injectBabelPlugin( |
| ['react-remove-properties', { properties: ['data-cy'] }], |
| config |
| ); |
| } |
|
|
| |
| |
| config.resolve.alias.b2a = path.resolve( |
| __dirname, |
| 'node_modules/b2a/lib/index.js' |
| ); |
| config.plugins.push( |
| new CircularDependencyPlugin({ |
| cwd: process.cwd(), |
| failOnError: true, |
| }) |
| ); |
| return rewireStyledComponents(config, env, { ssr: true }); |
| }; |
|
|