Created
April 24, 2023 19:31
-
-
Save 0x1eef/c59c3c2f28079217660663fa7c13f130 to your computer and use it in GitHub Desktop.
EJS vite plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PluginOption } from "vite"; | |
import { render } from 'ejs'; | |
import path from 'path'; | |
import fs from 'fs'; | |
type ReadFile = [string, object]; | |
const readFile = async (...args: ReadFile) => { | |
const [path, options] = args; | |
return new Promise((resolve, reject) => { | |
fs.readFile(path, options, (err, data) => { | |
err ? reject(err) : resolve(data); | |
}); | |
}); | |
}; | |
type WriteFile = [string, string, object]; | |
const writeFile = async (...args: WriteFile): Promise<void> => { | |
const [path, text, options] = args; | |
return new Promise((resolve, reject) => { | |
fs.writeFile(path, text, options || {}, (err) => { | |
err ? reject(err) : resolve(); | |
}); | |
}); | |
}; | |
type File = { | |
input: string, | |
output: string | |
variables: Record<string, string | Function | object> | |
} | |
type Vite = { | |
root: string, build: {outDir: string} | |
} | |
export default function(files: File[]): PluginOption { | |
const vite: Vite = {root: "", build: {outDir: ""}}; | |
const fileOpts = { encoding: 'utf-8' }; | |
return { | |
name: 'ejs', | |
enforce: 'pre', | |
async generateBundle(_options, _bundle) { | |
try { | |
for (const k in Object.keys(files)) { | |
const file = files[k] | |
const input = path.join(vite.root, file.input) | |
const { variables } = file | |
const text = await render(await readFile(input, fileOpts), variables) | |
const output = path.join(vite.root, vite.build.outDir, file.output) | |
await writeFile(output, text, fileOpts); | |
} | |
} catch (error: any) { | |
this.error(error) | |
} | |
}, | |
configResolved(resolvedConfig) { | |
Object.assign(vite, resolvedConfig) | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment