-
Notifications
You must be signed in to change notification settings - Fork 241
/
vite.config.mts
125 lines (114 loc) · 4.12 KB
/
vite.config.mts
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
import { execSync } from "child_process";
import path from "path";
import { defineConfig, normalizePath, type ConfigEnv, type Plugin } from "vite";
import dtsBundleGenerator from "vite-plugin-dts-bundle-generator";
import typescript from "vite-plugin-typescript";
import rollupLicensePlugin from "rollup-plugin-license";
export default function viteConfig(configEnv: ConfigEnv) {
const isServe = configEnv.command === "serve";
if (isServe) {
return serveConfig(configEnv);
}
const buildTestbed = process.env.BUILD_TESTBED === "true";
return buildConfig(configEnv, buildTestbed);
}
function buildConfig(configEnv: ConfigEnv, buildTestbed: boolean) {
const filename = buildTestbed ? "planck-with-testbed" : "planck";
const entry = normalizePath(path.resolve(__dirname, buildTestbed ? "testbed" : "src", "main.ts"));
return defineConfig({
define: {
ASSERT: "false",
_ASSERT: "false",
CONSTRUCTOR_FACTORY: "true",
_CONSTRUCTOR_FACTORY: "true",
},
build: {
lib: {
entry: entry,
name: "planck",
fileName: function (format) {
if (format === "umd") {
return filename + ".js";
} else if (format === "es") {
return filename + ".mjs";
}
return filename + "." + format + ".js";
},
formats: ["es", "umd"],
},
emptyOutDir: false,
minify: false,
sourcemap: true,
},
plugins: [
rollupLicensePlugin({
banner: getLicense(),
}) as Plugin,
typescript({}),
dtsBundleGenerator({
fileName: filename + ".d.ts",
})
]
});
}
function serveConfig(configEnv: ConfigEnv) {
const commitDate = execSync("git log -1 --format=%cI").toString().trimEnd();
const branchName = execSync("git rev-parse --abbrev-ref HEAD").toString().trimEnd();
const commitHash = execSync("git rev-parse HEAD").toString().trimEnd();
const lastCommitMessage = execSync("git show -s --format=%s").toString().trimEnd();
// used in benchmark
process.env.VITE_GIT_COMMIT_DATE = commitDate;
process.env.VITE_GIT_BRANCH_NAME = branchName;
process.env.VITE_GIT_COMMIT_HASH = commitHash;
process.env.VITE_GIT_LAST_COMMIT_MESSAGE = lastCommitMessage;
return defineConfig({
define: {
ASSERT: "false",
_ASSERT: "false",
CONSTRUCTOR_FACTORY: "false",
_CONSTRUCTOR_FACTORY: "false",
},
plugins: [
TestbedPlugin,
]
});
}
/** A plugin to serve the testbed for /example/* request */
const TestbedPlugin: Plugin = {
name: "testbed-plugin",
configureServer(server) {
server.middlewares.use(function (req, res, next) {
// console.log("middleware", req);
// todo: check if the url + ".js" or ".ts" is a file
if (req.originalUrl && /^\/example\/(\w|-)+$/.test(req.originalUrl)) {
req.url = "/testbed/";
}
next();
});
}
};
function getLicense() {
const version = process.env.npm_package_version;
const year = new Date().getFullYear();
const license = `
Planck.js v${version ?? "?"}
@license The MIT license
@copyright Copyright (c) ${year} Erin Catto, Ali Shakiba
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`;
return license;
}