-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwebpack.config.ts
88 lines (83 loc) · 2.23 KB
/
webpack.config.ts
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
import path from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";
import webpack from "webpack";
import { getGithubUserContribution } from "@snk/github-user-contribution";
import { config } from "dotenv";
import type { Configuration as WebpackConfiguration } from "webpack";
import {
ExpressRequestHandler,
type Configuration as WebpackDevServerConfiguration,
} from "webpack-dev-server";
config({ path: __dirname + "/../../.env" });
const demos: string[] = require("./demo.json");
const webpackDevServerConfiguration: WebpackDevServerConfiguration = {
open: { target: demos[1] + ".html" },
setupMiddlewares: (ms) => [
...ms,
(async (req, res, next) => {
const userName = req.url.match(
/\/api\/github-user-contribution\/(\w+)/
)?.[1];
if (userName)
res.send(
await getGithubUserContribution(userName, {
githubToken: process.env.GITHUB_TOKEN!,
})
);
else next();
}) as ExpressRequestHandler,
],
};
const webpackConfiguration: WebpackConfiguration = {
mode: "development",
entry: Object.fromEntries(
demos.map((demo: string) => [demo, `./demo.${demo}`])
),
target: ["web", "es2019"],
resolve: { extensions: [".ts", ".js"] },
output: {
path: path.join(__dirname, "dist"),
filename: "[contenthash].js",
},
module: {
rules: [
{
exclude: /node_modules/,
test: /\.ts$/,
loader: "ts-loader",
options: {
transpileOnly: true,
compilerOptions: {
lib: ["dom", "es2020"],
target: "es2019",
},
},
},
],
},
plugins: [
...demos.map(
(demo) =>
new HtmlWebpackPlugin({
title: "snk - " + demo,
filename: `${demo}.html`,
chunks: [demo],
})
),
new HtmlWebpackPlugin({
title: "snk - " + demos[0],
filename: `index.html`,
chunks: [demos[0]],
}),
new webpack.EnvironmentPlugin({
GITHUB_USER_CONTRIBUTION_API_ENDPOINT:
process.env.GITHUB_USER_CONTRIBUTION_API_ENDPOINT ??
"/api/github-user-contribution/",
}),
],
devtool: false,
};
export default {
...webpackConfiguration,
devServer: webpackDevServerConfiguration,
};