unplugin-version-injector 1.1.1 → 1.1.2-beta.0
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/rollup.d.mts +11 -0
- package/dist/rollup.d.ts +11 -0
- package/dist/rollup.js +72 -0
- package/dist/rollup.mjs +65 -0
- package/dist/vite.d.mts +5 -0
- package/dist/vite.d.ts +5 -0
- package/dist/vite.js +64 -0
- package/dist/vite.mjs +57 -0
- package/dist/webpack.d.mts +7 -0
- package/dist/webpack.d.ts +7 -0
- package/dist/webpack.js +81 -0
- package/dist/webpack.mjs +74 -0
- package/package.json +29 -14
- package/dist/index.d.mts +0 -20
- package/dist/index.d.ts +0 -20
- package/dist/index.js +0 -209971
- package/dist/index.mjs +0 -209970
package/dist/vite.js
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var fs = require('fs');
|
4
|
+
var path = require('path');
|
5
|
+
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
7
|
+
|
8
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
9
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
10
|
+
|
11
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
12
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
13
|
+
}) : x)(function(x) {
|
14
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
15
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
16
|
+
});
|
17
|
+
function getPackageVersion() {
|
18
|
+
try {
|
19
|
+
const packageJsonPath = path__default.default.resolve(process.cwd(), "package.json");
|
20
|
+
if (fs__default.default.existsSync(packageJsonPath)) {
|
21
|
+
return __require(packageJsonPath).version;
|
22
|
+
}
|
23
|
+
} catch (error) {
|
24
|
+
console.warn("[VersionInjector] Failed to read package.json:", error);
|
25
|
+
}
|
26
|
+
return "0.0.0";
|
27
|
+
}
|
28
|
+
function defaultFormatDate(date) {
|
29
|
+
return date.toISOString();
|
30
|
+
}
|
31
|
+
|
32
|
+
// src/core.ts
|
33
|
+
function createVersionInjector(options = {}) {
|
34
|
+
var _a, _b;
|
35
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
36
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
37
|
+
const metaTag = `<meta name="version" content="${version}">
|
38
|
+
`;
|
39
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
40
|
+
return function processHtml(html) {
|
41
|
+
if (!html.includes('<meta name="version"')) {
|
42
|
+
html = html.replace(/<head>/, `<head>
|
43
|
+
${metaTag}`);
|
44
|
+
}
|
45
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
46
|
+
html = html.replace("</body>", ` ${logScript}
|
47
|
+
</body>`);
|
48
|
+
}
|
49
|
+
return html;
|
50
|
+
};
|
51
|
+
}
|
52
|
+
|
53
|
+
// src/vite.ts
|
54
|
+
function versionInjectorPlugin(options = {}) {
|
55
|
+
const inject = createVersionInjector(options);
|
56
|
+
return {
|
57
|
+
name: "vite-version-injector",
|
58
|
+
transformIndexHtml(html) {
|
59
|
+
return inject(html);
|
60
|
+
}
|
61
|
+
};
|
62
|
+
}
|
63
|
+
|
64
|
+
module.exports = versionInjectorPlugin;
|
package/dist/vite.mjs
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
6
|
+
}) : x)(function(x) {
|
7
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
8
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
9
|
+
});
|
10
|
+
function getPackageVersion() {
|
11
|
+
try {
|
12
|
+
const packageJsonPath = path.resolve(process.cwd(), "package.json");
|
13
|
+
if (fs.existsSync(packageJsonPath)) {
|
14
|
+
return __require(packageJsonPath).version;
|
15
|
+
}
|
16
|
+
} catch (error) {
|
17
|
+
console.warn("[VersionInjector] Failed to read package.json:", error);
|
18
|
+
}
|
19
|
+
return "0.0.0";
|
20
|
+
}
|
21
|
+
function defaultFormatDate(date) {
|
22
|
+
return date.toISOString();
|
23
|
+
}
|
24
|
+
|
25
|
+
// src/core.ts
|
26
|
+
function createVersionInjector(options = {}) {
|
27
|
+
var _a, _b;
|
28
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
29
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
30
|
+
const metaTag = `<meta name="version" content="${version}">
|
31
|
+
`;
|
32
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
33
|
+
return function processHtml(html) {
|
34
|
+
if (!html.includes('<meta name="version"')) {
|
35
|
+
html = html.replace(/<head>/, `<head>
|
36
|
+
${metaTag}`);
|
37
|
+
}
|
38
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
39
|
+
html = html.replace("</body>", ` ${logScript}
|
40
|
+
</body>`);
|
41
|
+
}
|
42
|
+
return html;
|
43
|
+
};
|
44
|
+
}
|
45
|
+
|
46
|
+
// src/vite.ts
|
47
|
+
function versionInjectorPlugin(options = {}) {
|
48
|
+
const inject = createVersionInjector(options);
|
49
|
+
return {
|
50
|
+
name: "vite-version-injector",
|
51
|
+
transformIndexHtml(html) {
|
52
|
+
return inject(html);
|
53
|
+
}
|
54
|
+
};
|
55
|
+
}
|
56
|
+
|
57
|
+
export { versionInjectorPlugin as default };
|
package/dist/webpack.js
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var fs = require('fs');
|
4
|
+
var path = require('path');
|
5
|
+
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
7
|
+
|
8
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
9
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
10
|
+
|
11
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
12
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
13
|
+
}) : x)(function(x) {
|
14
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
15
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
16
|
+
});
|
17
|
+
function getPackageVersion() {
|
18
|
+
try {
|
19
|
+
const packageJsonPath = path__default.default.resolve(process.cwd(), "package.json");
|
20
|
+
if (fs__default.default.existsSync(packageJsonPath)) {
|
21
|
+
return __require(packageJsonPath).version;
|
22
|
+
}
|
23
|
+
} catch (error) {
|
24
|
+
console.warn("[VersionInjector] Failed to read package.json:", error);
|
25
|
+
}
|
26
|
+
return "0.0.0";
|
27
|
+
}
|
28
|
+
function defaultFormatDate(date) {
|
29
|
+
return date.toISOString();
|
30
|
+
}
|
31
|
+
|
32
|
+
// src/core.ts
|
33
|
+
function createVersionInjector(options = {}) {
|
34
|
+
var _a, _b;
|
35
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
36
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
37
|
+
const metaTag = `<meta name="version" content="${version}">
|
38
|
+
`;
|
39
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
40
|
+
return function processHtml(html) {
|
41
|
+
if (!html.includes('<meta name="version"')) {
|
42
|
+
html = html.replace(/<head>/, `<head>
|
43
|
+
${metaTag}`);
|
44
|
+
}
|
45
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
46
|
+
html = html.replace("</body>", ` ${logScript}
|
47
|
+
</body>`);
|
48
|
+
}
|
49
|
+
return html;
|
50
|
+
};
|
51
|
+
}
|
52
|
+
|
53
|
+
// src/webpack.ts
|
54
|
+
function versionInjectorPlugin(options = {}) {
|
55
|
+
const inject = createVersionInjector(options);
|
56
|
+
return {
|
57
|
+
apply(compiler) {
|
58
|
+
var _a, _b;
|
59
|
+
const isWebpack5 = (_b = (_a = compiler.webpack) == null ? void 0 : _a.version) == null ? void 0 : _b.startsWith("5");
|
60
|
+
compiler.hooks.compilation.tap("webpack-version-injector", (compilation) => {
|
61
|
+
if (isWebpack5) {
|
62
|
+
const { Compilation, sources } = compiler.webpack;
|
63
|
+
compilation.hooks.processAssets.tap(
|
64
|
+
{ name: "webpack-version-injector", stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE },
|
65
|
+
(assets) => {
|
66
|
+
for (const name in assets) {
|
67
|
+
if (name.endsWith(".html")) {
|
68
|
+
const html = assets[name].source().toString();
|
69
|
+
const result = inject(html);
|
70
|
+
compilation.updateAsset(name, new sources.RawSource(result));
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
);
|
75
|
+
}
|
76
|
+
});
|
77
|
+
}
|
78
|
+
};
|
79
|
+
}
|
80
|
+
|
81
|
+
module.exports = versionInjectorPlugin;
|
package/dist/webpack.mjs
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
6
|
+
}) : x)(function(x) {
|
7
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
8
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
9
|
+
});
|
10
|
+
function getPackageVersion() {
|
11
|
+
try {
|
12
|
+
const packageJsonPath = path.resolve(process.cwd(), "package.json");
|
13
|
+
if (fs.existsSync(packageJsonPath)) {
|
14
|
+
return __require(packageJsonPath).version;
|
15
|
+
}
|
16
|
+
} catch (error) {
|
17
|
+
console.warn("[VersionInjector] Failed to read package.json:", error);
|
18
|
+
}
|
19
|
+
return "0.0.0";
|
20
|
+
}
|
21
|
+
function defaultFormatDate(date) {
|
22
|
+
return date.toISOString();
|
23
|
+
}
|
24
|
+
|
25
|
+
// src/core.ts
|
26
|
+
function createVersionInjector(options = {}) {
|
27
|
+
var _a, _b;
|
28
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
29
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
30
|
+
const metaTag = `<meta name="version" content="${version}">
|
31
|
+
`;
|
32
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
33
|
+
return function processHtml(html) {
|
34
|
+
if (!html.includes('<meta name="version"')) {
|
35
|
+
html = html.replace(/<head>/, `<head>
|
36
|
+
${metaTag}`);
|
37
|
+
}
|
38
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
39
|
+
html = html.replace("</body>", ` ${logScript}
|
40
|
+
</body>`);
|
41
|
+
}
|
42
|
+
return html;
|
43
|
+
};
|
44
|
+
}
|
45
|
+
|
46
|
+
// src/webpack.ts
|
47
|
+
function versionInjectorPlugin(options = {}) {
|
48
|
+
const inject = createVersionInjector(options);
|
49
|
+
return {
|
50
|
+
apply(compiler) {
|
51
|
+
var _a, _b;
|
52
|
+
const isWebpack5 = (_b = (_a = compiler.webpack) == null ? void 0 : _a.version) == null ? void 0 : _b.startsWith("5");
|
53
|
+
compiler.hooks.compilation.tap("webpack-version-injector", (compilation) => {
|
54
|
+
if (isWebpack5) {
|
55
|
+
const { Compilation, sources } = compiler.webpack;
|
56
|
+
compilation.hooks.processAssets.tap(
|
57
|
+
{ name: "webpack-version-injector", stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE },
|
58
|
+
(assets) => {
|
59
|
+
for (const name in assets) {
|
60
|
+
if (name.endsWith(".html")) {
|
61
|
+
const html = assets[name].source().toString();
|
62
|
+
const result = inject(html);
|
63
|
+
compilation.updateAsset(name, new sources.RawSource(result));
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
);
|
68
|
+
}
|
69
|
+
});
|
70
|
+
}
|
71
|
+
};
|
72
|
+
}
|
73
|
+
|
74
|
+
export { versionInjectorPlugin as default };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "unplugin-version-injector",
|
3
|
-
"version": "1.1.
|
3
|
+
"version": "1.1.2-beta.0",
|
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)",
|
@@ -9,37 +9,52 @@
|
|
9
9
|
"url": "https://github.com/nianyi778/unplugin-version-injector.git"
|
10
10
|
},
|
11
11
|
"keywords": [
|
12
|
-
"unplugin",
|
13
12
|
"version",
|
14
13
|
"injector",
|
15
14
|
"webpack",
|
16
15
|
"vite",
|
17
16
|
"rollup"
|
18
17
|
],
|
19
|
-
"
|
20
|
-
|
21
|
-
|
18
|
+
"typesVersions": {
|
19
|
+
"*": {
|
20
|
+
"vite": [
|
21
|
+
"./dist/vite.d.ts"
|
22
|
+
],
|
23
|
+
"webpack": [
|
24
|
+
"./dist/webpack.d.ts"
|
25
|
+
],
|
26
|
+
"rollup": [
|
27
|
+
"./dist/rollup.d.ts"
|
28
|
+
]
|
29
|
+
}
|
30
|
+
},
|
22
31
|
"exports": {
|
23
|
-
"
|
24
|
-
"import": "./dist/
|
25
|
-
"require": "./dist/
|
32
|
+
"./vite": {
|
33
|
+
"import": "./dist/vite.mjs",
|
34
|
+
"require": "./dist/vite.js"
|
35
|
+
},
|
36
|
+
"./webpack": {
|
37
|
+
"import": "./dist/webpack.mjs",
|
38
|
+
"require": "./dist/webpack.js"
|
39
|
+
},
|
40
|
+
"./rollup": {
|
41
|
+
"import": "./dist/rollup.mjs",
|
42
|
+
"require": "./dist/rollup.js"
|
26
43
|
}
|
27
44
|
},
|
28
45
|
"scripts": {
|
29
46
|
"clean": "rm -rf dist",
|
30
47
|
"build": "tsup",
|
31
|
-
"
|
32
|
-
"prepublishOnly": "npm run clean && npm run build"
|
48
|
+
"prepublishOnly": "npm run build"
|
33
49
|
},
|
34
50
|
"dependencies": {
|
35
|
-
"
|
51
|
+
"tsup": "^8.4.0"
|
36
52
|
},
|
37
53
|
"devDependencies": {
|
38
|
-
"rollup": "^3",
|
39
|
-
"tsup": "^8.4.0",
|
40
54
|
"typescript": "^4.9.5",
|
55
|
+
"webpack": "^5",
|
41
56
|
"vite": "^4",
|
42
|
-
"
|
57
|
+
"rollup": "^3"
|
43
58
|
},
|
44
59
|
"publishConfig": {
|
45
60
|
"access": "public"
|
package/dist/index.d.mts
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
import * as _rollup from 'rollup';
|
2
|
-
import * as _webpack from 'webpack';
|
3
|
-
import * as _vite from 'vite';
|
4
|
-
import * as unplugin from 'unplugin';
|
5
|
-
|
6
|
-
interface VersionInjectorOptions {
|
7
|
-
version?: string;
|
8
|
-
formatDate?: (date: Date) => string;
|
9
|
-
dateFormat?: string;
|
10
|
-
injectToHead?: boolean;
|
11
|
-
injectToBody?: boolean;
|
12
|
-
}
|
13
|
-
|
14
|
-
declare const VersionInjectorPlugin: unplugin.UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
|
15
|
-
|
16
|
-
declare const vite: (options?: VersionInjectorOptions | undefined) => _vite.Plugin | _vite.Plugin[];
|
17
|
-
declare const webpack: (options?: VersionInjectorOptions | undefined) => _webpack.WebpackPluginInstance;
|
18
|
-
declare const rollup: (options?: VersionInjectorOptions | undefined) => _rollup.Plugin<any> | _rollup.Plugin<any>[];
|
19
|
-
|
20
|
-
export { VersionInjectorPlugin as default, rollup, vite, webpack };
|
package/dist/index.d.ts
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
import * as _rollup from 'rollup';
|
2
|
-
import * as _webpack from 'webpack';
|
3
|
-
import * as _vite from 'vite';
|
4
|
-
import * as unplugin from 'unplugin';
|
5
|
-
|
6
|
-
interface VersionInjectorOptions {
|
7
|
-
version?: string;
|
8
|
-
formatDate?: (date: Date) => string;
|
9
|
-
dateFormat?: string;
|
10
|
-
injectToHead?: boolean;
|
11
|
-
injectToBody?: boolean;
|
12
|
-
}
|
13
|
-
|
14
|
-
declare const VersionInjectorPlugin: unplugin.UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
|
15
|
-
|
16
|
-
declare const vite: (options?: VersionInjectorOptions | undefined) => _vite.Plugin | _vite.Plugin[];
|
17
|
-
declare const webpack: (options?: VersionInjectorOptions | undefined) => _webpack.WebpackPluginInstance;
|
18
|
-
declare const rollup: (options?: VersionInjectorOptions | undefined) => _rollup.Plugin<any> | _rollup.Plugin<any>[];
|
19
|
-
|
20
|
-
export { VersionInjectorPlugin as default, rollup, vite, webpack };
|