unplugin-version-injector 1.1.1-alpha.5 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -110
- package/README.zh-CN.md +66 -109
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +60 -0
- package/dist/cjs/types.d.ts +5 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utils.d.ts +4 -0
- package/dist/cjs/utils.js +27 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +58 -0
- package/dist/esm/types.d.ts +5 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/utils.d.ts +4 -0
- package/dist/esm/utils.js +19 -0
- package/package.json +25 -47
- package/dist/core.d.mts +0 -6
- package/dist/core.d.ts +0 -6
- package/dist/core.js +0 -140
- package/dist/core.mjs +0 -112
- package/dist/types-DEOBLqEx.d.mts +0 -9
- package/dist/types-DEOBLqEx.d.ts +0 -9
- package/dist/webpack.d.mts +0 -6
- package/dist/webpack.d.ts +0 -6
- package/dist/webpack.js +0 -146
- package/dist/webpack.mjs +0 -120
package/dist/webpack.mjs
DELETED
@@ -1,120 +0,0 @@
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
3
|
-
}) : x)(function(x) {
|
4
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
5
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
6
|
-
});
|
7
|
-
|
8
|
-
// src/core.ts
|
9
|
-
import { createUnplugin } from "unplugin";
|
10
|
-
|
11
|
-
// src/utils.ts
|
12
|
-
import fs from "node:fs";
|
13
|
-
import path from "node:path";
|
14
|
-
import { fileURLToPath } from "node:url";
|
15
|
-
var import_meta = {};
|
16
|
-
function getCurrentDir() {
|
17
|
-
try {
|
18
|
-
return path.dirname(fileURLToPath(import_meta.url));
|
19
|
-
} catch (e) {
|
20
|
-
return process.cwd();
|
21
|
-
}
|
22
|
-
}
|
23
|
-
function getPackageVersion() {
|
24
|
-
var _a;
|
25
|
-
try {
|
26
|
-
let dir = getCurrentDir();
|
27
|
-
while (dir !== path.parse(dir).root) {
|
28
|
-
const pkgPath = path.join(dir, "package.json");
|
29
|
-
if (fs.existsSync(pkgPath)) {
|
30
|
-
const content = fs.readFileSync(pkgPath, "utf-8");
|
31
|
-
const pkg = JSON.parse(content);
|
32
|
-
return (_a = pkg.version) != null ? _a : "0.0.0";
|
33
|
-
}
|
34
|
-
dir = path.dirname(dir);
|
35
|
-
}
|
36
|
-
console.warn("[VersionInjector] package.json not found");
|
37
|
-
return "0.0.0";
|
38
|
-
} catch (err) {
|
39
|
-
console.warn("[VersionInjector] Failed to read package.json:", err);
|
40
|
-
return "0.0.0";
|
41
|
-
}
|
42
|
-
}
|
43
|
-
function defaultFormatDate(date) {
|
44
|
-
return date.toISOString();
|
45
|
-
}
|
46
|
-
|
47
|
-
// src/core.ts
|
48
|
-
var dayjs;
|
49
|
-
var versionInjectorPlugin = createUnplugin((options = {}) => {
|
50
|
-
const {
|
51
|
-
version = getPackageVersion(),
|
52
|
-
formatDate,
|
53
|
-
dateFormat,
|
54
|
-
injectToHead = true,
|
55
|
-
injectToBody = true
|
56
|
-
} = options;
|
57
|
-
let resolvedFormatDate = defaultFormatDate;
|
58
|
-
if (typeof formatDate === "function") {
|
59
|
-
resolvedFormatDate = formatDate;
|
60
|
-
} else if (typeof dateFormat === "string") {
|
61
|
-
try {
|
62
|
-
dayjs = __require("dayjs");
|
63
|
-
resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
|
64
|
-
} catch (err) {
|
65
|
-
console.warn("[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.");
|
66
|
-
}
|
67
|
-
}
|
68
|
-
if (!injectToHead && !injectToBody) {
|
69
|
-
return { name: "unplugin-version-injector" };
|
70
|
-
}
|
71
|
-
const buildTime = resolvedFormatDate(/* @__PURE__ */ new Date());
|
72
|
-
const metaTag = `<meta name="version" content="${version}">
|
73
|
-
`;
|
74
|
-
const logScript = `
|
75
|
-
<script>
|
76
|
-
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
77
|
-
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
78
|
-
</script>`;
|
79
|
-
function processHtml(html) {
|
80
|
-
if (injectToHead && !html.includes('<meta name="version"')) {
|
81
|
-
html = html.replace(/<head>/, `<head>
|
82
|
-
${metaTag}`);
|
83
|
-
}
|
84
|
-
if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
|
85
|
-
html = html.replace("</body>", ` ${logScript}
|
86
|
-
</body>`);
|
87
|
-
}
|
88
|
-
return html;
|
89
|
-
}
|
90
|
-
return {
|
91
|
-
name: "unplugin-version-injector",
|
92
|
-
vite: {
|
93
|
-
transformIndexHtml(html) {
|
94
|
-
return processHtml(html);
|
95
|
-
}
|
96
|
-
},
|
97
|
-
rollup: {
|
98
|
-
generateBundle(_, bundle) {
|
99
|
-
for (const file of Object.values(bundle)) {
|
100
|
-
if (file.type === "asset" && file.fileName.endsWith(".html")) {
|
101
|
-
file.source = processHtml(file.source);
|
102
|
-
}
|
103
|
-
}
|
104
|
-
}
|
105
|
-
}
|
106
|
-
};
|
107
|
-
});
|
108
|
-
|
109
|
-
// src/webpack.ts
|
110
|
-
function versionInjector(options = {}) {
|
111
|
-
return versionInjectorPlugin.webpack(options);
|
112
|
-
}
|
113
|
-
var webpack_default = versionInjector;
|
114
|
-
if (typeof module !== "undefined") {
|
115
|
-
module.exports = versionInjector;
|
116
|
-
module.exports.default = versionInjector;
|
117
|
-
}
|
118
|
-
export {
|
119
|
-
webpack_default as default
|
120
|
-
};
|