提交 ba796cb6 编写于 作者: fxy060608's avatar fxy060608

feat: merge define

上级 366d792e
{
"uni-app": {
"scripts": {
"H5-WEIXIN": {
"title": "H5-WEIXIN",
"env": {
"UNI_PLATFORM": "H5"
},
"define": {
"H5-BASE": true,
"H5-WEIXIN": true
}
},
"H5-QQ": {
"title": "H5-QQ",
"env": {
"UNI_PLATFORM": "H5"
},
"define": {
"H5-BASE": false,
"H5-QQ": true
}
}
}
}
}
import path from 'path'
import { parseScripts } from '../src/scripts'
describe('filter', () => {
test(`basic`, () => {
const pkgPath = path.resolve(
__dirname,
'examples/custom-scripts/package.json'
)
expect(parseScripts('H5-WEIXIN', pkgPath)).toEqual({
name: 'H5-WEIXIN',
platform: 'H5',
define: {},
context: { 'H5-BASE': true, 'H5-WEIXIN': true, 'H5-QQ': false },
})
expect(parseScripts('H5-QQ', pkgPath)).toEqual({
name: 'H5-QQ',
platform: 'H5',
define: {},
context: { 'H5-BASE': false, 'H5-QQ': true, 'H5-WEIXIN': false },
})
})
})
import { hasOwn } from '@vue/shared'
import fs from 'fs'
interface Package {
......@@ -24,7 +25,8 @@ export function parseScripts(name: string, pkgPath: string) {
}
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as Package
const options = pkg['uni-app']?.scripts?.[name]
const scripts = pkg['uni-app']?.scripts || {}
const options = scripts[name]
if (!options) {
return
}
......@@ -35,10 +37,22 @@ export function parseScripts(name: string, pkgPath: string) {
process.exit(0)
}
const { UNI_PLATFORM, ...define } = options.env
const context = options.define || {}
// 补充当前编译环境未定义的其他编译环境 define
Object.keys(scripts).forEach((scriptName) => {
if (scriptName !== name) {
const scriptDefine = scripts[scriptName].define || {}
Object.keys(scriptDefine).forEach((key) => {
if (!hasOwn(context, key)) {
context[key] = false
}
})
}
})
return {
name: name,
platform: UNI_PLATFORM,
define,
context: options.define || {},
context,
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册