unplugin-version-injector 1.1.0 → 1.1.1-alpha.1
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/dist/core.d.mts +6 -0
- package/dist/core.d.ts +6 -0
- package/dist/core.js +132 -0
- package/dist/core.mjs +104 -0
- package/dist/{esm/types.d.ts → types-DEOBLqEx.d.mts} +3 -1
- package/dist/{cjs/types.d.ts → types-DEOBLqEx.d.ts} +3 -1
- package/dist/webpack.d.mts +6 -0
- package/dist/webpack.d.ts +6 -0
- package/dist/webpack.js +133 -0
- package/dist/webpack.mjs +107 -0
- package/package.json +59 -37
- package/dist/cjs/index.d.ts +0 -3
- package/dist/cjs/index.js +0 -95
- package/dist/cjs/types.js +0 -2
- package/dist/cjs/utils.d.ts +0 -4
- package/dist/cjs/utils.js +0 -27
- package/dist/esm/index.d.ts +0 -3
- package/dist/esm/index.js +0 -93
- package/dist/esm/types.js +0 -1
- package/dist/esm/utils.d.ts +0 -4
- package/dist/esm/utils.js +0 -19
package/dist/core.d.mts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import * as unplugin from 'unplugin';
|
2
|
+
import { V as VersionInjectorOptions } from './types-DEOBLqEx.mjs';
|
3
|
+
|
4
|
+
declare const versionInjectorPlugin: unplugin.UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
|
5
|
+
|
6
|
+
export { versionInjectorPlugin as default, versionInjectorPlugin };
|
package/dist/core.d.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import * as unplugin from 'unplugin';
|
2
|
+
import { V as VersionInjectorOptions } from './types-DEOBLqEx.js';
|
3
|
+
|
4
|
+
declare const versionInjectorPlugin: unplugin.UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
|
5
|
+
|
6
|
+
export { versionInjectorPlugin as default, versionInjectorPlugin };
|
package/dist/core.js
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// src/core.ts
|
31
|
+
var core_exports = {};
|
32
|
+
__export(core_exports, {
|
33
|
+
default: () => core_default,
|
34
|
+
versionInjectorPlugin: () => versionInjectorPlugin
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(core_exports);
|
37
|
+
var import_unplugin = require("unplugin");
|
38
|
+
|
39
|
+
// src/utils.ts
|
40
|
+
var import_node_path = __toESM(require("path"));
|
41
|
+
var import_node_fs = __toESM(require("fs"));
|
42
|
+
function getCallerDir() {
|
43
|
+
const _stack = new Error().stack;
|
44
|
+
if (!_stack) return process.cwd();
|
45
|
+
const stackLines = _stack.split("\n");
|
46
|
+
const callerLine = stackLines.find((l) => l.includes(".js") || l.includes(".ts")) || "";
|
47
|
+
const match = callerLine.match(/\((.*):\d+:\d+\)$/) || callerLine.match(/at (.*):\d+:\d+/);
|
48
|
+
if (match) return import_node_path.default.dirname(match[1]);
|
49
|
+
return process.cwd();
|
50
|
+
}
|
51
|
+
function getPackageVersion() {
|
52
|
+
try {
|
53
|
+
const baseDir = getCallerDir();
|
54
|
+
const pkgPath = import_node_path.default.resolve(baseDir, "package.json");
|
55
|
+
const content = import_node_fs.default.readFileSync(pkgPath, "utf-8");
|
56
|
+
const pkg = JSON.parse(content);
|
57
|
+
return pkg.version || "0.0.0";
|
58
|
+
} catch (e) {
|
59
|
+
console.warn("[VersionInjector] Failed to read package.json:", e);
|
60
|
+
return "0.0.0";
|
61
|
+
}
|
62
|
+
}
|
63
|
+
function defaultFormatDate(date) {
|
64
|
+
return date.toISOString();
|
65
|
+
}
|
66
|
+
|
67
|
+
// src/core.ts
|
68
|
+
var dayjs;
|
69
|
+
var versionInjectorPlugin = (0, import_unplugin.createUnplugin)((options = {}) => {
|
70
|
+
const {
|
71
|
+
version = getPackageVersion(),
|
72
|
+
formatDate,
|
73
|
+
dateFormat,
|
74
|
+
injectToHead = true,
|
75
|
+
injectToBody = true
|
76
|
+
} = options;
|
77
|
+
let resolvedFormatDate = defaultFormatDate;
|
78
|
+
if (typeof formatDate === "function") {
|
79
|
+
resolvedFormatDate = formatDate;
|
80
|
+
} else if (typeof dateFormat === "string") {
|
81
|
+
try {
|
82
|
+
dayjs = require("dayjs");
|
83
|
+
resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
|
84
|
+
} catch (err) {
|
85
|
+
console.warn("[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.");
|
86
|
+
}
|
87
|
+
}
|
88
|
+
if (!injectToHead && !injectToBody) {
|
89
|
+
return { name: "unplugin-version-injector" };
|
90
|
+
}
|
91
|
+
const buildTime = resolvedFormatDate(/* @__PURE__ */ new Date());
|
92
|
+
const metaTag = `<meta name="version" content="${version}">
|
93
|
+
`;
|
94
|
+
const logScript = `
|
95
|
+
<script>
|
96
|
+
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
97
|
+
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
98
|
+
</script>`;
|
99
|
+
function processHtml(html) {
|
100
|
+
if (injectToHead && !html.includes('<meta name="version"')) {
|
101
|
+
html = html.replace(/<head>/, `<head>
|
102
|
+
${metaTag}`);
|
103
|
+
}
|
104
|
+
if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
|
105
|
+
html = html.replace("</body>", ` ${logScript}
|
106
|
+
</body>`);
|
107
|
+
}
|
108
|
+
return html;
|
109
|
+
}
|
110
|
+
return {
|
111
|
+
name: "unplugin-version-injector",
|
112
|
+
vite: {
|
113
|
+
transformIndexHtml(html) {
|
114
|
+
return processHtml(html);
|
115
|
+
}
|
116
|
+
},
|
117
|
+
rollup: {
|
118
|
+
generateBundle(_, bundle) {
|
119
|
+
for (const file of Object.values(bundle)) {
|
120
|
+
if (file.type === "asset" && file.fileName.endsWith(".html")) {
|
121
|
+
file.source = processHtml(file.source);
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
};
|
127
|
+
});
|
128
|
+
var core_default = versionInjectorPlugin;
|
129
|
+
// Annotate the CommonJS export names for ESM import in node:
|
130
|
+
0 && (module.exports = {
|
131
|
+
versionInjectorPlugin
|
132
|
+
});
|
package/dist/core.mjs
ADDED
@@ -0,0 +1,104 @@
|
|
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 path from "node:path";
|
13
|
+
import fs from "node:fs";
|
14
|
+
function getCallerDir() {
|
15
|
+
const _stack = new Error().stack;
|
16
|
+
if (!_stack) return process.cwd();
|
17
|
+
const stackLines = _stack.split("\n");
|
18
|
+
const callerLine = stackLines.find((l) => l.includes(".js") || l.includes(".ts")) || "";
|
19
|
+
const match = callerLine.match(/\((.*):\d+:\d+\)$/) || callerLine.match(/at (.*):\d+:\d+/);
|
20
|
+
if (match) return path.dirname(match[1]);
|
21
|
+
return process.cwd();
|
22
|
+
}
|
23
|
+
function getPackageVersion() {
|
24
|
+
try {
|
25
|
+
const baseDir = getCallerDir();
|
26
|
+
const pkgPath = path.resolve(baseDir, "package.json");
|
27
|
+
const content = fs.readFileSync(pkgPath, "utf-8");
|
28
|
+
const pkg = JSON.parse(content);
|
29
|
+
return pkg.version || "0.0.0";
|
30
|
+
} catch (e) {
|
31
|
+
console.warn("[VersionInjector] Failed to read package.json:", e);
|
32
|
+
return "0.0.0";
|
33
|
+
}
|
34
|
+
}
|
35
|
+
function defaultFormatDate(date) {
|
36
|
+
return date.toISOString();
|
37
|
+
}
|
38
|
+
|
39
|
+
// src/core.ts
|
40
|
+
var dayjs;
|
41
|
+
var versionInjectorPlugin = createUnplugin((options = {}) => {
|
42
|
+
const {
|
43
|
+
version = getPackageVersion(),
|
44
|
+
formatDate,
|
45
|
+
dateFormat,
|
46
|
+
injectToHead = true,
|
47
|
+
injectToBody = true
|
48
|
+
} = options;
|
49
|
+
let resolvedFormatDate = defaultFormatDate;
|
50
|
+
if (typeof formatDate === "function") {
|
51
|
+
resolvedFormatDate = formatDate;
|
52
|
+
} else if (typeof dateFormat === "string") {
|
53
|
+
try {
|
54
|
+
dayjs = __require("dayjs");
|
55
|
+
resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
|
56
|
+
} catch (err) {
|
57
|
+
console.warn("[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.");
|
58
|
+
}
|
59
|
+
}
|
60
|
+
if (!injectToHead && !injectToBody) {
|
61
|
+
return { name: "unplugin-version-injector" };
|
62
|
+
}
|
63
|
+
const buildTime = resolvedFormatDate(/* @__PURE__ */ new Date());
|
64
|
+
const metaTag = `<meta name="version" content="${version}">
|
65
|
+
`;
|
66
|
+
const logScript = `
|
67
|
+
<script>
|
68
|
+
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
69
|
+
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
70
|
+
</script>`;
|
71
|
+
function processHtml(html) {
|
72
|
+
if (injectToHead && !html.includes('<meta name="version"')) {
|
73
|
+
html = html.replace(/<head>/, `<head>
|
74
|
+
${metaTag}`);
|
75
|
+
}
|
76
|
+
if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
|
77
|
+
html = html.replace("</body>", ` ${logScript}
|
78
|
+
</body>`);
|
79
|
+
}
|
80
|
+
return html;
|
81
|
+
}
|
82
|
+
return {
|
83
|
+
name: "unplugin-version-injector",
|
84
|
+
vite: {
|
85
|
+
transformIndexHtml(html) {
|
86
|
+
return processHtml(html);
|
87
|
+
}
|
88
|
+
},
|
89
|
+
rollup: {
|
90
|
+
generateBundle(_, bundle) {
|
91
|
+
for (const file of Object.values(bundle)) {
|
92
|
+
if (file.type === "asset" && file.fileName.endsWith(".html")) {
|
93
|
+
file.source = processHtml(file.source);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
};
|
99
|
+
});
|
100
|
+
var core_default = versionInjectorPlugin;
|
101
|
+
export {
|
102
|
+
core_default as default,
|
103
|
+
versionInjectorPlugin
|
104
|
+
};
|
@@ -1,7 +1,9 @@
|
|
1
|
-
|
1
|
+
interface VersionInjectorOptions {
|
2
2
|
version?: string;
|
3
3
|
formatDate?: (date: Date) => string;
|
4
4
|
dateFormat?: string;
|
5
5
|
injectToHead?: boolean;
|
6
6
|
injectToBody?: boolean;
|
7
7
|
}
|
8
|
+
|
9
|
+
export type { VersionInjectorOptions as V };
|
@@ -1,7 +1,9 @@
|
|
1
|
-
|
1
|
+
interface VersionInjectorOptions {
|
2
2
|
version?: string;
|
3
3
|
formatDate?: (date: Date) => string;
|
4
4
|
dateFormat?: string;
|
5
5
|
injectToHead?: boolean;
|
6
6
|
injectToBody?: boolean;
|
7
7
|
}
|
8
|
+
|
9
|
+
export type { VersionInjectorOptions as V };
|
package/dist/webpack.js
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// src/webpack.ts
|
31
|
+
var webpack_exports = {};
|
32
|
+
__export(webpack_exports, {
|
33
|
+
default: () => versionInjector
|
34
|
+
});
|
35
|
+
module.exports = __toCommonJS(webpack_exports);
|
36
|
+
|
37
|
+
// src/core.ts
|
38
|
+
var import_unplugin = require("unplugin");
|
39
|
+
|
40
|
+
// src/utils.ts
|
41
|
+
var import_node_path = __toESM(require("path"));
|
42
|
+
var import_node_fs = __toESM(require("fs"));
|
43
|
+
function getCallerDir() {
|
44
|
+
const _stack = new Error().stack;
|
45
|
+
if (!_stack) return process.cwd();
|
46
|
+
const stackLines = _stack.split("\n");
|
47
|
+
const callerLine = stackLines.find((l) => l.includes(".js") || l.includes(".ts")) || "";
|
48
|
+
const match = callerLine.match(/\((.*):\d+:\d+\)$/) || callerLine.match(/at (.*):\d+:\d+/);
|
49
|
+
if (match) return import_node_path.default.dirname(match[1]);
|
50
|
+
return process.cwd();
|
51
|
+
}
|
52
|
+
function getPackageVersion() {
|
53
|
+
try {
|
54
|
+
const baseDir = getCallerDir();
|
55
|
+
const pkgPath = import_node_path.default.resolve(baseDir, "package.json");
|
56
|
+
const content = import_node_fs.default.readFileSync(pkgPath, "utf-8");
|
57
|
+
const pkg = JSON.parse(content);
|
58
|
+
return pkg.version || "0.0.0";
|
59
|
+
} catch (e) {
|
60
|
+
console.warn("[VersionInjector] Failed to read package.json:", e);
|
61
|
+
return "0.0.0";
|
62
|
+
}
|
63
|
+
}
|
64
|
+
function defaultFormatDate(date) {
|
65
|
+
return date.toISOString();
|
66
|
+
}
|
67
|
+
|
68
|
+
// src/core.ts
|
69
|
+
var dayjs;
|
70
|
+
var versionInjectorPlugin = (0, import_unplugin.createUnplugin)((options = {}) => {
|
71
|
+
const {
|
72
|
+
version = getPackageVersion(),
|
73
|
+
formatDate,
|
74
|
+
dateFormat,
|
75
|
+
injectToHead = true,
|
76
|
+
injectToBody = true
|
77
|
+
} = options;
|
78
|
+
let resolvedFormatDate = defaultFormatDate;
|
79
|
+
if (typeof formatDate === "function") {
|
80
|
+
resolvedFormatDate = formatDate;
|
81
|
+
} else if (typeof dateFormat === "string") {
|
82
|
+
try {
|
83
|
+
dayjs = require("dayjs");
|
84
|
+
resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
|
85
|
+
} catch (err) {
|
86
|
+
console.warn("[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.");
|
87
|
+
}
|
88
|
+
}
|
89
|
+
if (!injectToHead && !injectToBody) {
|
90
|
+
return { name: "unplugin-version-injector" };
|
91
|
+
}
|
92
|
+
const buildTime = resolvedFormatDate(/* @__PURE__ */ new Date());
|
93
|
+
const metaTag = `<meta name="version" content="${version}">
|
94
|
+
`;
|
95
|
+
const logScript = `
|
96
|
+
<script>
|
97
|
+
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
98
|
+
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
99
|
+
</script>`;
|
100
|
+
function processHtml(html) {
|
101
|
+
if (injectToHead && !html.includes('<meta name="version"')) {
|
102
|
+
html = html.replace(/<head>/, `<head>
|
103
|
+
${metaTag}`);
|
104
|
+
}
|
105
|
+
if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
|
106
|
+
html = html.replace("</body>", ` ${logScript}
|
107
|
+
</body>`);
|
108
|
+
}
|
109
|
+
return html;
|
110
|
+
}
|
111
|
+
return {
|
112
|
+
name: "unplugin-version-injector",
|
113
|
+
vite: {
|
114
|
+
transformIndexHtml(html) {
|
115
|
+
return processHtml(html);
|
116
|
+
}
|
117
|
+
},
|
118
|
+
rollup: {
|
119
|
+
generateBundle(_, bundle) {
|
120
|
+
for (const file of Object.values(bundle)) {
|
121
|
+
if (file.type === "asset" && file.fileName.endsWith(".html")) {
|
122
|
+
file.source = processHtml(file.source);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
};
|
128
|
+
});
|
129
|
+
|
130
|
+
// src/webpack.ts
|
131
|
+
function versionInjector(options = {}) {
|
132
|
+
return versionInjectorPlugin.webpack(options);
|
133
|
+
}
|
package/dist/webpack.mjs
ADDED
@@ -0,0 +1,107 @@
|
|
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 path from "node:path";
|
13
|
+
import fs from "node:fs";
|
14
|
+
function getCallerDir() {
|
15
|
+
const _stack = new Error().stack;
|
16
|
+
if (!_stack) return process.cwd();
|
17
|
+
const stackLines = _stack.split("\n");
|
18
|
+
const callerLine = stackLines.find((l) => l.includes(".js") || l.includes(".ts")) || "";
|
19
|
+
const match = callerLine.match(/\((.*):\d+:\d+\)$/) || callerLine.match(/at (.*):\d+:\d+/);
|
20
|
+
if (match) return path.dirname(match[1]);
|
21
|
+
return process.cwd();
|
22
|
+
}
|
23
|
+
function getPackageVersion() {
|
24
|
+
try {
|
25
|
+
const baseDir = getCallerDir();
|
26
|
+
const pkgPath = path.resolve(baseDir, "package.json");
|
27
|
+
const content = fs.readFileSync(pkgPath, "utf-8");
|
28
|
+
const pkg = JSON.parse(content);
|
29
|
+
return pkg.version || "0.0.0";
|
30
|
+
} catch (e) {
|
31
|
+
console.warn("[VersionInjector] Failed to read package.json:", e);
|
32
|
+
return "0.0.0";
|
33
|
+
}
|
34
|
+
}
|
35
|
+
function defaultFormatDate(date) {
|
36
|
+
return date.toISOString();
|
37
|
+
}
|
38
|
+
|
39
|
+
// src/core.ts
|
40
|
+
var dayjs;
|
41
|
+
var versionInjectorPlugin = createUnplugin((options = {}) => {
|
42
|
+
const {
|
43
|
+
version = getPackageVersion(),
|
44
|
+
formatDate,
|
45
|
+
dateFormat,
|
46
|
+
injectToHead = true,
|
47
|
+
injectToBody = true
|
48
|
+
} = options;
|
49
|
+
let resolvedFormatDate = defaultFormatDate;
|
50
|
+
if (typeof formatDate === "function") {
|
51
|
+
resolvedFormatDate = formatDate;
|
52
|
+
} else if (typeof dateFormat === "string") {
|
53
|
+
try {
|
54
|
+
dayjs = __require("dayjs");
|
55
|
+
resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
|
56
|
+
} catch (err) {
|
57
|
+
console.warn("[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.");
|
58
|
+
}
|
59
|
+
}
|
60
|
+
if (!injectToHead && !injectToBody) {
|
61
|
+
return { name: "unplugin-version-injector" };
|
62
|
+
}
|
63
|
+
const buildTime = resolvedFormatDate(/* @__PURE__ */ new Date());
|
64
|
+
const metaTag = `<meta name="version" content="${version}">
|
65
|
+
`;
|
66
|
+
const logScript = `
|
67
|
+
<script>
|
68
|
+
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
69
|
+
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
70
|
+
</script>`;
|
71
|
+
function processHtml(html) {
|
72
|
+
if (injectToHead && !html.includes('<meta name="version"')) {
|
73
|
+
html = html.replace(/<head>/, `<head>
|
74
|
+
${metaTag}`);
|
75
|
+
}
|
76
|
+
if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
|
77
|
+
html = html.replace("</body>", ` ${logScript}
|
78
|
+
</body>`);
|
79
|
+
}
|
80
|
+
return html;
|
81
|
+
}
|
82
|
+
return {
|
83
|
+
name: "unplugin-version-injector",
|
84
|
+
vite: {
|
85
|
+
transformIndexHtml(html) {
|
86
|
+
return processHtml(html);
|
87
|
+
}
|
88
|
+
},
|
89
|
+
rollup: {
|
90
|
+
generateBundle(_, bundle) {
|
91
|
+
for (const file of Object.values(bundle)) {
|
92
|
+
if (file.type === "asset" && file.fileName.endsWith(".html")) {
|
93
|
+
file.source = processHtml(file.source);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
};
|
99
|
+
});
|
100
|
+
|
101
|
+
// src/webpack.ts
|
102
|
+
function versionInjector(options = {}) {
|
103
|
+
return versionInjectorPlugin.webpack(options);
|
104
|
+
}
|
105
|
+
export {
|
106
|
+
versionInjector as default
|
107
|
+
};
|
package/package.json
CHANGED
@@ -1,41 +1,63 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
2
|
+
"name": "unplugin-version-injector",
|
3
|
+
"version": "1.1.1-alpha.1",
|
4
|
+
"author": "Nian Yi <nianyi778@gmail.com>",
|
5
|
+
"license": "MIT",
|
6
|
+
"description": "A universal plugin to inject version and build time into HTML (supports Webpack, Vite, Rollup)",
|
7
|
+
"repository": {
|
8
|
+
"type": "git",
|
9
|
+
"url": "https://github.com/nianyi778/unplugin-version-injector.git"
|
10
|
+
},
|
11
|
+
"keywords": [
|
12
|
+
"unplugin",
|
13
|
+
"version",
|
14
|
+
"injector",
|
15
|
+
"webpack",
|
16
|
+
"vite",
|
17
|
+
"rollup"
|
18
|
+
],
|
19
|
+
"main": "./dist/core.js",
|
20
|
+
"module": "./dist/core.mjs",
|
21
|
+
"types": "./dist/core.d.ts",
|
22
|
+
"exports": {
|
23
|
+
".": {
|
24
|
+
"import": "./dist/core.mjs",
|
25
|
+
"require": "./dist/core.js"
|
10
26
|
},
|
11
|
-
"
|
12
|
-
|
13
|
-
|
14
|
-
"exports": {
|
15
|
-
"require": "./dist/cjs/index.js",
|
16
|
-
"import": "./dist/esm/index.js",
|
17
|
-
"default": "./dist/esm/index.js"
|
27
|
+
"./vite": {
|
28
|
+
"import": "./dist/core.mjs",
|
29
|
+
"require": "./dist/core.js"
|
18
30
|
},
|
19
|
-
"
|
20
|
-
|
21
|
-
|
22
|
-
"build:esm": "tsc --module ESNext --outDir dist/esm",
|
23
|
-
"build:cjs": "tsc --module CommonJS --outDir dist/cjs",
|
24
|
-
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
25
|
-
"prepublishOnly": "npm run build"
|
31
|
+
"./rollup": {
|
32
|
+
"import": "./dist/core.mjs",
|
33
|
+
"require": "./dist/core.js"
|
26
34
|
},
|
27
|
-
"
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
"
|
40
|
-
"
|
41
|
-
}
|
35
|
+
"./webpack": {
|
36
|
+
"import": "./dist/webpack.mjs",
|
37
|
+
"require": "./dist/webpack.js"
|
38
|
+
}
|
39
|
+
},
|
40
|
+
"dependencies": {
|
41
|
+
"tsup": "^8.4.0",
|
42
|
+
"unplugin": "^1.0.0"
|
43
|
+
},
|
44
|
+
"devDependencies": {
|
45
|
+
"rollup": "^3",
|
46
|
+
"typescript": "^4.9.5",
|
47
|
+
"vite": "^4",
|
48
|
+
"webpack": "^5"
|
49
|
+
},
|
50
|
+
"publishConfig": {
|
51
|
+
"access": "public"
|
52
|
+
},
|
53
|
+
"files": [
|
54
|
+
"dist",
|
55
|
+
"README.md",
|
56
|
+
"README.zh-CN.md"
|
57
|
+
],
|
58
|
+
"scripts": {
|
59
|
+
"clean": "rm -rf dist",
|
60
|
+
"build": "tsup",
|
61
|
+
"dev": "tsup --watch"
|
62
|
+
}
|
63
|
+
}
|
package/dist/cjs/index.d.ts
DELETED
package/dist/cjs/index.js
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
// Updated version with support for both `formatDate` and `dateFormat`
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
const unplugin_1 = require("unplugin");
|
5
|
-
const webpack_1 = require("webpack");
|
6
|
-
const utils_1 = require("./utils");
|
7
|
-
let dayjs;
|
8
|
-
const VersionInjectorPlugin = (0, unplugin_1.createUnplugin)((options = {}) => {
|
9
|
-
const { version = (0, utils_1.getPackageVersion)(), formatDate, dateFormat, injectToHead = true, injectToBody = true, } = options;
|
10
|
-
// resolve date formatter
|
11
|
-
let resolvedFormatDate = utils_1.defaultFormatDate;
|
12
|
-
if (typeof formatDate === 'function') {
|
13
|
-
resolvedFormatDate = formatDate;
|
14
|
-
}
|
15
|
-
else if (typeof dateFormat === 'string') {
|
16
|
-
try {
|
17
|
-
dayjs = require('dayjs');
|
18
|
-
resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
|
19
|
-
}
|
20
|
-
catch (err) {
|
21
|
-
console.warn('[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.');
|
22
|
-
}
|
23
|
-
}
|
24
|
-
if (!injectToHead && !injectToBody) {
|
25
|
-
return { name: 'unplugin-version-injector' };
|
26
|
-
}
|
27
|
-
const buildTime = resolvedFormatDate(new Date());
|
28
|
-
const metaTag = `<meta name="version" content="${version}">\n`;
|
29
|
-
const logScript = `
|
30
|
-
<script>
|
31
|
-
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
32
|
-
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
33
|
-
</script>`;
|
34
|
-
function processHtml(html) {
|
35
|
-
if (injectToHead && !html.includes('<meta name="version"')) {
|
36
|
-
html = html.replace(/<head>/, `<head>\n ${metaTag}`);
|
37
|
-
}
|
38
|
-
if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
|
39
|
-
html = html.replace('</body>', ` ${logScript}\n</body>`);
|
40
|
-
}
|
41
|
-
return html;
|
42
|
-
}
|
43
|
-
return {
|
44
|
-
name: 'unplugin-version-injector',
|
45
|
-
vite: {
|
46
|
-
transformIndexHtml(html) {
|
47
|
-
return processHtml(html);
|
48
|
-
},
|
49
|
-
},
|
50
|
-
rollup: {
|
51
|
-
generateBundle(_, bundle) {
|
52
|
-
for (const file of Object.values(bundle)) {
|
53
|
-
if (file.type === 'asset' && file.fileName.endsWith('.html')) {
|
54
|
-
file.source = processHtml(file.source);
|
55
|
-
}
|
56
|
-
}
|
57
|
-
},
|
58
|
-
},
|
59
|
-
webpack(compiler) {
|
60
|
-
const isWebpack5 = typeof webpack_1.sources !== 'undefined' && webpack_1.sources.RawSource;
|
61
|
-
if (isWebpack5) {
|
62
|
-
compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
|
63
|
-
compilation.hooks.processAssets.tap({
|
64
|
-
name: 'unplugin-version-injector',
|
65
|
-
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
|
66
|
-
}, (assets) => {
|
67
|
-
Object.keys(assets).forEach((filename) => {
|
68
|
-
if (filename.endsWith('.html')) {
|
69
|
-
let source = assets[filename].source().toString();
|
70
|
-
source = processHtml(source);
|
71
|
-
compilation.updateAsset(filename, new webpack_1.sources.RawSource(source));
|
72
|
-
}
|
73
|
-
});
|
74
|
-
});
|
75
|
-
});
|
76
|
-
}
|
77
|
-
else {
|
78
|
-
compiler.hooks.emit.tapAsync('unplugin-version-injector', (compilation, callback) => {
|
79
|
-
Object.keys(compilation.assets).forEach((filename) => {
|
80
|
-
if (filename.endsWith('.html')) {
|
81
|
-
const rawSource = compilation.assets[filename].source().toString();
|
82
|
-
const newSource = processHtml(rawSource);
|
83
|
-
compilation.assets[filename] = new webpack_1.sources.RawSource(newSource);
|
84
|
-
}
|
85
|
-
});
|
86
|
-
callback();
|
87
|
-
});
|
88
|
-
}
|
89
|
-
},
|
90
|
-
};
|
91
|
-
});
|
92
|
-
if (typeof module !== 'undefined' && module.exports) {
|
93
|
-
module.exports = VersionInjectorPlugin;
|
94
|
-
}
|
95
|
-
exports.default = VersionInjectorPlugin;
|
package/dist/cjs/types.js
DELETED
package/dist/cjs/utils.d.ts
DELETED
package/dist/cjs/utils.js
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.defaultFormatDate = exports.getPackageVersion = void 0;
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
8
|
-
const path_1 = __importDefault(require("path"));
|
9
|
-
/** 获取 package.json 版本 */
|
10
|
-
function getPackageVersion() {
|
11
|
-
try {
|
12
|
-
const packageJsonPath = path_1.default.resolve(process.cwd(), 'package.json');
|
13
|
-
if (fs_1.default.existsSync(packageJsonPath)) {
|
14
|
-
return require(packageJsonPath).version;
|
15
|
-
}
|
16
|
-
}
|
17
|
-
catch (error) {
|
18
|
-
console.warn('[VersionInjector] Failed to read package.json:', error);
|
19
|
-
}
|
20
|
-
return '0.0.0';
|
21
|
-
}
|
22
|
-
exports.getPackageVersion = getPackageVersion;
|
23
|
-
/** 默认格式化 build time */
|
24
|
-
function defaultFormatDate(date) {
|
25
|
-
return date.toISOString();
|
26
|
-
}
|
27
|
-
exports.defaultFormatDate = defaultFormatDate;
|
package/dist/esm/index.d.ts
DELETED
package/dist/esm/index.js
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
// Updated version with support for both `formatDate` and `dateFormat`
|
2
|
-
import { createUnplugin } from 'unplugin';
|
3
|
-
import { Compilation, sources } from 'webpack';
|
4
|
-
import { getPackageVersion, defaultFormatDate } from './utils';
|
5
|
-
let dayjs;
|
6
|
-
const VersionInjectorPlugin = createUnplugin((options = {}) => {
|
7
|
-
const { version = getPackageVersion(), formatDate, dateFormat, injectToHead = true, injectToBody = true, } = options;
|
8
|
-
// resolve date formatter
|
9
|
-
let resolvedFormatDate = defaultFormatDate;
|
10
|
-
if (typeof formatDate === 'function') {
|
11
|
-
resolvedFormatDate = formatDate;
|
12
|
-
}
|
13
|
-
else if (typeof dateFormat === 'string') {
|
14
|
-
try {
|
15
|
-
dayjs = require('dayjs');
|
16
|
-
resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
|
17
|
-
}
|
18
|
-
catch (err) {
|
19
|
-
console.warn('[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.');
|
20
|
-
}
|
21
|
-
}
|
22
|
-
if (!injectToHead && !injectToBody) {
|
23
|
-
return { name: 'unplugin-version-injector' };
|
24
|
-
}
|
25
|
-
const buildTime = resolvedFormatDate(new Date());
|
26
|
-
const metaTag = `<meta name="version" content="${version}">\n`;
|
27
|
-
const logScript = `
|
28
|
-
<script>
|
29
|
-
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
30
|
-
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
31
|
-
</script>`;
|
32
|
-
function processHtml(html) {
|
33
|
-
if (injectToHead && !html.includes('<meta name="version"')) {
|
34
|
-
html = html.replace(/<head>/, `<head>\n ${metaTag}`);
|
35
|
-
}
|
36
|
-
if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
|
37
|
-
html = html.replace('</body>', ` ${logScript}\n</body>`);
|
38
|
-
}
|
39
|
-
return html;
|
40
|
-
}
|
41
|
-
return {
|
42
|
-
name: 'unplugin-version-injector',
|
43
|
-
vite: {
|
44
|
-
transformIndexHtml(html) {
|
45
|
-
return processHtml(html);
|
46
|
-
},
|
47
|
-
},
|
48
|
-
rollup: {
|
49
|
-
generateBundle(_, bundle) {
|
50
|
-
for (const file of Object.values(bundle)) {
|
51
|
-
if (file.type === 'asset' && file.fileName.endsWith('.html')) {
|
52
|
-
file.source = processHtml(file.source);
|
53
|
-
}
|
54
|
-
}
|
55
|
-
},
|
56
|
-
},
|
57
|
-
webpack(compiler) {
|
58
|
-
const isWebpack5 = typeof sources !== 'undefined' && sources.RawSource;
|
59
|
-
if (isWebpack5) {
|
60
|
-
compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
|
61
|
-
compilation.hooks.processAssets.tap({
|
62
|
-
name: 'unplugin-version-injector',
|
63
|
-
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
|
64
|
-
}, (assets) => {
|
65
|
-
Object.keys(assets).forEach((filename) => {
|
66
|
-
if (filename.endsWith('.html')) {
|
67
|
-
let source = assets[filename].source().toString();
|
68
|
-
source = processHtml(source);
|
69
|
-
compilation.updateAsset(filename, new sources.RawSource(source));
|
70
|
-
}
|
71
|
-
});
|
72
|
-
});
|
73
|
-
});
|
74
|
-
}
|
75
|
-
else {
|
76
|
-
compiler.hooks.emit.tapAsync('unplugin-version-injector', (compilation, callback) => {
|
77
|
-
Object.keys(compilation.assets).forEach((filename) => {
|
78
|
-
if (filename.endsWith('.html')) {
|
79
|
-
const rawSource = compilation.assets[filename].source().toString();
|
80
|
-
const newSource = processHtml(rawSource);
|
81
|
-
compilation.assets[filename] = new sources.RawSource(newSource);
|
82
|
-
}
|
83
|
-
});
|
84
|
-
callback();
|
85
|
-
});
|
86
|
-
}
|
87
|
-
},
|
88
|
-
};
|
89
|
-
});
|
90
|
-
if (typeof module !== 'undefined' && module.exports) {
|
91
|
-
module.exports = VersionInjectorPlugin;
|
92
|
-
}
|
93
|
-
export default VersionInjectorPlugin;
|
package/dist/esm/types.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/dist/esm/utils.d.ts
DELETED
package/dist/esm/utils.js
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
import fs from 'fs';
|
2
|
-
import path from 'path';
|
3
|
-
/** 获取 package.json 版本 */
|
4
|
-
export function getPackageVersion() {
|
5
|
-
try {
|
6
|
-
const packageJsonPath = path.resolve(process.cwd(), 'package.json');
|
7
|
-
if (fs.existsSync(packageJsonPath)) {
|
8
|
-
return require(packageJsonPath).version;
|
9
|
-
}
|
10
|
-
}
|
11
|
-
catch (error) {
|
12
|
-
console.warn('[VersionInjector] Failed to read package.json:', error);
|
13
|
-
}
|
14
|
-
return '0.0.0';
|
15
|
-
}
|
16
|
-
/** 默认格式化 build time */
|
17
|
-
export function defaultFormatDate(date) {
|
18
|
-
return date.toISOString();
|
19
|
-
}
|