feat 打印storage
This commit is contained in:
@@ -9,6 +9,78 @@ 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: {
|
||||
@@ -20,6 +92,8 @@ export default defineConfig({
|
||||
},
|
||||
},
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user