forked from loftschool/course-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
83 lines (77 loc) · 1.96 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
const HtmlPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin').CleanWebpackPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const fs = require('fs');
const path = require('path');
const root = path.resolve('projects');
const projects = fs.readdirSync(root);
const entries = {};
const htmlPlugins = [];
const proxy = {};
for (const project of projects) {
const projectPath = path.join(root, project);
entries[project] = projectPath;
htmlPlugins.push(
new HtmlPlugin({
title: project,
template: path.resolve('./layout.html'),
filename: `${project}/index.html`,
chunks: [project],
})
);
const settingsPath = path.join(projectPath, 'settings.json');
if (fs.existsSync(settingsPath)) {
const settings = require(settingsPath);
Object.assign(proxy, settings.proxy);
}
}
const mode = process.env.NODE_ENV === 'production' ? 'production' : 'development';
module.exports = {
entry: entries,
output: {
filename: mode === 'production' ? 'name/[chunkhash].js' : '[name]/[name].js',
path: path.resolve('dist'),
},
mode,
devServer: {
proxy,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: { cacheDirectory: true },
},
{
test: /projects\/.+\.html/,
use: [
{ loader: './scripts/html-inject-loader.js' },
{
loader: 'raw-loader',
},
],
},
{
test: /\.(jpe?g|png|gif|svg|eot|ttf|woff|woff2)$/i,
loader: 'file-loader',
options: {
name: '[hash:8].[ext]',
outputPath: 'reosurces',
},
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
...htmlPlugins,
new CleanWebpackPlugin(),
],
};