-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebpack.config.js
More file actions
75 lines (73 loc) · 3.12 KB
/
webpack.config.js
File metadata and controls
75 lines (73 loc) · 3.12 KB
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
var HtmlWebpackPlugin = require("html-webpack-plugin");
var package = require("../package.json");
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
var path = require("path");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CopyWebpackPlugin = require("copy-webpack-plugin");
module.exports = {
entry: { // Here instead of one entry file we can use multiple entry file and created different chunk based on it
vendor: Object.keys(package.dependencies),
app: "./src/scripts/app.js",
settings: "./src/scripts/settings.js",
},
output: {
path: path.join(__dirname, "../dist/"),
filename: "[name].bundle.js", // [] means dynamic name and will be as per input file name
},
watch: true, // watch for changes and build the bundle as per changes
resolve: { extensions: [".js", ".ts"] }, // during import don't need to use extensions
devServer: { // to run the server locally and serve the files from dist folder and HMR will be enabled
contentBase: path.join(__dirname, "../dist/"),
port: 9000,
},
module: {
rules: [
{
test: /\.(s*)css$/,
use: ExtractTextPlugin.extract({ // without ExtractTextPlugin it will be injected in <style> tag
// and with use of plugin it will be injected in <link> tag and it will be extracted to a file
fallback: "style-loader", // inject css file in to HTML
use: ["css-loader", "sass-loader"], // sass-loader to convert scss to css
}),
},
{
test: /\.(png|jp(e*)g|svg)$/,
use: [
{
loader: "url-loader", // convert image to base64 string if file size is within limit and inject it in to HTML
options: {
limit: 8000, // Convert images < 8kb to base64 strings
name: "images/[hash]-[name].[ext]", // if file size exceeds limit then it is passed to file-loader which create seperate file in dist folder with hashed file name
},
},
],
},
],
},
plugins: [
new ExtractTextPlugin({ filename: "app.bundle.css" }),
new CommonsChunkPlugin({ // if a file is common to multiple chunks then it will be extracted to a seperate file and injected in to HTML
name: "shared",
minChunks: 2,
}),
new HtmlWebpackPlugin({
hash: true, // create hash for the file name to avoid cache busting
title: "My Awesome application",
myPageHeader: "Hello World",
template: "./src/index.html",
chunks: ["vendor", "shared", "app"], // mentioned which chunk to injected in <script> in HTML
path: path.join(__dirname, "../dist/"),
filename: "index.html",
}),
new HtmlWebpackPlugin({
hash: true,
title: "My Awesome application",
myPageHeader: "Settings",
template: "./src/index.html",
chunks: ["vendor", "shared", "settings"],
path: path.join(__dirname, "../dist/"),
filename: "settings.html",
}),
new CopyWebpackPlugin([{ from: "src/images", to: "images" }]), // copy images that from src to dist folder
],
};