pnpm create vite MY_APP -- --template vanilla && pnpm add -D vite-plugin-nunjucks @types/node eslint prettier
-
-
Save Breklin/95b0acabcc409599ecd704abccee08f6 to your computer and use it in GitHub Desktop.
Vite config: plain html + vanilla-js
This file contains hidden or 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
module.exports = { | |
trailingComma: 'es5', | |
tabWidth: 4, | |
semi: false, | |
singleQuote: true, | |
} |
This file contains hidden or 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 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 })) |
This file contains hidden or 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 { 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') |
- src/
- scripts/
- styles/
- layouts/
- public/
- img/
- fonts/
Main entrypoints in *.html
files (generated by getHtmlEntries()
)
To include js
just add script tag to desired *.html
Just import to desired *.js
This file contains hidden or 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 { 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