'use strict'; var pluginutils = require('@rollup/pluginutils'); var promises = require('node:fs/promises'); var node_path = require('node:path'); var MagicString = require('magic-string'); function shebang({ include = ['**/*.js'], exclude, regexp = /^\s*#!.*\n*/, shebang = '#!/usr/bin/env node', separator = '\n\n', mode = 0o755, insert = true, preserve = true, executable = true } = {}) { const scripts = new Set(); const shebangs = new Map(); const filter = pluginutils.createFilter(include, exclude); if (typeof shebang === 'function') shebang = shebang(); mode &= ~process.umask(); return { name: 'shebang-bin', transform(code, id) { if (!filter(id)) return const match = code.match(regexp); if (!match) return const [preserved] = match; const { index } = match; const magicString = new MagicString(code); magicString.remove(index, preserved.length); shebangs.set(id, preserved); return { code: magicString.toString(), map: magicString.generateMap({ includeContent: true, hires: true }) } }, renderChunk(code, { isEntry, facadeModuleId, fileName }, { file, dir, sourcemap }) { if (!isEntry || !facadeModuleId || !filter(facadeModuleId)) return if (executable) { scripts.add(file || node_path.join(dir || process.cwd(), fileName)); } if (preserve) { const preserved = shebangs.get(facadeModuleId); if (preserved) return prepend(preserved) } if (insert) return prepend(`${shebang}${separator}`) function prepend(prefix) { if (!sourcemap) { return `${prefix}${code}` } const magicString = new MagicString(code); magicString.prepend(`${prefix}${code}`); return { code: magicString.toString(), map: magicString.generateMap({ includeContent: true, hires: true }) } } }, async writeBundle() { await Promise.all( [...scripts].map(file => promises.chmod(file, mode)) ); } } } module.exports = shebang; //# sourceMappingURL=index.cjs.map