Skip to content

Commit

Permalink
wip(x): ext-api
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Sep 10, 2024
1 parent 03f1b67 commit 04964ac
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/shims-node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ declare namespace NodeJS {
UNI_APP_HARMONY_PROJECT_PATH?: string

UNI_COMPILE_EXT_API_INPUT?: string
UNI_APP_NEXT_WORKSPACE?: string
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/uni-app-uts/src/plugins/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { uniCloudPlugin } from './unicloud'

export function init() {
return [
uniDecryptUniModulesPlugin(),
...(isNormalCompileTarget() ? [uniDecryptUniModulesPlugin()] : []),
uniPrePlugin(),
...(isNormalCompileTarget()
? [
Expand Down
17 changes: 11 additions & 6 deletions packages/uni-app-uts/src/plugins/android/unicloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import { ENTRY_FILENAME, getUniCloudSpaceList } from './utils'
const uniCloudSpaceList = getUniCloudSpaceList()

export function uniCloudPlugin(): Plugin {
addUTSEasyComAutoImports(
normalizePath(
path.resolve(resolveComponentsLibPath(), 'unicloud-db', 'index.uts')
),
['mixinDatacom', 'uniCloudMixinDatacom']
)
if (
process.env.UNI_COMPILE_TARGET !== 'ext-api' &&
process.env.UNI_APP_NEXT_WORKSPACE
) {
addUTSEasyComAutoImports(
normalizePath(
path.resolve(resolveComponentsLibPath(), 'unicloud-db', 'index.uts')
),
['mixinDatacom', 'uniCloudMixinDatacom']
)
}
return {
name: 'uni:app-unicloud',
apply: 'build',
Expand Down
3 changes: 2 additions & 1 deletion packages/uni-app-uts/src/plugins/ios/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path'
import {
UNI_EASYCOM_EXCLUDE,
isNormalCompileTarget,
parseUniExtApiNamespacesOnce,
resolveUTSCompiler,
uniDecryptUniModulesPlugin,
Expand All @@ -22,7 +23,7 @@ import * as uniCliShared from '@dcloudio/uni-cli-shared'

export function init() {
return [
uniDecryptUniModulesPlugin(),
...(isNormalCompileTarget() ? [uniDecryptUniModulesPlugin()] : []),
uniHBuilderXConsolePlugin('uni.__log__'),
uniUTSAppUniModulesPlugin({
x: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/uni-cli-shared/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,9 @@ export function resolveComponentsLibPath() {
}
return componentsLibPath
}

export function resolveComponentsLibDirs() {
return process.env.UNI_COMPILE_TARGET === 'ext-api'
? []
: [resolveComponentsLibPath()]
}
20 changes: 20 additions & 0 deletions packages/uni-cli-shared/src/uts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ function resolveUTSFile(

export function resolveUTSCompiler(): typeof UTSCompiler {
let compilerPath: string = ''
if (
process.env.UNI_COMPILE_TARGET === 'ext-api' &&
process.env.UNI_APP_NEXT_WORKSPACE
) {
return require(path.resolve(
process.env.UNI_APP_NEXT_WORKSPACE,
'packages/uni-uts-v1'
))
}
if (isInHBuilderX()) {
try {
compilerPath = require.resolve(
Expand Down Expand Up @@ -565,3 +574,14 @@ export function uvueOutDir(
export function tscOutDir(platform: 'app-android' | 'app-ios' | 'app-harmony') {
return path.join(process.env.UNI_APP_X_TSC_DIR, platform)
}

const UTSProxyRE = /\?uts-proxy$/
const UniHelpersRE = /\?uni_helpers$/

export function isUTSProxy(id: string) {
return UTSProxyRE.test(id)
}

export function isUniHelpers(id: string) {
return UniHelpersRE.test(id)
}
11 changes: 11 additions & 0 deletions packages/uni-cli-shared/src/vite/extApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Plugin } from 'vite'
import { isUTSProxy } from '../uts'

export function uniUniModulesExtApiPlugin(): Plugin {
return {
Expand All @@ -21,5 +22,15 @@ export function uniUniModulesExtApiPlugin(): Plugin {
},
}
},
load(id) {
if (isUTSProxy(id)) {
return ''
}
},
generateBundle(_, bundle) {
Object.keys(bundle).forEach((fileName) => {
console.log('fileName', fileName)
})
},
}
}
13 changes: 2 additions & 11 deletions packages/uni-cli-shared/src/vite/plugins/uts/uni_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
genUniExtApiDeclarationFileOnce,
initUTSKotlinAutoImportsOnce,
initUTSSwiftAutoImportsOnce,
isUTSProxy,
isUniHelpers,
parseKotlinPackageWithPluginId,
parseSwiftPackageWithPluginId,
resolveUTSAppModule,
Expand All @@ -34,9 +36,6 @@ import { initScopedPreContext } from '../../../preprocess/context'
/* eslint-disable no-restricted-globals */
const { preprocess } = require('../../../../lib/preprocess')

const UTSProxyRE = /\?uts-proxy$/
const UniHelpersRE = /\?uni_helpers$/

function createUniModulesSyncFilePreprocessor(
platform: UniApp.PLATFORM,
utsPlatform: 'app-android' | 'app-ios' | 'app-harmony',
Expand Down Expand Up @@ -130,14 +129,6 @@ const createUniXAppHarmonyUniModulesSyncFilePreprocessorOnce = once(() => {
return createUniModulesSyncFilePreprocessor('app', 'app-harmony', true)
})

function isUTSProxy(id: string) {
return UTSProxyRE.test(id)
}

function isUniHelpers(id: string) {
return UniHelpersRE.test(id)
}

const utsModuleCaches = new Map<
string,
() => Promise<void | {
Expand Down
4 changes: 2 additions & 2 deletions packages/uni-h5-vite/src/plugin/handleHotUpdate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
parseManifestJson,
parsePagesJson,
resolveBuiltIn,
resolveComponentsLibPath,
resolveComponentsLibDirs,
} from '@dcloudio/uni-cli-shared'

const debugHmr = debug('uni:hmr')
Expand Down Expand Up @@ -72,7 +72,7 @@ export function createHandleHotUpdate(): Plugin['handleHotUpdate'] {
if (isPagesJson) {
const easycom = pagesJson.easycom || {}
const { options, refresh } = initEasycomsOnce(inputDir, {
dirs: [resolveComponentsLibPath()],
dirs: resolveComponentsLibDirs(),
platform,
isX: process.env.UNI_APP_X === 'true',
})
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-uni/src/cli/uvue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
output,
parseManifestJsonOnce,
resetOutput,
resolveComponentsLibPath,
resolveComponentsLibDirs,
} from '@dcloudio/uni-cli-shared'
import type { RollupWatcher } from 'rollup'

Expand Down Expand Up @@ -97,7 +97,7 @@ export async function runUVueAndroidDev(options: CliOptions & ServerOptions) {
export async function runUVueAndroidBuild(options: CliOptions & BuildOptions) {
try {
initEasycomsOnce(process.env.UNI_INPUT_DIR, {
dirs: [resolveComponentsLibPath()],
dirs: resolveComponentsLibDirs(),
platform: process.env.UNI_PLATFORM,
isX: true,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-uni/src/config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { UserConfig } from 'vite'
import {
cssTarget,
initEasycomsOnce,
resolveComponentsLibPath,
resolveComponentsLibDirs,
} from '@dcloudio/uni-cli-shared'
import type { VitePluginUniResolvedOptions } from '..'
import { hasOwn, isArray } from '@vue/shared'
Expand All @@ -12,7 +12,7 @@ export function createBuild(
config: UserConfig
): UserConfig['build'] {
initEasycomsOnce(options.inputDir, {
dirs: [resolveComponentsLibPath()],
dirs: resolveComponentsLibDirs(),
platform: process.env.UNI_PLATFORM,
isX: process.env.UNI_APP_X === 'true',
})
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-uni/src/utils/easycom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { debounce } from '@dcloudio/uni-shared'
import {
chokidar,
initEasycomsOnce,
resolveComponentsLibPath,
resolveComponentsLibDirs,
} from '@dcloudio/uni-cli-shared'

const debugEasycom = debug('uni:easycom')
export const initEasycom = (watcher?: FSWatcher) => {
const { filter, refresh, options } = initEasycomsOnce(
process.env.UNI_INPUT_DIR,
{
dirs: [resolveComponentsLibPath()],
dirs: resolveComponentsLibDirs(),
platform: process.env.UNI_PLATFORM,
isX: process.env.UNI_APP_X === 'true',
}
Expand Down
92 changes: 76 additions & 16 deletions packages/vite-plugin-uni/src/utils/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import fs from 'fs-extra'
import type { Plugin } from 'vite'
import { extend, isArray, isFunction, isString } from '@vue/shared'
import {
Expand All @@ -13,6 +14,7 @@ import type { VitePluginUniResolvedOptions } from '..'
interface PluginConfig {
id: string
name: string
pluginPath: string
apply?: UniApp.PLATFORM | UniApp.PLATFORM[]
uvue?: boolean
config: {
Expand Down Expand Up @@ -92,20 +94,24 @@ export function initExtraPlugins(
) {
return initPlugins(
cliRoot,
resolvePlugins(cliRoot, platform, options.uvue),
process.env.UNI_COMPILE_TARGET === 'ext-api' &&
process.env.UNI_APP_NEXT_WORKSPACE
? resolvePluginsByWorkSpace(
process.env.UNI_APP_NEXT_WORKSPACE,
platform,
options.uvue
)
: resolvePluginsByCliRoot(cliRoot, platform, options.uvue),
options
)
}

function initPlugin(
cliRoot: string,
{ id, config: { main } }: PluginConfig,
_cliRoot: string,
{ pluginPath, config: { main } }: PluginConfig,
options: VitePluginUniResolvedOptions
): Plugin | void {
let plugin = require(require.resolve(
path.join(id, main || '/lib/uni.plugin.js'),
{ paths: [cliRoot] }
))
let plugin = require(path.join(pluginPath, main || '/lib/uni.plugin.js'))
plugin = plugin.default || plugin
if (isFunction(plugin)) {
plugin = plugin(options)
Expand All @@ -131,20 +137,73 @@ function initPlugins(
.flat()
}

function resolvePlugins(
interface Pkg {
name: string
pluginPath: string
'uni-app'?: PluginConfig
}

function resolvePluginsByCliRoot(
cliRoot: string,
platform: UniApp.PLATFORM,
uvue: boolean = false
) {
const pkg = require(path.join(cliRoot, 'package.json'))
return Object.keys(pkg.devDependencies || {})
.concat(Object.keys(pkg.dependencies || {}))
.map<PluginConfig | void>((id) => {
return resolvePlugins(
Object.keys(pkg.devDependencies || {})
.concat(Object.keys(pkg.dependencies || {}))
.map((id) => {
try {
const pkgFileName = require.resolve(id + '/package.json', {
paths: [cliRoot],
})
const pkg = require(pkgFileName)
return {
...pkg,
pluginPath: path.dirname(pkgFileName),
}
} catch (e) {}
})
.filter<Pkg>(Boolean as any),
platform,
uvue
)
}

function resolvePluginsByWorkSpace(
workspaceFolder: string,
platform: UniApp.PLATFORM,
uvue: boolean = false
) {
const pkgDirs = path.resolve(workspaceFolder, 'packages')
return resolvePlugins(
fs
.readdirSync(pkgDirs)
.map((dir) => {
const pluginPath = path.join(pkgDirs, dir)
const pkgFileName = path.join(pluginPath, 'package.json')
if (fs.existsSync(pkgFileName)) {
return {
...require(pkgFileName),
pluginPath,
} as Pkg
}
})
.filter<Pkg>(Boolean as any),
platform,
uvue
)
}

function resolvePlugins(
pkgs: Pkg[],
platform: UniApp.PLATFORM,
uvue: boolean = false
) {
return pkgs
.map<PluginConfig | void>((pkg) => {
try {
const pluginPkg = require(require.resolve(id + '/package.json', {
paths: [cliRoot],
}))
const config = pluginPkg['uni-app'] as PluginConfig
const config = pkg['uni-app'] as PluginConfig
if (!config || !config.name) {
return
}
Expand All @@ -165,8 +224,9 @@ function resolvePlugins(
return
}
return {
id,
id: pkg.name,
name: config.name,
pluginPath: pkg.pluginPath,
config,
}
} catch (e) {}
Expand Down

0 comments on commit 04964ac

Please sign in to comment.