forked from iddan/react-native-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle-html.js
41 lines (35 loc) · 1.14 KB
/
bundle-html.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
const fs = require('fs');
const path = require('path');
const parse5 = require('parse5');
const flow = require('lodash/fp/flow');
const Node = {
map: transform => node => {
const transformed = transform(node);
const {childNodes} = transformed;
return {
...transformed,
childNodes: childNodes && childNodes.map(Node.map(transform)),
};
},
};
const ENTRY = process.argv[2];
const entryPath = require.resolve(ENTRY);
const entryContent = fs.readFileSync(entryPath, 'utf-8');
const newContent = flow([
parse5.parse,
Node.map(node => {
if (node.nodeName === 'script') {
const src = node.attrs.find(attr => attr.name === 'src');
if (src.value) {
const scriptPath = path.resolve(path.dirname(entryPath), src.value);
const scriptContent = fs.readFileSync(scriptPath, 'utf-8');
const [newScript] = parse5.parseFragment(`<script>${scriptContent}</script>`, node.parent).childNodes;
return newScript;
}
}
return node;
}),
parse5.serialize,
])(entryContent);
fs.writeFileSync(`${entryPath}.js`, `export default \`${newContent}\``);
console.log(`${ENTRY} -> ${ENTRY}.js`);