-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
142 lines (138 loc) · 3.88 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
/* 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 fileRule = {
test: /\.(svg|png|jpe?g|gif|woff|woff2|eot|ttf)$/i,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
publicPath: "assets",
outputPath: "assets"
}
}
]
};
const extensionConfig = (env, args) => {
return {
name: "extension",
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,
entry: {
popup: ["./src/ui/popup/popup.tsx"],
background: ["./src/background/background.ts"],
contentScripts: ["./src/content-scripts/content-scripts.ts"],
injectedScript: ["./src/content-scripts/inject/injected-script.ts"]
},
output: {
path: path.resolve(__dirname, "dist/extension"),
filename: "[name].bundle.js"
},
resolve: commonResolve("src/ui/popup/public/assets"),
module: {
rules: [sassRule, tsRule, fileRule]
},
plugins: [
// Remove all and write anyway
// TODO: Optimizing build process
new CleanWebpackPlugin(),
new ForkTsCheckerWebpackPlugin(),
new CopyWebpackPlugin(
[
{
from: "./src/manifest.json",
to: "./"
}
],
{ copyUnmodified: true }
),
new HtmlWebpackPlugin({
template: "./src/popup.html",
filename: "popup.html",
chunks: ["popup"]
}),
new WriteFilePlugin(),
new webpack.EnvironmentPlugin(["NODE_ENV"])
]
};
};
const webConfig = (env, args) => {
return {
name: "web",
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/ui/web/web.tsx"]
},
output: {
path: path.resolve(__dirname, "dist/web"),
filename: "[name].bundle.js"
},
resolve: commonResolve("src/ui/web/public/assets"),
module: {
rules: [sassRule, tsRule, fileRule]
},
plugins: [
new ForkTsCheckerWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./src/web.html",
filename: "index.html",
chunks: ["main"]
}),
new webpack.EnvironmentPlugin(["NODE_ENV"])
]
};
};
module.exports = [extensionConfig, webConfig];