-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget-asset-code.ts
77 lines (68 loc) · 1.99 KB
/
get-asset-code.ts
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
import * as path from "path";
import moduleName from "../shared/module-name";
import { VIRTUAL_SERVER_MANIFEST_PATH } from "../shared/virtual";
export default (
resourcePath: string,
runtimeId: string | undefined,
publicPath: string | undefined
): string => `
import template from ${JSON.stringify(`./${path.basename(resourcePath)}`)};
import manifest from ${JSON.stringify(
`./${path.relative(path.dirname(resourcePath), VIRTUAL_SERVER_MANIFEST_PATH)}`
)};
static function renderAssets() {
const assets = this.___assets;
const nonce = this.global.cspNonce;
this.___renderAssets = this.___assets = undefined;
this.flush = this.___flush;
this.end = this.___end;
if (assets) {
${
publicPath === undefined
? `__webpack_public_path__ && this.script(\`${
runtimeId ? `$mwp_${runtimeId}` : "$mwp"
}=\${JSON.stringify(__webpack_public_path__)}\`);`
: ""
}
if (assets.js) {
const nonceAttr = nonce ? \` nonce=\${JSON.stringify(nonce)}\` : "";
assets.js.forEach(js => {
this.write(
\`<script src=\${JSON.stringify(__webpack_public_path__+js)}\${nonceAttr} async></script>\`
);
});
}
if (assets.css) {
assets.css.forEach(css => {
this.write(
\`<link rel="stylesheet" href=\${JSON.stringify(__webpack_public_path__+css)}>\`
);
});
}
}
}
static function outFlushOverride() {
this.___renderAssets();
this.flush();
}
static function outEndOverride(data, encoding, callback) {
this.___renderAssets();
this.end(data, encoding, callback);
}
${
runtimeId === undefined
? ""
: `$ out.global.runtimeId = ${JSON.stringify(runtimeId)};`
}
$ out.___flush = out.flush;
$ out.___end = out.end;
$ out.___renderAssets = renderAssets;
$ out.___assets = manifest.getAssets(${JSON.stringify(
moduleName(resourcePath)
)}, out.global.buildName);
$ out.flush = outFlushOverride;
$ out.end = outEndOverride;
<\${template} ...input/>
<init-components/>
<await-reorderer/>
`;