-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
74 lines (67 loc) · 1.87 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
const HtmlPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const rules = require('./webpack.config.rules');
const fs = require('fs');
const path = require('path');
const root = path.resolve('src');
const files = fs.readdirSync(root)
.reduce((all, current) => {
const ext = path.extname(current);
const name = path.basename(current, ext);
const absPath = path.join(root, current);
if (!all.hasOwnProperty(ext)) {
all[ext] = [];
}
all[ext].push({ name, absPath });
return all;
}, { '.js': [], '.hbs': [] });
const entries = files['.js'].reduce((all, { name, absPath }) => {
all[name] = absPath;
return all;
}, {});
const html = files['.hbs']
.filter(file => entries.hasOwnProperty(file.name))
.map((file) => {
return new HtmlPlugin({
title: file.name,
template: file.absPath,
filename: `${file.name}.html`,
chunks: [file.name]
});
});
if (!html.length || !files['.hbs'].find(file => file.name === 'index')) {
html.push(new HtmlPlugin({
title: 'index',
template: 'index.hbs',
chunks: ['index']
}));
}
module.exports = {
entry: entries,
output: {
filename: '[name].[hash].js',
path: path.resolve('dist')
},
mode: 'development',
devtool: 'source-map',
module: {
rules: [
...rules,
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
...html,
new CleanWebpackPlugin(['dist'])
]
};