-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
117 lines (107 loc) · 2.9 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
import * as CleanWebpackPlugin from "clean-webpack-plugin"
import CopyWebpackPlugin from "copy-webpack-plugin"
import HtmlWebpackPlugin from "html-webpack-plugin"
import { join, resolve } from "path"
import * as UglifyJSPlugin from "uglifyjs-webpack-plugin"
import * as webpack from "webpack"
interface IMergedConfigParams {
env: "prod" | "dev"
}
interface IPattern {
from: string
to?: string
}
function getMergedConfig({ env }: IMergedConfigParams): webpack.Configuration {
const baseConfig = getBaseConfig()
const specificConfig = getSpecificConfig(env, baseConfig)
return { ...baseConfig, ...specificConfig }
}
function getPatterns(): IPattern[] {
return [
{ from: "_locales", to: "_locales" },
{ from: "icons", to: "icons" },
{ from: "src/popup.html" },
{ from: "src/popup.css" },
{ from: "src/successAuth.html" },
{ from: "src/successAuth.css" },
{ from: "manifest.json" }
]
}
function getBaseConfig(): webpack.Configuration {
return {
devtool: "source-map",
entry: {
inject: join(__dirname, "src", "inject.ts"),
popup: join(__dirname, "src", "popup.ts"),
successAuth: join(__dirname, "src", "successAuth.ts")
},
externals: {
electron: "electron"
},
output: {
filename: "[name].js",
path: join(__dirname, "dist"),
publicPath: "/"
},
plugins: [new CleanWebpackPlugin(["dist"]), CopyWebpackPlugin(getPatterns())],
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"]
}
}
}
function getSpecificConfig(env: "prod" | "dev", baseConfig): webpack.Configuration {
return env === "prod" ? getProdConfig(baseConfig) : getDevConfig(baseConfig)
}
function getProdConfig(baseConfig: webpack.Configuration): webpack.Configuration {
return {
module: {
rules: [
{
test: /\.(t|j)s?$/,
use: { loader: "awesome-typescript-loader" }
},
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
plugins: [
...(baseConfig.plugins as webpack.Plugin[]),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
}),
new UglifyJSPlugin({
comments: false,
compress: true,
compressor: {
screw_ie8: true,
warnings: true
}
})
]
}
}
function getDevConfig(baseConfig): webpack.Configuration {
return {
module: {
rules: [
{
exclude: /node_modules/,
test: /\.(t|j)s?$/,
use: { loader: "awesome-typescript-loader" }
},
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
plugins: [
...baseConfig.plugins,
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("development")
}
}),
new webpack.NamedModulesPlugin()
]
}
}
export default getMergedConfig