-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebpack.config.js
117 lines (110 loc) · 3.05 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
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const config = require('./config');
const { normalizeText } = require('./utils/normalize');
const webpackConfig = {
context: path.resolve(__dirname),
entry: {
main: './src/js/global.js',
home: './src/views/templates/home/home.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/[name].js'
},
optimization: {
minimizer: [new OptimizeCSSAssetsPlugin({})]
},
module: {
rules: [
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {}
}
]
},
{
test: /\.(handlebars|hbs)$/,
loader: 'handlebars-loader',
query: {
partialDirs: [
path.join(__dirname, 'src'),
path.join(__dirname, 'src', 'views'),
path.join(__dirname, 'src', 'views', 'layouts'),
path.join(__dirname, 'src', 'views', 'templates'),
path.join(__dirname, 'src', 'views', 'partials')
].concat(
glob.sync('**/', {
cwd: path.resolve(__dirname, 'src', 'views', 'partials'),
realpath: true
})
)
}
},
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader'
}
]
},
{
test: /\.(png|svg|jpg|gif)$/,
loader: 'file-loader',
options: {
context: './static'
}
},
{
test: /\.(scss|sass)$/,
use: [
'style-loader',
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: './scss',
hmr: process.env.NODE_ENV === 'development'
}
},
'css-loader',
'sass-loader'
]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: 'css/[name].css',
chunkFilename: '[id].css'
}),
new CopyPlugin([{ from: './src/static' }])
],
devServer: {
contentBase: path.join(__dirname, 'dist')
}
};
fs.readdirSync(path.join(__dirname, 'src', 'views', 'templates')).forEach(page => {
console.log(`Building page: ${page.toUpperCase()}`);
const htmlPageInit = new HtmlWebPackPlugin({
title: `${normalizeText(page)} | Bakery`,
template: `./src/views/templates/${page}/${page}.hbs`,
filename: `./${page != "home" ? page + "/" : ""}index.html`,
chunks: ['main', page],
minify: config.htmlMinifyOptions
});
webpackConfig.entry[page] = `./src/views/templates/${page}/${page}.js`;
webpackConfig.plugins.push(htmlPageInit);
});
module.exports = webpackConfig;