-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-script-plugin.ts
More file actions
103 lines (86 loc) · 3.11 KB
/
add-script-plugin.ts
File metadata and controls
103 lines (86 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { Plugin, ResolvedConfig } from 'vite';
import htmlnano from 'htmlnano';
const addScriptPlugin = (): Plugin => {
let config: ResolvedConfig;
return {
configResolved(resolvedConfig) {
config = resolvedConfig;
},
name: 'add-script',
async transformIndexHtml(html, { filename }) {
const appPublicBaseHref = (config.define as { __APP_PUBLIC_BASE_HREF__: string })['__APP_PUBLIC_BASE_HREF__'];
if (!appPublicBaseHref) {
return html;
}
const content = processHtml({ appPublicBaseHref, filename, html });
if (content) {
try {
return (await htmlnano.process(content)).html;
} catch (error) {
console.error('Error processing HTML with htmlnano:', error);
}
}
return html;
},
};
};
const processHtml = ({
appPublicBaseHref,
filename,
html,
}: {
appPublicBaseHref: string;
filename: string;
html: string;
}) => {
const scriptContent = getScript(appPublicBaseHref, filename);
if (scriptContent) {
return html.replace(/(<title>.*?<\/title>)/, `$1${scriptContent}`);
}
};
const getScript = (appPublicBaseHref: string, filename: string): string | undefined => {
if (filename.endsWith('/index.html')) {
return `
<script>
(function() {
const publicBaseHref = ${appPublicBaseHref};
const sanitizedBaseHref = publicBaseHref.slice(-1) === '/' ? publicBaseHref.slice(0, -1) : publicBaseHref;
const replaceUrlIfValid = (baseHref, queryString) => {
if (/^\\?\\/.+/.test(queryString)) {
history.replaceState(null, '', baseHref + queryString.replace('?', ''));
}
};
replaceUrlIfValid(sanitizedBaseHref, location.search);
})();
</script>
`;
}
if (filename.endsWith('/404.html')) {
return `
<script>
(function() {
const publicBaseHref = ${appPublicBaseHref};
const sanitizedBaseHref = publicBaseHref.slice(-1) === '/' ? publicBaseHref.slice(0, -1) : publicBaseHref;
const removeDuplicateBaseInPath = (baseHref, currentPath) => {
const baseHrefUrl = new URL(baseHref);
const baseHrefPath = baseHrefUrl.pathname;
const duplicateBaseIndex = currentPath.indexOf('/?/');
if (duplicateBaseIndex === -1) {
return currentPath;
}
const pathBeforeQuery = currentPath.slice(0, duplicateBaseIndex + 2);
const pathAfterQuery = currentPath.slice(duplicateBaseIndex + 2);
if (pathAfterQuery.startsWith(baseHrefPath)) {
const cleanedPathAfterQuery = pathAfterQuery.slice(baseHrefPath.length);
return pathBeforeQuery + (cleanedPathAfterQuery.startsWith('/') ? cleanedPathAfterQuery : "/" + cleanedPathAfterQuery);
}
return currentPath;
};
const buildNewUrl = (baseHref) => baseHref + "/?" + location.pathname;
location.replace(removeDuplicateBaseInPath(publicBaseHref, buildNewUrl(sanitizedBaseHref)));
})();
</script>
`;
}
};
export default addScriptPlugin;