|
| 1 | +import { readFile } from 'node:fs/promises' |
| 2 | +import { resolve } from 'node:path' |
| 3 | +import type { PluginImpl } from 'rollup' |
| 4 | +import { createFilter, dataToEsm } from '@rollup/pluginutils' |
| 5 | +import { walk } from './walk.js' |
| 6 | + |
| 7 | +export const defultExcludeFrom = [ |
| 8 | + '**/.DS_Store', |
| 9 | + '**/Thumbs.db', |
| 10 | + '**/.git/**', |
| 11 | + '**/.gitignore', |
| 12 | + '**/.vscode/**', |
| 13 | + '**/node_modules/**', |
| 14 | + '**/dist/**', |
| 15 | + '**/build/**', |
| 16 | + '**/out/**' |
| 17 | +] |
| 18 | + |
| 19 | +export const defultBinaryFile = [ |
| 20 | + '**/*.png', |
| 21 | + '**/*.jpg', |
| 22 | + '**/*.jpeg', |
| 23 | + '**/*.gif', |
| 24 | + '**/*.ico', |
| 25 | + '**/*.wasm' |
| 26 | +] |
| 27 | + |
| 28 | +export type NodeboxFsFilesOptions = { |
| 29 | + from: string |
| 30 | + insertTo: string |
| 31 | + excludeFrom?: string | string[] |
| 32 | + binaryFile?: string | string[] |
| 33 | +} |
| 34 | + |
| 35 | +export function makeFilesFilterOptsExclude( |
| 36 | + excludeFrom?: NodeboxFsFilesOptions['excludeFrom'] |
| 37 | +): Parameters<typeof createFilter>[1] { |
| 38 | + return typeof excludeFrom === 'string' && excludeFrom.length > 0 |
| 39 | + ? [...defultExcludeFrom, excludeFrom] |
| 40 | + : [...defultExcludeFrom, ...(excludeFrom || [])] |
| 41 | +} |
| 42 | + |
| 43 | +export function makeBinaryFilterOptsInclude( |
| 44 | + binaryFile?: NodeboxFsFilesOptions['binaryFile'] |
| 45 | +): Parameters<typeof createFilter>[1] { |
| 46 | + return typeof binaryFile === 'string' && binaryFile.length > 0 |
| 47 | + ? [...defultBinaryFile, binaryFile] |
| 48 | + : [...defultBinaryFile, ...(binaryFile || [])] |
| 49 | +} |
| 50 | + |
| 51 | +export function makeFilesFiter( |
| 52 | + excludeFrom?: NodeboxFsFilesOptions['excludeFrom'] |
| 53 | +): ReturnType<typeof createFilter> { |
| 54 | + return createFilter([], makeFilesFilterOptsExclude(excludeFrom)) |
| 55 | +} |
| 56 | +export function makebinaryFilesFiter( |
| 57 | + binaryFile?: NodeboxFsFilesOptions['binaryFile'] |
| 58 | +): ReturnType<typeof createFilter> { |
| 59 | + return createFilter(makeBinaryFilterOptsInclude(binaryFile), []) |
| 60 | +} |
| 61 | + |
| 62 | +export const nodeboxFsFiles: PluginImpl<NodeboxFsFilesOptions> = |
| 63 | + function nodeboxFsFiles(opts?: NodeboxFsFilesOptions) { |
| 64 | + if (!opts?.from) { |
| 65 | + throw new Error('from option should be specified') |
| 66 | + } |
| 67 | + if (!opts?.insertTo) { |
| 68 | + throw new Error('insertTo option should be specified') |
| 69 | + } |
| 70 | + |
| 71 | + const filter = createFilter(opts.insertTo, []) |
| 72 | + const filesFilter = makeFilesFiter(opts.excludeFrom) |
| 73 | + const binaryFilter = makebinaryFilesFiter(opts.binaryFile) |
| 74 | + let curFiles: Record<string, any> = {} |
| 75 | + |
| 76 | + return { |
| 77 | + name: 'nodebox-fs-files', |
| 78 | + |
| 79 | + async buildStart() { |
| 80 | + curFiles = {} |
| 81 | + const dir = resolve('.', opts.from) |
| 82 | + for await (const [entry, relEntry] of walk(filesFilter, dir, [])) { |
| 83 | + const content = await readFile(entry) |
| 84 | + curFiles[relEntry] = binaryFilter(entry) |
| 85 | + ? content |
| 86 | + : content.toString('utf-8') |
| 87 | + } |
| 88 | + }, |
| 89 | + |
| 90 | + async transform(code, id) { |
| 91 | + if (filter(id)) { |
| 92 | + // Once JSON.stringify and parse to make it available in fs.init of Nodebox. |
| 93 | + // It becomes { type: "Buffer", data: [ 97, 98, 99 ] }. |
| 94 | + // If you don't do this, it will be { type: "Buffer", 1: 97, 2: 98, 3: 99 }. |
| 95 | + return dataToEsm( |
| 96 | + { files: JSON.parse(JSON.stringify(curFiles)) }, |
| 97 | + { |
| 98 | + compact: false, |
| 99 | + indent: '\t', |
| 100 | + preferConst: false, |
| 101 | + objectShorthand: false, |
| 102 | + namedExports: true |
| 103 | + } |
| 104 | + ) |
| 105 | + } |
| 106 | + return null |
| 107 | + } |
| 108 | + } |
| 109 | + } |
0 commit comments