forked from libertylocked/chrome-extension-typescript-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
144 lines (139 loc) · 3.85 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as path from "path";
import * as webpack from "webpack";
// webpack plugins
const CleanWebpackPlugin = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
// postcss plugins
const autoprefixer = require("autoprefixer");
const isProd = (): boolean => {
return process.env.NODE_ENV === "production";
};
const buildConfig: webpack.Configuration = {
entry: {
background_script: path.join(__dirname, "src/background_script/index.ts"),
content_script: path.join(__dirname, "src/content_script/index.ts"),
options: path.join(__dirname, "src/options/index.tsx"),
popup: path.join(__dirname, "src/popup/index.tsx"),
},
// tslint:disable-next-line:no-object-literal-type-assertion
module: {
rules: [
// compile ts
{
exclude: /node_modules/,
loader: "ts-loader",
test: /\.tsx?$/,
},
// css loader
// source maps are generated only for dev builds
// css compression is only used for prod builds
{
test: /\.css$/,
use: [
{ loader: "style-loader", options: { sourceMap: !isProd() } },
{
loader: "css-loader", options: {
localIdentName: isProd() ? "[hash:base64]" : "[path][name]__[local]__[hash:base64:6]",
minimize: isProd(),
modules: true,
sourceMap: !isProd(),
},
},
{
loader: "postcss-loader",
options: {
plugins: () => [autoprefixer({
browsers: [
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9",
],
})],
sourceMap: !isProd(),
},
},
],
},
// file loader for media assets
{
exclude: [
/\.(html?)$/,
/\.(ts|tsx|js|jsx)$/,
/\.css$/,
/\.json$/,
],
loader: "file-loader",
query: {
name: "[hash].[ext]",
outputPath: "media/",
publicPath: "build/",
},
},
],
} as webpack.NewModule,
output: {
filename: "[name].js",
path: path.join(__dirname, "dist/build"),
},
plugins: [
// pack common chunks
new webpack.optimize.CommonsChunkPlugin({
chunks: ["background_script", "content_script"],
minChunks: 2,
name: "common_for_scripts",
}),
new webpack.optimize.CommonsChunkPlugin({
chunks: ["popup", "options"],
minChunks: 2,
name: "common_for_ui",
}),
// exclude locale files in moment
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// copy files in public to dist
new CopyWebpackPlugin([{
context: "public",
from: {
dot: false,
glob: "**/*",
},
to: path.join(__dirname, "dist/"),
}]),
],
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"],
},
};
if (isProd()) {
// Production build tweaks
buildConfig.devtool = false;
buildConfig.plugins = (buildConfig.plugins || []).concat([
new webpack.DefinePlugin({
"process.env": { NODE_ENV: JSON.stringify("production") },
}),
// clean output files
new CleanWebpackPlugin(["dist"]),
// minify
new webpack.optimize.UglifyJsPlugin(),
]);
} else {
// Development build tweaks
const buildConfigModule = buildConfig.module as webpack.NewModule;
buildConfigModule.rules = (buildConfigModule.rules || []).concat([
// tslint
{
enforce: "pre",
exclude: /node_modules/,
loader: "tslint-loader",
test: /\.tsx?$/,
},
]);
buildConfig.plugins = (buildConfig.plugins || []).concat([
// exclude source mapping for vendor libs
new webpack.SourceMapDevToolPlugin({
exclude: /^vendor.*.\.js$/,
filename: "[file].map",
}),
]);
}
export default buildConfig;