-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.common.js
90 lines (81 loc) · 2.89 KB
/
webpack.common.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
const fs = require("fs");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const FileManagerPlugin = require("filemanager-webpack-plugin");
const SimpleProgressWebpackPlugin = require("simple-progress-webpack-plugin");
const { DefinePlugin } = require("webpack");
const router = require("./router");
const paths = require("./webpack.paths");
const pages = router.map(config => ({
...config,
keyName: config.source.replace(/\//g, "sl").replace(/-/g, ""),
component: fs.readFileSync(`${paths.pages}${config.source}/index.ts`).toString().match(/@customElement\("([^"]+)"\)/)[1],
}));
for (let i = 0; i < pages.length; i++)
pages[i].outputPath = pages[i].outputPath === undefined ? pages[i].source : pages[i].outputPath;
const config = {
entry: Object.fromEntries(pages.map(page => [page.keyName, `${paths.pages}${page.source}/index.ts`])),
output: {
path: `${paths.build}`,
filename: "bundles/[name].[contenthash].js",
},
plugins: [
...pages.map(page => new HtmlWebpackPlugin({
template: `${paths.pages}/base.html`,
templateParameters: {
title: page.title,
description: page.description,
component: `<${page.component}></${page.component}>`,
},
filename: `.${page.outputPath}/index.html`,
chunks: [page.keyName],
})),
new DefinePlugin({
PRODUCTION: process.env.NODE_ENV.startsWith("production"),
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
}),
new FileManagerPlugin({
runTasksInSeries: true,
events: {
onStart: {
delete: [`${paths.build}/`],
mkdir: [`${paths.build}/`],
},
onEnd: {
copy: [
{ source: `${paths.src}/assets/to-root`, destination: `${paths.build}/` },
],
},
},
}),
],
module: {
rules: [{
test: /\.(ts|tsx)$/,
use: [{
loader: "esbuild-loader",
options: {
loader: "ts",
target: "es6",
},
}],
}, {
test: /.(scss|css)$/,
use: [
{ loader: "lit-css-loader" },
{ loader: "sass-loader" },
],
}, {
test: /\.(png|svg|jpg|jpeg|gif)$/,
type: "asset/resource",
}],
},
resolve: {
extensions: [".tsx", ".ts", ".js", ".css", ".scss"],
plugins: [new TsconfigPathsPlugin()],
},
};
// This plugin brakes stats.json
if (!process.env.NODE_ENV.endsWith("stats"))
config.plugins.push(new SimpleProgressWebpackPlugin());
module.exports = config;