const path = require('path'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const fs = require('fs'); const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin'); const TerserPlugin = require('terser-webpack-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const safePostCssParser = require('postcss-safe-parser'); const { WebpackManifestPlugin: ManifestPlugin } = require('webpack-manifest-plugin'); const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); const WorkboxWebpackPlugin = require('workbox-webpack-plugin'); const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin'); const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin'); const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent'); const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin'); const ForkTsCheckerWebpackPlugin = require('react-dev-utils/ForkTsCheckerWebpackPlugin'); const typescriptFormatter = require('react-dev-utils/typescriptFormatter'); const postcssNormalize = require('postcss-normalize'); const resolvePath = (relative) => path.resolve(fs.realpathSync(process.cwd()), relative); const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false'; const emitErrorsAsWarnings = process.env.ESLINT_NO_DEV_ERRORS === 'true'; const disableESLintPlugin = process.env.DISABLE_ESLINT_PLUGIN === 'true'; const imageInlineSizeLimit = parseInt(process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'); // Check if TypeScript is setup const useTypeScript = fs.existsSync(resolvePath('tsconfig.json')); const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false'; const cssRegex = /\.css$/; const cssModuleRegex = /\.module\.css$/; const sassRegex = /\.(scss|sass)$/; const sassModuleRegex = /\.module\.(scss|sass)$/; const swSrc = 'not-existing-file.txt'; module.exports = (webpackEnv) => { // const isEnvDevelopment = webpackEnv === 'development'; const isEnvDevelopment = false; // const isEnvProduction = webpackEnv === 'production'; const isEnvProduction = !isEnvDevelopment; const getStyleLoaders = (cssOptions, preProcessor) => { const loaders = [ isEnvDevelopment && require.resolve('style-loader'), isEnvProduction && { loader: MiniCssExtractPlugin.loader, // css is located in `static/css`, use '../../' to locate index.html folder // in production `paths.publicUrlOrPath` can be a relative path options: { publicPath: '../../' }, }, { loader: require.resolve('css-loader'), options: cssOptions, }, { // Options for PostCSS as we reference these options twice // Adds vendor prefixing based on your specified browser support in // package.json loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebook/create-react-app/issues/2677 postcssOptions: { ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), require('postcss-preset-env')({ autoprefixer: { flexbox: 'no-2009', }, stage: 3, }), // Adds PostCSS Normalize as the reset css with default options, // so that it honors browserslist config in package.json // which in turn let's users customize the target behavior as per their needs. postcssNormalize(), ], }, sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment, }, }, ].filter(Boolean); if (preProcessor) { loaders.push( { loader: require.resolve('resolve-url-loader'), options: { sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment, root: resolvePath('src/client'), }, }, { loader: require.resolve(preProcessor), options: { sourceMap: true, }, } ); } return loaders; }; return { mode: 'development', entry: './src/client/ts/index.tsx', output: { path: resolvePath('www'), pathinfo: isEnvDevelopment, filename: 'index.bundle.js', // futureEmitAssets: true, // publicPath: '.', // // Point sourcemap entries to original disk location (format as URL on Windows) // devtoolModuleFilenameTemplate: isEnvProduction // ? (info) => path.relative(paths.appSrc, info.absoluteResourcePath).replace(/\\/g, '/') // : isEnvDevelopment && ((info) => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')), // globalObject: 'this', }, devtool: 'inline-source-map', resolve: { extensions: ['.tsx', '.ts', '.js', '.jsx'], }, module: { strictExportPresence: true, rules: [ { // Disable require.ensure as it's not a standard language feature. parser: { requireEnsure: false, }, }, { // "oneOf" will traverse all following loaders until one will // match the requirements. When no loader matches it will fall // back to the "file" loader at the end of the loader list. oneOf: [ { test: /\.(ts|tsx)$/, exclude: /node_modules/, use: { loader: 'ts-loader', }, }, { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader', }, }, // "postcss" loader applies autoprefixer to our CSS. // "css" loader resolves paths in CSS and adds assets as dependencies. // "style" loader turns CSS into JS modules that inject