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

wip(uts): compiler

上级 e302f53b
......@@ -42,6 +42,7 @@
},
"dependencies": {
"@dcloudio/uni-app-vite": "3.0.0-alpha-3050220220719003",
"@dcloudio/uni-app-vue": "3.0.0-alpha-3050220220719003"
"@dcloudio/uni-app-vue": "3.0.0-alpha-3050220220719003",
"@dcloudio/uni-uts-vite": "3.0.0-alpha-3050220220719003"
}
}
......@@ -6,14 +6,14 @@ describe('uts-module', () => {
expect(normalizeArg('hello')).toBe('hello')
expect(normalizeArg(true)).toBe(true)
expect(normalizeArg({ callback: () => {} })).toEqual({
callback: { $$type: 'function', value: 1 },
callback: 1,
})
expect(
normalizeArg({ success: () => {}, fail: () => {}, complete: () => {} })
).toEqual({
success: { $$type: 'function', value: 2 },
fail: { $$type: 'function', value: 3 },
complete: { $$type: 'function', value: 4 },
success: 2,
fail: 3,
complete: 4,
})
expect(
normalizeArg({
......@@ -28,15 +28,9 @@ describe('uts-module', () => {
user: {
name: 'test',
age: 10,
callback: {
$$type: 'function',
value: 5,
},
},
success: {
$$type: 'function',
value: 6,
callback: 5,
},
success: 6,
})
})
})
declare const _default: never[];
declare const _default: import("vite").Plugin[];
export default _default;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = [];
const v1_1 = require("./plugins/v1");
exports.default = [(0, v1_1.uniUtsV1Plugin)()];
......@@ -10,10 +10,7 @@ function normalizeArg(arg) {
if (typeof arg === 'function') {
const id = callbackId++
callbacks[id] = arg
return {
$$type: 'function',
value: id,
}
return id
} else if (isPlainObject(arg)) {
Object.keys(arg).forEach((name) => {
arg[name] = normalizeArg(arg[name])
......
......@@ -28,10 +28,7 @@ export function normalizeArg(arg: unknown) {
if (typeof arg === 'function') {
const id = callbackId++
callbacks[id] = arg
return {
$$type: 'function',
value: id,
}
return id
} else if (isPlainObject(arg)) {
Object.keys(arg).forEach((name) => {
;(arg as any)[name] = normalizeArg((arg as any)[name])
......
......@@ -26,6 +26,7 @@
"main": "dist/index.js"
},
"dependencies": {
"@dcloudio/uni-cli-shared": "3.0.0-alpha-3050220220719003"
"@dcloudio/uni-cli-shared": "3.0.0-alpha-3050220220719003",
"@dcloudio/uts": "3.0.0-alpha-3050220220719003"
}
}
export default []
import { uniUtsV1Plugin } from './plugins/v1'
export default [uniUtsV1Plugin()]
import type { Plugin } from 'vite'
import fs from 'fs'
import path from 'path'
import { parseJson } from '@dcloudio/uni-cli-shared'
// 需要区分 android,iOS
export function uniUtsPlugin(): Plugin {
// TODO 1.0 版本,解析到 uts module 时,动态编译 uts ?
return {
name: 'uts',
apply: 'build',
enforce: 'pre',
load(id, opts) {
if (opts && opts.ssr) {
return id
}
if (!id.includes('uni_modules')) {
return
}
const pkgPath = path.join(id, 'package.json')
if (!fs.existsSync(pkgPath)) {
return
}
const pkg = parseJson(fs.readFileSync(pkgPath, 'utf-8'))
if (pkg.uni_modules?.type !== 'uts') {
return
}
// 加载接口类
return path.join(id, pkg.main || 'interface.uts')
},
transform(code, id, opts) {
if (opts && opts.ssr) {
return
}
if (path.extname(id.split('?')[0]) !== '.uts') {
return
}
},
}
}
import type { Plugin } from 'vite'
import fs from 'fs'
import path from 'path'
import { parseJson, parseVueRequest } from '@dcloudio/uni-cli-shared'
import {
ExportDefaultDeclaration,
Module,
TsTypeAnnotation,
} from '../../types/types'
// 需要区分 android,iOS
export function uniUtsV1Plugin(): Plugin {
// TODO 1.0 版本,解析到 uts module 时,动态编译 uts ?
let moduleCode: string
return {
name: 'uni:uts-v1',
apply: 'build',
enforce: 'pre',
resolveId(id) {
if (!id.includes('uni_modules')) {
return
}
const pkgPath = path.join(id, 'package.json')
if (!fs.existsSync(pkgPath)) {
return
}
const pkg = parseJson(fs.readFileSync(pkgPath, 'utf-8'))
if (pkg.uni_modules?.type !== 'uts') {
return
}
return (
path.join(id, pkg.main || 'interface.uts') +
'?module=' +
path.basename(path.dirname(pkgPath))
)
},
transform(code, id, opts) {
if (opts && opts.ssr) {
return
}
const { filename, query } = parseVueRequest(id)
if (path.extname(filename) !== '.uts') {
return
}
const moduleName = (query as any).module as string
if (!moduleName) {
return
}
// 懒加载 uts
// eslint-disable-next-line no-restricted-globals
const { parse } = require('@dcloudio/uts')
const ast = parse(code)
if (!moduleCode) {
moduleCode = fs.readFileSync(
path.resolve(__dirname, '../../lib/module.js'),
'utf8'
)
}
return moduleCode
.replace(`__MODULE_NAME__`, moduleName)
.replace(`'__MODULE_DEFINE__'`, JSON.stringify(parseModuleDefines(ast)))
},
}
}
function parseModuleDefines(ast: Module) {
const module: Record<string, { async: boolean }> = {}
const defaultDecl = ast.body.find(
(item) => item.type === 'ExportDefaultDeclaration'
) as ExportDefaultDeclaration
if (!defaultDecl || defaultDecl.decl.type !== 'TsInterfaceDeclaration') {
return 'only support `export default interface Module {}`'
}
const body = defaultDecl.decl.body.body
body.forEach((item) => {
if (item.type !== 'TsMethodSignature' || item.key.type !== 'Identifier') {
return
}
const methodName = item.key.value
module[methodName] = {
async: item.typeAnn ? isReturnPromise(item.typeAnn) : false,
}
})
return module
}
function isReturnPromise({ typeAnnotation }: TsTypeAnnotation) {
return (
typeAnnotation.type === 'TsTypeReference' &&
typeAnnotation.typeName.type === 'Identifier' &&
typeAnnotation.typeName.value === 'Promise'
)
}
此差异已折叠。
......@@ -190,6 +190,7 @@ importers:
'@dcloudio/uni-h5': 3.0.0-alpha-3050220220719003
'@dcloudio/uni-i18n': 3.0.0-alpha-3050220220719003
'@dcloudio/uni-shared': 3.0.0-alpha-3050220220719003
'@dcloudio/uni-uts-vite': 3.0.0-alpha-3050220220719003
'@types/pako': 1.0.2
'@vue/compiler-sfc': 3.2.37
autoprefixer: ^10.4.7
......@@ -199,6 +200,7 @@ importers:
dependencies:
'@dcloudio/uni-app-vite': link:../uni-app-vite
'@dcloudio/uni-app-vue': link:../uni-app-vue
'@dcloudio/uni-uts-vite': link:../uni-uts-vite
devDependencies:
'@dcloudio/uni-cli-shared': link:../uni-cli-shared
'@dcloudio/uni-components': link:../uni-components
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册