提交 0be34a20 编写于 作者: fxy060608's avatar fxy060608

wip(uts): compiler

上级 3c82233b
......@@ -10,6 +10,9 @@
"utssdk/common/utils.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"utssdk/index.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"package.json": {
"md5": "9595031a0d4158abb72060cdf3a200c1"
},
......
......@@ -10,6 +10,9 @@
"utssdk/common/utils.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"utssdk/index.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"package.json": {
"md5": "9595031a0d4158abb72060cdf3a200c1"
},
......
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)
})
})
......@@ -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 () => {
......
......@@ -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,
......
......@@ -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,
......
......@@ -15,6 +15,8 @@ import {
export { genManifestFile } from './manifest'
export { restoreSourceMap, storeSourceMap } from './sourceMap'
interface PlatformOptions {
customRes: string[]
}
......
......@@ -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,
})
}
......
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) || ''
}
......@@ -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,
......
......@@ -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(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册