Files
test-repo/scripts/build/rollup-plugins/executable.mjs
T
Sosuke Suzukiandfisker Cheung 2664fcb2ba Enable import/no-anonymous-default-export (#11478)
* Enable import/no-anonymous-default-export

* Add names to default exported

* More

* Ignore shims

* Update scripts/build/config.mjs

Co-authored-by: fisker Cheung <lionkay@gmail.com>

* lint  for shims

Co-authored-by: fisker Cheung <lionkay@gmail.com>
2021-09-10 11:17:09 +09:00

48 lines
923 B
JavaScript

import fs from "node:fs";
import path from "node:path";
export default function rollupPluginExecutable() {
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());
}
},
};
}