-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-firebase-json.js
62 lines (53 loc) · 1.77 KB
/
generate-firebase-json.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
const cheerio = require('cheerio');
const get = require('lodash/get');
const transformerFirebase = require('./transformer-firebase.js');
const { sources } = require('webpack');
/**
* The default function for adding the CSP to the head of a document
* Can be overwritten to allow the developer to process the CSP in their own way
* @param {string} builtPolicy
* @param {object} htmlPluginData
* @param {object} $
*/
const defaultProcessFn = (builtPolicy, htmlPluginData, $) => {
let metaTag = $('meta[http-equiv="Content-Security-Policy"]');
// Add element if it doesn't exist.
if (!metaTag.length) {
metaTag = cheerio.load('<meta http-equiv="Content-Security-Policy">')(
'meta'
);
metaTag.prependTo($('head'));
}
// build the policy into the context attr of the csp meta tag
metaTag.attr('content', builtPolicy);
// eslint-disable-next-line no-param-reassign
htmlPluginData.html = get(htmlPluginData, 'plugin.options.xhtml', false)
? $.xml()
: $.html();
};
var cspHeadersForSourceGlob = [];
const generateFirebaseJson = (
builtPolicy,
htmlPluginData,
$,
compilation,
) => {
const sourceGlob = htmlPluginData.outputName
.replace(/\.html/, '')
.replace(/index/, '/')
.replace(/404/, '**/*');
cspHeadersForSourceGlob = [
{
source: sourceGlob,
headers: [
{
key: 'Content-Security-Policy',
value: builtPolicy,
}
],
}
].concat(cspHeadersForSourceGlob);
transformerFirebase(cspHeadersForSourceGlob);
defaultProcessFn(builtPolicy, htmlPluginData, $, compilation);
};
module.exports = generateFirebaseJson;