-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
116 lines (108 loc) · 3.48 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
// @flow
import path from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin'; // eslint-disable-line import/no-extraneous-dependencies
import MiniCssExtractPlugin from 'mini-css-extract-plugin'; // eslint-disable-line import/no-extraneous-dependencies
import autoprefixer from 'autoprefixer'; // eslint-disable-line import/no-extraneous-dependencies
import getPort from './port';
const port = getPort();
const JS_REGEX = /\.js$|\.jsx$|\.es6$|\.babel$/;
const CSS_REGEX = /\.css$|\.scss$/;
const context = path.join(__dirname, '../src/js');
export const outputPath = 'www';
type GetRulesInputType = { useCssModules: boolean }
const getRules = ({ useCssModules }: GetRulesInputType): {}[] => [
{
test: JS_REGEX,
loaders: ['babel-loader'],
exclude: /node_modules/
},
{
test: CSS_REGEX,
use: [
{
loader: MiniCssExtractPlugin.loader,
query: {
modules: useCssModules,
importLoaders: useCssModules ? 2 : '',
localIdentName: useCssModules ? '[name]__[local]__[hash:base64:5]' : ''
}
},
{
loader: 'css-loader',
query: {
modules: useCssModules,
importLoaders: useCssModules ? 2 : '',
localIdentName: useCssModules ? '[name]__[local]__[hash:base64:5]' : ''
}
},
{
loader: 'postcss-loader',
options: {
plugins: [autoprefixer({ browsers: ['last 4 versions'] })]
}
},
{ loader: 'sass-loader', options: {} }
]
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
},
{
test: /\.json$/,
loader: 'json-loader'
}
];
type OptionsType = {
useCssModules?: boolean,
moduleVersionReactFlagIconCss: string,
moduleVersionWebpack: string
}
type MakeConfigReturnType = {
output: {
publicPath: string,
filename: string
}
}
const makeConfig = (options: OptionsType): MakeConfigReturnType => {
const useCssModules = JSON.parse(process.env.USE_CSS_MODULES || 'false');
const computedOptions = { useCssModules, ...options };
const { moduleVersionReactFlagIconCss, moduleVersionWebpack } = computedOptions;
return {
context,
entry: [`webpack-dev-server/client?http://0.0.0.0:${port}/`, path.join(context, 'index.js')],
output: {
path: path.join(__dirname, outputPath),
filename: 'bundle.js',
publicPath: '/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
title: 'React Flag Icon Css - Webpack 2 Example Multi',
devServerScript: `http://0.0.0.0:${port}/webpack-dev-server.js`,
template: path.join(__dirname, 'templates/index.ejs'),
githubRepositoryUrl: 'https://github.com/matteocng/react-flag-icon-css'
}),
new MiniCssExtractPlugin({ filename: 'bundle.css' }),
new webpack.DefinePlugin({
__USE_CSS_MODULES__: JSON.stringify(JSON.parse(process.env.USE_CSS_MODULES || 'true')),
MODULE_VERSION_REACT_FLAG_ICON_CSS: JSON.stringify(moduleVersionReactFlagIconCss),
MODULE_VERSION_WEBPACK: JSON.stringify(moduleVersionWebpack)
})
],
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
node: {
fs: 'empty',
net: 'empty',
tls: 'empty'
},
module: {
rules: getRules(computedOptions)
}
};
};
export default makeConfig;