unplugin-version-injector 1.0.1 → 1.0.3
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/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 +3 -2
- package/src/index.ts +0 -77
- package/src/types.ts +0 -6
- package/src/utils.ts +0 -20
- package/tsconfig.json +0 -15
@@ -0,0 +1,60 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const unplugin_1 = require("unplugin");
|
4
|
+
const webpack_1 = require("webpack");
|
5
|
+
const utils_1 = require("./utils");
|
6
|
+
const VersionInjectorPlugin = (0, unplugin_1.createUnplugin)((options = {}) => {
|
7
|
+
const shouldInject = options.log !== false; // ✅ 只有 log: false 时不注入
|
8
|
+
if (!shouldInject) {
|
9
|
+
return { name: 'unplugin-version-injector' }; // ❌ 直接返回空插件,避免无意义的操作
|
10
|
+
}
|
11
|
+
const version = options.version || (0, utils_1.getPackageVersion)();
|
12
|
+
const buildTime = options.formatDate ? options.formatDate(new Date()) : (0, utils_1.defaultFormatDate)(new Date());
|
13
|
+
const metaTag = `<meta name="version" content="${version}">\n`;
|
14
|
+
const logScript = `
|
15
|
+
<script>
|
16
|
+
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
17
|
+
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
18
|
+
</script>`;
|
19
|
+
function processHtml(html) {
|
20
|
+
if (!html.includes('<meta name="version"')) {
|
21
|
+
html = html.replace(/<head>/, `<head>\n ${metaTag}`);
|
22
|
+
}
|
23
|
+
if (!html.includes('<script>console.log("%c Version:')) {
|
24
|
+
html = html.replace('</body>', ` ${logScript}\n</body>`);
|
25
|
+
}
|
26
|
+
return html;
|
27
|
+
}
|
28
|
+
return {
|
29
|
+
name: 'unplugin-version-injector',
|
30
|
+
// ✅ Vite 适配
|
31
|
+
vite: {
|
32
|
+
transformIndexHtml(html) {
|
33
|
+
return processHtml(html);
|
34
|
+
}
|
35
|
+
},
|
36
|
+
// ✅ Webpack 适配
|
37
|
+
webpack(compiler) {
|
38
|
+
compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
|
39
|
+
compilation.hooks.processAssets.tap({
|
40
|
+
name: 'unplugin-version-injector',
|
41
|
+
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
|
42
|
+
}, (assets) => {
|
43
|
+
Object.keys(assets).forEach((filename) => {
|
44
|
+
if (filename.endsWith('.html')) {
|
45
|
+
let source = assets[filename].source().toString();
|
46
|
+
source = processHtml(source);
|
47
|
+
compilation.updateAsset(filename, new webpack_1.sources.RawSource(source) // ✅ 修正 updateAsset 类型
|
48
|
+
);
|
49
|
+
}
|
50
|
+
});
|
51
|
+
});
|
52
|
+
});
|
53
|
+
}
|
54
|
+
};
|
55
|
+
});
|
56
|
+
// ✅ 兼容 Webpack / Vite / Rollup
|
57
|
+
if (typeof module !== 'undefined' && module.exports) {
|
58
|
+
module.exports = VersionInjectorPlugin; // ✅ 让 CommonJS 直接拿到
|
59
|
+
}
|
60
|
+
exports.default = VersionInjectorPlugin;
|
@@ -0,0 +1,27 @@
|
|
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;
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import { createUnplugin } from 'unplugin';
|
2
|
+
import { Compilation, sources } from 'webpack';
|
3
|
+
import { getPackageVersion, defaultFormatDate } from './utils';
|
4
|
+
const VersionInjectorPlugin = createUnplugin((options = {}) => {
|
5
|
+
const shouldInject = options.log !== false; // ✅ 只有 log: false 时不注入
|
6
|
+
if (!shouldInject) {
|
7
|
+
return { name: 'unplugin-version-injector' }; // ❌ 直接返回空插件,避免无意义的操作
|
8
|
+
}
|
9
|
+
const version = options.version || getPackageVersion();
|
10
|
+
const buildTime = options.formatDate ? options.formatDate(new Date()) : defaultFormatDate(new Date());
|
11
|
+
const metaTag = `<meta name="version" content="${version}">\n`;
|
12
|
+
const logScript = `
|
13
|
+
<script>
|
14
|
+
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
15
|
+
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
16
|
+
</script>`;
|
17
|
+
function processHtml(html) {
|
18
|
+
if (!html.includes('<meta name="version"')) {
|
19
|
+
html = html.replace(/<head>/, `<head>\n ${metaTag}`);
|
20
|
+
}
|
21
|
+
if (!html.includes('<script>console.log("%c Version:')) {
|
22
|
+
html = html.replace('</body>', ` ${logScript}\n</body>`);
|
23
|
+
}
|
24
|
+
return html;
|
25
|
+
}
|
26
|
+
return {
|
27
|
+
name: 'unplugin-version-injector',
|
28
|
+
// ✅ Vite 适配
|
29
|
+
vite: {
|
30
|
+
transformIndexHtml(html) {
|
31
|
+
return processHtml(html);
|
32
|
+
}
|
33
|
+
},
|
34
|
+
// ✅ Webpack 适配
|
35
|
+
webpack(compiler) {
|
36
|
+
compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
|
37
|
+
compilation.hooks.processAssets.tap({
|
38
|
+
name: 'unplugin-version-injector',
|
39
|
+
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
|
40
|
+
}, (assets) => {
|
41
|
+
Object.keys(assets).forEach((filename) => {
|
42
|
+
if (filename.endsWith('.html')) {
|
43
|
+
let source = assets[filename].source().toString();
|
44
|
+
source = processHtml(source);
|
45
|
+
compilation.updateAsset(filename, new sources.RawSource(source) // ✅ 修正 updateAsset 类型
|
46
|
+
);
|
47
|
+
}
|
48
|
+
});
|
49
|
+
});
|
50
|
+
});
|
51
|
+
}
|
52
|
+
};
|
53
|
+
});
|
54
|
+
// ✅ 兼容 Webpack / Vite / Rollup
|
55
|
+
if (typeof module !== 'undefined' && module.exports) {
|
56
|
+
module.exports = VersionInjectorPlugin; // ✅ 让 CommonJS 直接拿到
|
57
|
+
}
|
58
|
+
export default VersionInjectorPlugin;
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,19 @@
|
|
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
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "unplugin-version-injector",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.3",
|
4
4
|
"author": "Nian Yi <nianyi778@gmail.com>",
|
5
5
|
"license": "MIT",
|
6
6
|
"description": "A universal plugin to inject version and build time into HTML (supports Webpack, Vite, Rollup)",
|
@@ -22,7 +22,7 @@
|
|
22
22
|
"build:esm": "tsc --module ESNext --outDir dist/esm",
|
23
23
|
"build:cjs": "tsc --module CommonJS --outDir dist/cjs",
|
24
24
|
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
25
|
-
"publish": "npm run build && npm publish"
|
25
|
+
"publish": "npm run build && npm publish --access public"
|
26
26
|
},
|
27
27
|
"dependencies": {
|
28
28
|
"unplugin": "^1.0.0"
|
@@ -36,5 +36,6 @@
|
|
36
36
|
"publishConfig": {
|
37
37
|
"access": "public"
|
38
38
|
},
|
39
|
+
"files": ["dist","README.md","README.zh-CN.md"],
|
39
40
|
"packageManager": "pnpm@10.5.2+sha512.da9dc28cd3ff40d0592188235ab25d3202add8a207afbedc682220e4a0029ffbff4562102b9e6e46b4e3f9e8bd53e6d05de48544b0c57d4b0179e22c76d1199b"
|
40
41
|
}
|
package/src/index.ts
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
import { createUnplugin } from 'unplugin';
|
2
|
-
import { Compilation, sources } from 'webpack';
|
3
|
-
import type { VersionInjectorOptions } from './types';
|
4
|
-
import { getPackageVersion, defaultFormatDate } from './utils';
|
5
|
-
|
6
|
-
const VersionInjectorPlugin = createUnplugin((options: VersionInjectorOptions = {}) => {
|
7
|
-
|
8
|
-
const shouldInject = options.log !== false; // ✅ 只有 log: false 时不注入
|
9
|
-
|
10
|
-
if (!shouldInject) {
|
11
|
-
return { name: 'unplugin-version-injector' }; // ❌ 直接返回空插件,避免无意义的操作
|
12
|
-
}
|
13
|
-
|
14
|
-
const version = options.version || getPackageVersion();
|
15
|
-
const buildTime = options.formatDate ? options.formatDate(new Date()) : defaultFormatDate(new Date());
|
16
|
-
|
17
|
-
const metaTag = `<meta name="version" content="${version}">\n`;
|
18
|
-
const logScript = `
|
19
|
-
<script>
|
20
|
-
console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
21
|
-
console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
|
22
|
-
</script>`;
|
23
|
-
|
24
|
-
function processHtml(html: string): string {
|
25
|
-
if (!html.includes('<meta name="version"')) {
|
26
|
-
html = html.replace(/<head>/, `<head>\n ${metaTag}`);
|
27
|
-
}
|
28
|
-
if (!html.includes('<script>console.log("%c Version:')) {
|
29
|
-
html = html.replace('</body>', ` ${logScript}\n</body>`);
|
30
|
-
}
|
31
|
-
return html;
|
32
|
-
}
|
33
|
-
|
34
|
-
return {
|
35
|
-
name: 'unplugin-version-injector',
|
36
|
-
|
37
|
-
// ✅ Vite 适配
|
38
|
-
vite: {
|
39
|
-
transformIndexHtml(html: string) {
|
40
|
-
return processHtml(html);
|
41
|
-
}
|
42
|
-
},
|
43
|
-
|
44
|
-
// ✅ Webpack 适配
|
45
|
-
webpack(compiler) {
|
46
|
-
compiler.hooks.compilation.tap('unplugin-version-injector', (compilation: Compilation) => {
|
47
|
-
compilation.hooks.processAssets.tap(
|
48
|
-
{
|
49
|
-
name: 'unplugin-version-injector',
|
50
|
-
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
|
51
|
-
},
|
52
|
-
(assets) => {
|
53
|
-
Object.keys(assets).forEach((filename) => {
|
54
|
-
if (filename.endsWith('.html')) {
|
55
|
-
let source = assets[filename].source().toString();
|
56
|
-
source = processHtml(source);
|
57
|
-
|
58
|
-
compilation.updateAsset(
|
59
|
-
filename,
|
60
|
-
new sources.RawSource(source) // ✅ 修正 updateAsset 类型
|
61
|
-
);
|
62
|
-
}
|
63
|
-
});
|
64
|
-
}
|
65
|
-
);
|
66
|
-
});
|
67
|
-
}
|
68
|
-
};
|
69
|
-
});
|
70
|
-
|
71
|
-
// ✅ 兼容 Webpack / Vite / Rollup
|
72
|
-
|
73
|
-
if (typeof module !== 'undefined' && module.exports) {
|
74
|
-
module.exports = VersionInjectorPlugin; // ✅ 让 CommonJS 直接拿到
|
75
|
-
}
|
76
|
-
|
77
|
-
export default VersionInjectorPlugin;
|
package/src/types.ts
DELETED
package/src/utils.ts
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
import fs from 'fs';
|
2
|
-
import path from 'path';
|
3
|
-
|
4
|
-
/** 获取 package.json 版本 */
|
5
|
-
export function getPackageVersion(): string {
|
6
|
-
try {
|
7
|
-
const packageJsonPath = path.resolve(process.cwd(), 'package.json');
|
8
|
-
if (fs.existsSync(packageJsonPath)) {
|
9
|
-
return require(packageJsonPath).version;
|
10
|
-
}
|
11
|
-
} catch (error) {
|
12
|
-
console.warn('[VersionInjector] Failed to read package.json:', error);
|
13
|
-
}
|
14
|
-
return '0.0.0';
|
15
|
-
}
|
16
|
-
|
17
|
-
/** 默认格式化 build time */
|
18
|
-
export function defaultFormatDate(date: Date): string {
|
19
|
-
return date.toISOString();
|
20
|
-
}
|
package/tsconfig.json
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"compilerOptions": {
|
3
|
-
"target": "ES6",
|
4
|
-
"module": "ESNext",
|
5
|
-
"lib": ["ESNext", "DOM"],
|
6
|
-
"moduleResolution": "Node",
|
7
|
-
"strict": true,
|
8
|
-
"esModuleInterop": true,
|
9
|
-
"skipLibCheck": true,
|
10
|
-
"declaration": true,
|
11
|
-
"outDir": "dist"
|
12
|
-
},
|
13
|
-
"include": ["src"]
|
14
|
-
}
|
15
|
-
|