-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.common.ts
128 lines (121 loc) · 3.67 KB
/
webpack.common.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
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
126
127
128
import * as cheerio from "cheerio";
import CopyWebpackPlugin from "copy-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import path from "path";
import type { Configuration as WebpackConfiguration } from "webpack";
import ZipPlugin from "zip-webpack-plugin";
import type { WebpackEnv } from "./apps";
import { appData } from "./apps";
import packageJson from "./package.json";
interface PackageJson {
version: string;
}
const getZipFilename = (env: WebpackEnv): string => {
const ZIP_NAME_FORMATS: Record<string, string> = {
default: "dist-{platform}-v{version}",
JetBrains: "digma-ui-{version}"
};
const argFormat = env.ZIP_FILE_FORMAT;
const format =
(argFormat && ZIP_NAME_FORMATS[argFormat]) ?? ZIP_NAME_FORMATS.default;
return format
.replace("{platform}", (env.PLATFORM ?? "").toLocaleLowerCase())
.replace("{version}", (packageJson as PackageJson).version)
.replace(/-{2,}/g, "-");
};
const getConfig = (env: WebpackEnv): WebpackConfiguration => {
const entriesToBuild: Record<string, string> = Object.entries(appData)
.filter(
([, entry]) => !env.PLATFORM || entry.platforms.includes(env.PLATFORM)
)
.reduce(
(acc, [name, entry]) => ({
...acc,
[name]: entry.entry
}),
{}
);
return {
entry: entriesToBuild,
resolve: {
extensions: [".ts", ".tsx", ".js"]
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name]/index.[contenthash].js",
publicPath: ""
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "./public")
},
...(env.PLATFORM === "JetBrains"
? [
{
from: path.resolve(__dirname, `./jaeger-ui/dist/static`),
to: "jaeger-ui/static"
},
{
from: path.resolve(__dirname, `./jaeger-ui/dist/index.html`),
to: "jaeger-ui/index.html",
transform(content: Buffer) {
const BASE_URL = "/jaeger-ui/";
const $ = cheerio.load(content.toString());
$('base[data-inject-target="BASE_URL"]').attr(
"href",
BASE_URL
);
$("head").append('<script src="./env.js"></script>');
return Buffer.from($.html());
}
}
]
: [])
]
}),
...Object.keys(entriesToBuild).map((app) => {
return new HtmlWebpackPlugin({
template: path.resolve(
__dirname,
`./assets/${env.PLATFORM === "Web" ? "index.web.ejs" : "index.ejs"}`
),
filename: `${app}/index.html`,
chunks: [app],
inject: false,
minify: false,
scriptLoading: "blocking",
templateParameters: (compilation, assets, assetTags, options) => ({
compilation,
webpackConfig: compilation.options,
htmlWebpackPlugin: {
tags: assetTags,
files: assets,
options
},
version: (packageJson as PackageJson).version,
jsAssets: assets.js.map((js) => path.basename(js)),
baseUrl: `/${app}/`
})
});
}),
...(env.ZIP
? [
new ZipPlugin({
filename: getZipFilename(env)
})
]
: [])
]
};
};
export default getConfig;