-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
169 lines (163 loc) · 5.21 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var path = require("path");
const webpack = require("webpack");
var merge = require("webpack-merge");
var CopyWebpackPlugin = require("copy-webpack-plugin");
var HTMLWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
var TARGET_ENV =
process.env.npm_lifecycle_event === "prod" ? "production" : "development";
var filename = TARGET_ENV == "production" ? "[name]-[hash].js" : "[name].js";
var common = {
entry: {
index: "./src/index.js",
'custom-element': "./src/custom-element.js"
},
output: {
path: path.join(__dirname, "dist"),
// webpack -p automatically adds hash when building for production
filename: '[name].js' //filename
},
plugins: [
new HTMLWebpackPlugin({
// using .ejs prevents other loaders causing errors
template: "src/index.ejs",
// inject details of output file at end of body
inject: "body"
}),
new UglifyJsPlugin({
uglifyOptions: {
ecma: 6,
compress: {
pure_funcs: "F2,F3,F4,F5,F6,F7,F8,F9",
pure_getters: true,
keep_fargs: false,
unsafe_comps: true,
unsafe_methods: true,
unsafe_arrows: true
}
}
})
],
resolve: {
modules: [path.join(__dirname, "src"), "node_modules"],
extensions: [".js", ".elm", ".png"]
},
module: {
rules: [
{
test: /\.html$/,
exclude: /node_modules/,
loader: "file-loader?name=[name].[ext]"
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
// env: automatically determines the Babel plugins you need based on your supported environments
// presets: ["env"]
presets: [["env", {
"targets": {
"chrome": 52
}}]]
}
}
},
{
test: /\.css$/,
exclude: [/elm-stuff/, /node_modules/],
loaders: ["to-string-loader", "css-loader"]
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
exclude: [/elm-stuff/, /node_modules/],
loader: "url-loader",
options: {
limit: 10000,
mimetype: "application/font-woff"
}
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
exclude: [/elm-stuff/, /node_modules/],
loader: "file-loader"
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loader: "file-loader"
}
]
}
};
if (TARGET_ENV === "development") {
console.log("Building for dev...");
module.exports = merge(common, {
plugins: [
// Suggested for hot-loading
new webpack.NamedModulesPlugin(),
// Prevents compilation errors causing the hot loader to lose state
new webpack.NoEmitOnErrorsPlugin()
],
module: {
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
{
loader: "elm-hot-loader"
},
{
loader: "elm-webpack-loader",
// add Elm's debug overlay to output
options: {
debug: false
}
}
]
}
]
},
devServer: {
inline: true,
stats: "errors-only",
contentBase: path.join(__dirname, "src/assets"),
// For SPAs: serve index.html in place of 404 responses
historyApiFallback: true
}
});
}
if (TARGET_ENV === "production") {
console.log("Building for prod...");
module.exports = merge(common, {
plugins: [
// Delete everything from output directory and report to user
new CleanWebpackPlugin(["dist"], {
root: __dirname,
exclude: [],
verbose: true,
dry: false
}),
new CopyWebpackPlugin([
{
from: "src/assets"
}
]),
],
module: {
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
{
loader: "elm-webpack-loader"
}
]
}
]
}
});
}