@hitachivantara/app-shell-vite-plugin 1.4.7 → 1.5.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/dist/tests/vite-crossorigin-fix-plugin.test.d.ts +2 -0
- package/dist/tests/vite-crossorigin-fix-plugin.test.d.ts.map +1 -0
- package/dist/tests/vite-crossorigin-fix-plugin.test.js +104 -0
- package/dist/tests/vite-crossorigin-fix-plugin.test.js.map +1 -0
- package/dist/vite-crossorigin-fix-plugin.d.ts +10 -0
- package/dist/vite-crossorigin-fix-plugin.d.ts.map +1 -0
- package/dist/vite-crossorigin-fix-plugin.js +84 -0
- package/dist/vite-crossorigin-fix-plugin.js.map +1 -0
- package/dist/vite-plugin.d.ts.map +1 -1
- package/dist/vite-plugin.js +3 -0
- package/dist/vite-plugin.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-crossorigin-fix-plugin.test.d.ts","sourceRoot":"","sources":["../../src/tests/vite-crossorigin-fix-plugin.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { checkCrossOrigin, addUseCredentials } from "../vite-crossorigin-fix-plugin";
|
|
3
|
+
describe("vite-crossorigin-fix", () => {
|
|
4
|
+
describe("checkCrossOrigin", () => {
|
|
5
|
+
it('should return an empty array if no scripts have crossorigin="use-credentials"', () => {
|
|
6
|
+
const html = `
|
|
7
|
+
<!DOCTYPE html>
|
|
8
|
+
<html lang="en">
|
|
9
|
+
<head>
|
|
10
|
+
<title>Test</title>
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<script src="/assets/main.js"></script>
|
|
14
|
+
</body>
|
|
15
|
+
</html>
|
|
16
|
+
`;
|
|
17
|
+
const result = checkCrossOrigin(html);
|
|
18
|
+
expect(result).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
it('should return an array of script sources with crossorigin="use-credentials"', () => {
|
|
21
|
+
const html = `
|
|
22
|
+
<!DOCTYPE html>
|
|
23
|
+
<html lang="en">
|
|
24
|
+
<head>
|
|
25
|
+
<title>Test</title>
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
<script src="/assets/main.js" crossorigin="use-credentials" type="module"></script>
|
|
29
|
+
</body>
|
|
30
|
+
</html>
|
|
31
|
+
`;
|
|
32
|
+
const result = checkCrossOrigin(html);
|
|
33
|
+
expect(result).toEqual(["/assets/main.js"]);
|
|
34
|
+
});
|
|
35
|
+
it("should ignore scripts that are not modules", () => {
|
|
36
|
+
const html = `
|
|
37
|
+
<!DOCTYPE html>
|
|
38
|
+
<html lang="en">
|
|
39
|
+
<head>
|
|
40
|
+
<title>Test</title>
|
|
41
|
+
</head>
|
|
42
|
+
<body>
|
|
43
|
+
<script src="/assets/main.js" crossorigin="use-credentials"></script>
|
|
44
|
+
</body>
|
|
45
|
+
</html>
|
|
46
|
+
`;
|
|
47
|
+
const result = checkCrossOrigin(html);
|
|
48
|
+
expect(result).toEqual([]);
|
|
49
|
+
});
|
|
50
|
+
it("should ignore external and data URLs", () => {
|
|
51
|
+
const html = `
|
|
52
|
+
<!DOCTYPE html>
|
|
53
|
+
<html lang="en">
|
|
54
|
+
<head>
|
|
55
|
+
<title>Test</title>
|
|
56
|
+
</head>
|
|
57
|
+
<body>
|
|
58
|
+
<script src="https://external.com/main.js" crossorigin="use-credentials" type="module"></script>
|
|
59
|
+
<script src="data:text/javascript;base64,abcd" crossorigin="use-credentials" type="module"></script>
|
|
60
|
+
</body>
|
|
61
|
+
</html>
|
|
62
|
+
`;
|
|
63
|
+
const result = checkCrossOrigin(html);
|
|
64
|
+
expect(result).toEqual([]);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
describe("addUseCredentials", () => {
|
|
68
|
+
it('should add crossorigin="use-credentials" to all script tags', () => {
|
|
69
|
+
const html = `
|
|
70
|
+
<!DOCTYPE html>
|
|
71
|
+
<html lang="en">
|
|
72
|
+
<head>
|
|
73
|
+
<title>Test</title>
|
|
74
|
+
</head>
|
|
75
|
+
<body>
|
|
76
|
+
<script
|
|
77
|
+
src="/assets/main.js"
|
|
78
|
+
crossorigin type="module"></script>
|
|
79
|
+
</body>
|
|
80
|
+
</html>
|
|
81
|
+
`;
|
|
82
|
+
const result = addUseCredentials("main.js", html);
|
|
83
|
+
expect(result).toContain('crossorigin="use-credentials"');
|
|
84
|
+
});
|
|
85
|
+
it('should handle link rel="modulepreload" tags', () => {
|
|
86
|
+
const html = `
|
|
87
|
+
<!DOCTYPE html>
|
|
88
|
+
<html lang="en">
|
|
89
|
+
<head>
|
|
90
|
+
<title>Test</title>
|
|
91
|
+
<link rel="modulepreload"
|
|
92
|
+
href="/assets/main.js"
|
|
93
|
+
crossorigin>
|
|
94
|
+
</head>
|
|
95
|
+
<body>
|
|
96
|
+
</body>
|
|
97
|
+
</html>
|
|
98
|
+
`;
|
|
99
|
+
const result = addUseCredentials("main.js", html);
|
|
100
|
+
expect(result).toContain('crossorigin="use-credentials"');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
//# sourceMappingURL=vite-crossorigin-fix-plugin.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-crossorigin-fix-plugin.test.js","sourceRoot":"","sources":["../../src/tests/vite-crossorigin-fix-plugin.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,gCAAgC,CAAC;AAExC,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IACpC,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAChC,EAAE,CAAC,+EAA+E,EAAE,GAAG,EAAE;YACvF,MAAM,IAAI,GAAG;;;;;;;;;;OAUZ,CAAC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6CAA6C,EAAE,GAAG,EAAE;YACrD,MAAM,IAAI,GAAG;;;;;;;;;;;;OAYZ,CAAC;YAEF,MAAM,MAAM,GAAG,iBAAiB,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;YAClD,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,+BAA+B,CAAC,CAAC;QAC5D,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { describe, it, expect } from \"vitest\";\nimport {\n checkCrossOrigin,\n addUseCredentials\n} from \"../vite-crossorigin-fix-plugin\";\n\ndescribe(\"vite-crossorigin-fix\", () => {\n describe(\"checkCrossOrigin\", () => {\n it('should return an empty array if no scripts have crossorigin=\"use-credentials\"', () => {\n const html = `\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <title>Test</title>\n </head>\n <body>\n <script src=\"/assets/main.js\"></script>\n </body>\n </html>\n `;\n\n const result = checkCrossOrigin(html);\n expect(result).toEqual([]);\n });\n\n it('should return an array of script sources with crossorigin=\"use-credentials\"', () => {\n const html = `\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <title>Test</title>\n </head>\n <body>\n <script src=\"/assets/main.js\" crossorigin=\"use-credentials\" type=\"module\"></script>\n </body>\n </html>\n `;\n\n const result = checkCrossOrigin(html);\n expect(result).toEqual([\"/assets/main.js\"]);\n });\n\n it(\"should ignore scripts that are not modules\", () => {\n const html = `\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <title>Test</title>\n </head>\n <body>\n <script src=\"/assets/main.js\" crossorigin=\"use-credentials\"></script>\n </body>\n </html>\n `;\n\n const result = checkCrossOrigin(html);\n expect(result).toEqual([]);\n });\n\n it(\"should ignore external and data URLs\", () => {\n const html = `\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <title>Test</title>\n </head>\n <body>\n <script src=\"https://external.com/main.js\" crossorigin=\"use-credentials\" type=\"module\"></script>\n <script src=\"data:text/javascript;base64,abcd\" crossorigin=\"use-credentials\" type=\"module\"></script>\n </body>\n </html>\n `;\n\n const result = checkCrossOrigin(html);\n expect(result).toEqual([]);\n });\n });\n\n describe(\"addUseCredentials\", () => {\n it('should add crossorigin=\"use-credentials\" to all script tags', () => {\n const html = `\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <title>Test</title>\n </head>\n <body>\n <script \n src=\"/assets/main.js\" \n crossorigin type=\"module\"></script>\n </body>\n </html>\n `;\n\n const result = addUseCredentials(\"main.js\", html);\n expect(result).toContain('crossorigin=\"use-credentials\"');\n });\n\n it('should handle link rel=\"modulepreload\" tags', () => {\n const html = `\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <title>Test</title>\n <link rel=\"modulepreload\"\n href=\"/assets/main.js\"\n crossorigin>\n </head>\n <body>\n </body>\n </html>\n `;\n\n const result = addUseCredentials(\"main.js\", html);\n expect(result).toContain('crossorigin=\"use-credentials\"');\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PluginOption } from "vite";
|
|
2
|
+
export declare function addUseCredentials(scriptSrc: string, html: string): string;
|
|
3
|
+
export declare const externalRE: RegExp;
|
|
4
|
+
export declare const isExternalUrl: (url: string) => boolean;
|
|
5
|
+
export declare const dataUrlRE: RegExp;
|
|
6
|
+
export declare const isDataUrl: (url: string) => boolean;
|
|
7
|
+
export declare function checkCrossOrigin(html: string): string[];
|
|
8
|
+
declare const fixCrossOrigin: () => PluginOption;
|
|
9
|
+
export default fixCrossOrigin;
|
|
10
|
+
//# sourceMappingURL=vite-crossorigin-fix-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-crossorigin-fix-plugin.d.ts","sourceRoot":"","sources":["../src/vite-crossorigin-fix-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AASpC,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAuBhE;AA0BD,eAAO,MAAM,UAAU,QAAoB,CAAC;AAC5C,eAAO,MAAM,aAAa,QAAS,MAAM,KAAG,OAA+B,CAAC;AAE5E,eAAO,MAAM,SAAS,QAAe,CAAC;AACtC,eAAO,MAAM,SAAS,QAAS,MAAM,KAAG,OAA8B,CAAC;AAKvE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,YA2B5C;AAED,QAAA,MAAM,cAAc,QAAO,YAqC1B,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const replacer = (match) => match.replace(/crossorigin(?!\s*=\s*["'][^"']*["'])/gi, 'crossorigin="use-credentials"');
|
|
2
|
+
export function addUseCredentials(scriptSrc, html) {
|
|
3
|
+
const escapedScriptSrc = scriptSrc.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4
|
+
return (html
|
|
5
|
+
.replace(
|
|
6
|
+
// src attribute ending with scriptSrc
|
|
7
|
+
new RegExp(`<script(?:\\s+\\w+(?:=(?:"[^"]*"|'[^']*'))?)*\\s+src=(?:"[^"]*${escapedScriptSrc}"|'[^']*')(?:\\s+\\w+(?:=(?:"[^"]*"|'[^']*'))?)*\\s*>`, "gi"), replacer)
|
|
8
|
+
// the same for <link rel="modulepreload" crossorigin href=""> tags
|
|
9
|
+
.replace(
|
|
10
|
+
// href attribute ending with scriptSrc
|
|
11
|
+
new RegExp(`<link(?:\\s+\\w+(?:=(?:"[^"]*"|'[^']*'))?)*\\s+rel=(?:"modulepreload"|'modulepreload')(?:\\s+\\w+(?:=(?:"[^"]*"|'[^']*'))?)*\\s+href=(?:"[^"]*${escapedScriptSrc}"|'[^']*')(?:\\s+\\w+(?:=(?:"[^"]*"|'[^']*'))?)*\\s*>`, "gi"), replacer));
|
|
12
|
+
}
|
|
13
|
+
function processScript(bundle, scriptSrc, html, seen = new Set()) {
|
|
14
|
+
seen.add(scriptSrc);
|
|
15
|
+
const script = bundle[scriptSrc];
|
|
16
|
+
if (!script || script.type !== "chunk") {
|
|
17
|
+
return html;
|
|
18
|
+
}
|
|
19
|
+
let newHtml = addUseCredentials(script.fileName, html);
|
|
20
|
+
script.imports.forEach(importedScript => {
|
|
21
|
+
if (!seen.has(importedScript)) {
|
|
22
|
+
newHtml = processScript(bundle, importedScript, newHtml, seen);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return newHtml;
|
|
26
|
+
}
|
|
27
|
+
export const externalRE = /^(https?:)?\/\//;
|
|
28
|
+
export const isExternalUrl = (url) => externalRE.test(url);
|
|
29
|
+
export const dataUrlRE = /^\s*data:/i;
|
|
30
|
+
export const isDataUrl = (url) => dataUrlRE.test(url);
|
|
31
|
+
const isExcludedUrl = (url) => url[0] === "#" || isExternalUrl(url) || isDataUrl(url);
|
|
32
|
+
export function checkCrossOrigin(html) {
|
|
33
|
+
// before the html is transformed, we need to check if any of the
|
|
34
|
+
// entrypoint's scripts that will be bundled have crossorigin="use-credentials"
|
|
35
|
+
// search for script tags with crossorigin attribute set to use-credentials
|
|
36
|
+
const scriptTagsWithCors = html.match(/<script(?:\s+\w+(?:=(?:"[^"]*"|'[^']*'))?)*\s+crossorigin=(?:"use-credentials"|'use-credentials')(?:\s+\w+(?:=(?:"[^"]*"|'[^']*'))?)*\s*>/gi);
|
|
37
|
+
// ignore the script tags that are not modules
|
|
38
|
+
const modulesWithCors = scriptTagsWithCors?.filter(script => /type\s*=\s*(?:"module"|'module')/i.test(script));
|
|
39
|
+
const withCredentials = [];
|
|
40
|
+
if (modulesWithCors != null && modulesWithCors.length !== 0) {
|
|
41
|
+
for (let i = 0; i !== modulesWithCors.length; i += 1) {
|
|
42
|
+
const src = modulesWithCors[i].match(/src="([^"]*)"|src='([^']*)'/i);
|
|
43
|
+
// only take into consideration valid non-external and non-data urls
|
|
44
|
+
if (src != null && !isExcludedUrl(src[1] || src[2])) {
|
|
45
|
+
withCredentials.push(src[1] || src[2]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return withCredentials;
|
|
50
|
+
}
|
|
51
|
+
const fixCrossOrigin = () => {
|
|
52
|
+
const withCredentials = {};
|
|
53
|
+
return [
|
|
54
|
+
{
|
|
55
|
+
name: "vite-crossorigin-fix-collect-info-plugin",
|
|
56
|
+
transformIndexHtml: {
|
|
57
|
+
order: "pre",
|
|
58
|
+
handler(html, info) {
|
|
59
|
+
withCredentials[info.filename] = checkCrossOrigin(html);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: "vite-crossorigin-fix-replace-plugin",
|
|
65
|
+
transformIndexHtml: {
|
|
66
|
+
order: "post",
|
|
67
|
+
handler(html, context) {
|
|
68
|
+
if (context.bundle == null || context.chunk == null) {
|
|
69
|
+
return html;
|
|
70
|
+
}
|
|
71
|
+
// it there was at least one script with crossorigin="use-credentials" in the entrypoint
|
|
72
|
+
// we need to add crossorigin="use-credentials" to all the script tags of exported chunks
|
|
73
|
+
const toAddUseCredentials = withCredentials[context.filename];
|
|
74
|
+
if (toAddUseCredentials != null && toAddUseCredentials.length !== 0) {
|
|
75
|
+
return processScript(context.bundle, context.chunk.fileName, html);
|
|
76
|
+
}
|
|
77
|
+
return html;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
];
|
|
82
|
+
};
|
|
83
|
+
export default fixCrossOrigin;
|
|
84
|
+
//# sourceMappingURL=vite-crossorigin-fix-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-crossorigin-fix-plugin.js","sourceRoot":"","sources":["../src/vite-crossorigin-fix-plugin.ts"],"names":[],"mappings":"AAGA,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAE,EAAE,CACjC,KAAK,CAAC,OAAO,CACX,wCAAwC,EACxC,+BAA+B,CAChC,CAAC;AAEJ,MAAM,UAAU,iBAAiB,CAAC,SAAiB,EAAE,IAAY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,eAAe,GAA6B,EAAE,CAAC;IAErD,OAAO;QACL;YACE,IAAI,EAAE,0CAA0C;YAEhD,kBAAkB,EAAE;gBAClB,KAAK,EAAE,KAAK;gBACZ,OAAO,CAAC,IAAI,EAAE,IAAI;oBAChB,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;gBAC1D,CAAC;aACF;SACF;QACD;YACE,IAAI,EAAE,qCAAqC;YAE3C,kBAAkB,EAAE;gBAClB,KAAK,EAAE,MAAM;gBACb,OAAO,CAAC,IAAI,EAAE,OAAO;oBACnB,IAAI,OAAO,CAAC,MAAM,IAAI,IAAI,IAAI,OAAO,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;wBACpD,OAAO,IAAI,CAAC;oBACd,CAAC;oBAED,wFAAwF;oBACxF,yFAAyF;oBAEzF,MAAM,mBAAmB,GAAG,eAAe,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;oBAC9D,IAAI,mBAAmB,IAAI,IAAI,IAAI,mBAAmB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;wBACpE,OAAO,aAAa,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;oBACrE,CAAC;oBAED,OAAO,IAAI,CAAC;gBACd,CAAC;aACF;SACF;KACF,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC","sourcesContent":["import { PluginOption } from \"vite\";\nimport { OutputBundle } from \"rollup\";\n\nconst replacer = (match: string) =>\n match.replace(\n /crossorigin(?!\\s*=\\s*[\"'][^\"']*[\"'])/gi,\n 'crossorigin=\"use-credentials\"'\n );\n\nexport function addUseCredentials(scriptSrc: string, html: string) {\n const escapedScriptSrc = scriptSrc.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\n return (\n html\n .replace(\n // src attribute ending with scriptSrc\n new RegExp(\n `<script(?:\\\\s+\\\\w+(?:=(?:\"[^\"]*\"|'[^']*'))?)*\\\\s+src=(?:\"[^\"]*${escapedScriptSrc}\"|'[^']*')(?:\\\\s+\\\\w+(?:=(?:\"[^\"]*\"|'[^']*'))?)*\\\\s*>`,\n \"gi\"\n ),\n replacer\n )\n // the same for <link rel=\"modulepreload\" crossorigin href=\"\"> tags\n .replace(\n // href attribute ending with scriptSrc\n new RegExp(\n `<link(?:\\\\s+\\\\w+(?:=(?:\"[^\"]*\"|'[^']*'))?)*\\\\s+rel=(?:\"modulepreload\"|'modulepreload')(?:\\\\s+\\\\w+(?:=(?:\"[^\"]*\"|'[^']*'))?)*\\\\s+href=(?:\"[^\"]*${escapedScriptSrc}\"|'[^']*')(?:\\\\s+\\\\w+(?:=(?:\"[^\"]*\"|'[^']*'))?)*\\\\s*>`,\n \"gi\"\n ),\n replacer\n )\n );\n}\n\nfunction processScript(\n bundle: OutputBundle,\n scriptSrc: string,\n html: string,\n seen: Set<string> = new Set()\n) {\n seen.add(scriptSrc);\n\n const script = bundle[scriptSrc];\n if (!script || script.type !== \"chunk\") {\n return html;\n }\n\n let newHtml = addUseCredentials(script.fileName, html);\n\n script.imports.forEach(importedScript => {\n if (!seen.has(importedScript)) {\n newHtml = processScript(bundle, importedScript, newHtml, seen);\n }\n });\n\n return newHtml;\n}\n\nexport const externalRE = /^(https?:)?\\/\\//;\nexport const isExternalUrl = (url: string): boolean => externalRE.test(url);\n\nexport const dataUrlRE = /^\\s*data:/i;\nexport const isDataUrl = (url: string): boolean => dataUrlRE.test(url);\n\nconst isExcludedUrl = (url: string) =>\n url[0] === \"#\" || isExternalUrl(url) || isDataUrl(url);\n\nexport function checkCrossOrigin(html: string) {\n // before the html is transformed, we need to check if any of the\n // entrypoint's scripts that will be bundled have crossorigin=\"use-credentials\"\n\n // search for script tags with crossorigin attribute set to use-credentials\n const scriptTagsWithCors = html.match(\n /<script(?:\\s+\\w+(?:=(?:\"[^\"]*\"|'[^']*'))?)*\\s+crossorigin=(?:\"use-credentials\"|'use-credentials')(?:\\s+\\w+(?:=(?:\"[^\"]*\"|'[^']*'))?)*\\s*>/gi\n );\n\n // ignore the script tags that are not modules\n const modulesWithCors = scriptTagsWithCors?.filter(script =>\n /type\\s*=\\s*(?:\"module\"|'module')/i.test(script)\n );\n\n const withCredentials = [];\n if (modulesWithCors != null && modulesWithCors.length !== 0) {\n for (let i = 0; i !== modulesWithCors.length; i += 1) {\n const src = modulesWithCors[i].match(/src=\"([^\"]*)\"|src='([^']*)'/i);\n\n // only take into consideration valid non-external and non-data urls\n if (src != null && !isExcludedUrl(src[1] || src[2])) {\n withCredentials.push(src[1] || src[2]);\n }\n }\n }\n\n return withCredentials;\n}\n\nconst fixCrossOrigin = (): PluginOption => {\n const withCredentials: Record<string, string[]> = {};\n\n return [\n {\n name: \"vite-crossorigin-fix-collect-info-plugin\",\n\n transformIndexHtml: {\n order: \"pre\",\n handler(html, info) {\n withCredentials[info.filename] = checkCrossOrigin(html);\n }\n }\n },\n {\n name: \"vite-crossorigin-fix-replace-plugin\",\n\n transformIndexHtml: {\n order: \"post\",\n handler(html, context) {\n if (context.bundle == null || context.chunk == null) {\n return html;\n }\n\n // it there was at least one script with crossorigin=\"use-credentials\" in the entrypoint\n // we need to add crossorigin=\"use-credentials\" to all the script tags of exported chunks\n\n const toAddUseCredentials = withCredentials[context.filename];\n if (toAddUseCredentials != null && toAddUseCredentials.length !== 0) {\n return processScript(context.bundle, context.chunk.fileName, html);\n }\n\n return html;\n }\n }\n }\n ];\n};\n\nexport default fixCrossOrigin;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite-plugin.d.ts","sourceRoot":"","sources":["../src/vite-plugin.ts"],"names":[],"mappings":"AAMA,OAAO,EAAW,YAAY,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"vite-plugin.d.ts","sourceRoot":"","sources":["../src/vite-plugin.ts"],"names":[],"mappings":"AAMA,OAAO,EAAW,YAAY,EAAE,MAAM,MAAM,CAAC;AAc7C,OAAO,EACL,iBAAiB,EAIlB,MAAM,mBAAmB,CAAC;AAc3B,MAAM,MAAM,qBAAqB,GAAG,KAAK,GAAG,QAAQ,CAAC;AAErD,MAAM,WAAW,yBAAyB;IACxC;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,qBAAqB,CAAC;IAE7B;;;;;;;;;;;OAWG;IACH,kBAAkB,CAAC,EAAE,iBAAiB,EAAE,CAAC;IAEzC;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;;;;;;OAQG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;;;;;;;OAWG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED;;;;GAIG;AACH,iBAAS,kBAAkB,CACzB,IAAI,GAAE,yBAA8B,EACpC,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GAC/B,YAAY,CAmLd;AAED,eAAe,kBAAkB,CAAC"}
|
package/dist/vite-plugin.js
CHANGED
|
@@ -9,6 +9,7 @@ import generateBaseTag from "./vite-generate-base-plugin.js";
|
|
|
9
9
|
import injectMetadata from "./vite-metadata-plugin.js";
|
|
10
10
|
import serveAppShellConfig from "./vite-watch-config-plugin.js";
|
|
11
11
|
import generateBashScript from "./vite-generate-bash-script-plugin.js";
|
|
12
|
+
import fixCrossOrigin from "./vite-crossorigin-fix-plugin.js";
|
|
12
13
|
import { findAppShellConfigFile, getFinalModuleName, loadConfigFile } from "./config-utils.js";
|
|
13
14
|
import { getModulePath, resolveModule } from "./nodeModule.js";
|
|
14
15
|
import { applyAutomaticMenu, applyAutomaticViewsAndRoutes } from "./automatic-utils.js";
|
|
@@ -117,6 +118,8 @@ function appShellVitePlugin(opts = {}, env = {}) {
|
|
|
117
118
|
generateBaseTag(appShellConfiguration, generateEmptyShell),
|
|
118
119
|
// configure the build process based on the config file
|
|
119
120
|
processConfiguration(root, appShellConfiguration, packageJson.name, buildEntryPoint, inlineConfig, generateEmptyShell, modules.concat(autoViewsBundles)),
|
|
121
|
+
// allow crossorigin="use-credentials" in the index.html
|
|
122
|
+
fixCrossOrigin(),
|
|
120
123
|
// serve the app shell config file as json and watch for changes
|
|
121
124
|
serveAppShellConfig(appShellConfiguration, root, packageJson.name, appShellConfigFile, autoViewsAndRoutes ? viewsFolder : undefined),
|
|
122
125
|
// generate the shell script to replace the placeholders in the index.html
|
package/dist/vite-plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite-plugin.js","sourceRoot":"","sources":["../src/vite-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,OAAO,MAAM,wBAAwB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD,OAAO,EAAE,OAAO,EAAgB,MAAM,MAAM,CAAC;AAI7C,OAAO,iBAAiB,EAAE,EACxB,iBAAiB,EAClB,MAAM,4BAA4B,CAAC;AACpC,OAAO,oBAAoB,MAAM,0CAA0C,CAAC;AAC5E,OAAO,eAAe,MAAM,gCAAgC,CAAC;AAC7D,OAAO,cAAc,MAAM,2BAA2B,CAAC;AACvD,OAAO,mBAAmB,MAAM,+BAA+B,CAAC;AAChE,OAAO,kBAAkB,MAAM,uCAAuC,CAAC;AAEvE,OAAO,EAEL,sBAAsB,EACtB,kBAAk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ugin-virtual\";\nimport { viteStaticCopy } from \"vite-plugin-static-copy\";\n\nimport { loadEnv, PluginOption } from \"vite\";\n\nimport type { HvAppShellConfig } from \"@hitachivantara/app-shell-shared\";\n\nimport generateImportmap, {\n extraDependencies\n} from \"./vite-importmap-plugin.js\";\nimport processConfiguration from \"./vite-configuration-processor-plugin.js\";\nimport generateBaseTag from \"./vite-generate-base-plugin.js\";\nimport injectMetadata from \"./vite-metadata-plugin.js\";\nimport serveAppShellConfig from \"./vite-watch-config-plugin.js\";\nimport generateBashScript from \"./vite-generate-bash-script-plugin.js\";\n\nimport {\n ConfigReplacement,\n findAppShellConfigFile,\n getFinalModuleName,\n loadConfigFile\n} from \"./config-utils.js\";\nimport { getModulePath, resolveModule } from \"./nodeModule.js\";\nimport {\n applyAutomaticMenu,\n applyAutomaticViewsAndRoutes\n} from \"./automatic-utils.js\";\nimport getVirtualEntrypoints from \"./virtual-entrypoints.js\";\nimport SHARED_DEPENDENCIES from \"./shared-dependencies.js\";\n\nenum ViteBuildMode {\n PRODUCTION = \"production\",\n DEVELOPMENT = \"development\"\n}\n\nexport type ApplicationBundleType = \"app\" | \"bundle\";\n\nexport interface AppShellVitePluginOptions {\n /**\n * Project root directory. Most likely the location of the vite config file.\n *\n * @default process.cwd()\n */\n root?: string;\n /**\n * Execution mode.\n */\n mode?: string;\n\n /**\n * Type of application bundle being built. Can be \"app\" or \"bundle\".\n *\n * - \"app\": The application bundle includes both the index.html entry point and the exported modules.\n * - \"bundle\": The application bundle will not include the index.html entry point.\n *\n * @default \"app\"\n */\n type?: ApplicationBundleType;\n\n /** Array of tokens that are replaced at app-shell.config.json during the build of the solution.\n * e.g.\n * {\n * token: \"USER_NOTIFICATIONS_URL\",\n * value: \"http://localhost:8080\"\n * }\n * Tokens used at config file must be wrapped (at the beginning and at the end) by @@ sequence\n * e.g.\n * {\n * \"@hv/user-notifications-client\": \"@@USER_NOTIFICATIONS_URL@@\"\n * }\n */\n configReplacements?: ConfigReplacement[];\n\n /**\n * The folder containing Views to be shared as Shared Modules. Defaults to \"src/pages\".\n *\n * The folder path must be relative to the project root (e.g. \"src/pages\").\n */\n viewsFolder?: string;\n /**\n * If set, the plugin will search for Views at the folder specified by `viewsFolder` and will add them to the App Shell configuration as views.\n * The views' modules will be exported accordingly, and a route will be created from the folder structure.\n * Dynamic route parameters are supported by prefixing the folder name with a $ (e.g. \"src/pages/List/$id/index.tsx\" will be configured as \"/list/:id\").\n */\n autoViewsAndRoutes?: boolean;\n /**\n * If true, the plugin will try to automatically add the views to the menu.\n * Any menu defined in the App Shell config file will be overwritten.\n */\n autoMenu?: boolean;\n\n /**\n * If true, the plugin will generate the importmap with an external js file instead of inline in the html.\n * The map will be saved at the root of the application destination dir and named as \"importmap.js\".\n *\n * This option is not for general use. It is only intended to be used for easing the automated testing of the App Shell.\n *\n * @default false\n * @private\n */\n externalImportMap?: boolean;\n /**\n * If true, the plugin will inline the app-shell.config.json file in a script tag of the index html.\n *\n * This option is not for general use. Its value will be automatically managed by the App Shell build process.\n *\n * @default false, true if generateEmptyShell is true\n * @private\n */\n inlineConfig?: boolean;\n /**\n * If true, the config file is ignored, only the App Shell is built and the generated index.html will contain a placeholders\n * for importmap (if externalImportMap is false), app shell config (if inlineConfig is true) and title.\n *\n * A bash script will also be added to the dist folder to replace the placeholders with the actual content,\n * when provided with a concrete configuration.\n *\n * This option is not for general use. It is used for generating the App Shell container image.\n *\n * @default false\n * @private\n */\n generateEmptyShell?: boolean;\n /**\n * The modules to be exported by the application.\n *\n * All the modules that the application need to export should be declared here in order to have them built properly.\n *\n */\n modules?: string[];\n}\n\n/**\n * Vite plugin to support App Shell apps setup\n * @param opts Plugin options\n * @param env Environment variable\n */\nfunction appShellVitePlugin(\n opts: AppShellVitePluginOptions = {},\n env: Record<string, string> = {}\n): PluginOption {\n const {\n root = process.cwd(),\n mode = ViteBuildMode.PRODUCTION,\n externalImportMap = false,\n viewsFolder = \"src/pages\",\n autoViewsAndRoutes = false,\n autoMenu = false,\n inlineConfig = opts.generateEmptyShell ?? false,\n generateEmptyShell = false,\n modules = []\n } = opts;\n\n const globalEnv = loadEnv(mode, process.cwd(), \"\");\n\n const { type = globalEnv.CI ? \"bundle\" : \"app\" } = opts;\n\n console.info(`Vite running in mode: ${mode}`);\n console.info(`AppShell Vite plugin running with type: ${type}`);\n\n const devMode = mode === ViteBuildMode.DEVELOPMENT;\n const buildEntryPoint = type !== \"bundle\";\n\n const packageJsonRaw = fs.readFileSync(\n path.resolve(root, \"package.json\"),\n \"utf-8\"\n );\n const packageJson = JSON.parse(packageJsonRaw);\n\n const appShellConfigFile = !generateEmptyShell\n ? findAppShellConfigFile(root)\n : undefined;\n const appShellConfiguration: HvAppShellConfig = loadConfigFile(\n appShellConfigFile,\n opts,\n env\n );\n\n let autoViewsBundles: string[] = [];\n\n if (!generateEmptyShell) {\n if (autoViewsAndRoutes) {\n autoViewsBundles = applyAutomaticViewsAndRoutes(\n appShellConfiguration,\n packageJson.name,\n root,\n viewsFolder\n );\n }\n\n if (autoMenu) {\n applyAutomaticMenu(appShellConfiguration);\n }\n }\n\n return [\n // copy the shared dependencies js bundles to the \"bundles\" folder\n (buildEntryPoint || devMode) &&\n viteStaticCopy({\n targets: [\n {\n src: getModulePath(\"es-module-shims\", \"dist/*\"),\n dest: \"bundles\"\n },\n // copy the ui kit icons' sprites to the \"icons\" folder\n {\n src: [\n getModulePath(\n \"@hitachivantara/uikit-react-icons\",\n \"dist/sprites/*.svg\"\n )\n ],\n dest: \"icons\"\n },\n ...(!devMode && buildEntryPoint\n ? [\n {\n src: SHARED_DEPENDENCIES.flatMap(dep => {\n const module = resolveModule(dep.bundleSrc);\n try {\n return [module, resolveModule(`${dep.bundleSrc}.map`)];\n } catch {\n return [module];\n }\n }),\n dest: \"bundles\"\n }\n ]\n : [])\n ]\n }),\n\n // create virtual endpoints for shell code and for shared dependencies\n virtual({\n ...getVirtualEntrypoints(inlineConfig),\n\n ...SHARED_DEPENDENCIES.reduce(\n (acc, dep) => {\n acc[`/bundles/${dep.bundle}`] = dep.virtualSrc;\n return acc;\n },\n {} as Record<string, string>\n )\n }),\n\n // generate the importmap for shared dependencies and for apps referenced in the config file\n generateImportmap(\n {\n ...SHARED_DEPENDENCIES.reduce(\n (acc, dep) => {\n acc[dep.moduleId] = `./bundles/${dep.bundle}`;\n return acc;\n },\n {} as Record<string, string>\n ),\n\n ...extraDependencies,\n\n ...Object.entries(appShellConfiguration?.apps ?? {}).reduce(\n (acc, [key, value]) => {\n acc[`${key}/`] = value;\n return acc;\n },\n {} as Record<string, string>\n ),\n\n [`${packageJson.name}/`]: appShellConfiguration.baseUrl ?? \"/\",\n\n ...(devMode\n ? modules.concat(autoViewsBundles).reduce(\n (acc, module) => {\n const finalModule = getFinalModuleName(module);\n acc[`${packageJson.name}/${finalModule}.js`] =\n (appShellConfiguration.baseUrl ?? \"/\") + module;\n return acc;\n },\n {} as Record<string, string>\n )\n : {})\n },\n packageJson.name,\n [...SHARED_DEPENDENCIES.map(dep => dep.moduleId), packageJson.name],\n externalImportMap && buildEntryPoint,\n generateEmptyShell\n ),\n\n // inject version metadata in the index.html\n buildEntryPoint && injectMetadata(),\n\n // set the base tag and replace the title in the index.html\n buildEntryPoint &&\n generateBaseTag(appShellConfiguration, generateEmptyShell),\n\n // configure the build process based on the config file\n processConfiguration(\n root,\n appShellConfiguration,\n packageJson.name,\n buildEntryPoint,\n inlineConfig,\n generateEmptyShell,\n modules.concat(autoViewsBundles)\n ),\n\n // serve the app shell config file as json and watch for changes\n serveAppShellConfig(\n appShellConfiguration,\n root,\n packageJson.name,\n appShellConfigFile,\n autoViewsAndRoutes ? viewsFolder : undefined\n ),\n\n // generate the shell script to replace the placeholders in the index.html\n generateEmptyShell && generateBashScript(externalImportMap, inlineConfig)\n ];\n}\n\nexport default appShellVitePlugin;\n"]}
|
|
1
|
+
{"version":3,"file":"vite-plugin.js","sourceRoot":"","sources":["../src/vite-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,I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B,EAAE,kBAAkB,CAAC;QAE5D,uDAAuD;QACvD,oBAAoB,CAClB,IAAI,EACJ,qBAAqB,EACrB,WAAW,CAAC,IAAI,EAChB,eAAe,EACf,YAAY,EACZ,kBAAkB,EAClB,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC,CACjC;QAED,wDAAwD;QACxD,cAAc,EAAE;QAEhB,gEAAgE;QAChE,mBAAmB,CACjB,qBAAqB,EACrB,IAAI,EACJ,WAAW,CAAC,IAAI,EAChB,kBAAkB,EAClB,kBAAkB,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAC7C;QAED,0EAA0E;QAC1E,kBAAkB,IAAI,kBAAkB,CAAC,iBAAiB,EAAE,YAAY,CAAC;KAC1E,CAAC;AACJ,CAAC;AAED,eAAe,kBAAkB,CAAC","sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\n\nimport virtual from \"@rollup/plugin-virtual\";\nimport { viteStaticCopy } from \"vite-plugin-static-copy\";\n\nimport { loadEnv, PluginOption } from \"vite\";\n\nimport type { HvAppShellConfig } from \"@hitachivantara/app-shell-shared\";\n\nimport generateImportmap, {\n extraDependencies\n} from \"./vite-importmap-plugin.js\";\nimport processConfiguration from \"./vite-configuration-processor-plugin.js\";\nimport generateBaseTag from \"./vite-generate-base-plugin.js\";\nimport injectMetadata from \"./vite-metadata-plugin.js\";\nimport serveAppShellConfig from \"./vite-watch-config-plugin.js\";\nimport generateBashScript from \"./vite-generate-bash-script-plugin.js\";\nimport fixCrossOrigin from \"./vite-crossorigin-fix-plugin.js\";\n\nimport {\n ConfigReplacement,\n findAppShellConfigFile,\n getFinalModuleName,\n loadConfigFile\n} from \"./config-utils.js\";\nimport { getModulePath, resolveModule } from \"./nodeModule.js\";\nimport {\n applyAutomaticMenu,\n applyAutomaticViewsAndRoutes\n} from \"./automatic-utils.js\";\nimport getVirtualEntrypoints from \"./virtual-entrypoints.js\";\nimport SHARED_DEPENDENCIES from \"./shared-dependencies.js\";\n\nenum ViteBuildMode {\n PRODUCTION = \"production\",\n DEVELOPMENT = \"development\"\n}\n\nexport type ApplicationBundleType = \"app\" | \"bundle\";\n\nexport interface AppShellVitePluginOptions {\n /**\n * Project root directory. Most likely the location of the vite config file.\n *\n * @default process.cwd()\n */\n root?: string;\n /**\n * Execution mode.\n */\n mode?: string;\n\n /**\n * Type of application bundle being built. Can be \"app\" or \"bundle\".\n *\n * - \"app\": The application bundle includes both the index.html entry point and the exported modules.\n * - \"bundle\": The application bundle will not include the index.html entry point.\n *\n * @default \"app\"\n */\n type?: ApplicationBundleType;\n\n /** Array of tokens that are replaced at app-shell.config.json during the build of the solution.\n * e.g.\n * {\n * token: \"USER_NOTIFICATIONS_URL\",\n * value: \"http://localhost:8080\"\n * }\n * Tokens used at config file must be wrapped (at the beginning and at the end) by @@ sequence\n * e.g.\n * {\n * \"@hv/user-notifications-client\": \"@@USER_NOTIFICATIONS_URL@@\"\n * }\n */\n configReplacements?: ConfigReplacement[];\n\n /**\n * The folder containing Views to be shared as Shared Modules. Defaults to \"src/pages\".\n *\n * The folder path must be relative to the project root (e.g. \"src/pages\").\n */\n viewsFolder?: string;\n /**\n * If set, the plugin will search for Views at the folder specified by `viewsFolder` and will add them to the App Shell configuration as views.\n * The views' modules will be exported accordingly, and a route will be created from the folder structure.\n * Dynamic route parameters are supported by prefixing the folder name with a $ (e.g. \"src/pages/List/$id/index.tsx\" will be configured as \"/list/:id\").\n */\n autoViewsAndRoutes?: boolean;\n /**\n * If true, the plugin will try to automatically add the views to the menu.\n * Any menu defined in the App Shell config file will be overwritten.\n */\n autoMenu?: boolean;\n\n /**\n * If true, the plugin will generate the importmap with an external js file instead of inline in the html.\n * The map will be saved at the root of the application destination dir and named as \"importmap.js\".\n *\n * This option is not for general use. It is only intended to be used for easing the automated testing of the App Shell.\n *\n * @default false\n * @private\n */\n externalImportMap?: boolean;\n /**\n * If true, the plugin will inline the app-shell.config.json file in a script tag of the index html.\n *\n * This option is not for general use. Its value will be automatically managed by the App Shell build process.\n *\n * @default false, true if generateEmptyShell is true\n * @private\n */\n inlineConfig?: boolean;\n /**\n * If true, the config file is ignored, only the App Shell is built and the generated index.html will contain a placeholders\n * for importmap (if externalImportMap is false), app shell config (if inlineConfig is true) and title.\n *\n * A bash script will also be added to the dist folder to replace the placeholders with the actual content,\n * when provided with a concrete configuration.\n *\n * This option is not for general use. It is used for generating the App Shell container image.\n *\n * @default false\n * @private\n */\n generateEmptyShell?: boolean;\n /**\n * The modules to be exported by the application.\n *\n * All the modules that the application need to export should be declared here in order to have them built properly.\n *\n */\n modules?: string[];\n}\n\n/**\n * Vite plugin to support App Shell apps setup\n * @param opts Plugin options\n * @param env Environment variable\n */\nfunction appShellVitePlugin(\n opts: AppShellVitePluginOptions = {},\n env: Record<string, string> = {}\n): PluginOption {\n const {\n root = process.cwd(),\n mode = ViteBuildMode.PRODUCTION,\n externalImportMap = false,\n viewsFolder = \"src/pages\",\n autoViewsAndRoutes = false,\n autoMenu = false,\n inlineConfig = opts.generateEmptyShell ?? false,\n generateEmptyShell = false,\n modules = []\n } = opts;\n\n const globalEnv = loadEnv(mode, process.cwd(), \"\");\n\n const { type = globalEnv.CI ? \"bundle\" : \"app\" } = opts;\n\n console.info(`Vite running in mode: ${mode}`);\n console.info(`AppShell Vite plugin running with type: ${type}`);\n\n const devMode = mode === ViteBuildMode.DEVELOPMENT;\n const buildEntryPoint = type !== \"bundle\";\n\n const packageJsonRaw = fs.readFileSync(\n path.resolve(root, \"package.json\"),\n \"utf-8\"\n );\n const packageJson = JSON.parse(packageJsonRaw);\n\n const appShellConfigFile = !generateEmptyShell\n ? findAppShellConfigFile(root)\n : undefined;\n const appShellConfiguration: HvAppShellConfig = loadConfigFile(\n appShellConfigFile,\n opts,\n env\n );\n\n let autoViewsBundles: string[] = [];\n\n if (!generateEmptyShell) {\n if (autoViewsAndRoutes) {\n autoViewsBundles = applyAutomaticViewsAndRoutes(\n appShellConfiguration,\n packageJson.name,\n root,\n viewsFolder\n );\n }\n\n if (autoMenu) {\n applyAutomaticMenu(appShellConfiguration);\n }\n }\n\n return [\n // copy the shared dependencies js bundles to the \"bundles\" folder\n (buildEntryPoint || devMode) &&\n viteStaticCopy({\n targets: [\n {\n src: getModulePath(\"es-module-shims\", \"dist/*\"),\n dest: \"bundles\"\n },\n // copy the ui kit icons' sprites to the \"icons\" folder\n {\n src: [\n getModulePath(\n \"@hitachivantara/uikit-react-icons\",\n \"dist/sprites/*.svg\"\n )\n ],\n dest: \"icons\"\n },\n ...(!devMode && buildEntryPoint\n ? [\n {\n src: SHARED_DEPENDENCIES.flatMap(dep => {\n const module = resolveModule(dep.bundleSrc);\n try {\n return [module, resolveModule(`${dep.bundleSrc}.map`)];\n } catch {\n return [module];\n }\n }),\n dest: \"bundles\"\n }\n ]\n : [])\n ]\n }),\n\n // create virtual endpoints for shell code and for shared dependencies\n virtual({\n ...getVirtualEntrypoints(inlineConfig),\n\n ...SHARED_DEPENDENCIES.reduce(\n (acc, dep) => {\n acc[`/bundles/${dep.bundle}`] = dep.virtualSrc;\n return acc;\n },\n {} as Record<string, string>\n )\n }),\n\n // generate the importmap for shared dependencies and for apps referenced in the config file\n generateImportmap(\n {\n ...SHARED_DEPENDENCIES.reduce(\n (acc, dep) => {\n acc[dep.moduleId] = `./bundles/${dep.bundle}`;\n return acc;\n },\n {} as Record<string, string>\n ),\n\n ...extraDependencies,\n\n ...Object.entries(appShellConfiguration?.apps ?? {}).reduce(\n (acc, [key, value]) => {\n acc[`${key}/`] = value;\n return acc;\n },\n {} as Record<string, string>\n ),\n\n [`${packageJson.name}/`]: appShellConfiguration.baseUrl ?? \"/\",\n\n ...(devMode\n ? modules.concat(autoViewsBundles).reduce(\n (acc, module) => {\n const finalModule = getFinalModuleName(module);\n acc[`${packageJson.name}/${finalModule}.js`] =\n (appShellConfiguration.baseUrl ?? \"/\") + module;\n return acc;\n },\n {} as Record<string, string>\n )\n : {})\n },\n packageJson.name,\n [...SHARED_DEPENDENCIES.map(dep => dep.moduleId), packageJson.name],\n externalImportMap && buildEntryPoint,\n generateEmptyShell\n ),\n\n // inject version metadata in the index.html\n buildEntryPoint && injectMetadata(),\n\n // set the base tag and replace the title in the index.html\n buildEntryPoint &&\n generateBaseTag(appShellConfiguration, generateEmptyShell),\n\n // configure the build process based on the config file\n processConfiguration(\n root,\n appShellConfiguration,\n packageJson.name,\n buildEntryPoint,\n inlineConfig,\n generateEmptyShell,\n modules.concat(autoViewsBundles)\n ),\n\n // allow crossorigin=\"use-credentials\" in the index.html\n fixCrossOrigin(),\n\n // serve the app shell config file as json and watch for changes\n serveAppShellConfig(\n appShellConfiguration,\n root,\n packageJson.name,\n appShellConfigFile,\n autoViewsAndRoutes ? viewsFolder : undefined\n ),\n\n // generate the shell script to replace the placeholders in the index.html\n generateEmptyShell && generateBashScript(externalImportMap, inlineConfig)\n ];\n}\n\nexport default appShellVitePlugin;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hitachivantara/app-shell-vite-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "AppShell Vite Plugin",
|
|
5
5
|
"author": "Hitachi Vantara - Boba Fett Team",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"vite": "^4.1.4 || ^5.0.4"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "2d6356b1837352e42f019b4e8fcac9131dea52e1"
|
|
68
68
|
}
|