Skip to content

Commit

Permalink
wip: uni_modules 编译模式
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed May 6, 2024
1 parent f4ee0ce commit 46f2122
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`uni_modules playground uni-app-x build:app-android 1`] = `"logo.46719607.png"`;

exports[`uni_modules playground uni-app-x build:app-ios 1`] = `
"import { defineComponent, openBlock, createElementBlock, Fragment, createElementVNode, toDisplayString } from 'vue';
import Logo from './test-com1/components/test-com1-1/logo.png';
Expand Down
8 changes: 6 additions & 2 deletions packages/playground/__tests__/uni_modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('uni_modules playground', () => {
// // 'build:mp-weixin',
// ],
'uni-app-x': [
// "build:app-android",
'build:app-android',
'build:app-ios',
'build:h5',
// 'build:mp-alipay',
Expand Down Expand Up @@ -56,7 +56,11 @@ describe('uni_modules playground', () => {
},
})
sync('**/*', { cwd: outDir, absolute: true }).forEach((file) => {
expect(fs.readFileSync(file, 'utf-8')).toMatchSnapshot()
if (file.endsWith('.png')) {
expect(path.basename(file)).toMatchSnapshot()
} else {
expect(fs.readFileSync(file, 'utf-8')).toMatchSnapshot()
}
})
})
})
Expand Down
10 changes: 5 additions & 5 deletions packages/uni-app-uts/src/plugins/android/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
getResolvedOptions,
} from './uvue/descriptorCache'
import { isVue } from './utils'
import { createEncryptCssUrlReplacer } from '@dcloudio/uni-cli-shared'

export function uniAppCssPlugin(): Plugin {
const mainUTS = resolveMainPathOnce(process.env.UNI_INPUT_DIR)
Expand Down Expand Up @@ -89,10 +88,11 @@ export function uniAppCssPlugin(): Plugin {
insertBeforePlugin(
cssPlugin(config, {
isAndroidX: true,
createUrlReplacer:
process.env.UNI_COMPILE_TARGET === 'uni_modules'
? createEncryptCssUrlReplacer
: undefined,
// android 不处理 css url
// createUrlReplacer:
// process.env.UNI_COMPILE_TARGET === 'uni_modules'
// ? createEncryptCssUrlReplacer
// : undefined,
getDescriptor: (filename) => {
return getDescriptor(filename, descriptorOptions, false)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export function getResolvedOptions(): ResolvedOptions {
return {
root: process.env.UNI_INPUT_DIR,
sourceMap:
process.env.UNI_APP_SOURCEMAP === 'true' ||
process.env.NODE_ENV === 'development',
(process.env.UNI_APP_SOURCEMAP === 'true' ||
process.env.NODE_ENV === 'development') &&
process.env.UNI_COMPILE_TARGET !== 'uni_modules',
// eslint-disable-next-line no-restricted-globals
compiler: require('@vue/compiler-sfc'),
targetLanguage: process.env.UNI_UTS_TARGET_LANGUAGE as 'kotlin',
Expand Down
4 changes: 3 additions & 1 deletion packages/uni-app-uts/src/plugins/android/uvue/sfc/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export async function transformMain(
inline: isInline,
className,
rootDir: options.root,
sourceMap: process.env.NODE_ENV === 'development',
sourceMap:
process.env.NODE_ENV === 'development' &&
process.env.UNI_COMPILE_TARGET !== 'uni_modules',
bindingMetadata,
})
)
Expand Down
58 changes: 55 additions & 3 deletions packages/uni-cli-shared/src/uni_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,58 @@ export { ${ids.join(',')} }
`
}

export function parseEncryptUniModulesWithDeps(inputDir: string) {
const modulesDir = path.resolve(inputDir, 'uni_modules')
const uniModules: Record<string, { type: 'uts' | 'other'; deps: string[] }> =
{}
if (fs.existsSync(modulesDir)) {
fs.readdirSync(modulesDir).forEach((uniModuleDir) => {
const uniModuleRootDir = path.resolve(modulesDir, uniModuleDir)
// 判断是否是utssdk加密插件
if (fs.existsSync(path.resolve(uniModuleRootDir, 'encrypt'))) {
const pkgPath = path.resolve(uniModuleRootDir, 'package.json')
if (!fs.existsSync(pkgPath)) {
return
}
try {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
uniModules[uniModuleDir] = {
type: fs.existsSync(path.resolve(uniModuleRootDir, 'utssdk'))
? 'uts'
: 'other',
deps: Array.isArray(pkg.uni_modules.dependencies)
? pkg.uni_modules.dependencies
: [],
}
} catch (e) {}
}
})
}
return uniModules
}

export function parseUniModulesWithoutUTSModules(inputDir: string) {
const modulesDir = path.resolve(inputDir, 'uni_modules')
const uniModules: Record<string, string[]> = {}
if (fs.existsSync(modulesDir)) {
fs.readdirSync(modulesDir).forEach((uniModuleDir) => {
if (
!fs.existsSync(path.resolve(modulesDir, uniModuleDir, 'package.json'))
) {
return
}
// 非utssdk插件
if (fs.existsSync(path.resolve(modulesDir, uniModuleDir, 'utssdk'))) {
return
}
// 解析加密的 easyCom 插件列表
const components = parseEasyComComponents(uniModuleDir, inputDir, false)
uniModules[uniModuleDir] = components
})
}
return uniModules
}

export function parseEncryptUniModules(
inputDir: string,
detectBinary: boolean = true
Expand All @@ -390,7 +442,7 @@ export function parseEncryptUniModules(
return
}
// 解析加密的 easyCom 插件列表
const components = parseEncryptEasyComComponents(
const components = parseEasyComComponents(
uniModuleDir,
inputDir,
detectBinary
Expand All @@ -403,12 +455,12 @@ export function parseEncryptUniModules(
}

/**
* 解析加密的 easyCom 组件列表
* 解析 easyCom 组件列表
* @param pluginId
* @param inputDir
* @returns
*/
export function parseEncryptEasyComComponents(
export function parseEasyComComponents(
pluginId: string,
inputDir: string,
detectBinary = true
Expand Down
74 changes: 71 additions & 3 deletions packages/uni-cli-shared/src/vite/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import type {
} from 'vite'
import {
genEncryptEasyComModuleIndex,
parseEncryptUniModules,
parseUniModulesWithoutUTSModules,
} from '../uni_modules'
import { cleanUrl, normalizePath } from './plugins/vitejs/utils'
import type { CssUrlReplacer } from './plugins/vitejs/plugins/css'
import { resolveUTSCompiler } from '../uts'

export function createEncryptCssUrlReplacer(
resolve: ResolveFn
Expand Down Expand Up @@ -71,12 +72,79 @@ export function uniEncryptUniModulesPlugin(): Plugin {
}
})
},
async writeBundle() {
if (process.env.UNI_UTS_PLATFORM !== 'app-android') {
return
}
// 编译所有 uni_modules 插件
const inputDir = process.env.UNI_INPUT_DIR
const tempOutputDir = uvueOutDir()
const tempUniModulesDir = path.join(tempOutputDir, 'uni_modules')
// 非 uts 插件
const tempUniModules: string[] = []
if (fs.existsSync(tempUniModulesDir)) {
fs.readdirSync(tempUniModulesDir).forEach((uniModuleDir) => {
if (
fs.existsSync(
path.join(tempUniModulesDir, uniModuleDir, 'index.encrypt.uts')
)
) {
tempUniModules.push(uniModuleDir)
}
})
}
const uniModulesDir = path.join(inputDir, 'uni_modules')
// uts 插件
const utsModules: string[] = []
if (fs.existsSync(uniModulesDir)) {
fs.readdirSync(uniModulesDir).forEach((uniModuleDir) => {
// 已经在临时目录
if (tempUniModules.includes(uniModuleDir)) {
return
}
if (
fs.existsSync(path.resolve(uniModulesDir, uniModuleDir, 'utssdk'))
) {
utsModules.push(uniModuleDir)
}
})
}
const compiler = resolveUTSCompiler()
for (const uniModule of tempUniModules) {
const pluginDir = path.resolve(tempUniModulesDir, uniModule)
await compiler.compile(pluginDir, {
isX: process.env.UNI_APP_X === 'true',
isSingleThread: true,
isPlugin: false,
sourceMap: false,
uni_modules: [],
})
}
for (const uniModule of utsModules) {
const pluginDir = path.resolve(uniModulesDir, uniModule)
await compiler.compile(pluginDir, {
isX: process.env.UNI_APP_X === 'true',
isSingleThread: true,
isPlugin: true,
sourceMap: false,
uni_modules: [],
})
}
},
}
}

function uvueOutDir() {
return path.join(process.env.UNI_OUTPUT_DIR, '../.uvue')
}

function createExternal(config: ResolvedConfig) {
return function external(source) {
if (config.assetsInclude(cleanUrl(source))) {
if (
// android 平台需要编译 assets 资源
process.env.UNI_UTS_PLATFORM !== 'app-android' &&
config.assetsInclude(cleanUrl(source))
) {
return true
}
if (
Expand Down Expand Up @@ -119,7 +187,7 @@ function hasIndexFile(uniModuleDir: string) {
}

function initEncryptUniModulesBuildOptions(inputDir: string): BuildOptions {
const modules = parseEncryptUniModules(inputDir, false)
const modules = parseUniModulesWithoutUTSModules(inputDir)
const moduleNames = Object.keys(modules)
if (!moduleNames.length) {
throw new Error('No encrypt uni_modules found')
Expand Down
7 changes: 5 additions & 2 deletions packages/uni-uts-v1/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,18 @@ export async function compile(
typeParams: [],
components: [],
}
let moduleName = pkg.id
try {
moduleName = require(join(pluginDir, 'package.json')).displayName
} catch (e) {}
const proxyCodeOptions: GenProxyCodeOptions = extend(
{
androidComponents,
iosComponents,
format:
process.env.UNI_UTS_JS_CODE_FORMAT === 'cjs' ? FORMATS.CJS : FORMATS.ES,
pluginRelativeDir,
moduleName:
require(join(pluginDir, 'package.json')).displayName || pkg.id,
moduleName,
moduleType: process.env.UNI_UTS_MODULE_TYPE || '',
meta,
inputDir,
Expand Down

0 comments on commit 46f2122

Please sign in to comment.