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

wip(uts): compiler

上级 3c82233b
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
"utssdk/common/utils.uts": { "utssdk/common/utils.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e" "md5": "d41d8cd98f00b204e9800998ecf8427e"
}, },
"utssdk/index.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"package.json": { "package.json": {
"md5": "9595031a0d4158abb72060cdf3a200c1" "md5": "9595031a0d4158abb72060cdf3a200c1"
}, },
......
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
"utssdk/common/utils.uts": { "utssdk/common/utils.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e" "md5": "d41d8cd98f00b204e9800998ecf8427e"
}, },
"utssdk/index.uts": {
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"package.json": { "package.json": {
"md5": "9595031a0d4158abb72060cdf3a200c1" "md5": "9595031a0d4158abb72060cdf3a200c1"
}, },
......
import { resolve } from 'path' import path, { resolve } from 'path'
import { import {
checkManifest, checkManifest,
genManifestFile,
resolveManifestJson, resolveManifestJson,
resolvePluginFiles, resolvePluginFiles,
} from '../src/manifest/manifest' } from '../src/manifest/manifest'
import { checkKotlinCompile, checkSwiftCompile } from '../src/manifest/index' 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 pluginModuleDir = resolve(__dirname, 'examples/uts/uni_modules/test-uts')
const pluginDir = resolve(__dirname, 'examples/uts/utssdk/test-uts') const pluginDir = resolve(__dirname, 'examples/uts/utssdk/test-uts')
...@@ -45,6 +50,7 @@ describe('manifest', () => { ...@@ -45,6 +50,7 @@ describe('manifest', () => {
await resolvePluginFiles('app-android', pluginModuleDir, true) await resolvePluginFiles('app-android', pluginModuleDir, true)
).toEqual([ ).toEqual([
'package.json', 'package.json',
'utssdk/index.uts',
'utssdk/common/utils.uts', 'utssdk/common/utils.uts',
'utssdk/app-android/index.uts', 'utssdk/app-android/index.uts',
'utssdk/app-android/assets/test.json', 'utssdk/app-android/assets/test.json',
...@@ -59,16 +65,17 @@ describe('manifest', () => { ...@@ -59,16 +65,17 @@ describe('manifest', () => {
]) ])
expect(await resolvePluginFiles('app-ios', pluginModuleDir, true)).toEqual([ expect(await resolvePluginFiles('app-ios', pluginModuleDir, true)).toEqual([
'package.json', 'package.json',
'utssdk/index.uts',
'utssdk/common/utils.uts', 'utssdk/common/utils.uts',
'utssdk/app-ios/index.uts', 'utssdk/app-ios/index.uts',
'utssdk/app-ios/assets/test.json', 'utssdk/app-ios/assets/test.json',
]) ])
}) })
test('check manifest', async () => { test('check manifest', async () => {
// await genManifestFile('app-android',pluginModuleOptions) await genManifestFile('app-android', pluginModuleOptions)
// await genManifestFile('app-ios',pluginModuleOptions) await genManifestFile('app-ios', pluginModuleOptions)
// await genManifestFile('app-android',pluginOptions) await genManifestFile('app-android', pluginOptions)
// await genManifestFile('app-ios',pluginOptions) await genManifestFile('app-ios', pluginOptions)
const manifest = resolveManifestJson( const manifest = resolveManifestJson(
'app-android', 'app-android',
pluginModuleOptions.pluginRelativeDir, pluginModuleOptions.pluginRelativeDir,
...@@ -102,7 +109,7 @@ describe('manifest', () => { ...@@ -102,7 +109,7 @@ describe('manifest', () => {
test('gen android manifest', async () => { test('gen android manifest', async () => {
const res = await checkKotlinCompile('standard', pluginModuleOptions) const res = await checkKotlinCompile('standard', pluginModuleOptions)
expect(res.expired).toBe(false) expect(res.expired).toBe(false)
expect(res.files.length).toBe(4) expect(res.files.length).toBe(5)
expect(res.tips).toBeTruthy() expect(res.tips).toBeTruthy()
const res1 = await checkKotlinCompile('standard', pluginOptions) const res1 = await checkKotlinCompile('standard', pluginOptions)
...@@ -113,11 +120,118 @@ describe('manifest', () => { ...@@ -113,11 +120,118 @@ describe('manifest', () => {
test('gen ios manifest', async () => { test('gen ios manifest', async () => {
const res = await checkSwiftCompile('standard', pluginModuleOptions) const res = await checkSwiftCompile('standard', pluginModuleOptions)
expect(res.expired).toBe(false) expect(res.expired).toBe(false)
expect(res.files.length).toBe(4) expect(res.files.length).toBe(5)
expect(res.tips).toBe('') expect(res.tips).toBe('')
const res1 = await checkSwiftCompile('standard', pluginOptions) const res1 = await checkSwiftCompile('standard', pluginOptions)
expect(res1.expired).toBe(false) expect(res1.expired).toBe(false)
expect(res1.files.length).toBe(4) expect(res1.files.length).toBe(4)
expect(res1.tips).toBe('') 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', () => { ...@@ -22,7 +22,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
expect( expect(
resolveUtsPluginSourceMapFile( resolveUtsPluginSourceMapFile(
...@@ -53,7 +52,6 @@ describe('uts:sourceMap', () => { ...@@ -53,7 +52,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
}) })
...@@ -64,7 +62,6 @@ describe('uts:sourceMap', () => { ...@@ -64,7 +62,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
}) })
test('resolveUtsPluginSourceMapFile with utssdk uts=>swift', () => { test('resolveUtsPluginSourceMapFile with utssdk uts=>swift', () => {
...@@ -74,7 +71,6 @@ describe('uts:sourceMap', () => { ...@@ -74,7 +71,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
}) })
...@@ -85,7 +81,6 @@ describe('uts:sourceMap', () => { ...@@ -85,7 +81,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
}) })
test('resolveUtsPluginSourceMapFile with uni_modules swift', () => { test('resolveUtsPluginSourceMapFile with uni_modules swift', () => {
...@@ -95,7 +90,6 @@ describe('uts:sourceMap', () => { ...@@ -95,7 +90,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
}) })
test('resolveUtsPluginSourceMapFile with utssdk kt', () => { test('resolveUtsPluginSourceMapFile with utssdk kt', () => {
...@@ -105,7 +99,6 @@ describe('uts:sourceMap', () => { ...@@ -105,7 +99,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
}) })
test('resolveUtsPluginSourceMapFile with utssdk swift', () => { test('resolveUtsPluginSourceMapFile with utssdk swift', () => {
...@@ -115,7 +108,6 @@ describe('uts:sourceMap', () => { ...@@ -115,7 +108,6 @@ describe('uts:sourceMap', () => {
inputDir, inputDir,
outputDir outputDir
) )
console.log(sourceMapFile)
expect(sourceMapFile).toBeDefined() expect(sourceMapFile).toBeDefined()
}) })
test('generatedPositionFor', async () => { test('generatedPositionFor', async () => {
......
...@@ -14,7 +14,13 @@ import { ...@@ -14,7 +14,13 @@ import {
generateCodeFrameWithKotlinStacktrace, generateCodeFrameWithKotlinStacktrace,
generateCodeFrameWithSwiftStacktrace, generateCodeFrameWithSwiftStacktrace,
} from './legacy' } from './legacy'
import { checkCompile, genManifestFile, initCheckOptionsEnv } from './manifest' import {
checkCompile,
genManifestFile,
initCheckOptionsEnv,
restoreSourceMap,
storeSourceMap,
} from './manifest'
export const sourcemap = { export const sourcemap = {
generateCodeFrameWithKotlinStacktrace, generateCodeFrameWithKotlinStacktrace,
...@@ -90,14 +96,14 @@ export async function compile(pluginDir: string) { ...@@ -90,14 +96,14 @@ export async function compile(pluginDir: string) {
if (isWindows) { if (isWindows) {
process.env.UNI_UTS_TIPS = `iOS手机在windows上真机运行时uts插件代码修改需提交云端打包自定义基座才能生效` process.env.UNI_UTS_TIPS = `iOS手机在windows上真机运行时uts插件代码修改需提交云端打包自定义基座才能生效`
return { return {
code, code: parseErrMsg(code, errMsg),
deps, deps,
} }
} }
if (process.env.HX_USE_BASE_TYPE === 'standard') { if (process.env.HX_USE_BASE_TYPE === 'standard') {
process.env.UNI_UTS_TIPS = `iOS手机在标准基座真机或模拟器运行暂不支持uts插件,如需调用uts插件请使用自定义基座` process.env.UNI_UTS_TIPS = `iOS手机在标准基座真机或模拟器运行暂不支持uts插件,如需调用uts插件请使用自定义基座`
return { return {
code, code: parseErrMsg(code, errMsg),
deps, deps,
} }
} }
...@@ -106,7 +112,8 @@ export async function compile(pluginDir: string) { ...@@ -106,7 +112,8 @@ export async function compile(pluginDir: string) {
// dev 模式 // dev 模式
if (cacheDir) { if (cacheDir) {
// 检查缓存 // 检查缓存
let start = Date.now() // let start = Date.now()
// console.log('uts插件[' + pkg.id + ']start', start)
const res = await checkCompile( const res = await checkCompile(
utsPlatform, utsPlatform,
process.env.HX_USE_BASE_TYPE, process.env.HX_USE_BASE_TYPE,
...@@ -120,9 +127,11 @@ export async function compile(pluginDir: string) { ...@@ -120,9 +127,11 @@ export async function compile(pluginDir: string) {
is_uni_modules: pkg.is_uni_modules, 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 (!res.expired) {
if (utsPlatform === 'app-android') { if (utsPlatform === 'app-android') {
// 拷贝 dex
const cacheFile = resolveDexCacheFile(pluginRelativeDir, outputDir) const cacheFile = resolveDexCacheFile(pluginRelativeDir, outputDir)
if (cacheFile) { if (cacheFile) {
copySync( copySync(
...@@ -131,12 +140,22 @@ export async function compile(pluginDir: string) { ...@@ -131,12 +140,22 @@ export async function compile(pluginDir: string) {
) )
} }
} }
// 还原 sourcemap
restoreSourceMap(
utsPlatform,
pluginRelativeDir,
outputDir,
cacheDir,
pkg.is_uni_modules
)
if (res.tips) { if (res.tips) {
console.warn(res.tips) console.warn(res.tips)
} }
return { return {
code, code: parseErrMsg(code, errMsg),
// 所有文件加入依赖 // 所有文件加入依赖
deps: res.files.map((name) => join(pluginDir, name)), deps: res.files.map((name) => join(pluginDir, name)),
} }
...@@ -188,6 +207,15 @@ export async function compile(pluginDir: string) { ...@@ -188,6 +207,15 @@ export async function compile(pluginDir: string) {
} }
// 生成缓存文件 // 生成缓存文件
if (cacheDir && isSuccess) { if (cacheDir && isSuccess) {
// 存储 sourcemap
storeSourceMap(
utsPlatform,
pluginRelativeDir,
outputDir,
cacheDir,
pkg.is_uni_modules
)
// 生成 manifest
genManifestFile(utsPlatform, { genManifestFile(utsPlatform, {
pluginDir, pluginDir,
env, env,
......
...@@ -306,7 +306,7 @@ export async function compile( ...@@ -306,7 +306,7 @@ export async function compile(
isPlugin: true, isPlugin: true,
outDir: outputDir, outDir: outputDir,
package: parseKotlinPackage(filename).package, package: parseKotlinPackage(filename).package,
sourceMap: sourceMap ? resolveUTSSourceMapPath(filename) : false, sourceMap: sourceMap ? resolveUTSSourceMapPath() : false,
extname: 'kt', extname: 'kt',
imports, imports,
logFilename: true, logFilename: true,
......
...@@ -15,6 +15,8 @@ import { ...@@ -15,6 +15,8 @@ import {
export { genManifestFile } from './manifest' export { genManifestFile } from './manifest'
export { restoreSourceMap, storeSourceMap } from './sourceMap'
interface PlatformOptions { interface PlatformOptions {
customRes: string[] customRes: string[]
} }
......
...@@ -130,7 +130,15 @@ async function resolvePluginCommonFiles( ...@@ -130,7 +130,15 @@ async function resolvePluginCommonFiles(
pluginDir: string, pluginDir: string,
is_uni_modules: boolean 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, 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( ...@@ -146,7 +146,7 @@ export async function compile(
isPlugin: true, isPlugin: true,
outDir: outputDir, outDir: outputDir,
package: parseSwiftPackage(filename).namespace, package: parseSwiftPackage(filename).namespace,
sourceMap: sourceMap ? resolveUTSSourceMapPath(filename) : false, sourceMap: sourceMap ? resolveUTSSourceMapPath() : false,
extname: 'swift', extname: 'swift',
imports: ['DCUTSFoundation'], imports: ['DCUTSFoundation'],
logFilename: true, logFilename: true,
......
...@@ -20,7 +20,7 @@ export type ToSwiftOptions = ToOptions ...@@ -20,7 +20,7 @@ export type ToSwiftOptions = ToOptions
export const ERR_MSG_PLACEHOLDER = `___ERR_MSG___` export const ERR_MSG_PLACEHOLDER = `___ERR_MSG___`
export function resolveUTSSourceMapPath(_filename: string) { export function resolveUTSSourceMapPath() {
return resolveSourceMapPath() return resolveSourceMapPath()
} }
...@@ -98,10 +98,7 @@ export function moveRootIndexSourceMap( ...@@ -98,10 +98,7 @@ export function moveRootIndexSourceMap(
) { ) {
if (isRootIndex(filename, platform)) { if (isRootIndex(filename, platform)) {
const sourceMapFilename = path const sourceMapFilename = path
.resolve( .resolve(resolveUTSSourceMapPath(), path.relative(inputDir, filename))
resolveUTSSourceMapPath(filename),
path.relative(inputDir, filename)
)
.replace(path.extname(filename), extname + '.map') .replace(path.extname(filename), extname + '.map')
if (fs.existsSync(sourceMapFilename)) { if (fs.existsSync(sourceMapFilename)) {
const newSourceMapFilename = path.resolve( const newSourceMapFilename = path.resolve(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册