unplugin-version-injector 2.2.0 → 2.2.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/README.md +118 -15
- package/README.zh-CN.md +119 -15
- package/dist/index.js +45 -26
- package/dist/index.mjs +45 -26
- package/dist/rolldown.js +45 -26
- package/dist/rolldown.mjs +45 -26
- package/dist/rollup.js +45 -26
- package/dist/rollup.mjs +45 -26
- package/dist/rspack.js +45 -26
- package/dist/rspack.mjs +45 -26
- package/dist/vite.js +45 -26
- package/dist/vite.mjs +45 -26
- package/dist/webpack.js +45 -26
- package/dist/webpack.mjs +45 -26
- package/package.json +1 -1
package/dist/rolldown.js
CHANGED
|
@@ -100,18 +100,27 @@ function toScriptString(value) {
|
|
|
100
100
|
var INJECTED_MARK = 'data-injected="unplugin-version-injector"';
|
|
101
101
|
var HEADERS_INJECTED_MARK = 'data-injected="unplugin-version-injector-headers"';
|
|
102
102
|
var HEADER_NAME_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
103
|
+
var DEFAULT_VERSION_HEADER = "X-Client-Version";
|
|
104
|
+
var DEFAULT_BUILD_TIME_HEADER = "X-Client-Build-Time";
|
|
103
105
|
function normalizeRequestHeaders(opt) {
|
|
104
|
-
var _a, _b, _c;
|
|
105
106
|
if (!opt) return null;
|
|
106
107
|
const o = typeof opt === "object" ? opt : {};
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
const pickHeaderName = (value, fallback) => {
|
|
109
|
+
if (value == null) return fallback;
|
|
110
|
+
if (HEADER_NAME_RE.test(value)) return value;
|
|
111
|
+
console.warn(
|
|
112
|
+
`[VersionInjector] invalid request header name "${value}", falling back to "${fallback}"`
|
|
113
|
+
);
|
|
114
|
+
return fallback;
|
|
115
|
+
};
|
|
116
|
+
const include = (Array.isArray(o.include) ? o.include : []).filter(
|
|
117
|
+
(p) => typeof p === "string" || p instanceof RegExp
|
|
118
|
+
);
|
|
119
|
+
return {
|
|
120
|
+
versionHeaderName: pickHeaderName(o.versionHeaderName, DEFAULT_VERSION_HEADER),
|
|
121
|
+
buildTimeHeaderName: pickHeaderName(o.buildTimeHeaderName, DEFAULT_BUILD_TIME_HEADER),
|
|
122
|
+
include
|
|
123
|
+
};
|
|
115
124
|
}
|
|
116
125
|
function sanitizeHeaderValue(value) {
|
|
117
126
|
return value.replace(/[^\t\x20-\x7E]/g, "").trim();
|
|
@@ -135,11 +144,13 @@ function createVersionInjector(options = {}) {
|
|
|
135
144
|
const buildLogScript = (buildTime) => `
|
|
136
145
|
<script ${INJECTED_MARK}>
|
|
137
146
|
(function () {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
147
|
+
try {
|
|
148
|
+
var isDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
149
|
+
var bg = isDark ? '#ffffff' : '#1e1e1e';
|
|
150
|
+
var base = 'background: ' + bg + '; border-radius: 4px; padding: 4px; font-size: 12px;';
|
|
151
|
+
console.log('%c' + ${toScriptString(` ${name}@${version} `)}, base + ' color: #00c853;');
|
|
152
|
+
console.log('%c' + ${toScriptString(` Build Time: ${buildTime} `)}, base + ' color: #ffab00;');
|
|
153
|
+
} catch (e) {}
|
|
143
154
|
})();
|
|
144
155
|
</script>`;
|
|
145
156
|
const buildHeaderScript = (buildTime, cfg) => {
|
|
@@ -148,6 +159,7 @@ function createVersionInjector(options = {}) {
|
|
|
148
159
|
return `
|
|
149
160
|
<script ${HEADERS_INJECTED_MARK}>
|
|
150
161
|
(function () {
|
|
162
|
+
try {
|
|
151
163
|
if (window.__UVI_HEADERS_PATCHED__) return;
|
|
152
164
|
window.__UVI_HEADERS_PATCHED__ = true;
|
|
153
165
|
var VERSION_HEADER = ${toScriptString(cfg.versionHeaderName)};
|
|
@@ -226,27 +238,34 @@ function createVersionInjector(options = {}) {
|
|
|
226
238
|
};
|
|
227
239
|
}
|
|
228
240
|
}
|
|
241
|
+
} catch (e) {}
|
|
229
242
|
})();
|
|
230
243
|
</script>`;
|
|
231
244
|
};
|
|
232
245
|
const processHtml = function processHtml2(html) {
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
246
|
+
const original = html;
|
|
247
|
+
try {
|
|
248
|
+
const buildTime = formatDate2(/* @__PURE__ */ new Date());
|
|
249
|
+
if (!html.includes('<meta name="version"')) {
|
|
250
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
236
251
|
${metaTag}`);
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
252
|
+
}
|
|
253
|
+
if (headersConfig && !html.includes(HEADERS_INJECTED_MARK)) {
|
|
254
|
+
const headerScript = buildHeaderScript(buildTime, headersConfig);
|
|
255
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
241
256
|
${headerScript}
|
|
242
257
|
`);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
258
|
+
}
|
|
259
|
+
if (options.log !== false && !html.includes(INJECTED_MARK)) {
|
|
260
|
+
const logScript = buildLogScript(buildTime);
|
|
261
|
+
html = html.replace(/<\/body>/i, () => ` ${logScript}
|
|
247
262
|
</body>`);
|
|
263
|
+
}
|
|
264
|
+
return html;
|
|
265
|
+
} catch (err) {
|
|
266
|
+
console.warn("[VersionInjector] injection failed, returning original HTML unchanged:", err);
|
|
267
|
+
return original;
|
|
248
268
|
}
|
|
249
|
-
return html;
|
|
250
269
|
};
|
|
251
270
|
processHtml.resetBuildTime = () => {
|
|
252
271
|
};
|
package/dist/rolldown.mjs
CHANGED
|
@@ -93,18 +93,27 @@ function toScriptString(value) {
|
|
|
93
93
|
var INJECTED_MARK = 'data-injected="unplugin-version-injector"';
|
|
94
94
|
var HEADERS_INJECTED_MARK = 'data-injected="unplugin-version-injector-headers"';
|
|
95
95
|
var HEADER_NAME_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
96
|
+
var DEFAULT_VERSION_HEADER = "X-Client-Version";
|
|
97
|
+
var DEFAULT_BUILD_TIME_HEADER = "X-Client-Build-Time";
|
|
96
98
|
function normalizeRequestHeaders(opt) {
|
|
97
|
-
var _a, _b, _c;
|
|
98
99
|
if (!opt) return null;
|
|
99
100
|
const o = typeof opt === "object" ? opt : {};
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
101
|
+
const pickHeaderName = (value, fallback) => {
|
|
102
|
+
if (value == null) return fallback;
|
|
103
|
+
if (HEADER_NAME_RE.test(value)) return value;
|
|
104
|
+
console.warn(
|
|
105
|
+
`[VersionInjector] invalid request header name "${value}", falling back to "${fallback}"`
|
|
106
|
+
);
|
|
107
|
+
return fallback;
|
|
108
|
+
};
|
|
109
|
+
const include = (Array.isArray(o.include) ? o.include : []).filter(
|
|
110
|
+
(p) => typeof p === "string" || p instanceof RegExp
|
|
111
|
+
);
|
|
112
|
+
return {
|
|
113
|
+
versionHeaderName: pickHeaderName(o.versionHeaderName, DEFAULT_VERSION_HEADER),
|
|
114
|
+
buildTimeHeaderName: pickHeaderName(o.buildTimeHeaderName, DEFAULT_BUILD_TIME_HEADER),
|
|
115
|
+
include
|
|
116
|
+
};
|
|
108
117
|
}
|
|
109
118
|
function sanitizeHeaderValue(value) {
|
|
110
119
|
return value.replace(/[^\t\x20-\x7E]/g, "").trim();
|
|
@@ -128,11 +137,13 @@ function createVersionInjector(options = {}) {
|
|
|
128
137
|
const buildLogScript = (buildTime) => `
|
|
129
138
|
<script ${INJECTED_MARK}>
|
|
130
139
|
(function () {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
140
|
+
try {
|
|
141
|
+
var isDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
142
|
+
var bg = isDark ? '#ffffff' : '#1e1e1e';
|
|
143
|
+
var base = 'background: ' + bg + '; border-radius: 4px; padding: 4px; font-size: 12px;';
|
|
144
|
+
console.log('%c' + ${toScriptString(` ${name}@${version} `)}, base + ' color: #00c853;');
|
|
145
|
+
console.log('%c' + ${toScriptString(` Build Time: ${buildTime} `)}, base + ' color: #ffab00;');
|
|
146
|
+
} catch (e) {}
|
|
136
147
|
})();
|
|
137
148
|
</script>`;
|
|
138
149
|
const buildHeaderScript = (buildTime, cfg) => {
|
|
@@ -141,6 +152,7 @@ function createVersionInjector(options = {}) {
|
|
|
141
152
|
return `
|
|
142
153
|
<script ${HEADERS_INJECTED_MARK}>
|
|
143
154
|
(function () {
|
|
155
|
+
try {
|
|
144
156
|
if (window.__UVI_HEADERS_PATCHED__) return;
|
|
145
157
|
window.__UVI_HEADERS_PATCHED__ = true;
|
|
146
158
|
var VERSION_HEADER = ${toScriptString(cfg.versionHeaderName)};
|
|
@@ -219,27 +231,34 @@ function createVersionInjector(options = {}) {
|
|
|
219
231
|
};
|
|
220
232
|
}
|
|
221
233
|
}
|
|
234
|
+
} catch (e) {}
|
|
222
235
|
})();
|
|
223
236
|
</script>`;
|
|
224
237
|
};
|
|
225
238
|
const processHtml = function processHtml2(html) {
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
239
|
+
const original = html;
|
|
240
|
+
try {
|
|
241
|
+
const buildTime = formatDate2(/* @__PURE__ */ new Date());
|
|
242
|
+
if (!html.includes('<meta name="version"')) {
|
|
243
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
229
244
|
${metaTag}`);
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
245
|
+
}
|
|
246
|
+
if (headersConfig && !html.includes(HEADERS_INJECTED_MARK)) {
|
|
247
|
+
const headerScript = buildHeaderScript(buildTime, headersConfig);
|
|
248
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
234
249
|
${headerScript}
|
|
235
250
|
`);
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
251
|
+
}
|
|
252
|
+
if (options.log !== false && !html.includes(INJECTED_MARK)) {
|
|
253
|
+
const logScript = buildLogScript(buildTime);
|
|
254
|
+
html = html.replace(/<\/body>/i, () => ` ${logScript}
|
|
240
255
|
</body>`);
|
|
256
|
+
}
|
|
257
|
+
return html;
|
|
258
|
+
} catch (err) {
|
|
259
|
+
console.warn("[VersionInjector] injection failed, returning original HTML unchanged:", err);
|
|
260
|
+
return original;
|
|
241
261
|
}
|
|
242
|
-
return html;
|
|
243
262
|
};
|
|
244
263
|
processHtml.resetBuildTime = () => {
|
|
245
264
|
};
|
package/dist/rollup.js
CHANGED
|
@@ -100,18 +100,27 @@ function toScriptString(value) {
|
|
|
100
100
|
var INJECTED_MARK = 'data-injected="unplugin-version-injector"';
|
|
101
101
|
var HEADERS_INJECTED_MARK = 'data-injected="unplugin-version-injector-headers"';
|
|
102
102
|
var HEADER_NAME_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
103
|
+
var DEFAULT_VERSION_HEADER = "X-Client-Version";
|
|
104
|
+
var DEFAULT_BUILD_TIME_HEADER = "X-Client-Build-Time";
|
|
103
105
|
function normalizeRequestHeaders(opt) {
|
|
104
|
-
var _a, _b, _c;
|
|
105
106
|
if (!opt) return null;
|
|
106
107
|
const o = typeof opt === "object" ? opt : {};
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
const pickHeaderName = (value, fallback) => {
|
|
109
|
+
if (value == null) return fallback;
|
|
110
|
+
if (HEADER_NAME_RE.test(value)) return value;
|
|
111
|
+
console.warn(
|
|
112
|
+
`[VersionInjector] invalid request header name "${value}", falling back to "${fallback}"`
|
|
113
|
+
);
|
|
114
|
+
return fallback;
|
|
115
|
+
};
|
|
116
|
+
const include = (Array.isArray(o.include) ? o.include : []).filter(
|
|
117
|
+
(p) => typeof p === "string" || p instanceof RegExp
|
|
118
|
+
);
|
|
119
|
+
return {
|
|
120
|
+
versionHeaderName: pickHeaderName(o.versionHeaderName, DEFAULT_VERSION_HEADER),
|
|
121
|
+
buildTimeHeaderName: pickHeaderName(o.buildTimeHeaderName, DEFAULT_BUILD_TIME_HEADER),
|
|
122
|
+
include
|
|
123
|
+
};
|
|
115
124
|
}
|
|
116
125
|
function sanitizeHeaderValue(value) {
|
|
117
126
|
return value.replace(/[^\t\x20-\x7E]/g, "").trim();
|
|
@@ -135,11 +144,13 @@ function createVersionInjector(options = {}) {
|
|
|
135
144
|
const buildLogScript = (buildTime) => `
|
|
136
145
|
<script ${INJECTED_MARK}>
|
|
137
146
|
(function () {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
147
|
+
try {
|
|
148
|
+
var isDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
149
|
+
var bg = isDark ? '#ffffff' : '#1e1e1e';
|
|
150
|
+
var base = 'background: ' + bg + '; border-radius: 4px; padding: 4px; font-size: 12px;';
|
|
151
|
+
console.log('%c' + ${toScriptString(` ${name}@${version} `)}, base + ' color: #00c853;');
|
|
152
|
+
console.log('%c' + ${toScriptString(` Build Time: ${buildTime} `)}, base + ' color: #ffab00;');
|
|
153
|
+
} catch (e) {}
|
|
143
154
|
})();
|
|
144
155
|
</script>`;
|
|
145
156
|
const buildHeaderScript = (buildTime, cfg) => {
|
|
@@ -148,6 +159,7 @@ function createVersionInjector(options = {}) {
|
|
|
148
159
|
return `
|
|
149
160
|
<script ${HEADERS_INJECTED_MARK}>
|
|
150
161
|
(function () {
|
|
162
|
+
try {
|
|
151
163
|
if (window.__UVI_HEADERS_PATCHED__) return;
|
|
152
164
|
window.__UVI_HEADERS_PATCHED__ = true;
|
|
153
165
|
var VERSION_HEADER = ${toScriptString(cfg.versionHeaderName)};
|
|
@@ -226,27 +238,34 @@ function createVersionInjector(options = {}) {
|
|
|
226
238
|
};
|
|
227
239
|
}
|
|
228
240
|
}
|
|
241
|
+
} catch (e) {}
|
|
229
242
|
})();
|
|
230
243
|
</script>`;
|
|
231
244
|
};
|
|
232
245
|
const processHtml = function processHtml2(html) {
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
246
|
+
const original = html;
|
|
247
|
+
try {
|
|
248
|
+
const buildTime = formatDate2(/* @__PURE__ */ new Date());
|
|
249
|
+
if (!html.includes('<meta name="version"')) {
|
|
250
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
236
251
|
${metaTag}`);
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
252
|
+
}
|
|
253
|
+
if (headersConfig && !html.includes(HEADERS_INJECTED_MARK)) {
|
|
254
|
+
const headerScript = buildHeaderScript(buildTime, headersConfig);
|
|
255
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
241
256
|
${headerScript}
|
|
242
257
|
`);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
258
|
+
}
|
|
259
|
+
if (options.log !== false && !html.includes(INJECTED_MARK)) {
|
|
260
|
+
const logScript = buildLogScript(buildTime);
|
|
261
|
+
html = html.replace(/<\/body>/i, () => ` ${logScript}
|
|
247
262
|
</body>`);
|
|
263
|
+
}
|
|
264
|
+
return html;
|
|
265
|
+
} catch (err) {
|
|
266
|
+
console.warn("[VersionInjector] injection failed, returning original HTML unchanged:", err);
|
|
267
|
+
return original;
|
|
248
268
|
}
|
|
249
|
-
return html;
|
|
250
269
|
};
|
|
251
270
|
processHtml.resetBuildTime = () => {
|
|
252
271
|
};
|
package/dist/rollup.mjs
CHANGED
|
@@ -93,18 +93,27 @@ function toScriptString(value) {
|
|
|
93
93
|
var INJECTED_MARK = 'data-injected="unplugin-version-injector"';
|
|
94
94
|
var HEADERS_INJECTED_MARK = 'data-injected="unplugin-version-injector-headers"';
|
|
95
95
|
var HEADER_NAME_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
96
|
+
var DEFAULT_VERSION_HEADER = "X-Client-Version";
|
|
97
|
+
var DEFAULT_BUILD_TIME_HEADER = "X-Client-Build-Time";
|
|
96
98
|
function normalizeRequestHeaders(opt) {
|
|
97
|
-
var _a, _b, _c;
|
|
98
99
|
if (!opt) return null;
|
|
99
100
|
const o = typeof opt === "object" ? opt : {};
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
101
|
+
const pickHeaderName = (value, fallback) => {
|
|
102
|
+
if (value == null) return fallback;
|
|
103
|
+
if (HEADER_NAME_RE.test(value)) return value;
|
|
104
|
+
console.warn(
|
|
105
|
+
`[VersionInjector] invalid request header name "${value}", falling back to "${fallback}"`
|
|
106
|
+
);
|
|
107
|
+
return fallback;
|
|
108
|
+
};
|
|
109
|
+
const include = (Array.isArray(o.include) ? o.include : []).filter(
|
|
110
|
+
(p) => typeof p === "string" || p instanceof RegExp
|
|
111
|
+
);
|
|
112
|
+
return {
|
|
113
|
+
versionHeaderName: pickHeaderName(o.versionHeaderName, DEFAULT_VERSION_HEADER),
|
|
114
|
+
buildTimeHeaderName: pickHeaderName(o.buildTimeHeaderName, DEFAULT_BUILD_TIME_HEADER),
|
|
115
|
+
include
|
|
116
|
+
};
|
|
108
117
|
}
|
|
109
118
|
function sanitizeHeaderValue(value) {
|
|
110
119
|
return value.replace(/[^\t\x20-\x7E]/g, "").trim();
|
|
@@ -128,11 +137,13 @@ function createVersionInjector(options = {}) {
|
|
|
128
137
|
const buildLogScript = (buildTime) => `
|
|
129
138
|
<script ${INJECTED_MARK}>
|
|
130
139
|
(function () {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
140
|
+
try {
|
|
141
|
+
var isDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
142
|
+
var bg = isDark ? '#ffffff' : '#1e1e1e';
|
|
143
|
+
var base = 'background: ' + bg + '; border-radius: 4px; padding: 4px; font-size: 12px;';
|
|
144
|
+
console.log('%c' + ${toScriptString(` ${name}@${version} `)}, base + ' color: #00c853;');
|
|
145
|
+
console.log('%c' + ${toScriptString(` Build Time: ${buildTime} `)}, base + ' color: #ffab00;');
|
|
146
|
+
} catch (e) {}
|
|
136
147
|
})();
|
|
137
148
|
</script>`;
|
|
138
149
|
const buildHeaderScript = (buildTime, cfg) => {
|
|
@@ -141,6 +152,7 @@ function createVersionInjector(options = {}) {
|
|
|
141
152
|
return `
|
|
142
153
|
<script ${HEADERS_INJECTED_MARK}>
|
|
143
154
|
(function () {
|
|
155
|
+
try {
|
|
144
156
|
if (window.__UVI_HEADERS_PATCHED__) return;
|
|
145
157
|
window.__UVI_HEADERS_PATCHED__ = true;
|
|
146
158
|
var VERSION_HEADER = ${toScriptString(cfg.versionHeaderName)};
|
|
@@ -219,27 +231,34 @@ function createVersionInjector(options = {}) {
|
|
|
219
231
|
};
|
|
220
232
|
}
|
|
221
233
|
}
|
|
234
|
+
} catch (e) {}
|
|
222
235
|
})();
|
|
223
236
|
</script>`;
|
|
224
237
|
};
|
|
225
238
|
const processHtml = function processHtml2(html) {
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
239
|
+
const original = html;
|
|
240
|
+
try {
|
|
241
|
+
const buildTime = formatDate2(/* @__PURE__ */ new Date());
|
|
242
|
+
if (!html.includes('<meta name="version"')) {
|
|
243
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
229
244
|
${metaTag}`);
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
245
|
+
}
|
|
246
|
+
if (headersConfig && !html.includes(HEADERS_INJECTED_MARK)) {
|
|
247
|
+
const headerScript = buildHeaderScript(buildTime, headersConfig);
|
|
248
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
234
249
|
${headerScript}
|
|
235
250
|
`);
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
251
|
+
}
|
|
252
|
+
if (options.log !== false && !html.includes(INJECTED_MARK)) {
|
|
253
|
+
const logScript = buildLogScript(buildTime);
|
|
254
|
+
html = html.replace(/<\/body>/i, () => ` ${logScript}
|
|
240
255
|
</body>`);
|
|
256
|
+
}
|
|
257
|
+
return html;
|
|
258
|
+
} catch (err) {
|
|
259
|
+
console.warn("[VersionInjector] injection failed, returning original HTML unchanged:", err);
|
|
260
|
+
return original;
|
|
241
261
|
}
|
|
242
|
-
return html;
|
|
243
262
|
};
|
|
244
263
|
processHtml.resetBuildTime = () => {
|
|
245
264
|
};
|
package/dist/rspack.js
CHANGED
|
@@ -100,18 +100,27 @@ function toScriptString(value) {
|
|
|
100
100
|
var INJECTED_MARK = 'data-injected="unplugin-version-injector"';
|
|
101
101
|
var HEADERS_INJECTED_MARK = 'data-injected="unplugin-version-injector-headers"';
|
|
102
102
|
var HEADER_NAME_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
103
|
+
var DEFAULT_VERSION_HEADER = "X-Client-Version";
|
|
104
|
+
var DEFAULT_BUILD_TIME_HEADER = "X-Client-Build-Time";
|
|
103
105
|
function normalizeRequestHeaders(opt) {
|
|
104
|
-
var _a, _b, _c;
|
|
105
106
|
if (!opt) return null;
|
|
106
107
|
const o = typeof opt === "object" ? opt : {};
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
const pickHeaderName = (value, fallback) => {
|
|
109
|
+
if (value == null) return fallback;
|
|
110
|
+
if (HEADER_NAME_RE.test(value)) return value;
|
|
111
|
+
console.warn(
|
|
112
|
+
`[VersionInjector] invalid request header name "${value}", falling back to "${fallback}"`
|
|
113
|
+
);
|
|
114
|
+
return fallback;
|
|
115
|
+
};
|
|
116
|
+
const include = (Array.isArray(o.include) ? o.include : []).filter(
|
|
117
|
+
(p) => typeof p === "string" || p instanceof RegExp
|
|
118
|
+
);
|
|
119
|
+
return {
|
|
120
|
+
versionHeaderName: pickHeaderName(o.versionHeaderName, DEFAULT_VERSION_HEADER),
|
|
121
|
+
buildTimeHeaderName: pickHeaderName(o.buildTimeHeaderName, DEFAULT_BUILD_TIME_HEADER),
|
|
122
|
+
include
|
|
123
|
+
};
|
|
115
124
|
}
|
|
116
125
|
function sanitizeHeaderValue(value) {
|
|
117
126
|
return value.replace(/[^\t\x20-\x7E]/g, "").trim();
|
|
@@ -135,11 +144,13 @@ function createVersionInjector(options = {}) {
|
|
|
135
144
|
const buildLogScript = (buildTime) => `
|
|
136
145
|
<script ${INJECTED_MARK}>
|
|
137
146
|
(function () {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
147
|
+
try {
|
|
148
|
+
var isDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
149
|
+
var bg = isDark ? '#ffffff' : '#1e1e1e';
|
|
150
|
+
var base = 'background: ' + bg + '; border-radius: 4px; padding: 4px; font-size: 12px;';
|
|
151
|
+
console.log('%c' + ${toScriptString(` ${name}@${version} `)}, base + ' color: #00c853;');
|
|
152
|
+
console.log('%c' + ${toScriptString(` Build Time: ${buildTime} `)}, base + ' color: #ffab00;');
|
|
153
|
+
} catch (e) {}
|
|
143
154
|
})();
|
|
144
155
|
</script>`;
|
|
145
156
|
const buildHeaderScript = (buildTime, cfg) => {
|
|
@@ -148,6 +159,7 @@ function createVersionInjector(options = {}) {
|
|
|
148
159
|
return `
|
|
149
160
|
<script ${HEADERS_INJECTED_MARK}>
|
|
150
161
|
(function () {
|
|
162
|
+
try {
|
|
151
163
|
if (window.__UVI_HEADERS_PATCHED__) return;
|
|
152
164
|
window.__UVI_HEADERS_PATCHED__ = true;
|
|
153
165
|
var VERSION_HEADER = ${toScriptString(cfg.versionHeaderName)};
|
|
@@ -226,27 +238,34 @@ function createVersionInjector(options = {}) {
|
|
|
226
238
|
};
|
|
227
239
|
}
|
|
228
240
|
}
|
|
241
|
+
} catch (e) {}
|
|
229
242
|
})();
|
|
230
243
|
</script>`;
|
|
231
244
|
};
|
|
232
245
|
const processHtml = function processHtml2(html) {
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
246
|
+
const original = html;
|
|
247
|
+
try {
|
|
248
|
+
const buildTime = formatDate2(/* @__PURE__ */ new Date());
|
|
249
|
+
if (!html.includes('<meta name="version"')) {
|
|
250
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
236
251
|
${metaTag}`);
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
252
|
+
}
|
|
253
|
+
if (headersConfig && !html.includes(HEADERS_INJECTED_MARK)) {
|
|
254
|
+
const headerScript = buildHeaderScript(buildTime, headersConfig);
|
|
255
|
+
html = html.replace(/<head[^>]*>/i, (match) => `${match}
|
|
241
256
|
${headerScript}
|
|
242
257
|
`);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
258
|
+
}
|
|
259
|
+
if (options.log !== false && !html.includes(INJECTED_MARK)) {
|
|
260
|
+
const logScript = buildLogScript(buildTime);
|
|
261
|
+
html = html.replace(/<\/body>/i, () => ` ${logScript}
|
|
247
262
|
</body>`);
|
|
263
|
+
}
|
|
264
|
+
return html;
|
|
265
|
+
} catch (err) {
|
|
266
|
+
console.warn("[VersionInjector] injection failed, returning original HTML unchanged:", err);
|
|
267
|
+
return original;
|
|
248
268
|
}
|
|
249
|
-
return html;
|
|
250
269
|
};
|
|
251
270
|
processHtml.resetBuildTime = () => {
|
|
252
271
|
};
|