|
| 1 | +// Common configuration applying to client and server configuration |
| 2 | +const { generateWebpackConfig, merge } = require('shakapacker'); |
| 3 | + |
| 4 | +const baseClientRspackConfig = generateWebpackConfig(); |
| 5 | +const commonOptions = { |
| 6 | + resolve: { |
| 7 | + extensions: ['.css', '.ts', '.tsx'], |
| 8 | + }, |
| 9 | +}; |
| 10 | + |
| 11 | +// add sass resource loader |
| 12 | +const sassLoaderConfig = { |
| 13 | + loader: 'sass-resources-loader', |
| 14 | + options: { |
| 15 | + resources: './client/app/assets/styles/app-variables.scss', |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +const ignoreWarningsConfig = { |
| 20 | + ignoreWarnings: [/Module not found: Error: Can't resolve 'react-dom\/client'/], |
| 21 | +}; |
| 22 | + |
| 23 | +const scssConfigIndex = baseClientRspackConfig.module.rules.findIndex((config) => |
| 24 | + '.scss'.match(config.test) && config.use, |
| 25 | +); |
| 26 | + |
| 27 | +if (scssConfigIndex === -1) { |
| 28 | + console.warn('No SCSS rule with use array found in rspack config'); |
| 29 | +} else { |
| 30 | + // Configure sass-loader to use the modern API |
| 31 | + const scssRule = baseClientRspackConfig.module.rules[scssConfigIndex]; |
| 32 | + const sassLoaderIndex = scssRule.use.findIndex((loader) => { |
| 33 | + if (typeof loader === 'string') { |
| 34 | + return loader.includes('sass-loader'); |
| 35 | + } |
| 36 | + return loader.loader && loader.loader.includes('sass-loader'); |
| 37 | + }); |
| 38 | + |
| 39 | + if (sassLoaderIndex !== -1) { |
| 40 | + const sassLoader = scssRule.use[sassLoaderIndex]; |
| 41 | + if (typeof sassLoader === 'string') { |
| 42 | + scssRule.use[sassLoaderIndex] = { |
| 43 | + loader: sassLoader, |
| 44 | + options: { |
| 45 | + api: 'modern' |
| 46 | + } |
| 47 | + }; |
| 48 | + } else { |
| 49 | + sassLoader.options = sassLoader.options || {}; |
| 50 | + sassLoader.options.api = 'modern'; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Fix css-loader configuration for CSS modules if namedExport is enabled |
| 55 | + // When namedExport is true, exportLocalsConvention must be camelCaseOnly or dashesOnly |
| 56 | + const cssLoader = scssRule.use.find((loader) => { |
| 57 | + const loaderName = typeof loader === 'string' ? loader : loader?.loader; |
| 58 | + return loaderName?.includes('css-loader'); |
| 59 | + }); |
| 60 | + |
| 61 | + if (cssLoader?.options?.modules?.namedExport) { |
| 62 | + cssLoader.options.modules.exportLocalsConvention = 'camelCaseOnly'; |
| 63 | + } |
| 64 | + |
| 65 | + baseClientRspackConfig.module.rules[scssConfigIndex].use.push(sassLoaderConfig); |
| 66 | +} |
| 67 | + |
| 68 | +// Copy the object using merge b/c the baseClientRspackConfig and commonOptions are mutable globals |
| 69 | +const commonRspackConfig = () => merge({}, baseClientRspackConfig, commonOptions, ignoreWarningsConfig); |
| 70 | + |
| 71 | +module.exports = commonRspackConfig; |
0 commit comments