Skip to content

Commit c2d098c

Browse files
committed
add missing file
1 parent feac209 commit c2d098c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

lib/build-tools/include-bytes.d.ts

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build-tools/include-bytes.js

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/build-tools/include-bytes.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
export default function ({ types: t }) {
4+
return {
5+
visitor: {
6+
CallExpression(p, state) {
7+
let name = p.node.callee.name
8+
let args = p.node.arguments
9+
10+
if (name === 'includeBytes') {
11+
// Get the path of file
12+
var filename = this.file.opts.filename
13+
14+
// User settings
15+
let root = state.opts.root || path.dirname(filename)
16+
17+
// Read binary file into bytes, so encoding is 'latin1' (each byte is 0-255, become one character)
18+
const encoding = 'latin1'
19+
20+
// Require first arg to be string
21+
t.assertStringLiteral(args[0])
22+
23+
// Error if filename is not found
24+
if (filename === undefined || filename === 'unknown')
25+
throw new Error('`includeBytes` function called outside of file')
26+
27+
// Generate and locate the file
28+
let fileRelPath = args[0].value // Get literal string value
29+
let filePath = path.join(root, fileRelPath)
30+
let fileSrc = fs.readFileSync(filePath, { encoding }).toString(encoding)
31+
32+
p.replaceWith(t.stringLiteral(fileSrc))
33+
}
34+
},
35+
},
36+
}
37+
}

0 commit comments

Comments
 (0)