-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (72 loc) · 2.76 KB
/
index.js
File metadata and controls
84 lines (72 loc) · 2.76 KB
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
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
/**
* @param {object} config
* @param {string} config.filename - HTML file to process and override
* @param {boolean} config.env - Whether to replace env vars or not (default - `false`)
* @param {string} config.envPrefix - Limit env vars to pick (default - `REACT_APP_`)
*/
const HtmlPlugin = (config) => ({
name: 'html',
setup(build) {
build.onResolve({ filter: /\.html$/ }, args => ({
path: path.resolve(args.resolveDir, args.path),
namespace: 'html',
}));
build.onLoad({ filter: /.html/, namespace: 'html' }, (args) => {
let htmlContent = fs.readFileSync(args.path).toString('utf-8');
// replace env vars
if (config.env) {
const envPrefix = config.envPrefix || 'REACT_APP_';
const envVars = Object.entries(process.env || {}).filter(([name]) => name.startsWith(envPrefix));
htmlContent = envVars.reduce(
(memo, [name, value]) => memo.replace(new RegExp(`%${name}%`, 'igm'), value),
htmlContent,
);
}
return {
contents: htmlContent,
loader: 'file'
};
});
build.onEnd((result) => {
const outFiles = Object.keys((result.metafile || {}).outputs);
const jsFiles = outFiles.filter((p) => p.endsWith('.js'));
const cssFiles = outFiles.filter((p) => p.endsWith('.css'));
const htmlFiles = outFiles.filter((p) => p.endsWith('.html'));
const headerAppends = cssFiles.reduce(
(memo, p) => {
const filename = p.split(path.sep).slice(-1)[0];
return [...memo, `<link href="${filename}" rel="stylesheet">`];
},
[],
);
const bodyAppends = jsFiles.reduce(
(memo, p) => {
const filename = p.split(path.sep).slice(-1)[0];
return [...memo, `<script src="${filename}"></script>`];
},
[],
);
for (const htmlFile of htmlFiles) {
let htmlContent = fs.readFileSync(htmlFile).toString('utf-8');
// replace env vars
if (config.env) {
const envPrefix = config.envPrefix || 'REACT_APP_';
const envVars = Object.entries(process.env).filter(([name]) => name.startsWith(envPrefix));
htmlContent = envVars.reduce(
(memo, [name, value]) => memo.replace(new RegExp(`%${name}%`, 'igm'), value),
htmlContent,
);
}
// inject references to js and css files
htmlContent = htmlContent
.replace('</head>', [...headerAppends, '</head>'].join("\n"))
.replace('</body>', [...bodyAppends, '</body>'].join("\n"));
fs.writeFileSync(config.filename.replace('-[^.]+', ''), htmlContent);
}
});
},
});
module.exports = HtmlPlugin;