Files
test-repo/scripts/build/rollup-plugins/executable.mjs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
923 B
JavaScript
Raw Normal View History

2021-05-15 20:09:52 +08:00
import fs from "node:fs";
import path from "node:path";
2018-05-24 15:30:45 -03:00
export default function rollupPluginExecutable() {
2018-05-24 15:30:45 -03:00
let banner;
let entry;
2019-11-10 00:42:52 +08:00
let file;
2018-05-24 15:30:45 -03:00
return {
2019-06-08 03:58:05 +08:00
name: "executable",
2019-11-10 00:42:52 +08:00
options(inputOptions) {
entry = path.resolve(inputOptions.input);
},
generateBundle(outputOptions) {
file = outputOptions.file;
2018-05-24 15:30:45 -03:00
},
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)
);
}
},
2019-06-08 03:58:05 +08:00
renderChunk(code) {
2018-05-24 15:30:45 -03:00
if (banner) {
return { code: banner + "\n" + code };
}
},
2019-11-10 00:42:52 +08:00
writeBundle() {
if (banner && file) {
fs.chmodSync(file, 0o755 & ~process.umask());
2018-05-24 15:30:45 -03:00
}
},
};
2021-05-15 20:09:52 +08:00
}