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

feat: support custom scripts (#3093)

上级 cf00c403
......@@ -22,5 +22,8 @@ declare namespace NodeJS {
UNI_AUTOMATOR_WS_ENDPOINT?: string
UNI_H5_BASE?: string
UNI_H5_BROWSER?: 'builtin'
UNI_CUSTOM_SCRIPT?: string
UNI_CUSTOM_DEFINE?: string
UNI_CUSTOM_CONTEXT?: string
}
}
......@@ -381,9 +381,13 @@ function getTestTemplate(test) {
return new Function("context", "with (context||{}){ return ( " + test + " ); }");
}
function testPasses(test,context) {
var testFn = getTestTemplate(test);
return testFn(context, getDeepPropFromObj);
// fixed by xxxxxx
function testPasses(test, context) {
var testFn = getTestTemplate(test)
try {
return testFn(context, getDeepPropFromObj)
} catch (e) {}
return false
}
function getFileContents(path, failSilent, requesterPath) {
......
......@@ -5,7 +5,9 @@ export function initDefine(stringifyBoolean: boolean = false) {
const manifestJson = parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
const isRunByHBuilderX = runByHBuilderX()
const isDebug = !!manifestJson.debug
return {
...initCustomDefine(),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.UNI_DEBUG': stringifyBoolean
? JSON.stringify(isDebug)
......@@ -32,3 +34,16 @@ export function initDefine(stringifyBoolean: boolean = false) {
'process.env.VUE_APP_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM),
}
}
function initCustomDefine() {
let define: Record<string, string> = {}
if (process.env.UNI_CUSTOM_DEFINE) {
try {
define = JSON.parse(process.env.UNI_CUSTOM_DEFINE)
} catch (e: any) {}
}
return Object.keys(define).reduce<Record<string, string>>((res, name) => {
res['process.env.' + name] = JSON.stringify(define[name])
return res
}, {})
}
......@@ -18,6 +18,7 @@ export * from './postcss'
export * from './filter'
export * from './esbuild'
export * from './resolve'
export * from './scripts'
export { M } from './messages'
......
import { extend } from '@vue/shared'
import { extend, isString, isPlainObject } from '@vue/shared'
const DEFAULT_KEYS = [
'APP',
'APP_NVUE',
......@@ -37,7 +37,7 @@ export function getPreNVueContext() {
export function initPreContext(
platform: UniApp.PLATFORM,
userPreContext?: Record<string, boolean>
userPreContext?: Record<string, boolean> | string
) {
const vueContext = Object.create(null)
const nvueContext = Object.create(null)
......@@ -67,9 +67,18 @@ export function initPreContext(
}
if (userPreContext) {
Object.keys(userPreContext).forEach((key) => {
defaultContext[normalizeKey(key)] = !!userPreContext[key]
})
if (isString(userPreContext)) {
try {
userPreContext = JSON.parse(userPreContext)
} catch (e) {}
}
if (isPlainObject(userPreContext)) {
Object.keys(userPreContext).forEach((key) => {
defaultContext[normalizeKey(key)] = !!(
userPreContext as Record<string, boolean>
)[key]
})
}
}
extend(preVueContext, defaultContext, vueContext)
extend(preNVueContext, defaultContext, nvueContext)
......
import fs from 'fs'
interface Package {
'uni-app'?: {
scripts?: {
[name: string]: {
title?: string
BROWSER?: 'Chrome' | 'Firefox' | 'IE' | 'Edge' | 'Safari' | 'HBuilderX'
env?: {
UNI_PLATFORM: UniApp.PLATFORM
[name: string]: string
}
define: {
[name: string]: boolean
}
}
}
}
}
export function parseScripts(name: string, pkgPath: string) {
if (!fs.existsSync(pkgPath)) {
return
}
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as Package
const options = pkg['uni-app']?.scripts?.[name]
if (!options) {
return
}
if (!options.env?.UNI_PLATFORM) {
console.error(
`package.json->uni-app->scripts->${name}->env->UNI_PLATFORM is required`
)
process.exit(0)
}
const { UNI_PLATFORM, ...define } = options.env
return {
name: name,
platform: UNI_PLATFORM,
define,
context: options.define || {},
}
}
......@@ -5,7 +5,12 @@ import chalk from 'chalk'
import { performance } from 'perf_hooks'
import { BuildOptions, InlineConfig, Logger } from 'vite'
import { M, isInHBuilderX, initModulePaths } from '@dcloudio/uni-cli-shared'
import {
M,
isInHBuilderX,
initModulePaths,
parseScripts,
} from '@dcloudio/uni-cli-shared'
import { CliOptions } from '.'
import { initNVueEnv } from './nvue'
......@@ -82,14 +87,15 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) {
process.env.UNI_SUB_PLATFORM = options.platform
options.platform = 'quickapp-webview'
}
process.env.UNI_PLATFORM = options.platform as UniApp.PLATFORM
process.env.VITE_ROOT_DIR = process.env.UNI_INPUT_DIR || process.cwd()
process.env.UNI_INPUT_DIR =
process.env.UNI_INPUT_DIR || path.resolve(process.cwd(), 'src')
initCustomScripts(options)
process.env.UNI_PLATFORM = options.platform as UniApp.PLATFORM
const hasOutputDir = !!process.env.UNI_OUTPUT_DIR
if (hasOutputDir) {
;(options as BuildOptions).outDir = process.env.UNI_OUTPUT_DIR
......@@ -209,3 +215,17 @@ export function printStartupDuration(
)
}
}
function initCustomScripts(options: CliOptions) {
const custom = parseScripts(
process.env.UNI_SCRIPT || options.platform!, // process.env.UNI_SCRIPT 是 HBuilderX 传递的
path.join(process.env.VITE_ROOT_DIR!, 'package.json')
)
if (!custom) {
return
}
options.platform = custom.platform
process.env.UNI_CUSTOM_SCRIPT = custom.name
process.env.UNI_CUSTOM_DEFINE = JSON.stringify(custom.define)
process.env.UNI_CUSTOM_CONTEXT = JSON.stringify(custom.context)
}
......@@ -78,7 +78,7 @@ export default function uniPlugin(
options.platform = (process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5'
options.inputDir = process.env.UNI_INPUT_DIR
initPreContext(options.platform)
initPreContext(options.platform, process.env.UNI_CUSTOM_CONTEXT)
const plugins: Plugin[] = []
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册