|
1 |
| -var AssetGraph = require('assetgraph'); |
| 1 | +'use strict'; |
2 | 2 |
|
3 |
| -var headers = [ |
4 |
| - 'Content-Security-Policy' |
| 3 | +const AssetGraph = require('assetgraph'); |
| 4 | + |
| 5 | +const headers = [ |
| 6 | + 'Content-Security-Policy' |
5 | 7 | ];
|
6 | 8 |
|
7 |
| -var resourceHintTypeMap = { |
8 |
| - HtmlPreloadLink: 'preload', |
9 |
| - HtmlPrefetchLink: 'prefetch', |
10 |
| - HtmlPreconnectLink: 'preconnect', |
11 |
| - HtmlDnsPrefetchLink: 'dns-prefetch' |
| 9 | +const resourceHintTypeMap = { |
| 10 | + HtmlPreloadLink: 'preload', |
| 11 | + HtmlPrefetchLink: 'prefetch', |
| 12 | + HtmlPreconnectLink: 'preconnect', |
| 13 | + HtmlDnsPrefetchLink: 'dns-prefetch' |
12 | 14 | };
|
13 | 15 |
|
14 | 16 | function getHeaderForRelation (rel) {
|
15 |
| - let header = `Link: <${rel.href}>; rel=${resourceHintTypeMap[rel.type]}; as=${rel.as}; type=${rel.to.contentType}`; |
| 17 | + let header = `Link: <${rel.href}>; rel=${resourceHintTypeMap[rel.type]}; as=${rel.as}; type=${rel.to.contentType}`; |
16 | 18 |
|
17 |
| - if (rel.as === 'font') { |
18 |
| - header = `${header}; crossorigin=anonymous`; |
19 |
| - } |
| 19 | + if (rel.as === 'font') { |
| 20 | + header = `${header}; crossorigin=anonymous`; |
| 21 | + } |
20 | 22 |
|
21 |
| - return header; |
| 23 | + return header; |
22 | 24 | }
|
23 | 25 |
|
24 | 26 | new AssetGraph({ root: 'docs/_dist' })
|
25 |
| - .loadAssets('*.html') |
26 |
| - .populate({ |
27 |
| - followRelations: { type: 'HtmlAnchor', crossorigin: false } |
28 |
| - }) |
29 |
| - .queue(function (assetGraph) { |
30 |
| - var assets = assetGraph.findAssets({ type: 'Html', isInline: false }); |
31 |
| - |
32 |
| - var headerMap = {}; |
33 |
| - |
34 |
| - assets.forEach(function (asset) { |
35 |
| - var url = '/' + asset.url.replace(assetGraph.root, '').replace(/#.*/, '').replace('index.html', ''); |
36 |
| - if (!headerMap[url]) { |
37 |
| - headerMap[url] = []; |
38 |
| - } |
| 27 | + .loadAssets('*.html') |
| 28 | + .populate({ |
| 29 | + followRelations: { type: 'HtmlAnchor', crossorigin: false } |
| 30 | + }) |
| 31 | + .queue(function (assetGraph) { |
| 32 | + const assets = assetGraph.findAssets({ type: 'Html', isInline: false }); |
39 | 33 |
|
40 |
| - headers.forEach(function (header) { |
41 |
| - var node = asset.parseTree.querySelector('meta[http-equiv=' + header + ']'); |
| 34 | + const headerMap = {}; |
42 | 35 |
|
43 |
| - if (node) { |
44 |
| - headerMap[url].push(`${header}: ${node.getAttribute('content')}`) |
| 36 | + assets.forEach(function (asset) { |
| 37 | + const url = '/' + asset.url.replace(assetGraph.root, '').replace(/#.*/, '').replace('index.html', ''); |
| 38 | + if (!headerMap[url]) { |
| 39 | + headerMap[url] = []; |
| 40 | + } |
45 | 41 |
|
46 |
| - node.parentNode.removeChild(node); |
47 |
| - asset.markDirty(); |
48 |
| - } |
49 |
| - }); |
| 42 | + headers.forEach(function (header) { |
| 43 | + const node = asset.parseTree.querySelector('meta[http-equiv=' + header + ']'); |
50 | 44 |
|
51 |
| - var firstCssRel = asset.outgoingRelations.filter(r => { |
52 |
| - return r.type === 'HtmlStyle' |
53 |
| - && r.crossorigin === false |
54 |
| - && r.href !== undefined; |
55 |
| - })[0]; |
| 45 | + if (node) { |
| 46 | + headerMap[url].push(`${header}: ${node.getAttribute('content')}`); |
56 | 47 |
|
57 |
| - if (firstCssRel) { |
58 |
| - const header = `Link: <${firstCssRel.href}>; rel=preload; as=style`; |
| 48 | + node.parentNode.removeChild(node); |
| 49 | + asset.markDirty(); |
| 50 | + } |
| 51 | + }); |
59 | 52 |
|
| 53 | + const firstCssRel = asset.outgoingRelations.filter(r => { |
| 54 | + return r.type === 'HtmlStyle' && |
| 55 | + r.crossorigin === false && |
| 56 | + r.href !== undefined; |
| 57 | + })[0]; |
60 | 58 |
|
61 |
| - headerMap[url].push(header); |
62 |
| - } |
| 59 | + if (firstCssRel) { |
| 60 | + const header = `Link: <${firstCssRel.href}>; rel=preload; as=style`; |
63 | 61 |
|
64 |
| - var resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreloadLink', 'HtmlPrefetchLink'].includes(r.type)); |
| 62 | + headerMap[url].push(header); |
| 63 | + } |
65 | 64 |
|
66 |
| - resourceHintRelations.forEach(rel => { |
67 |
| - headerMap[url].push(getHeaderForRelation(rel)); |
| 65 | + const resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreloadLink', 'HtmlPrefetchLink'].includes(r.type)); |
68 | 66 |
|
69 |
| - rel.detach(); |
70 |
| - }); |
| 67 | + resourceHintRelations.forEach(rel => { |
| 68 | + headerMap[url].push(getHeaderForRelation(rel)); |
71 | 69 |
|
72 |
| - var resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreconnectLink'].includes(r.type)); |
| 70 | + rel.detach(); |
| 71 | + }); |
73 | 72 |
|
74 |
| - resourceHintRelations.forEach(rel => { |
75 |
| - let header = `Link: <${rel.href}>; rel=preconnect`; |
| 73 | + const preconnectRelations = asset.outgoingRelations.filter(r => ['HtmlPreconnectLink'].includes(r.type)); |
76 | 74 |
|
77 |
| - headerMap[url].push(header); |
| 75 | + preconnectRelations.forEach(rel => { |
| 76 | + let header = `Link: <${rel.href}>; rel=preconnect`; |
78 | 77 |
|
79 |
| - rel.detach(); |
80 |
| - }); |
81 |
| - }); |
| 78 | + headerMap[url].push(header); |
82 | 79 |
|
83 |
| - console.log('\n## Autogenerated headers:\n') |
| 80 | + rel.detach(); |
| 81 | + }); |
| 82 | + }); |
84 | 83 |
|
85 |
| - Object.keys(headerMap).forEach(function (url) { |
86 |
| - console.log(url); |
| 84 | + console.log('\n## Autogenerated headers:\n'); |
87 | 85 |
|
88 |
| - var httpHeaders = headerMap[url]; |
| 86 | + Object.keys(headerMap).forEach(function (url) { |
| 87 | + console.log(url); |
89 | 88 |
|
90 |
| - httpHeaders.forEach(function (header) { |
91 |
| - console.log(` ${header}`) |
92 |
| - }); |
| 89 | + const httpHeaders = headerMap[url]; |
93 | 90 |
|
94 |
| - console.log(''); |
95 |
| - }); |
| 91 | + httpHeaders.forEach(function (header) { |
| 92 | + console.log(` ${header}`); |
| 93 | + }); |
96 | 94 |
|
97 |
| - }) |
98 |
| - .writeAssetsToDisc({ isLoaded: true }) |
99 |
| - .run(); |
| 95 | + console.log(''); |
| 96 | + }); |
| 97 | + }) |
| 98 | + .writeAssetsToDisc({ isLoaded: true }) |
| 99 | + .run(); |
0 commit comments