Files
Road_roller/vite.config.ts
2026-09-27 09:55:18 +08:00

127 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Uni from '@uni-helper/plugin-uni'
import UniHelperComponents from '@uni-helper/vite-plugin-uni-components'
import UniHelperLayouts from '@uni-helper/vite-plugin-uni-layouts'
import UniHelperManifest from '@uni-helper/vite-plugin-uni-manifest'
import UniHelperPages from '@uni-helper/vite-plugin-uni-pages'
import { UniEchartsResolver } from 'uni-echarts/resolver'
import UnoCSS from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { defineConfig } from 'vite'
import UniPolyfill from 'vite-plugin-uni-polyfill'
/**
* 兜底处理 uni:json 插件对 pages.json 的解析失败
*
* 背景:vite-plugin-uni-pages 会用 comment-json 往 pages.json 里写入
* `// GENERATED BY UNI-PAGES...` 注释。uni-app 的 uni:json 插件本应用
* jsonc-parser 吃掉注释,但当它拿到半截文件内容(HBuilderX 写入与读取竞态)
* 时 parse 会返回 undefined,导致插件没产出 code,随后 Vite 内置 vite:json
* 用严格 JSON.parse 接手,撞上注释直接报错:
* Failed to parse JSON file, invalid JSON syntax found at position -1
*
* 这个插件挂在最前面(enforce: 'pre'),保证 vite:json 拿到的一定是无注释的
* 合法 JSON,从而绕开该问题。
*/
function stripJsonComments() {
return {
name: 'strip-json-comments',
enforce: 'pre' as const,
transform(code: string, id: string) {
// 只处理 src 下的 pages.json / manifest.json
if (!/[\\/](?:pages|manifest)\.json(?:\?.*)?$/.test(id))
return null
// 本身就不含注释,交回后续插件处理
if (!code.includes('//'))
return null
let inString = false
let escaped = false
let result = ''
for (let i = 0; i < code.length; i++) {
const ch = code[i]
if (inString) {
result += ch
if (escaped)
escaped = false
else if (ch === '\\')
escaped = true
else if (ch === '"')
inString = false
continue
}
if (ch === '"') {
inString = true
result += ch
continue
}
// 行注释:跳到行尾
if (ch === '/' && code[i + 1] === '/') {
while (i < code.length && code[i] !== '\n') i++
result += '\n'
continue
}
// 块注释:跳到结束
if (ch === '/' && code[i + 1] === '*') {
i += 2
while (i < code.length && !(code[i] === '*' && code[i + 1] === '/')) i++
i++
continue
}
result += ch
}
return { code: result, map: null }
},
}
}
// https://vitejs.dev/config/
export default defineConfig({
css: {
preprocessorOptions: {
scss: {
// 压制依赖包(uni-ui 等)的 Sass 旧 API / 全局函数弃用警告
silenceDeprecations: ['legacy-js-api', 'global-builtin', 'color-functions', 'import'],
},
},
},
plugins: [
// 必须放最前:在 uni:json / vite:json 之前剥掉 pages.json 里的注释
stripJsonComments(),
// https://uni-helper.js.org/vite-plugin-uni-manifest
UniHelperManifest(),
// https://uni-helper.js.org/vite-plugin-uni-pages
UniHelperPages({
dts: 'src/uni-pages.d.ts',
homePage: 'pages/Ahome/index',
}),
// https://uni-helper.js.org/vite-plugin-uni-layouts
UniHelperLayouts(),
// https://uni-helper.js.org/vite-plugin-uni-components
UniHelperComponents({
dts: 'src/components.d.ts',
directoryAsNamespace: true,
resolvers: [UniEchartsResolver()],
}),
// https://uni-helper.js.org/plugin-uni
Uni(),
UniPolyfill(),
// https://github.com/antfu/unplugin-auto-import
AutoImport({
imports: ['vue', '@vueuse/core', 'uni-app'],
dts: 'src/auto-imports.d.ts',
dirs: ['src/composables', 'src/stores', 'src/utils'],
vueTemplate: true,
}),
// https://github.com/antfu/unocss
// see unocss.config.ts for config
UnoCSS(),
],
})