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

49 lines
919 B
JavaScript
Raw Normal View History

2018-05-24 15:30:45 -03:00
"use strict";
const fs = require("fs");
const path = require("path");
2020-03-21 22:07:52 +02:00
module.exports = function () {
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
}
2020-03-21 22:07:52 +02:00
},
2018-05-24 15:30:45 -03:00
};
};