|
| 1 | +// vite.config.mts |
| 2 | +import { defineConfig } from "@solidjs/start/config"; |
| 3 | +import { fileURLToPath, URL } from "node:url"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import { generateSW } from "@winter-love/sw"; |
| 6 | +import unoCss from "unocss/vite"; |
| 7 | +import * as nodeFs from "node:fs"; |
| 8 | +import legacy from "@vitejs/plugin-legacy"; |
| 9 | +import { targets } from "@winter-love/vite-lib-config"; |
| 10 | +var fixNitroFunction = async () => { |
| 11 | + const source = process.env.VERCEL === "1" ? path.resolve(".vercel/output/functions/__nitro.func/chunks/nitro/nitro.mjs") : path.resolve(".output/server/chunks/nitro/nitro.mjs"); |
| 12 | + const scriptJs = await nodeFs.promises.readFile(source, "utf8"); |
| 13 | + await nodeFs.promises.writeFile( |
| 14 | + source, |
| 15 | + scriptJs.replace( |
| 16 | + ` |
| 17 | +function getDefaultExportFromNamespaceIfNotNamed (n) { |
| 18 | + return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n; |
| 19 | +}`, |
| 20 | + ` |
| 21 | +// hot fixed |
| 22 | +function getDefaultExportFromNamespaceIfNotNamed (n) { |
| 23 | + return n && Object.prototype.hasOwnProperty.call(n, 'default') && (Object.keys(n).length === 1 || (Object.prototype.hasOwnProperty.call(n, '__esModule') && Object.keys(n).length === 2) ) ? n['default'] : n; |
| 24 | +}` |
| 25 | + ), |
| 26 | + "utf8" |
| 27 | + ); |
| 28 | +}; |
| 29 | +var fixSw = async () => { |
| 30 | + return nodeFs.promises.rm(path.resolve("public/sw.js")); |
| 31 | +}; |
| 32 | +var hotfix = async () => { |
| 33 | + await fixNitroFunction(); |
| 34 | + await fixSw(); |
| 35 | +}; |
| 36 | +var createGenerateSwPlugin = () => { |
| 37 | + let _config; |
| 38 | + return { |
| 39 | + async closeBundle() { |
| 40 | + if (!_config) { |
| 41 | + return; |
| 42 | + } |
| 43 | + const { outDir, root } = _config.router; |
| 44 | + const swOutPath = path.join(root, "public/sw.js"); |
| 45 | + await generateSW(swOutPath, { |
| 46 | + assets: "_build/assets/**/*", |
| 47 | + assetsRoot: outDir, |
| 48 | + cwd: "" |
| 49 | + }); |
| 50 | + }, |
| 51 | + configResolved(config) { |
| 52 | + if (config.router.type === "client" && config.mode === "production") { |
| 53 | + _config = config; |
| 54 | + } |
| 55 | + }, |
| 56 | + name: "generate-sw" |
| 57 | + }; |
| 58 | +}; |
| 59 | +var vite_config_default = defineConfig({ |
| 60 | + server: { |
| 61 | + hooks: { |
| 62 | + close: () => { |
| 63 | + return hotfix(); |
| 64 | + } |
| 65 | + } |
| 66 | + }, |
| 67 | + vite: { |
| 68 | + // build: { |
| 69 | + // minify: false, |
| 70 | + // }, |
| 71 | + plugins: [ |
| 72 | + // |
| 73 | + unoCss("../../uno.config.ts"), |
| 74 | + createGenerateSwPlugin(), |
| 75 | + legacy({ |
| 76 | + targets |
| 77 | + }) |
| 78 | + ], |
| 79 | + resolve: { |
| 80 | + alias: { |
| 81 | + // fix #start/app is app.tsx only @solid/state error |
| 82 | + "#start/app": fileURLToPath(new URL("src/App.tsx", import.meta.url)), |
| 83 | + // fix @tonejs/midi is not module js |
| 84 | + "@tonejs/midi": fileURLToPath( |
| 85 | + new URL("node_modules/@tonejs/midi/src/Midi.ts", import.meta.url) |
| 86 | + ), |
| 87 | + src: fileURLToPath(new URL("src", import.meta.url)) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +}); |
| 92 | +export { |
| 93 | + vite_config_default as default |
| 94 | +}; |
0 commit comments