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

48 lines
950 B
JavaScript
Raw Normal View History

2018-05-24 15:30:45 -03:00
"use strict";
const fs = require("fs");
const path = require("path");
module.exports = function() {
let banner;
let entry;
return {
2019-06-08 03:58:05 +08:00
name: "executable",
2018-05-24 15:30:45 -03:00
options(options) {
2019-06-08 03:58:05 +08:00
entry = path.resolve(options.input);
2018-05-24 15:30:45 -03:00
return options;
},
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-06-08 03:58:05 +08:00
writeBundle(bundle) {
2018-05-24 15:30:45 -03:00
if (banner) {
2019-06-08 03:58:05 +08:00
const files = Object.keys(bundle);
files.forEach(file => {
fs.chmodSync(bundle[file].facadeModuleId, 0o755 & ~process.umask());
});
2018-05-24 15:30:45 -03:00
}
}
};
};