-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathcommonWebpackConfig.js
More file actions
63 lines (52 loc) · 1.92 KB
/
commonWebpackConfig.js
File metadata and controls
63 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/commonWebpackConfig.js
// Common configuration applying to client and server configuration
const { generateWebpackConfig, merge } = require('shakapacker');
const baseClientWebpackConfig = generateWebpackConfig();
const commonOptions = {
resolve: {
extensions: ['.css', '.ts', '.tsx'],
},
};
// add sass resource loader
const sassLoaderConfig = {
loader: 'sass-resources-loader',
options: {
resources: './client/app/assets/styles/app-variables.scss',
},
};
const ignoreWarningsConfig = {
ignoreWarnings: [/Module not found: Error: Can't resolve 'react-dom\/client'/],
};
const scssConfigIndex = baseClientWebpackConfig.module.rules.findIndex((config) =>
'.scss'.match(config.test),
);
// Configure sass-loader to use the modern API
const scssRule = baseClientWebpackConfig.module.rules[scssConfigIndex];
const sassLoaderIndex = scssRule.use.findIndex((loader) => {
if (typeof loader === 'string') {
return loader.includes('sass-loader');
}
return loader.loader && loader.loader.includes('sass-loader');
});
if (sassLoaderIndex !== -1) {
const sassLoader = scssRule.use[sassLoaderIndex];
if (typeof sassLoader === 'string') {
scssRule.use[sassLoaderIndex] = {
loader: sassLoader,
options: {
api: 'modern',
sassOptions: {
includePaths: []
}
}
};
} else {
sassLoader.options = sassLoader.options || {};
sassLoader.options.api = 'modern';
}
}
baseClientWebpackConfig.module.rules[scssConfigIndex].use.push(sassLoaderConfig);
// Copy the object using merge b/c the baseClientWebpackConfig and commonOptions are mutable globals
const commonWebpackConfig = () => merge({}, baseClientWebpackConfig, commonOptions, ignoreWarningsConfig);
module.exports = commonWebpackConfig;