From f9dd77e20cbe233dfab7a897fd1b365a6f3487b7 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 22 Dec 2021 15:14:38 +0800 Subject: [PATCH] feat: support custom scripts (#3093) --- packages/shims-node.d.ts | 3 ++ .../lib/preprocess/lib/preprocess.js | 10 +++-- packages/uni-cli-shared/src/env/define.ts | 15 +++++++ packages/uni-cli-shared/src/index.ts | 1 + .../uni-cli-shared/src/preprocess/context.ts | 19 +++++--- packages/uni-cli-shared/src/scripts.ts | 44 +++++++++++++++++++ packages/vite-plugin-uni/src/cli/utils.ts | 28 ++++++++++-- packages/vite-plugin-uni/src/index.ts | 2 +- 8 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 packages/uni-cli-shared/src/scripts.ts diff --git a/packages/shims-node.d.ts b/packages/shims-node.d.ts index 191d1939b..70c56d1e1 100644 --- a/packages/shims-node.d.ts +++ b/packages/shims-node.d.ts @@ -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 } } diff --git a/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js b/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js index 17f61a2cc..304967bf0 100755 --- a/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js +++ b/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js @@ -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) { diff --git a/packages/uni-cli-shared/src/env/define.ts b/packages/uni-cli-shared/src/env/define.ts index 68c59322e..2dab40329 100644 --- a/packages/uni-cli-shared/src/env/define.ts +++ b/packages/uni-cli-shared/src/env/define.ts @@ -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 = {} + if (process.env.UNI_CUSTOM_DEFINE) { + try { + define = JSON.parse(process.env.UNI_CUSTOM_DEFINE) + } catch (e: any) {} + } + return Object.keys(define).reduce>((res, name) => { + res['process.env.' + name] = JSON.stringify(define[name]) + return res + }, {}) +} diff --git a/packages/uni-cli-shared/src/index.ts b/packages/uni-cli-shared/src/index.ts index b7660b58c..6146402d2 100644 --- a/packages/uni-cli-shared/src/index.ts +++ b/packages/uni-cli-shared/src/index.ts @@ -18,6 +18,7 @@ export * from './postcss' export * from './filter' export * from './esbuild' export * from './resolve' +export * from './scripts' export { M } from './messages' diff --git a/packages/uni-cli-shared/src/preprocess/context.ts b/packages/uni-cli-shared/src/preprocess/context.ts index 8ec287969..5669cb599 100644 --- a/packages/uni-cli-shared/src/preprocess/context.ts +++ b/packages/uni-cli-shared/src/preprocess/context.ts @@ -1,4 +1,4 @@ -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 + userPreContext?: Record | 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 + )[key] + }) + } } extend(preVueContext, defaultContext, vueContext) extend(preNVueContext, defaultContext, nvueContext) diff --git a/packages/uni-cli-shared/src/scripts.ts b/packages/uni-cli-shared/src/scripts.ts new file mode 100644 index 000000000..9f7cf1bb2 --- /dev/null +++ b/packages/uni-cli-shared/src/scripts.ts @@ -0,0 +1,44 @@ +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 || {}, + } +} diff --git a/packages/vite-plugin-uni/src/cli/utils.ts b/packages/vite-plugin-uni/src/cli/utils.ts index 5499828b3..7b1aaf3f0 100644 --- a/packages/vite-plugin-uni/src/cli/utils.ts +++ b/packages/vite-plugin-uni/src/cli/utils.ts @@ -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) +} diff --git a/packages/vite-plugin-uni/src/index.ts b/packages/vite-plugin-uni/src/index.ts index 94b013883..b588ee9a8 100644 --- a/packages/vite-plugin-uni/src/index.ts +++ b/packages/vite-plugin-uni/src/index.ts @@ -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[] = [] -- GitLab