Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// vite-plugin-raw
/*
How to use.
// vite.config.js
const raw = require('<this>')
module.exports = {
plugins: [raw()]
}
// import via raw
import foo from "./foo.js?raw";
console.log(foo);
*/
module.exports = () => ({
enforce: "post",
name: "raw",
resolveId(id) {
if (id.endsWith("?raw")) {
return id;
}
},
load(id) {
if (id.endsWith("?raw")) {
const fpath = id.replace("?raw", "");
const raw = fs.readFileSync(fpath, "utf-8");
return `export default ${JSON.stringify(raw)};`;
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment