Skip to content

Instantly share code, notes, and snippets.

@Breklin
Forked from mistical2008/prettierrc.js
Created June 29, 2025 16:09
Show Gist options
  • Save Breklin/95b0acabcc409599ecd704abccee08f6 to your computer and use it in GitHub Desktop.
Save Breklin/95b0acabcc409599ecd704abccee08f6 to your computer and use it in GitHub Desktop.
Vite config: plain html + vanilla-js
module.exports = {
trailingComma: 'es5',
tabWidth: 4,
semi: false,
singleQuote: true,
}
import path from 'path'
import { getHtmlFiles } from './fs'
export async function getHtmlEntries(src = 'src') {
const files = await getHtmlFiles(src)
return files.reduce((prev, record) => {
const merged = Object.assign(prev, record)
Object.entries(merged).forEach(([key, value]) => {
merged[key] = path.resolve(process.cwd(), src, value)
})
return merged
}, {})
}
// getHtmlEntries().then((res) => console.log({ entries: res }))
import { readdir } from 'fs/promises'
import path from 'path'
/*
* @usage
* const getHtmlFiles = getFilesByExt("html")
* getHtmlFiles(resolve(process.cwd(), "src"))
* .then(files => console.log({files}))
*/
export function getFilesByExt(
ext: string
): (src: string) => Promise<Record<string, string>[] | undefined> {
return async function getFiles(src: string) {
try {
const files = await readdir(src, { encoding: 'utf-8' })
const extFmt = `.${ext}`
return files
.filter((file) => path.extname(file) === extFmt)
.map((file) => ({
[path.basename(file, extFmt)]: file,
}))
} catch (error) {
console.log({ error })
}
}
}
export const getHtmlFiles = getFilesByExt('html')
pnpm create vite MY_APP -- --template vanilla && pnpm add -D vite-plugin-nunjucks @types/node eslint prettier

Vite configuration for plain HTML with vanilla JavaScript and Nunjuck templating

Project folder file structure:

  - src/
      - scripts/
      - styles/
      - layouts/
      - public/
      - img/
      - fonts/

Etrypoints

HTML

Main entrypoints in *.html files (generated by getHtmlEntries())

JavaSript

To include js just add script tag to desired *.html

CSS

Just import to desired *.js

import { defineConfig } from 'vite'
import nunjucks from 'vite-plugin-nunjucks'
import path from 'path'
import { getHtmlEntries } from './utils/bundler'
export default defineConfig(async () => {
return {
root: 'src',
build: {
rollupOptions: {
input: await getHtmlEntries(),
},
outDir: path.resolve(__dirname, 'dist'),
},
resolve: {
alias: {
src: path.resolve(process.cwd(), 'src'),
scripts: path.resolve(process.cwd(), 'src/scripts'),
styles: path.resolve(process.cwd(), 'src/styles'),
},
},
plugins: [
nunjucks({
templatesDir: path.resolve(process.cwd(), 'src'),
}),
],
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment