-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
108 lines (100 loc) · 2.91 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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HTMLInlineCSSWebpackPlugin = require('html-inline-css-webpack-plugin').default;
const config = require('./js-control.config');
module.exports = env => {
kickoffLogging(env);
return {
mode: isProduction(env) ? 'production' : undefined,
entry: getEntry(env),
devtool: getDevTool(env),
devServer: {
contentBase: './dist',
hot: true,
},
stats: 'minimal',
plugins: [
isProduction(env) && new CleanWebpackPlugin(),
isDev(env) &&
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html',
chunks: config.JS_CONTROL_NAME,
hash: true,
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
}),
isDev(env) && new HTMLInlineCSSWebpackPlugin({ leaveCSSFile: true }),
].filter(plugin => !!plugin),
output: {
filename: isProduction(env) ? '[name].[contentHash].js' : '[name].js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: [ '.tsx', '.ts', '.js', '.jsx' ],
},
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
// exclude: /(node_modules|bower_components)/,
include: [path.resolve(__dirname, 'src')],
use: {
loader: 'ts-loader',
},
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
test: /\.s[ac]ss$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'],
},
{
test: /\.less$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'less-loader'],
},
],
},
externals: config.externals
};
};
// helper functions:
function isProduction(env) {
return env && env.production;
}
function isDev(env) {
return !isProduction(env);
}
function includeSourceMaps(env) {
return env && env.sourceMaps;
}
function getDevTool(env) {
return !isProduction(env) ? 'source-map' : includeSourceMaps(env) ? 'inline-source-map' : false;
}
function getEntry(env) {
const entry = {};
const ext = config.useTypeScript ? 'ts' : 'js';
entry[config.JS_CONTROL_NAME] = isProduction(env)
? `./src/${config.JS_CONTROL_FILE_NAME}`
: `./src/index.${ext}`;
return entry;
}
function kickoffLogging(env) {
if (isProduction(env)) {
console.log(
'Creating production build...',
includeSourceMaps(env) ? 'with inline source maps!' : ''
);
if (includeSourceMaps(env)) {
console.log("^^ Don't forget to change this when deploying! ^^");
}
} else {
console.log('Creating dev build...');
}
}