/
executable.js
49 lines (41 loc) · 917 Bytes
/
executable.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
"use strict";
const fs = require("fs");
const path = require("path");
module.exports = function() {
let banner;
let entry;
let file;
return {
name: "executable",
options(inputOptions) {
entry = path.resolve(inputOptions.input);
},
generateBundle(outputOptions) {
file = outputOptions.file;
},
load(id) {
if (id !== entry) {
return;
}
const source = fs.readFileSync(id, "utf-8");
const match = source.match(/^\s*(#!.*)/);
if (match) {
banner = match[1];
return (
source.slice(0, match.index) +
source.slice(match.index + banner.length)
);
}
},
renderChunk(code) {
if (banner) {
return { code: banner + "\n" + code };
}
},
writeBundle() {
if (banner && file) {
fs.chmodSync(file, 0o755 & ~process.umask());
}
}
};
};