From 165cf86abe165d4e8b95fb28e4d3e2a7a58c75d4 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Sun, 2 Jun 2024 20:39:03 -0400 Subject: [PATCH] step2 --- vite.config.ts | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 05c1740..11bf014 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,39 @@ import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' +import { readFileSync } from 'fs'; +import { read, utils } from 'xlsx'; -// https://vitejs.dev/config/ export default defineConfig({ - plugins: [vue()], -}) + assetsInclude: ['**/*.xlsx'], // xlsx file should be treated as assets + + plugins: [ + vue(), + { // this plugin handles ?sheetjs tags + name: "vite-sheet", + transform(code, id) { + if(!id.match(/\?sheetjs$/)) return; + var wb = read(readFileSync(id.replace(/\?sheetjs$/, ""))); + var data = utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); + return `export default JSON.parse('${JSON.stringify(data).replace(/\\/g, "\\\\")}')`; + } + }, + { // this plugin handles ?html tags + name: "vite-sheet-html", + transform(code, id) { + if(!id.match(/\?html/)) return; + var wb = read(readFileSync(id.replace(/\?html/, ""))); + var html = utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]); + return (`export default JSON.parse('${JSON.stringify(html).replace(/\\/g, "\\\\")}')`); + } + }, + { // this plugin handles ?b64 tags + name: "vite-b64-plugin", + transform(code, id) { + if(!id.match(/\?b64$/)) return; + var path = id.replace(/\?b64/, ""); + var data = readFileSync(path, "base64"); + return `export default '${data}'`; + } + } + ] +}); \ No newline at end of file