UNPKG

@hankei6km/rollup-plugin-nodebox-fs-files

Version:
80 lines (79 loc) 2.8 kB
import { readFile } from 'node:fs/promises'; import { resolve } from 'node:path'; import { createFilter, dataToEsm } from '@rollup/pluginutils'; import { walk } from './walk.js'; export const defultExcludeFrom = [ '**/.DS_Store', '**/Thumbs.db', '**/.git/**', '**/.gitignore', '**/.vscode/**', '**/node_modules/**', '**/dist/**', '**/build/**', '**/out/**' ]; export const defultBinaryFile = [ '**/*.png', '**/*.jpg', '**/*.jpeg', '**/*.gif', '**/*.ico', '**/*.wasm' ]; export function makeFilesFilterOptsExclude(excludeFrom) { return typeof excludeFrom === 'string' && excludeFrom.length > 0 ? [...defultExcludeFrom, excludeFrom] : [...defultExcludeFrom, ...(excludeFrom || [])]; } export function makeBinaryFilterOptsInclude(binaryFile) { return typeof binaryFile === 'string' && binaryFile.length > 0 ? [...defultBinaryFile, binaryFile] : [...defultBinaryFile, ...(binaryFile || [])]; } export function makeFilesFiter(excludeFrom) { return createFilter([], makeFilesFilterOptsExclude(excludeFrom)); } export function makebinaryFilesFiter(binaryFile) { return createFilter(makeBinaryFilterOptsInclude(binaryFile), []); } export const nodeboxFsFiles = function nodeboxFsFiles(opts) { if (!opts?.from) { throw new Error('from option should be specified'); } if (!opts?.insertTo) { throw new Error('insertTo option should be specified'); } const filter = createFilter(opts.insertTo, []); const filesFilter = makeFilesFiter(opts.excludeFrom); const binaryFilter = makebinaryFilesFiter(opts.binaryFile); let curFiles = {}; return { name: 'nodebox-fs-files', async buildStart() { curFiles = {}; const dir = resolve('.', opts.from); for await (const [entry, relEntry] of walk(filesFilter, dir, [])) { const content = await readFile(entry); curFiles[relEntry] = binaryFilter(entry) ? content : content.toString('utf-8'); } }, async transform(code, id) { if (filter(id)) { // Once JSON.stringify and parse to make it available in fs.init of Nodebox. // It becomes { type: "Buffer", data: [ 97, 98, 99 ] }. // If you don't do this, it will be { type: "Buffer", 1: 97, 2: 98, 3: 99 }. return dataToEsm({ files: JSON.parse(JSON.stringify(curFiles)) }, { compact: false, indent: '\t', preferConst: false, objectShorthand: false, namedExports: true }); } return null; } }; };