-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
151 lines (145 loc) · 5.84 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
const fs = require("fs"),
path = require("path"),
webpack = require("webpack"),
ExtractTextPlugin = require("extract-text-webpack-plugin"),
extractLESS = new ExtractTextPlugin({filename: "css/style_vendor.[hash].css", allChunks: false}),
extractSASS = new ExtractTextPlugin({filename: "css/style_bundle.[hash].css", allChunks: true}),
OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
module.exports = function Compile(env) {
env = env || "development";
return {
entry: {
main: ["babel-polyfill", "./frontend/app.js", "./frontend/index.scss"],
vendor: ['jquery', 'angular', "bootstrap/dist/js/bootstrap.min.js", "bootstrap/less/bootstrap.less",
"angular-ui-bootstrap", "moment"]
},
output: {
path: path.resolve("CarSale/static/base/"),
publicPath: "/static/base/",
filename: "js/bundle.[hash].js"
},
externals: {
jquery: 'jQuery',
angular: "angular",
moment: "moment",
"uibootstrap": "'ui.bootstrap'",
"ngRoute": "angular-route"
},
resolve: {
alias: {}
},
module: {
rules: [
{
loaders: ["babel-loader"],
include: [
path.resolve(__dirname, "frontend")
],
test: /\.js$/
},
{
test: /\.scss$/,
loader: extractSASS.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader']
})
},
{
test: /\.less$/,
loader: extractLESS.extract({
fallback: 'style-loader',
use: ['css-loader', 'less-loader']
})
},
{
test: /\.(eot|svg|ttf|woff|woff2)(\?[a-z0-9]+)?$/,
loader: "file-loader"
},
{
test: /\.json$/,
loader: "json-loader"
},
{
test: /\.html$/,
loader: "raw-loader"
}
]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
"jQuery": "jquery",
"window.jQuery": "jquery",
"window.angular": "angular",
"moment": "moment"
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.optimize.OccurrenceOrderPlugin(true),
extractLESS,
extractSASS,
new OptimizeCssAssetsPlugin({
cssProcessor: require("cssnano"),
cssProcessorOptions: {discardComments: {removeAll: true}}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: env !== "production",
drop_console: env === "production",
unsafe: true
},
output: {
comments: false
},
sourceMap: false
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'js/vendor.[hash].js',
minChunks: Infinity
}),
function () {
this.plugin("done", function (stats) {
const replaceInJsFile = function (filePath, toReplace, replacement, kw = "bundle") {
const replacer = function (match) {
console.log('Replacing in %s: %s => %s', filePath, match, replacement);
return replacement;
};
const str = fs.readFileSync(filePath, "utf8");
const out = str.replace(new RegExp(`${kw}\\.?(.+)?\.js`, "gi"), replacer);
fs.writeFileSync(filePath, out);
};
const replaceInCssFile = function (filePath, toReplace, replacement, kw = "style_bundle") {
const replacer = function (match) {
console.log('Replacing in %s: %s => %s', filePath, match, replacement);
return replacement;
};
const str = fs.readFileSync(filePath, "utf8");
const out = str.replace(new RegExp(`${kw}\\.?(.+)?\.css`, "gi"), replacer);
fs.writeFileSync(filePath, out);
};
const hash = stats.hash; // Build's hash, found in `stats` since build lifecycle is done.
replaceInJsFile(path.resolve("./templates/", "index.html"),
"bundle.js",
"bundle." + hash + ".js"
);
replaceInJsFile(path.resolve("./templates/", "index.html"),
"vendor.js",
"vendor." + hash + ".js",
"vendor"
);
replaceInCssFile(path.resolve("./templates/", "index.html"),
"style_bundle.css",
"style_bundle." + hash + ".css"
);
replaceInCssFile(path.resolve("./templates/", "index.html"),
"style_vendor.css",
"style_vendor." + hash + ".css",
"style_vendor"
);
});
}
],
devtool: env === "development" ? "eval" : false,
watch: env === "development"
};
};