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

wip(uts): compiler

上级 86e54ebe
......@@ -17434,6 +17434,17 @@ function initUtsClassName(moduleName, className, is_uni_modules) {
capitalize(className));
}
return '';
}
const pluginDefines = {};
function registerUTSPlugin(name, define) {
pluginDefines[name] = define;
}
function requireUTSPlugin(name) {
const define = pluginDefines[name];
if (!define) {
console.error(`${name} is not found`);
}
return define;
}
const EventType = {
......@@ -19556,6 +19567,8 @@ var uni$1 = {
initUtsIndexClassName: initUtsIndexClassName,
initUtsClassName: initUtsClassName,
initUtsPackageName: initUtsPackageName,
requireUTSPlugin: requireUTSPlugin,
registerUTSPlugin: registerUTSPlugin,
navigateTo: navigateTo,
reLaunch: reLaunch,
switchTab: switchTab,
......
......@@ -90,6 +90,8 @@ export {
initUtsIndexClassName,
initUtsClassName,
initUtsPackageName,
requireUTSPlugin,
registerUTSPlugin,
} from './plugin/uts'
export * from './ad/rewardedVideoAd'
......
......@@ -387,3 +387,19 @@ export function initUtsClassName(
}
return ''
}
const pluginDefines: Record<string, Record<string, unknown>> = {}
export function registerUTSPlugin(
name: string,
define: Record<string, unknown>
) {
pluginDefines[name] = define
}
export function requireUTSPlugin(name: string) {
const define = pluginDefines[name]
if (!define) {
console.error(`${name} is not found`)
}
return define
}
......@@ -12,5 +12,26 @@ const cls = initUtsIndexClassName(name, is_uni_modules)
export const onMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarning', params: [{"name":"callback","type":"UTSCallback"}]})
export const offMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarning', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}]})
export default initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, 'User', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} ))
export const a = 1
"
`;
exports[`code genProxyCode cjs 1`] = `
"
import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app'
// const { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } = uni
const name = 'test-uts'
const errMsg = \`\`
const is_uni_modules = false
const pkg = initUtsPackageName(name, is_uni_modules)
const cls = initUtsIndexClassName(name, is_uni_modules)
Object.defineProperty(exports, '__esModule', { value: true })
exports.onMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarning', params: [{"name":"callback","type":"UTSCallback"}]})
exports.offMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarning', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}]})
exports.default = initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, 'User', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} ))
const a = 1
exports.a = a
"
`;
import { resolve } from 'path'
import { genProxyCode } from '../src/code'
import { FORMATS, genProxyCode } from '../src/code'
import { ERR_MSG_PLACEHOLDER } from '../src/utils'
const pluginDir = resolve(__dirname, 'examples/uts/utssdk/test-uts')
......@@ -18,4 +18,18 @@ describe('code', () => {
).replace(ERR_MSG_PLACEHOLDER, '')
).toMatchSnapshot()
})
test('genProxyCode cjs', async () => {
expect(
(
await genProxyCode(pluginDir, {
id: 'test-uts',
is_uni_modules: false,
name: 'test-uts',
namespace: 'uts.sdk.testUts',
extname: '.uts',
format: FORMATS.CJS,
})
).replace(ERR_MSG_PLACEHOLDER, '')
).toMatchSnapshot()
})
})
......@@ -11,7 +11,7 @@
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"index.uts": {
"md5": "012e7488376fec3b6d104a0242a63535"
"md5": "0ab09e7091607dbfb059a9745160ad25"
},
"package.json": {
"md5": "9595031a0d4158abb72060cdf3a200c1"
......
......@@ -11,7 +11,7 @@
"md5": "d41d8cd98f00b204e9800998ecf8427e"
},
"index.uts": {
"md5": "012e7488376fec3b6d104a0242a63535"
"md5": "0ab09e7091607dbfb059a9745160ad25"
},
"package.json": {
"md5": "9595031a0d4158abb72060cdf3a200c1"
......
export function onMemoryWarning(callback: (level: number) => void) {}
export function onMemoryWarning(callback: (level: number) => void) { }
export function offMemoryWarning(
callback: ((level: number) => void) | null = null
) {}
\ No newline at end of file
) { }
export default class User {
}
export const a = 1
export let b = 2, c = 3
\ No newline at end of file
......@@ -13,10 +13,16 @@ import {
Param,
TsTypeAnnotation,
VariableDeclaration,
VariableDeclarationKind,
} from '../types/types'
import { createResolveTypeReferenceName, ERR_MSG_PLACEHOLDER } from './utils'
import { isInHBuilderX } from './shared'
import { camelize, capitalize } from '@vue/shared'
export const enum FORMATS {
ES = 'es',
CJS = 'cjs',
}
interface GenProxyCodeOptions {
is_uni_modules: boolean
id: string
......@@ -25,13 +31,14 @@ interface GenProxyCodeOptions {
namespace: string
androidComponents?: Record<string, string>
iosComponents?: Record<string, string>
format?: FORMATS
}
export async function genProxyCode(
module: string,
options: GenProxyCodeOptions
) {
const { name, is_uni_modules } = options
const { name, is_uni_modules, format } = options
return `
import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app'
// const { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } = uni
......@@ -44,7 +51,7 @@ ${genComponentsCode(
options.androidComponents || {},
options.iosComponents || {}
)}
${genModuleCode(await parseModuleDecls(module, options))}
${genModuleCode(await parseModuleDecls(module, options), format)}
`
}
......@@ -92,19 +99,35 @@ export function resolvePlatformIndex(
return fs.existsSync(filename) && filename
}
function genModuleCode(decls: ProxyDecl[]) {
function exportDefaultCode(format: FORMATS) {
return format === FORMATS.ES ? 'export default ' : 'exports.default = '
}
function exportVarCode(format: FORMATS, kind: VariableDeclarationKind) {
if (format === FORMATS.ES) {
return `export ${kind} `
}
return `exports.`
}
function genModuleCode(decls: ProxyDecl[], format: FORMATS = FORMATS.ES) {
const codes: string[] = []
if (format === FORMATS.CJS) {
codes.push(`Object.defineProperty(exports, '__esModule', { value: true })`)
}
const exportDefault = exportDefaultCode(format)
const exportConst = exportVarCode(format, 'const')
decls.forEach((decl) => {
if (decl.type === 'Class') {
if (decl.isDefault) {
codes.push(
`export default initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '${
`${exportDefault}initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '${
decl.cls
}', is_uni_modules) }, ${JSON.stringify(decl.options)} ))`
)
} else {
codes.push(
`export const ${
`${exportConst}${
decl.cls
} = initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '${
decl.cls
......@@ -114,7 +137,7 @@ function genModuleCode(decls: ProxyDecl[]) {
} else if (decl.type === 'FunctionDeclaration') {
if (decl.isDefault) {
codes.push(
`export default initUtsProxyFunction(${
`${exportDefault}nitUtsProxyFunction(${
decl.async
}, { errMsg, main: true, package: pkg, class: cls, name: '${
decl.method
......@@ -122,7 +145,7 @@ function genModuleCode(decls: ProxyDecl[]) {
)
} else {
codes.push(
`export const ${decl.method} = initUtsProxyFunction(${
`${exportConst}${decl.method} = initUtsProxyFunction(${
decl.async
}, { errMsg, main: true, package: pkg, class: cls, name: '${
decl.method
......@@ -130,11 +153,28 @@ function genModuleCode(decls: ProxyDecl[]) {
)
}
} else if (decl.type === 'VariableDeclaration') {
codes.push(
`export ${decl.kind} ${decl.declarations
.map((d) => `${(d.id as Identifier).value} = ${genInitCode(d.init!)}`)
.join(', ')}`
)
if (format === FORMATS.ES) {
codes.push(
`export ${decl.kind} ${decl.declarations
.map(
(d) => `${(d.id as Identifier).value} = ${genInitCode(d.init!)}`
)
.join(', ')}`
)
} else if (format === FORMATS.CJS) {
codes.push(
`${decl.kind} ${decl.declarations
.map(
(d) => `${(d.id as Identifier).value} = ${genInitCode(d.init!)}`
)
.join(', ')}`
)
const exportVar = exportVarCode(format, decl.kind)
decl.declarations.forEach((d) => {
const name = (d.id as Identifier).value
codes.push(`${exportVar}${name} = ${name}`)
})
}
}
})
return codes.join(`\n`)
......
......@@ -15,6 +15,7 @@ import {
} from './swift'
import {
FORMATS,
genProxyCode,
resolvePlatformIndex,
resolvePlatformIndexFilename,
......@@ -93,7 +94,17 @@ export async function compile(pluginDir: string) {
const deps: string[] = []
const code = await genProxyCode(
pluginDir,
extend({ androidComponents, iosComponents }, pkg)
extend(
{
androidComponents,
iosComponents,
format:
process.env.UNI_UTS_JS_CODE_FORMAT === 'cjs'
? FORMATS.CJS
: FORMATS.ES,
},
pkg
)
)
let errMsg = ''
if (process.env.NODE_ENV !== 'development') {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册