-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
125 lines (117 loc) · 2.74 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
const fs = require('fs');
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
const isDev = process.env.NODE_ENV === 'development';
require('dotenv').config({
path: path.resolve(__dirname, `.env.${process.env.NODE_ENV}`),
});
// Helper function to generate entries
function generateEntries(directory) {
const files = fs.readdirSync(directory);
const entries = {};
files.forEach((file) => {
console.log(file);
const name = path.parse(file).name;
if (path.extname(file) == '.hbs') {
entries[name] = path.join(directory, file);
}
});
return entries;
}
// Set the directory path for pages
const pagesDirectory = path.join(__dirname, 'src/pages/');
module.exports = {
target: 'web',
mode: isDev ? 'development' : 'production',
devtool: 'eval-cheap-source-map',
output: {
path: path.resolve(__dirname, 'build/'),
},
resolve: {
alias: {
'@fonts': path.join(__dirname, 'src/fonts'),
'@images': path.join(__dirname, 'src/images'),
'@scripts': path.join(__dirname, 'src/scripts'),
'@styles': path.join(__dirname, 'src/styles'),
},
modules: [path.resolve(__dirname, 'node_modules'), 'node_modules'],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlBundlerPlugin({
// entry: 'src/pages/',
entry: generateEntries(pagesDirectory),
data: 'src/data/global.json',
preprocessor: 'handlebars',
preprocessorOptions: {
partials: ['src/partials/', 'src/pages/'],
helpers: {
pageData: function (context) {
context = this;
console.log('context: ', context);
},
},
},
js: {
filename: 'js/[name].[contenthash:8].js',
},
css: {
filename: 'css/[name].[contenthash:8].css',
},
// verbose: true,
}),
function () {
console.log('NODE_ENV: ', process.env.NODE_ENV);
console.log('isDev: ', isDev);
console.log(
'Resolved .env path: ',
path.resolve(__dirname, `.env.${process.env.NODE_ENV}`)
);
console.log('BASE_URL: ', process.env.BASE_URL);
console.log('entries: ', generateEntries(pagesDirectory));
},
],
module: {
rules: [
{
test: /\.js$/,
include: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.s?css/i,
use: [
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
'postcss-loader',
'sass-loader',
],
},
{
test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
type: 'asset/resource',
},
],
},
devServer: {
port: 8000,
open: true,
static: path.resolve(__dirname, 'build'),
watchFiles: {
paths: ['src/**/*.*'],
options: {
usePolling: true,
},
},
client: {
logging: 'error',
},
},
};