From 0be34a200edb716e6f545542c0215156c248db41 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 23 Nov 2022 18:01:30 +0800 Subject: [PATCH] wip(uts): compiler --- .../uts/uni_modules/test-uts/manifest.json | 3 + .../uts/uni_modules/test-uts/manifest.json | 3 + .../uni-uts-v1/__tests__/manifest.spec.ts | 128 +++++++++++++++++- .../uni-uts-v1/__tests__/sourceMap.spec.ts | 8 -- packages/uni-uts-v1/src/index.ts | 40 +++++- packages/uni-uts-v1/src/kotlin.ts | 2 +- packages/uni-uts-v1/src/manifest/index.ts | 2 + packages/uni-uts-v1/src/manifest/manifest.ts | 10 +- packages/uni-uts-v1/src/manifest/sourceMap.ts | 109 +++++++++++++++ packages/uni-uts-v1/src/swift.ts | 2 +- packages/uni-uts-v1/src/utils.ts | 7 +- 11 files changed, 285 insertions(+), 29 deletions(-) create mode 100644 packages/uni-uts-v1/src/manifest/sourceMap.ts diff --git a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/uni_modules/test-uts/manifest.json b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/uni_modules/test-uts/manifest.json index bb1c36bbfd..60197e1c4a 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/uni_modules/test-uts/manifest.json +++ b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/uni_modules/test-uts/manifest.json @@ -10,6 +10,9 @@ "utssdk/common/utils.uts": { "md5": "d41d8cd98f00b204e9800998ecf8427e" }, + "utssdk/index.uts": { + "md5": "d41d8cd98f00b204e9800998ecf8427e" + }, "package.json": { "md5": "9595031a0d4158abb72060cdf3a200c1" }, diff --git a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/uni_modules/test-uts/manifest.json b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/uni_modules/test-uts/manifest.json index d708a01f89..54e6a74f16 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/uni_modules/test-uts/manifest.json +++ b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/uni_modules/test-uts/manifest.json @@ -10,6 +10,9 @@ "utssdk/common/utils.uts": { "md5": "d41d8cd98f00b204e9800998ecf8427e" }, + "utssdk/index.uts": { + "md5": "d41d8cd98f00b204e9800998ecf8427e" + }, "package.json": { "md5": "9595031a0d4158abb72060cdf3a200c1" }, diff --git a/packages/uni-uts-v1/__tests__/manifest.spec.ts b/packages/uni-uts-v1/__tests__/manifest.spec.ts index d544ac0a2b..f9d03a36ad 100644 --- a/packages/uni-uts-v1/__tests__/manifest.spec.ts +++ b/packages/uni-uts-v1/__tests__/manifest.spec.ts @@ -1,10 +1,15 @@ -import { resolve } from 'path' +import path, { resolve } from 'path' import { checkManifest, + genManifestFile, resolveManifestJson, resolvePluginFiles, } from '../src/manifest/manifest' import { checkKotlinCompile, checkSwiftCompile } from '../src/manifest/index' +import { + resolveSourceMapCacheFilename, + resolveSourceMapFilename, +} from '../src/manifest/sourceMap' const pluginModuleDir = resolve(__dirname, 'examples/uts/uni_modules/test-uts') const pluginDir = resolve(__dirname, 'examples/uts/utssdk/test-uts') @@ -45,6 +50,7 @@ describe('manifest', () => { await resolvePluginFiles('app-android', pluginModuleDir, true) ).toEqual([ 'package.json', + 'utssdk/index.uts', 'utssdk/common/utils.uts', 'utssdk/app-android/index.uts', 'utssdk/app-android/assets/test.json', @@ -59,16 +65,17 @@ describe('manifest', () => { ]) expect(await resolvePluginFiles('app-ios', pluginModuleDir, true)).toEqual([ 'package.json', + 'utssdk/index.uts', 'utssdk/common/utils.uts', 'utssdk/app-ios/index.uts', 'utssdk/app-ios/assets/test.json', ]) }) test('check manifest', async () => { - // await genManifestFile('app-android',pluginModuleOptions) - // await genManifestFile('app-ios',pluginModuleOptions) - // await genManifestFile('app-android',pluginOptions) - // await genManifestFile('app-ios',pluginOptions) + await genManifestFile('app-android', pluginModuleOptions) + await genManifestFile('app-ios', pluginModuleOptions) + await genManifestFile('app-android', pluginOptions) + await genManifestFile('app-ios', pluginOptions) const manifest = resolveManifestJson( 'app-android', pluginModuleOptions.pluginRelativeDir, @@ -102,7 +109,7 @@ describe('manifest', () => { test('gen android manifest', async () => { const res = await checkKotlinCompile('standard', pluginModuleOptions) expect(res.expired).toBe(false) - expect(res.files.length).toBe(4) + expect(res.files.length).toBe(5) expect(res.tips).toBeTruthy() const res1 = await checkKotlinCompile('standard', pluginOptions) @@ -113,11 +120,118 @@ describe('manifest', () => { test('gen ios manifest', async () => { const res = await checkSwiftCompile('standard', pluginModuleOptions) expect(res.expired).toBe(false) - expect(res.files.length).toBe(4) + expect(res.files.length).toBe(5) expect(res.tips).toBe('') const res1 = await checkSwiftCompile('standard', pluginOptions) expect(res1.expired).toBe(false) expect(res1.files.length).toBe(4) expect(res1.tips).toBe('') }) + test('sourcemap', () => { + expect( + resolveSourceMapFilename( + 'app-android', + pluginModuleOptions.pluginRelativeDir, + outputDir, + true + ).endsWith( + path.join( + '.sourcemap', + 'app', + 'uni_modules', + pluginModuleOptions.id, + 'utssdk', + 'app-android', + 'index.kt.map' + ) + ) + ).toBe(true) + + expect( + resolveSourceMapFilename( + 'app-ios', + pluginModuleOptions.pluginRelativeDir, + outputDir, + true + ).endsWith( + path.join( + '.sourcemap', + 'app', + 'uni_modules', + pluginModuleOptions.id, + 'utssdk', + 'app-ios', + 'index.swift.map' + ) + ) + ).toBe(true) + + expect( + resolveSourceMapCacheFilename( + 'app-android', + cacheDir, + pluginModuleOptions.pluginRelativeDir + ).endsWith(path.join('uni_modules', 'test-uts', 'index.kt.map')) + ).toBe(true) + + expect( + resolveSourceMapCacheFilename( + 'app-ios', + cacheDir, + pluginModuleOptions.pluginRelativeDir + ).endsWith(path.join('uni_modules', 'test-uts', 'index.swift.map')) + ).toBe(true) + + expect( + resolveSourceMapFilename( + 'app-android', + pluginOptions.pluginRelativeDir, + outputDir, + false + ).endsWith( + path.join( + '.sourcemap', + 'app', + 'utssdk', + pluginOptions.id, + 'app-android', + 'index.kt.map' + ) + ) + ).toBe(true) + + expect( + resolveSourceMapFilename( + 'app-ios', + pluginOptions.pluginRelativeDir, + outputDir, + false + ).endsWith( + path.join( + '.sourcemap', + 'app', + 'utssdk', + pluginOptions.id, + 'app-ios', + 'index.swift.map' + ) + ) + ).toBe(true) + + expect( + resolveSourceMapCacheFilename( + 'app-android', + cacheDir, + pluginOptions.pluginRelativeDir + ).endsWith(path.join('utssdk', 'test-uts', 'index.kt.map')) + ).toBe(true) + + expect( + resolveSourceMapCacheFilename( + 'app-ios', + cacheDir, + pluginOptions.pluginRelativeDir + ).endsWith(path.join('utssdk', 'test-uts', 'index.swift.map')) + ).toBe(true) + }) }) diff --git a/packages/uni-uts-v1/__tests__/sourceMap.spec.ts b/packages/uni-uts-v1/__tests__/sourceMap.spec.ts index 82bf64510b..53a0a66ced 100644 --- a/packages/uni-uts-v1/__tests__/sourceMap.spec.ts +++ b/packages/uni-uts-v1/__tests__/sourceMap.spec.ts @@ -22,7 +22,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() expect( resolveUtsPluginSourceMapFile( @@ -53,7 +52,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() }) @@ -64,7 +62,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() }) test('resolveUtsPluginSourceMapFile with utssdk uts=>swift', () => { @@ -74,7 +71,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() }) @@ -85,7 +81,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() }) test('resolveUtsPluginSourceMapFile with uni_modules swift', () => { @@ -95,7 +90,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() }) test('resolveUtsPluginSourceMapFile with utssdk kt', () => { @@ -105,7 +99,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() }) test('resolveUtsPluginSourceMapFile with utssdk swift', () => { @@ -115,7 +108,6 @@ describe('uts:sourceMap', () => { inputDir, outputDir ) - console.log(sourceMapFile) expect(sourceMapFile).toBeDefined() }) test('generatedPositionFor', async () => { diff --git a/packages/uni-uts-v1/src/index.ts b/packages/uni-uts-v1/src/index.ts index ddc5dc149a..586f60daa9 100644 --- a/packages/uni-uts-v1/src/index.ts +++ b/packages/uni-uts-v1/src/index.ts @@ -14,7 +14,13 @@ import { generateCodeFrameWithKotlinStacktrace, generateCodeFrameWithSwiftStacktrace, } from './legacy' -import { checkCompile, genManifestFile, initCheckOptionsEnv } from './manifest' +import { + checkCompile, + genManifestFile, + initCheckOptionsEnv, + restoreSourceMap, + storeSourceMap, +} from './manifest' export const sourcemap = { generateCodeFrameWithKotlinStacktrace, @@ -90,14 +96,14 @@ export async function compile(pluginDir: string) { if (isWindows) { process.env.UNI_UTS_TIPS = `iOS手机在windows上真机运行时uts插件代码修改需提交云端打包自定义基座才能生效` return { - code, + code: parseErrMsg(code, errMsg), deps, } } if (process.env.HX_USE_BASE_TYPE === 'standard') { process.env.UNI_UTS_TIPS = `iOS手机在标准基座真机或模拟器运行暂不支持uts插件,如需调用uts插件请使用自定义基座` return { - code, + code: parseErrMsg(code, errMsg), deps, } } @@ -106,7 +112,8 @@ export async function compile(pluginDir: string) { // dev 模式 if (cacheDir) { // 检查缓存 - let start = Date.now() + // let start = Date.now() + // console.log('uts插件[' + pkg.id + ']start', start) const res = await checkCompile( utsPlatform, process.env.HX_USE_BASE_TYPE, @@ -120,9 +127,11 @@ export async function compile(pluginDir: string) { is_uni_modules: pkg.is_uni_modules, } ) - console.log('uts插件[' + pkg.id + ']缓存检查耗时:', Date.now() - start) + // console.log('uts插件[' + pkg.id + ']end', Date.now()) + // console.log('uts插件[' + pkg.id + ']缓存检查耗时:', Date.now() - start) if (!res.expired) { if (utsPlatform === 'app-android') { + // 拷贝 dex const cacheFile = resolveDexCacheFile(pluginRelativeDir, outputDir) if (cacheFile) { copySync( @@ -131,12 +140,22 @@ export async function compile(pluginDir: string) { ) } } + + // 还原 sourcemap + restoreSourceMap( + utsPlatform, + pluginRelativeDir, + outputDir, + cacheDir, + pkg.is_uni_modules + ) + if (res.tips) { console.warn(res.tips) } return { - code, + code: parseErrMsg(code, errMsg), // 所有文件加入依赖 deps: res.files.map((name) => join(pluginDir, name)), } @@ -188,6 +207,15 @@ export async function compile(pluginDir: string) { } // 生成缓存文件 if (cacheDir && isSuccess) { + // 存储 sourcemap + storeSourceMap( + utsPlatform, + pluginRelativeDir, + outputDir, + cacheDir, + pkg.is_uni_modules + ) + // 生成 manifest genManifestFile(utsPlatform, { pluginDir, env, diff --git a/packages/uni-uts-v1/src/kotlin.ts b/packages/uni-uts-v1/src/kotlin.ts index a84cfe44cb..cf55d733fe 100644 --- a/packages/uni-uts-v1/src/kotlin.ts +++ b/packages/uni-uts-v1/src/kotlin.ts @@ -306,7 +306,7 @@ export async function compile( isPlugin: true, outDir: outputDir, package: parseKotlinPackage(filename).package, - sourceMap: sourceMap ? resolveUTSSourceMapPath(filename) : false, + sourceMap: sourceMap ? resolveUTSSourceMapPath() : false, extname: 'kt', imports, logFilename: true, diff --git a/packages/uni-uts-v1/src/manifest/index.ts b/packages/uni-uts-v1/src/manifest/index.ts index 482217b7d8..20d6faa9f3 100644 --- a/packages/uni-uts-v1/src/manifest/index.ts +++ b/packages/uni-uts-v1/src/manifest/index.ts @@ -15,6 +15,8 @@ import { export { genManifestFile } from './manifest' +export { restoreSourceMap, storeSourceMap } from './sourceMap' + interface PlatformOptions { customRes: string[] } diff --git a/packages/uni-uts-v1/src/manifest/manifest.ts b/packages/uni-uts-v1/src/manifest/manifest.ts index 5b5dc2c4cf..7848bf7846 100644 --- a/packages/uni-uts-v1/src/manifest/manifest.ts +++ b/packages/uni-uts-v1/src/manifest/manifest.ts @@ -130,7 +130,15 @@ async function resolvePluginCommonFiles( pluginDir: string, is_uni_modules: boolean ) { - return glob(['*', (is_uni_modules ? 'utssdk/' : '') + 'common/**/*'], { + const patterns = ['*'] + if (is_uni_modules) { + patterns.push('utssdk/*.uts') + patterns.push('utssdk/common/**/*') + } else { + patterns.push('common/**/*') + } + return glob(patterns, { + ignore: ['changelog.md', 'readme.md'], cwd: pluginDir, }) } diff --git a/packages/uni-uts-v1/src/manifest/sourceMap.ts b/packages/uni-uts-v1/src/manifest/sourceMap.ts new file mode 100644 index 0000000000..b671984dd5 --- /dev/null +++ b/packages/uni-uts-v1/src/manifest/sourceMap.ts @@ -0,0 +1,109 @@ +import { copySync, existsSync } from 'fs-extra' +import { join } from 'path' +import { resolveSourceMapPath } from '../shared' +import { APP_PLATFORM } from './utils' + +function getSourceMapFilename(platform: APP_PLATFORM) { + return `index${platform === 'app-android' ? '.kt' : '.swift'}.map` +} + +/** + * 缓存 sourcemap + * @param pluginRelativeDir + * @param outputDir + * @param cacheDir + */ +export function storeSourceMap( + platform: APP_PLATFORM, + pluginRelativeDir: string, + outputDir: string, + cacheDir: string, + is_uni_modules: boolean +) { + const sourceMapFilename = resolveSourceMapFilename( + platform, + pluginRelativeDir, + outputDir, + is_uni_modules + ) + if (existsSync(sourceMapFilename)) { + copySync( + sourceMapFilename, + resolveSourceMapCacheFilename(platform, cacheDir, pluginRelativeDir) + ) + return true + } + return false +} +/** + * 拷贝 sourcemap + * @param pluginRelativeDir + * @param outputDir + * @param cacheDir + */ +export function restoreSourceMap( + platform: APP_PLATFORM, + pluginRelativeDir: string, + outputDir: string, + cacheDir: string, + is_uni_modules: boolean +) { + const sourceMapCacheFile = resolveSourceMapCacheFile( + platform, + cacheDir, + pluginRelativeDir + ) + if (sourceMapCacheFile) { + copySync( + sourceMapCacheFile, + resolveSourceMapFilename( + platform, + pluginRelativeDir, + outputDir, + is_uni_modules + ) + ) + } +} + +export function resolveSourceMapFilename( + platform: APP_PLATFORM, + pluginRelativeDir: string, + outputDir: string, + is_uni_modules: boolean +) { + return join( + resolveSourceMapPath(outputDir, 'app'), + pluginRelativeDir, + is_uni_modules ? 'utssdk' : '', + platform, + getSourceMapFilename(platform) + ) +} + +export function resolveSourceMapCacheFilename( + platform: APP_PLATFORM, + cacheDir: string, + pluginRelativeDir: string +) { + return join( + cacheDir, + platform, + 'uts', + pluginRelativeDir, + getSourceMapFilename(platform) + ) +} + +function resolveSourceMapCacheFile( + platform: APP_PLATFORM, + cacheDir: string, + pluginRelativeDir: string +) { + const file = resolveSourceMapCacheFilename( + platform, + cacheDir, + pluginRelativeDir + ) + return (existsSync(file) && file) || '' +} diff --git a/packages/uni-uts-v1/src/swift.ts b/packages/uni-uts-v1/src/swift.ts index 3acd971eba..3429f87799 100644 --- a/packages/uni-uts-v1/src/swift.ts +++ b/packages/uni-uts-v1/src/swift.ts @@ -146,7 +146,7 @@ export async function compile( isPlugin: true, outDir: outputDir, package: parseSwiftPackage(filename).namespace, - sourceMap: sourceMap ? resolveUTSSourceMapPath(filename) : false, + sourceMap: sourceMap ? resolveUTSSourceMapPath() : false, extname: 'swift', imports: ['DCUTSFoundation'], logFilename: true, diff --git a/packages/uni-uts-v1/src/utils.ts b/packages/uni-uts-v1/src/utils.ts index 3474367e07..2b8ca2734d 100644 --- a/packages/uni-uts-v1/src/utils.ts +++ b/packages/uni-uts-v1/src/utils.ts @@ -20,7 +20,7 @@ export type ToSwiftOptions = ToOptions export const ERR_MSG_PLACEHOLDER = `___ERR_MSG___` -export function resolveUTSSourceMapPath(_filename: string) { +export function resolveUTSSourceMapPath() { return resolveSourceMapPath() } @@ -98,10 +98,7 @@ export function moveRootIndexSourceMap( ) { if (isRootIndex(filename, platform)) { const sourceMapFilename = path - .resolve( - resolveUTSSourceMapPath(filename), - path.relative(inputDir, filename) - ) + .resolve(resolveUTSSourceMapPath(), path.relative(inputDir, filename)) .replace(path.extname(filename), extname + '.map') if (fs.existsSync(sourceMapFilename)) { const newSourceMapFilename = path.resolve( -- GitLab