-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Description
Clear and concise description of the problem
Think of this directory structure-
|- nested /
|--|- about.ejs
|- index.ejs
|- main.js
|- vite.config.js
Currently, seems like there is no clear way to do the <-- template -->
to HTML
conversion during SSG. transformIndexHtml
only runs for files with .html
extension and transform
can only output JavaScript and not HTML.
In the above example, I am guessing the implementation would look something like this-
- Turn EJS to temporary HTML files
- Set those newly created HTML files in the `input` option while calling vite.build
- Delete the temporary HTML files (Since they are not going to have all the styles and scripts included)
This is exactly how I am doing things in the SSG that I am working on-
- I turn
.abell
-> temporary.html
files (they don't have prod scripts and styles in them) - I run Vite's build by passing these new HTML files as
input
so that they will take care of injecting scripts and styles and generatedist
- Then I delete the temporary HTML files
This seems to be missing for all template engines.
For example-
- vite-plugin-ejs seems to suggest people to keep the extension as
.html
- vite-plugin-pug requires users to inject pug link in the
index.html
file
Suggested solution
A new hook called transformEntryToHtml
.
Example-
// plugin code
export function ViteEjsPlugin(): Plugin {
return {
name: "vite-plugin-ejs",
// Similar to `transformIndexHtml` except it runs for any entry file irrespective of the extension
transformEntryToHtml(ejsCode: string): string {
const html = ejs.render(ejsCode)
return html
}
};
}
// Plugin consumer's vite.config.ts
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
build: {
rollupOptions: {
input: ['./index.ejs', './nested/about.ejs']
}
}
})
This will simplify the SSG flow a lot and will allow plugins like vite-plugin-ejs
to have .ejs
extension.
It will allow SSGs like Abell to build the output without generating temporary HTML files during the build.
Alternative
Alternative would be some way to call transformIndexHtml
hook for files that are not HTML (don't have .html
extension).
Additional context
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.