-
Notifications
You must be signed in to change notification settings - Fork 756
/
webpack.config.ts
128 lines (116 loc) · 3.03 KB
/
webpack.config.ts
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
import path = require('path');
import webpack = require('webpack');
import HtmlWebpackPlugin = require('html-webpack-plugin');
import MiniCssExtractPlugin = require('mini-css-extract-plugin');
import fs = require('fs');
const WebpackShellPlugin = require('webpack-shell-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
import {loader} from 'webpack';
const _DEV_ = (process.env.NODE_ENV || '').toLowerCase().startsWith('dev');
const WEB_OUTPUT_PATH = path.join(__dirname, 'docs');
const WEB_SRC_PATH = path.join(__dirname, 'src/web');
const plugins = [
new HtmlWebpackPlugin({
template: path.join(WEB_SRC_PATH, 'index.html'),
inlineSource: '.(js|css)$',
filename: 'beta.html'
}),
new HtmlWebpackInlineSourcePlugin(),
// Generate TypeScript types for css modules
new WebpackShellPlugin({
onBuildStart: ['npm run cssd']
}),
{
apply(compiler: webpack.Compiler) {
compiler.hooks.afterEmit.tap('CleanInlinedFile', () => {
let files = ['main.css', 'main.js'].map(f => path.join(WEB_OUTPUT_PATH, f));
for (let f of files) {
try {
fs.unlinkSync(f);
} catch (e) {}
}
});
}
},
new webpack.WatchIgnorePlugin([/css\.d\.ts$/]),
new MiniCssExtractPlugin({
filename: 'main.css'
})
];
function cssModules(regex: RegExp, mode: 'local' | 'global'): webpack.RuleSetRule {
return {
test: regex,
include: WEB_SRC_PATH,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: false
}
},
{
loader: 'css-loader',
options: {
modules: {
mode: mode,
context: WEB_SRC_PATH,
localIdentName: '[local]-[hash:4]'
},
localsConvention: 'camelCaseOnly',
sourceMap: _DEV_
}
}
]
};
}
if (!_DEV_) {
plugins.push(
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: ['default', {discardComments: {removeAll: true}}]
},
canPrint: true
})
);
}
const config: webpack.Configuration = {
mode: _DEV_ ? 'development' : 'production',
entry: path.join(WEB_SRC_PATH, 'main.ts'),
output: {
path: WEB_OUTPUT_PATH,
publicPath: '/',
filename: 'main.js'
},
devtool: _DEV_ ? 'inline-source-map' : false,
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
},
cssModules(/\.local\.css$/, 'local'),
cssModules(/(?<!\.local)\.css$/, 'global'),
{
test: /\.(png|jpg)$/,
loader: 'url-loader',
include: WEB_SRC_PATH
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
devServer: {
contentBase: WEB_OUTPUT_PATH,
compress: false,
hot: false,
inline: false,
liveReload: false
} as any,
plugins
};
export default config;