This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.base.js
149 lines (137 loc) · 3.49 KB
/
webpack.config.base.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
process.traceDeprecation = true;
const path = require('path');
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' );
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const postCSSConfig = require('./postcss.config.js');
const get = require('lodash/get');
const assign = require('lodash/assign');
const variant = process.env.CSS_VARIANT;
const baseDir = variant ? `tmp/${variant}` : 'src';
const includePaths = [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'styleguide_assets'),
];
const isProd = process.env.NODE_ENV === 'production';
const cssModulesName = (modulesName) => {
if (!isProd) {
return 'rcl-[name]__[local]--[hash:base64:5]';
}
return `rcl_${modulesName || ''}_[local]-[hash:base64:3]`;
};
const baseRules = modulesName => [
{
test: /\.js$/,
include: includePaths,
use: [
{
// babel-loader will throw a deprecation warning
// waiting for stable v7.0.0 to upgrade
loader: 'babel-loader',
options: {
presets: [ 'env', 'react', 'stage-2' ]
}
}
]
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 3,
localIdentName: cssModulesName(modulesName),
sourceMap: !isProd,
minimize: !!isProd
}
},
{
loader: 'postcss-loader?parser=postcss-scss',
options: {
postCSSConfig
}
},
{
loader: 'sass-loader',
options: {
sourceMap: !isProd,
sourceComments: !isProd,
includePaths: [ baseDir ],
data: "@import 'assets/stylesheets/global';"
}
},
{
loader: 'resolve-url-loader'
},
]
})
},
{
test: /\.(jpe?g|png|gif)$/i,
include: includePaths,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'assets/images/',
publicPath: '/'
}
}
]
},
{
test: /\.(ttf|eot|woff|woff2)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'assets/fonts/',
publicPath: '/'
}
}
]
}
];
module.exports = (options) => {
const rules = get(options, 'module.rules', []).concat(baseRules(options.modulesName));
const plugins = get(options, 'plugins', []).concat([
new ExtractTextPlugin({
filename: variant ? `assets/${variant}_styles.css` : 'assets/styles.css',
allChunks: true
})
]);
if (process.env.ANALYZE) {
plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
generateStatsFile: true,
reportFilename: `../webpack-bundle-report/report-${process.env.ANALYZE}${Date.now()}.html`,
statsFilename: `../webpack-bundle-report/stats-${process.env.ANALYZE}${Date.now()}.json`,
analyzerPort: 3000,
production: true,
})
);
}
return {
entry: options.entry,
output: options.output,
externals: options.externals,
devtool: 'source-map',
context: path.resolve(__dirname, baseDir),
resolve: assign({
modules: [
path.resolve(__dirname, baseDir),
'node_modules'
],
}, options.resolve),
module: {
rules,
},
plugins
};
};