forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
175 lines (160 loc) · 4.25 KB
/
webpack.config.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const rtlcss = require('rtlcss');
const customProperties = require('postcss-custom-properties');
const {
CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES = 'false',
CARBON_REACT_STORYBOOK_USE_EXTERNAL_CSS,
CARBON_REACT_USE_CONTROLLED_STATE_WITH_EVENT_LISTENER,
CARBON_REACT_STORYBOOK_USE_RTL,
NODE_ENV = 'development',
} = process.env;
const useExternalCss = NODE_ENV === 'production';
const useControlledStateWithEventListener =
CARBON_REACT_USE_CONTROLLED_STATE_WITH_EVENT_LISTENER === 'true';
const useRtl = CARBON_REACT_STORYBOOK_USE_RTL === 'true';
const replaceTable = {
useControlledStateWithEventListener,
};
class FeatureFlagProxyPlugin {
/**
* A WebPack resolver plugin that proxies module request
* for `carbon-components/es/globals/js/settings` to `./settings`.
*/
constructor() {
this.source = 'before-described-relative';
}
apply(resolver) {
resolver.plugin(this.source, (request, callback) => {
if (/[\\/]globals[\\/]js[\\/]settings$/.test(request.path)) {
request.path = path.resolve(__dirname, './settings');
}
callback();
});
}
}
module.exports = ({ config, mode }) => {
config.devtool =
NODE_ENV === 'production' ? 'source-map' : 'cheap-module-eval-source-map';
config.optimization = {
...config.optimization,
minimizer: [
new TerserPlugin({
sourceMap: true,
terserOptions: {
mangle: false,
},
}),
],
};
config.module.rules.push({
test: /(\/|\\)FeatureFlags\.js$/,
loader: 'string-replace-loader',
options: {
multiple: Object.keys(replaceTable).map((key) => ({
search: `export\\s+const\\s+${key}\\s*=\\s*false`,
replace: `export const ${key} = ${replaceTable[key]}`,
flags: 'i',
})),
},
});
config.module.rules.push({
test: /-story\.jsx?$/,
loaders: [
{
loader: require.resolve('@storybook/source-loader'),
options: {
prettierConfig: {
parser: 'babylon',
printWidth: 80,
tabWidth: 2,
bracketSpacing: true,
trailingComma: 'es5',
singleQuote: true,
},
},
},
],
enforce: 'pre',
});
const sassLoader = {
loader: 'sass-loader',
options: {
prependData() {
return `
$feature-flags: (
ui-shell: true,
enable-css-custom-properties: ${CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES},
);
`;
},
sassOptions: {
includePaths: [path.resolve(__dirname, '..', 'node_modules')],
},
sourceMap: true,
},
};
const fastSassLoader = {
loader: 'fast-sass-loader',
options: {
data: `
$feature-flags: (
ui-shell: true,
enable-css-custom-properties: ${CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES},
);
`,
},
};
config.module.rules.push({
test: /\.scss$/,
sideEffects: true,
use: [
{
loader: useExternalCss ? MiniCssExtractPlugin.loader : 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 2,
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => {
const autoPrefixer = require('autoprefixer')({
overrideBrowserslist: ['last 1 version', 'ie >= 11'],
});
return [
customProperties(),
autoPrefixer,
...(useRtl ? [rtlcss] : []),
];
},
sourceMap: true,
},
},
NODE_ENV === 'production' ? sassLoader : fastSassLoader,
],
});
if (useExternalCss) {
config.plugins.push(
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
})
);
}
config.resolve = {
modules: ['node_modules'],
plugins: [new FeatureFlagProxyPlugin()],
};
return config;
};