-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
104 lines (101 loc) · 2.69 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
/* eslint-disable @typescript-eslint/no-var-requires */
const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const WriteFilePlugin = require("write-file-webpack-plugin");
const isEnvDevelopment = process.env.NODE_ENV !== "production";
const commonResolve = dir => ({
extensions: [".ts", ".tsx", ".js", ".jsx", ".css", ".scss"],
alias: {
assets: path.resolve(__dirname, dir)
}
});
const sassRule = {
test: /(\.s?css)|(\.sass)$/,
oneOf: [
// if ext includes module as prefix, it perform by css loader.
{
test: /.module(\.s?css)|(\.sass)$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: {
localIdentName: "[local]-[hash:base64]"
},
localsConvention: "camelCase"
}
},
"sass-loader"
]
},
{
use: [
"style-loader",
{ loader: "css-loader", options: { modules: false } },
"sass-loader"
]
}
]
};
const tsRule = { test: /\.tsx?$/, loader: "ts-loader" };
const jsxRule = {
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
};
const fileRule = {
test: /\.(svg|png|jpe?g|gif|woff|woff2|eot|ttf|pdf)$/i,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
publicPath: "assets",
outputPath: "assets"
}
}
]
};
const webConfig = (env, args) => {
return {
mode: isEnvDevelopment ? "development" : "production",
// In development environment, turn on source map.
devtool: isEnvDevelopment ? "inline-source-map" : false,
// In development environment, webpack watch the file changes, and recompile
watch: isEnvDevelopment,
devServer: {
port: 8081
},
entry: {
main: ["./src/index.tsx"]
},
output: {
path: path.resolve(__dirname, isEnvDevelopment ? "dist" : "prod"),
filename: "[name].bundle.js"
},
resolve: commonResolve("src/assets"),
module: {
rules: [sassRule, tsRule, jsxRule, fileRule]
},
plugins: [
// Remove all and write anyway
// TODO: Optimizing build process
new CleanWebpackPlugin(),
new ForkTsCheckerWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./src/index.html",
chunks: ["main"]
}),
new WriteFilePlugin(),
new webpack.EnvironmentPlugin(["NODE_ENV"])
]
};
};
module.exports = webConfig;