-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintercept-upward-file.js
48 lines (40 loc) · 1.6 KB
/
intercept-upward-file.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
const path = require('path');
const fs = require('fs');
const jsYaml = require('js-yaml');
const UPWARD_FILENAME = 'upward.yml';
const DEF_NAME = 'pwaExperimentContentSecurityPolicy';
module.exports = targets => {
const builtins = targets.of('@magento/pwa-buildpack');
builtins.webpackCompiler.tap(compiler => {
compiler.hooks.emit.tapPromise({
name: targets.name,
stage: 2,
async fn(compilation) {
const upwardAsset = compilation.assets[UPWARD_FILENAME];
if (upwardAsset) {
const definitions = jsYaml.safeLoad(upwardAsset.source());
const cspDefinitions = jsYaml.safeLoad(
await fs.promises.readFile(
path.resolve(__dirname, 'upward.yml'),
'utf8'
)
);
Object.assign(definitions, cspDefinitions);
definitions.veniaAppShell.inline.headers.inline[
'Content-Security-Policy-Report-Only'
] = DEF_NAME;
const newSource = jsYaml.safeDump(definitions);
const newSourceSize = Buffer.from(newSource).byteLength;
compilation.assets[UPWARD_FILENAME] = {
size() {
return newSourceSize;
},
source() {
return newSource;
}
};
}
}
});
});
};