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 4, 2024
1 parent b66f12b commit c2cc53d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
5 changes: 5 additions & 0 deletions packages/uni-app-uts/src/plugins/android/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 @@ -88,6 +89,10 @@ export function uniAppCssPlugin(): Plugin {
insertBeforePlugin(
cssPlugin(config, {
isAndroidX: true,
createUrlReplacer:
process.env.UNI_COMPILE_TARGET === 'uni_modules'
? createEncryptCssUrlReplacer
: undefined,
getDescriptor: (filename) => {
return getDescriptor(filename, descriptorOptions, false)
},
Expand Down
34 changes: 22 additions & 12 deletions packages/uni-app-uts/src/plugins/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import {
parseUniExtApiNamespacesOnce,
resolveUTSCompiler,
uniEncryptUniModulesPlugin,
uniUTSUniModulesPlugin,
uniViteSfcSrcImportPlugin,
} from '@dcloudio/uni-cli-shared'
Expand All @@ -18,19 +19,28 @@ import { parseImports, parseUTSRelativeFilename } from './utils'
export function init() {
return [
uniPrePlugin(),
uniUTSUniModulesPlugin({
x: true,
isSingleThread: process.env.UNI_APP_X_SINGLE_THREAD !== 'false',
extApis: parseUniExtApiNamespacesOnce(
process.env.UNI_UTS_PLATFORM,
process.env.UNI_UTS_TARGET_LANGUAGE
),
}),
...(process.env.UNI_COMPILE_TARGET === 'uni_modules'
? []
: [
uniUTSUniModulesPlugin({
x: true,
isSingleThread: process.env.UNI_APP_X_SINGLE_THREAD !== 'false',
extApis: parseUniExtApiNamespacesOnce(
process.env.UNI_UTS_PLATFORM,
process.env.UNI_UTS_TARGET_LANGUAGE
),
}),
]),
uniAppPlugin(),
// 需要放到 uniAppPlugin 之后(TSC模式无需),让 uniAppPlugin 先 emit 出真实的 main.uts,然后 MainPlugin 再返回仅包含 import 的 js code
uniAppMainPlugin(),
uniAppManifestPlugin(),
uniAppPagesPlugin(),
...(process.env.UNI_COMPILE_TARGET === 'uni_modules'
? [uniEncryptUniModulesPlugin()]
: [
// 需要放到 uniAppPlugin 之后(TSC模式无需),让 uniAppPlugin 先 emit 出真实的 main.uts,然后 MainPlugin 再返回仅包含 import 的 js code
uniAppMainPlugin(),
uniAppManifestPlugin(),
uniAppPagesPlugin(),
]),

uniAppCssPlugin(),
// 解决所有的src引入
uniViteSfcSrcImportPlugin({ onlyVue: false }),
Expand Down
6 changes: 6 additions & 0 deletions packages/uni-app-uts/src/plugins/android/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ export function uniAppPlugin(): UniVitePlugin {
return code
},
generateBundle(_, bundle) {
if (process.env.UNI_COMPILE_TARGET === 'uni_modules') {
return
}
// 开发者仅在 script 中引入了 easyCom 类型,但模板里边没用到,此时额外生成一个辅助的.uvue文件
checkUTSEasyComAutoImports(inputDir, bundle, this)
},
async writeBundle() {
if (process.env.UNI_COMPILE_TARGET === 'uni_modules') {
return
}
let pageCount = 0
if (isFirst) {
isFirst = false
Expand Down
17 changes: 10 additions & 7 deletions packages/uni-cli-shared/src/vite/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ export function uniEncryptUniModulesPlugin(): Plugin {
generateBundle(_, bundle) {
Object.keys(bundle).forEach((fileName) => {
if (fileName.endsWith('.encrypt.js')) {
const newFileName =
'uni_modules/' +
fileName.replace('.encrypt.js', '/index.encrypt.js')
bundle[newFileName] = bundle[fileName]
bundle[newFileName].fileName = newFileName
// app-android 不需要 js
if (process.env.UNI_UTS_PLATFORM !== 'app-android') {
const newFileName =
'uni_modules/' +
fileName.replace('.encrypt.js', '/index.encrypt.js')
bundle[newFileName] = bundle[fileName]
bundle[newFileName].fileName = newFileName
}
delete bundle[fileName]
}
})
Expand Down Expand Up @@ -128,7 +131,7 @@ function initEncryptUniModulesBuildOptions(inputDir: string): BuildOptions {
inputDir,
'uni_modules',
module,
'index.encrypt.js'
'index.encrypt.uts'
)
const codes: string[] = []
if (hasIndexFile(path.resolve(inputDir, 'uni_modules', module))) {
Expand All @@ -147,7 +150,7 @@ function initEncryptUniModulesBuildOptions(inputDir: string): BuildOptions {
return {
lib: false, // 不使用 lib 模式,lib模式会直接内联资源
cssCodeSplit: false,
outDir: process.env.UNI_OUTPUT_DIR,
// outDir: process.env.UNI_OUTPUT_DIR,
rollupOptions: {
preserveEntrySignatures: 'strict',
input,
Expand Down

0 comments on commit c2cc53d

Please sign in to comment.