Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shared): 添加对pages.js钩子的支持,能够对pages.json进行预处理 #3235

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/uni-cli-shared/src/vite/plugins/jsonJs.ts
Expand Up @@ -51,7 +51,28 @@ function createDefineJsonJsPlugin(name: 'pages.json' | 'manifest.json') {
if (!opts.filter(id)) {
return
}
return fs.readFileSync(jsonPath, 'utf8')

var pagesJson = fs.readFileSync(jsonPath, 'utf8')
const pagesJsonJsFileName = name.split('.')[0] + '.js'
const pagesJsonJsPath = path.join(
process.env.UNI_INPUT_DIR,
pagesJsonJsFileName
)

if (fs.existsSync(pagesJsonJsPath)) {
delete require.cache[pagesJsonJsPath]
const pagesJsonJsFn = require(pagesJsonJsPath)
if (typeof pagesJsonJsFn === 'function') {
pagesJson = pagesJsonJsFn(pagesJson)
if (!pagesJson) {
console.error(`${pagesJsonJsFileName} 必须返回一个 json 对象`)
}
} else {
console.error(`${pagesJsonJsFileName} 必须导出 function`)
}
}

return pagesJson
}
return plugin
}
Expand Down