-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
143 lines (124 loc) · 4.2 KB
/
index.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Generates Netlify HTTP2 Server Push headers and redirect rules for preact-cli
* A lot of this code is borrowed from https://github.com/developit/preact-cli/blob/master/src/lib/webpack/push-manifest.js
*/
class NetlifyServerPushPlugin {
constructor({ redirects, brotli = false, mutateManifest } = {}) {
this.redirects = [];
this.brotli = brotli;
this.mutateManifest_ = mutateManifest;
if (redirects !== undefined) {
if (Array.isArray(redirects)) {
this.redirects = redirects;
} else {
throw new TypeError(
`redirects should be an array, but was of type '${typeof redirects}'`
);
}
}
}
apply(compiler) {
const handler = (compilation, callback) => {
let manifest = compilation.assets['push-manifest.json'];
if (!manifest) {
// On pre-render build this is not present and thus need an early exit
callback();
return;
}
manifest = JSON.parse(manifest.source());
// This lets user adds additional resources and mutate some route to become dynamic.
if (this.mutateManifest_) {
manifest = this.mutateManifest_(manifest);
}
let headers =
'/*\n\tCache-Control: public, max-age=3600, no-cache\n\tAccess-Control-Max-Age: 600\n/sw.js\n\tCache-Control: private, no-cache\n/*.chunk.*.js\n\tCache-Control: public, max-age=31536000';
if (this.brotli) {
headers += '\n/*.br\n\tcontent-encoding: br';
}
const redirects = `${this.redirects.join('\n')}\n/* /index.html 200`;
// eslint-disable-next-line guard-for-in
for (const route in manifest) {
const files = Object.keys(manifest[route]);
let routePreloadText = `${route}`;
const cssFiles = files.filter(file => file.endsWith('.css'));
const jsFiles = files.filter(file => file.endsWith('.js'));
const otherFiles = files.filter(
file => !file.endsWith('.js') && !file.endsWith('.css')
);
const addFileToHeader = file => {
const details = manifest[route][file];
routePreloadText += `\n\tLink: </${file}>; rel=preload; as=${details.type}`;
if (/^bundle(.+)\.esm\.js$/.test(file)) {
routePreloadText += '; crossorigin=anonymous';
}
};
cssFiles.forEach(addFileToHeader);
jsFiles.forEach(addFileToHeader);
otherFiles.forEach(addFileToHeader);
headers = `${headers}\n${routePreloadText}`;
}
compilation.assets._headers = {
source() {
return headers;
},
size() {
return headers.length;
},
};
compilation.assets._redirects = {
source() {
return redirects;
},
size() {
return redirects.length;
},
};
callback();
return compilation;
};
if (compiler.hooks) {
compiler.hooks.emit.tapAsync('NetlifyServerPushPlugin', handler);
} else {
compiler.plugin('emit', handler);
}
}
}
module.exports = function(config, options = {}) {
if (!config || !config.plugins) {
throw new Error(
'You need to pass the webpack config to preact-cli-plugin-netlify!'
);
}
const swBuilder = getPluginsByName(config, 'SWBuilderPlugin');
if (
swBuilder &&
swBuilder.length > 0 &&
swBuilder[0].plugin &&
swBuilder[0].plugin.brotli_
) {
options.brotli = true;
}
config.plugins.push(new NetlifyServerPushPlugin(options));
const { plugins } = config;
for (let pluginIndex = 0; pluginIndex < plugins.length; pluginIndex++) {
const plugin = plugins[pluginIndex];
if (plugin && plugin.options && plugin.options.cacheId) {
// Ignore genearted _headers and _redirects files from SW precaching
Object.assign(plugin.options, {
staticFileGlobsIgnorePatterns: [
...plugin.options.staticFileGlobsIgnorePatterns,
/_headers/,
/_redirects/,
],
});
}
}
};
function getPluginsByName(config, name) {
return getPlugins(config).filter(
w => w.plugin && w.plugin.constructor && w.plugin.constructor.name === name
);
}
function getPlugins(config) {
return (config.plugins || []).map((plugin, index) => ({ index, plugin }));
}