Skip to content

Commit

Permalink
wip(uts): buildUniModules 支持 transformVueFile
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Sep 6, 2024
1 parent 7509ecb commit da80561
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/uni-app-uts/src/extApiComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { parse } from '@dcloudio/uni-nvue-styler'

import { transformMain as transformAndroid } from './plugins/android/uvue/sfc/main'

export async function transformExtApiComponent(
export async function transformExtApiVueFile(
platform: 'app-android' | 'app-ios', // | 'app-harmony',
vueFileName: string
) {
Expand Down Expand Up @@ -41,7 +41,7 @@ async function transformAppAndroidExtApiComponent(
classNamePrefix: 'Uni',
genDefaultAs: '__sfc__',
sourceMap: false,
componentType: 'app',
componentType: 'component',
})
if (!result) {
return null
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-app-uts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export { genUTSClassName as genClassName } from '@dcloudio/uni-cli-shared'

export { transformMain as transformVue } from './plugins/android/uvue/sfc/main'

export { transformExtApiComponent } from './extApiComponents'
export { transformExtApiVueFile } from './extApiComponents'
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function createUniModulesSyncFilePreprocessor(
return preprocess(htmlCode, preContext, { type: 'html' })
}

return (content, fileName) => {
return async (content, fileName) => {
const extname = path.extname(fileName)
if (extname === '.json') {
return dataToEsm(JSON.parse(preJs(content)), {
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-uts-v1/__tests__/tsc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('uni_modules', () => {
platform === 'app-android'
? createUniXKotlinCompilerOnce()
: createUniXSwiftCompilerOnce(),
(content) => {
async (content) => {
return content
}
)
Expand Down
67 changes: 63 additions & 4 deletions packages/uni-uts-v1/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extend, isArray } from '@vue/shared'
import { join, relative, resolve } from 'path'
import { extname, join, relative, resolve } from 'path'

import { resolveAndroidDepFiles } from './kotlin'
import { resolveIOSDepFiles } from './swift'
Expand Down Expand Up @@ -43,6 +43,7 @@ import { getCompiler } from './compiler'
import { uvueOutDir } from './uvue/index'
import {
type SyncUniModulesFilePreprocessor,
type UniXCompilerPlatform,
compileUniModuleWithTsc,
createUniXArkTSCompilerOnce,
createUniXKotlinCompilerOnce,
Expand Down Expand Up @@ -656,6 +657,7 @@ function emptyDir(dir: string) {

/**
* 发行模式(会自动清理临时目录)
* 目前仅 ext-api 框架的地方使用了
* @param platform
* @param pluginDir
* @param param2
Expand All @@ -664,18 +666,28 @@ function emptyDir(dir: string) {
export async function buildUniModules(
platform: 'app' | 'app-android' | 'app-ios' | 'app-harmony',
pluginDir: string,
{
syncUniModulesFilePreprocessors,
}: {
options: {
syncUniModulesFilePreprocessors: {
android: SyncUniModulesFilePreprocessor
ios: SyncUniModulesFilePreprocessor
harmony: SyncUniModulesFilePreprocessor
}
transformVueFile?: (
platform: UniXCompilerPlatform,
fileName: string
) => Promise<string>
},
compilerOptions: UTSPluginCompilerOptions
) {
const inputDir = process.env.UNI_INPUT_DIR

const syncUniModulesFilePreprocessors = options.transformVueFile
? patchSyncUniModulesFilePreprocessors(
options.syncUniModulesFilePreprocessors,
options.transformVueFile
)
: options.syncUniModulesFilePreprocessors

if (platform === 'app-android' || platform === 'app') {
const platform = 'app-android'
emptyCacheDir(platform, inputDir, pluginDir)
Expand Down Expand Up @@ -709,4 +721,51 @@ export async function buildUniModules(
return compile(pluginDir, compilerOptions)
}

function patchSyncUniModulesFilePreprocessors(
syncUniModulesFilePreprocessors: {
android: SyncUniModulesFilePreprocessor
ios: SyncUniModulesFilePreprocessor
harmony: SyncUniModulesFilePreprocessor
},
transformVueFile: (
platform: UniXCompilerPlatform,
fileName: string
) => Promise<string>
) {
return {
android: patchSyncUniModulesFilePreprocessor(
'app-android',
syncUniModulesFilePreprocessors.android,
transformVueFile
),
ios: patchSyncUniModulesFilePreprocessor(
'app-ios',
syncUniModulesFilePreprocessors.ios,
transformVueFile
),
harmony: patchSyncUniModulesFilePreprocessor(
'app-harmony',
syncUniModulesFilePreprocessors.harmony,
transformVueFile
),
}
}

function patchSyncUniModulesFilePreprocessor(
platform: UniXCompilerPlatform,
preprocessor: SyncUniModulesFilePreprocessor,
transformVueFile: (
platform: UniXCompilerPlatform,
fileName: string
) => Promise<string>
) {
return async (content: string, fileName: string) => {
const fileExtname = extname(fileName)
if (['.vue', '.uvue'].includes(fileExtname)) {
return transformVueFile(platform, fileName)
}
return preprocessor(content, fileName)
}
}

export * from './uni_modules'
9 changes: 6 additions & 3 deletions packages/uni-uts-v1/src/uni_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ function resolveUniModuleGlobs() {
// test-uts/common/**/*
`common/**/*${extname}`,
`utssdk/**/*${extname}`,
`components/**/*`,
]
return globs
}
Expand Down Expand Up @@ -304,7 +305,7 @@ async function syncUniModulesFiles(
export type SyncUniModulesFilePreprocessor = (
code: string,
fileName: string
) => string
) => Promise<string>

async function syncUniModulesFile(
relativeFileName: string,
Expand All @@ -316,7 +317,7 @@ async function syncUniModulesFile(
const src = path.resolve(pluginDir, relativeFileName)
if (rename) {
const extname = path.extname(relativeFileName)
if (['.uts', '.json'].includes(extname)) {
if (['.uts', '.json', '.vue', '.uvue'].includes(extname)) {
// test.uts => test.uts.ts
// test.json => test.json.ts
return writeFile(
Expand All @@ -342,7 +343,9 @@ async function writeFile(
return
}
utsModuleFileCaches.set(key, stat.mtimeMs)
return fs.outputFile(dest, preprocessor(fs.readFileSync(src, 'utf-8'), src))
return preprocessor(fs.readFileSync(src, 'utf-8'), src).then((content) =>
fs.outputFile(dest, content)
)
}

async function copyFile(src: string, dest: string) {
Expand Down

0 comments on commit da80561

Please sign in to comment.