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

wip(uts): compiler

上级 376a4838
......@@ -109,9 +109,12 @@ export function pathToGlob(
return path.posix.join(safeStr, glob)
}
export function resolveSourceMapPath() {
export function resolveSourceMapPath(
outputDir?: string,
platform?: UniApp.PLATFORM
) {
return path.resolve(
process.env.UNI_OUTPUT_DIR,
'../.sourcemap/' + getPlatformDir()
outputDir || process.env.UNI_OUTPUT_DIR,
'../.sourcemap/' + (platform || getPlatformDir())
)
}
......@@ -20447,7 +20447,7 @@ function removeNonTabBarPages() {
}
}
function isSamePage(url, $page) {
return url === $page.fullPath;
return url === $page.fullPath || url === "/" && $page.meta.isEntry;
}
function getTabBarPageId(url) {
const pages = getCurrentPagesMap().values();
......
......@@ -714,7 +714,7 @@ function initTriggerEvent(mpInstance) {
function initMiniProgramHook(name, options, isComponent) {
if (isComponent) {
// fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created
options = options.lifetimes;
options = options.lifetimes || {};
}
const oldHook = options[name];
if (!oldHook) {
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`code-frame-uts android 1`] = `
"error: The integer literal does not conform to the expected type String
at uni_modules/test-uts1/utssdk/index.uts:2:10
1 | export function test(): string {
2 | return 1;
| ^
3 | }
4 |
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':uni_modules:test-uts1:compileReleaseKotlin'.
> Compilation error. See log for more details
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 50s"
`;
exports[`code-frame-uts android 2`] = `
"error: The integer literal does not conform to the expected type String
at utssdk/test2/index.uts:2:10
1 | export function test(): string {
2 | return 2;
| ^
3 | }
4 |
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':uni_modules:test-uts1:compileReleaseKotlin'.
> Compilation error. See log for more details
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 50s"
`;
import path from 'path'
const {
generateCodeFrameWithAndroidStacktrace,
} = require('../dist/uni-stacktracey.cjs.js')
const utsProjectDir = path.resolve(__dirname, '../test/uts')
describe('code-frame-uts', () => {
test('android', async () => {
expect(
await generateCodeFrameWithAndroidStacktrace(androidUniModulesError, {
name: 'uni_modules/test-uts1',
inputDir: '/Users/fxy/DCloud/test-uts',
outputDir: path.resolve(utsProjectDir, 'unpackage/dist/dev/app-plus'),
})
).toMatchSnapshot()
expect(
await generateCodeFrameWithAndroidStacktrace(androidUtssdkError, {
name: 'utssdk/test2',
inputDir: '/Users/fxy/DCloud/test-uts',
outputDir: path.resolve(utsProjectDir, 'unpackage/dist/dev/app-plus'),
})
).toMatchSnapshot()
})
})
const androidUniModulesError = `e: uni_modules/test-uts1/utssdk/app-android/index.kt: (8, 12): The integer literal does not conform to the expected type String
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':uni_modules:test-uts1:compileReleaseKotlin'.
> Compilation error. See log for more details
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 50s`
const androidUtssdkError = `e: utssdk/test2/app-android/index.kt: (8, 12): The integer literal does not conform to the expected type String
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':uni_modules:test-uts1:compileReleaseKotlin'.
> Compilation error. See log for more details
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 50s`
......@@ -3707,6 +3707,42 @@ function generateCodeFrameWithSourceMapPath(filename, messages, options = {}) {
});
}
return Promise.resolve([]);
}
function resolveSourceMapPath(sourceMapFilename, name, outputDir) {
const is_uni_modules = path__default["default"].basename(path__default["default"].dirname(name)) === 'uni_modules';
return path__default["default"].resolve(outputDir, '../.sourcemap/app', name, is_uni_modules ? 'utssdk' : '', sourceMapFilename);
}
function generateCodeFrameWithAndroidStacktrace(stacktrace, { name, inputDir, outputDir }) {
const sourceMapFilename = resolveSourceMapPath('app-android/index.kt.map', name, outputDir);
return generateCodeFrameWithStacktrace(stacktrace, /e:\s+(.*):\s+\(([0-9]+),\s+([0-9]+)\):\s+(.*)/g, {
sourceRoot: inputDir,
sourceMapFilename,
});
}
function generateCodeFrameWithStacktrace(stacktrace, regexp, { sourceRoot, sourceMapFilename, }) {
return new Promise((resolve) => {
initConsumer(sourceMapFilename).then((consumer) => {
if (!consumer) {
return resolve(stacktrace);
}
resolve(stacktrace.replace(regexp, (substring, file, line, column, message) => {
const m = generateCodeFrameSourceMapConsumer(consumer, {
type: 'error',
file,
message,
line: parseInt(line),
column: parseInt(column),
}, { sourceRoot });
if (!m) {
return substring;
}
return `error: ${message}
at ${m.file}:${m.line}:${m.column}
${m.code}
`;
}));
});
});
}
const nixSlashes = (x) => x.replace(/\\/g, '/');
......@@ -4034,6 +4070,7 @@ ${_stack.errMsg}`;
exports.SourceMapConsumer = SourceMapConsumer;
exports.generateCodeFrame = generateCodeFrame;
exports.generateCodeFrameSourceMapConsumer = generateCodeFrameSourceMapConsumer;
exports.generateCodeFrameWithAndroidStacktrace = generateCodeFrameWithAndroidStacktrace;
exports.generateCodeFrameWithSourceMapPath = generateCodeFrameWithSourceMapPath;
exports.stacktracey = stacktracey;
exports.uniStracktraceyPreset = uniStracktraceyPreset;
......
......@@ -3690,6 +3690,42 @@ function generateCodeFrameWithSourceMapPath(filename, messages, options = {}) {
});
}
return Promise.resolve([]);
}
function resolveSourceMapPath(sourceMapFilename, name, outputDir) {
const is_uni_modules = path.basename(path.dirname(name)) === 'uni_modules';
return path.resolve(outputDir, '../.sourcemap/app', name, is_uni_modules ? 'utssdk' : '', sourceMapFilename);
}
function generateCodeFrameWithAndroidStacktrace(stacktrace, { name, inputDir, outputDir }) {
const sourceMapFilename = resolveSourceMapPath('app-android/index.kt.map', name, outputDir);
return generateCodeFrameWithStacktrace(stacktrace, /e:\s+(.*):\s+\(([0-9]+),\s+([0-9]+)\):\s+(.*)/g, {
sourceRoot: inputDir,
sourceMapFilename,
});
}
function generateCodeFrameWithStacktrace(stacktrace, regexp, { sourceRoot, sourceMapFilename, }) {
return new Promise((resolve) => {
initConsumer(sourceMapFilename).then((consumer) => {
if (!consumer) {
return resolve(stacktrace);
}
resolve(stacktrace.replace(regexp, (substring, file, line, column, message) => {
const m = generateCodeFrameSourceMapConsumer(consumer, {
type: 'error',
file,
message,
line: parseInt(line),
column: parseInt(column),
}, { sourceRoot });
if (!m) {
return substring;
}
return `error: ${message}
at ${m.file}:${m.line}:${m.column}
${m.code}
`;
}));
});
});
}
// @ts-ignore
......@@ -4024,4 +4060,4 @@ ${_stack.errMsg}`;
};
}
export { SourceMapConsumer, generateCodeFrame, generateCodeFrameSourceMapConsumer, generateCodeFrameWithSourceMapPath, stacktracey, uniStracktraceyPreset, utsStracktraceyPreset };
export { SourceMapConsumer, generateCodeFrame, generateCodeFrameSourceMapConsumer, generateCodeFrameWithAndroidStacktrace, generateCodeFrameWithSourceMapPath, stacktracey, uniStracktraceyPreset, utsStracktraceyPreset };
......@@ -14,6 +14,7 @@ export {
generateCodeFrame,
generateCodeFrameSourceMapConsumer,
generateCodeFrameWithSourceMapPath,
generateCodeFrameWithAndroidStacktrace,
} from './utils'
// @ts-ignore
if (__PLATFORM_WEB__) {
......
......@@ -163,3 +163,85 @@ export function generateCodeFrameWithSourceMapPath(
}
return Promise.resolve([])
}
interface GenerateCodeFrameWithStacktraceOptions {
name: string
inputDir: string
outputDir: string
}
function resolveSourceMapPath(
sourceMapFilename: string,
name: string,
outputDir: string
) {
const is_uni_modules = path.basename(path.dirname(name)) === 'uni_modules'
return path.resolve(
outputDir,
'../.sourcemap/app',
name,
is_uni_modules ? 'utssdk' : '',
sourceMapFilename
)
}
export function generateCodeFrameWithAndroidStacktrace(
stacktrace: string,
{ name, inputDir, outputDir }: GenerateCodeFrameWithStacktraceOptions
) {
const sourceMapFilename = resolveSourceMapPath(
'app-android/index.kt.map',
name,
outputDir
)
return generateCodeFrameWithStacktrace(
stacktrace,
/e:\s+(.*):\s+\(([0-9]+),\s+([0-9]+)\):\s+(.*)/g,
{
sourceRoot: inputDir,
sourceMapFilename,
}
)
}
function generateCodeFrameWithStacktrace(
stacktrace: string,
regexp: RegExp,
{
sourceRoot,
sourceMapFilename,
}: {
sourceRoot: string
sourceMapFilename: string
}
) {
return new Promise((resolve) => {
initConsumer(sourceMapFilename).then((consumer) => {
if (!consumer) {
return resolve(stacktrace)
}
resolve(
stacktrace.replace(regexp, (substring, file, line, column, message) => {
const m = generateCodeFrameSourceMapConsumer(
consumer,
{
type: 'error',
file,
message,
line: parseInt(line),
column: parseInt(column),
},
{ sourceRoot }
)
if (!m) {
return substring
}
return `error: ${message}
at ${m.file}:${m.line}:${m.column}
${m.code}
`
})
)
})
})
}
{"version":3,"sources":["/Users/fxy/DCloud/test-uts/uni_modules/test-uts1/utssdk/index.uts"],"sourcesContent":["export function test(): string {\n return 1;\n}\n"],"names":[],"mappings":";;;;;;AAAO,IAAS,QAAQ,MAAM,CAAC;IAC7B,OAAO,CAAC;AACV"}
\ No newline at end of file
{"version":3,"sources":["/Users/fxy/DCloud/test-uts/uni_modules/test-uts2/utssdk/app-android/index.uts"],"sourcesContent":["export function test(): string {\n return 2;\n}\n"],"names":[],"mappings":";;;;;;AAAO,IAAS,QAAQ,MAAM,CAAC;IAC7B,OAAO,CAAC;AACV"}
\ No newline at end of file
{"version":3,"sources":["/Users/fxy/DCloud/test-uts/utssdk/test/app-android/index.uts"],"sourcesContent":["export function test(): string {\n return 2;\n}\n"],"names":[],"mappings":";;;;;;AAAO,IAAS,QAAQ,MAAM,CAAC;IAC7B,OAAO,CAAC;AACV"}
\ No newline at end of file
{"version":3,"sources":["/Users/fxy/DCloud/test-uts/utssdk/test2/index.uts"],"sourcesContent":["export function test(): string {\n return 2;\n}\n"],"names":[],"mappings":";;;;;;AAAO,IAAS,QAAQ,MAAM,CAAC;IAC7B,OAAO,CAAC;AACV"}
\ No newline at end of file
......@@ -13,9 +13,11 @@ import {
import {
genUTSPlatformResource,
getUtsCompiler,
moveRootIndexSourceMap,
resolveAndroidDir,
resolvePackage,
resolveUTSPlatformFile,
resolveUTSSourceMapPath,
} from './utils'
import { Module } from '../../../types/types'
......@@ -124,7 +126,7 @@ async function compile(filename: string) {
isPlugin: true,
outDir: outputDir,
package: parseKotlinPackage(filename).package,
sourceMap: resolveSourceMapPath(),
sourceMap: resolveUTSSourceMapPath(filename),
extname: 'kt',
imports: [
'kotlinx.coroutines.async',
......@@ -137,6 +139,13 @@ async function compile(filename: string) {
noColor: isInHBuilderX(),
},
})
moveRootIndexSourceMap(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
platform: 'app-android',
extname: '.kt',
})
}
function resolveKotlincArgs(filename: string, kotlinc: string, jars: string[]) {
......
import { isInHBuilderX, resolveSourceMapPath } from '@dcloudio/uni-cli-shared'
import { isInHBuilderX } from '@dcloudio/uni-cli-shared'
import { capitalize } from '@vue/shared'
import { genUTSPlatformResource, getUtsCompiler, resolvePackage } from './utils'
import {
genUTSPlatformResource,
getUtsCompiler,
moveRootIndexSourceMap,
resolvePackage,
resolveUTSSourceMapPath,
} from './utils'
function parseSwiftPackage(filename: string) {
const res = resolvePackage(filename)
......@@ -49,11 +55,17 @@ async function compile(filename: string) {
isPlugin: true,
outDir: outputDir,
package: parseSwiftPackage(filename).namespace,
sourceMap: resolveSourceMapPath(),
sourceMap: resolveUTSSourceMapPath(filename),
extname: 'swift',
imports: ['DCUTSPlugin'],
logFilename: true,
noColor: isInHBuilderX(),
},
})
moveRootIndexSourceMap(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
platform: 'app-ios',
extname: '.swift',
})
}
import path from 'path'
import fs from 'fs-extra'
import type { parse, bundle, UtsTarget } from '@dcloudio/uts'
import { normalizePath } from '@dcloudio/uni-cli-shared'
import { normalizePath, resolveSourceMapPath } from '@dcloudio/uni-cli-shared'
import { camelize, capitalize } from '@vue/shared'
import { Module, ModuleItem } from '../../../types/types'
export function resolveUTSSourceMapPath(_filename: string) {
return resolveSourceMapPath()
}
export function getUtsCompiler(): {
parse: typeof parse
bundle: typeof bundle
......@@ -71,6 +75,38 @@ export function genUTSPlatformResource(
}
}
export function moveRootIndexSourceMap(
filename: string,
{ inputDir, outputDir, platform, extname }: UTSPlatformResourceOptions
) {
if (isRootIndex(filename, platform)) {
const sourceMapFilename = path
.resolve(
resolveUTSSourceMapPath(filename),
path.relative(inputDir, filename)
)
.replace(path.extname(filename), extname + '.map')
if (fs.existsSync(sourceMapFilename)) {
const newSourceMapFilename = path.resolve(
path.dirname(sourceMapFilename),
platform,
path.basename(sourceMapFilename)
)
console.log('move', sourceMapFilename, newSourceMapFilename)
fs.moveSync(sourceMapFilename, newSourceMapFilename, {
overwrite: true,
})
}
}
}
export function isRootIndex(
filename: string,
platform: typeof process.env.UNI_UTS_PLATFORM
) {
return path.basename(path.dirname(filename)) !== platform
}
export function resolveAndroidDir(filename: string) {
return resolveUTSPlatformDir(filename, 'app-android')
}
......@@ -80,9 +116,7 @@ function resolveUTSPlatformDir(
platform: typeof process.env.UNI_UTS_PLATFORM
) {
const maybePlatformDir = path.dirname(filename)
// 如果是根目录的 index.uts,需要定向到真正的平台目录
const isRootIndex = path.basename(maybePlatformDir) !== platform
if (isRootIndex) {
if (isRootIndex(filename, platform)) {
return path.join(maybePlatformDir, platform)
}
return maybePlatformDir
......@@ -95,10 +129,8 @@ export function resolveUTSPlatformFile(
let platformFile = path
.resolve(outputDir, path.relative(inputDir, filename))
.replace(path.extname(filename), extname)
const maybeModuleDir = path.dirname(filename)
// 如果是根目录的 index.uts 编译出来的 index.kt,则移动到平台目录下
const isRootIndex = path.basename(maybeModuleDir) !== platform
if (isRootIndex) {
if (isRootIndex(filename, platform)) {
if (fs.existsSync(platformFile)) {
const newPlatformFile = path.resolve(
path.dirname(platformFile),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册