import { merge } from 'lodash'; import { sep } from 'path'; import 'style-loader'; import MiniCssExtractPlugin from 'mini-css-extract-plugin'; import getCSSModuleLocalIdent from 'react-dev-utils/getCSSModuleLocalIdent'; import type { Configuration } from 'webpack'; import { IgnorePlugin } from 'webpack'; import * as stylesRegexps from '@teambit/webpack.modules.style-regexps'; import { generateStyleLoaders } from '@teambit/webpack.modules.generate-style-loaders'; import { postCssConfig } from './postcss.config'; // Make sure the bit-react-transformer is a dependency // TODO: remove it once we can set policy from component to component then set it via the component.json import '@teambit/react.babel.bit-react-transformer'; // eslint-disable-next-line import/no-unresolved import '@mdx-js/loader'; import { mdxOptions } from '@teambit/mdx.modules.mdx-v3-options'; const styleLoaderPath = require.resolve('style-loader'); const moduleFileExtensions = [ 'web.mjs', 'mjs', 'web.js', 'js', 'cjs', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'mdx', 'md', ]; // Source maps are resource heavy and can cause out of memory issue for large source files. const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false'; const imageInlineSizeLimit = parseInt(process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'); // This is the production and development configuration. // It is focused on developer experience, fast rebuilds, and a minimal bundle. // eslint-disable-next-line complexity export default function (isEnvProduction = false): Configuration { // Variable used for enabling profiling in Production // passed into alias object. Uses a flag if passed into the build command const isEnvProductionProfile = process.argv.includes('--profile'); const baseStyleLoadersOptions = { injectingLoader: isEnvProduction ? MiniCssExtractPlugin.loader : styleLoaderPath, cssLoaderPath: require.resolve('css-loader'), postCssLoaderPath: require.resolve('postcss-loader'), postCssConfig, }; // We will provide `paths.publicUrlOrPath` to our app // as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript. // Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz. // Get environment variables to inject into our app. // const env = getClientEnvironment(publicUrlOrPath.slice(0, -1)); return { // TODO: make the dev tool according to shouldUseSourceMap and isEnvProduction // devtool: 'eval-cheap-module-source-map', resolve: { // These are the reasonable defaults supported by the Node ecosystem. // We also include JSX as a common component filename extension to support // some tools, although we do not recommend using it, see: // https://github.com/facebook/create-react-app/issues/290 // `web` extension prefixes have been added for better support // for React Native Web. extensions: moduleFileExtensions.map((ext) => `.${ext}`), alias: { 'react/jsx-dev-runtime': require.resolve('react/jsx-dev-runtime'), 'react/jsx-runtime': require.resolve('react/jsx-runtime'), 'react-dom/server': require.resolve('react-dom/server'), // Allows for better profiling with ReactDevTools ...(isEnvProductionProfile && { 'react-dom$': 'react-dom/profiling', 'scheduler/tracing': 'scheduler/tracing-profiling', }), }, }, module: { strictExportPresence: true, rules: [ { test: /\.m?js/, resolve: { fullySpecified: 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: [ // "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