Skip to content

Commit f7f6f6c

Browse files
committed
feat(alita): support typescript
1 parent eddcb91 commit f7f6f6c

20 files changed

+259
-149
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@babel/plugin-transform-react-jsx": "^7.1.6",
5353
"@babel/plugin-transform-runtime": "^7.5.0",
5454
"@babel/preset-flow": "^7.0.0",
55+
"@babel/preset-typescript": "^7.6.0",
5556
"@babel/traverse": "^7.1.6",
5657
"@babel/types": "^7.1.6",
5758
"babel-eslint": "^8.2.6",

src/basetran/geneJS.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*/
88

99
import fse from "fs-extra"
10+
import {miscNameToJSName} from '../util/util'
1011
const prettier = require("prettier")
1112
const path = require('path')
1213

1314

1415
export default function (code, info) {
1516
let {filepath} = info
16-
filepath = filepath.replace('.wx.js', '.js')
17+
filepath = miscNameToJSName(filepath)
1718

1819
const prettierCode = prettier.format(code, {
1920
semi: false,

src/basetran/handleModules.js

+65-35
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {isStaticRes, RNWXLIBMaps} from '../util/util'
1313
import isReactCompFile from '../util/isReactCompFile'
1414
import {RNCOMPSET} from "../constants";
1515
const npath = require('path')
16-
const colors = require('colors');
16+
1717

1818
/**
1919
* 1. 移除unused的import/require
@@ -113,7 +113,7 @@ export default function (ast, info) {
113113
}
114114
})
115115
} else {
116-
console.log(colors.error(`需要使用解构的方式引入react-native组件, error file:, ${filepath}`))
116+
console.log(`${filepath.replace(global.execArgs.OUT_DIR, '')}: 需要使用解构的方式引入react-native组件!`.error)
117117
}
118118
return
119119
}
@@ -220,39 +220,7 @@ function getRealSource(source, filepath) {
220220
|| source.startsWith('./')
221221
|| source.startsWith('../')
222222
) {
223-
const originalPath = npath
224-
.resolve(npath.dirname(filepath), source)
225-
.replace(global.execArgs.OUT_DIR, global.execArgs.INPUT_DIR)
226-
.replace(/\\/g, '/')
227-
228-
let finalSource = source
229-
let isReactComp
230-
if (fse.existsSync(originalPath + '.wx.js')) {
231-
isReactComp = isReactCompFile(originalPath + '.wx.js')
232-
} else if (fse.existsSync(originalPath + '.jsx')) {
233-
isReactComp = isReactCompFile(originalPath + '.jsx')
234-
} else if (fse.existsSync(originalPath + '.js')) {
235-
isReactComp = isReactCompFile(originalPath + '.js')
236-
} else {
237-
// 导入目录
238-
const fileDicPath = npath.resolve(originalPath, 'index')
239-
finalSource = source + '/index'
240-
241-
if (fse.existsSync(fileDicPath + '.wx.js')) {
242-
isReactComp = isReactCompFile(fileDicPath + '.wx.js')
243-
} else if (fse.existsSync(fileDicPath + '.jsx')) {
244-
isReactComp = isReactCompFile(fileDicPath + '.jsx')
245-
} else if (fse.existsSync(fileDicPath + '.js')) {
246-
isReactComp = isReactCompFile(fileDicPath + '.js')
247-
}
248-
249-
}
250-
251-
if (isReactComp) {
252-
finalSource = finalSource + '.comp'
253-
}
254-
255-
return finalSource
223+
return getFinalSource(filepath, source)
256224
} else {
257225
return source
258226
}
@@ -330,3 +298,65 @@ function insertIntoRequireBody(nodepath, newnode) {
330298
const ppp = nodepath.parentPath.parentPath.parentPath
331299
ppp.insertAfter(newnode)
332300
}
301+
302+
/**
303+
* 获取 最终的导入路径,如果导入的是目录,需要补全index
304+
* 如果导入的是组件,需要添加.comp
305+
* @param filepath
306+
* @param source
307+
*/
308+
function getFinalSource(filepath, source) {
309+
const originalPath = npath
310+
.resolve(npath.dirname(filepath), source)
311+
.replace(global.execArgs.OUT_DIR, global.execArgs.INPUT_DIR)
312+
.replace(/\\/g, '/')
313+
314+
const extname = npath.extname(filepath)
315+
316+
const indexFiles = npath.resolve(originalPath, 'index')
317+
318+
let fileSufix = '.js'
319+
if (extname === '.ts' || extname === '.tsx') {
320+
fileSufix = '.ts'
321+
}
322+
323+
const allFiles = [
324+
`${originalPath}.wx${fileSufix}`,
325+
`${originalPath}${fileSufix}`,
326+
`${originalPath}.wx${fileSufix}x`,
327+
`${originalPath}${fileSufix}x`
328+
]
329+
330+
331+
332+
for(let i = 0; i < allFiles.length; i ++ ) {
333+
const filePath = allFiles[i]
334+
335+
if (fse.existsSync(filePath)) {
336+
if (isReactCompFile(filePath)) {
337+
return `${source}.comp`
338+
} else {
339+
return source
340+
}
341+
}
342+
}
343+
344+
const allIndexFiles = [
345+
`${indexFiles}.wx${fileSufix}`,
346+
`${indexFiles}${fileSufix}`,
347+
`${indexFiles}.wx${fileSufix}x`,
348+
`${indexFiles}${fileSufix}x`
349+
]
350+
351+
for(let i = 0; i < allIndexFiles.length; i ++ ) {
352+
const filePath = allIndexFiles[i]
353+
354+
if (fse.existsSync(filePath)) {
355+
if (isReactCompFile(filePath)) {
356+
return `${source}/index.comp`
357+
} else {
358+
return `${source}/index`
359+
}
360+
}
361+
}
362+
}

src/basetran/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88

9+
import path from 'path'
910
import handleModules from './handleModules'
1011
import handleGlobalApi from './handleGlobalApi'
1112
import handleMisc from './handleMisc'
@@ -25,6 +26,7 @@ export default function (ast, filepath, justTran, isFuncComp) {
2526
if (justTran) {
2627
return
2728
} else {
28-
geneJS(geneReactCode(ast), info)
29+
const extname = path.extname(filepath)
30+
geneJS(geneReactCode(ast, extname), info)
2931
}
3032
}

src/constants.js

+7
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@ export const RNNOTSUPPORTCOMP = new Set([
7373
'Animated',
7474
'TimePickerAndroid',
7575
'ToastAndroid',
76+
])
77+
78+
export const supportExtname = new Set([
79+
'.js',
80+
'.jsx',
81+
'.ts',
82+
'.tsx'
7683
])

src/filewatch/addFile.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@
99
import fse from 'fs-extra'
1010
import {isStaticRes} from "../util/util"
1111
import struc from "../struc"
12+
import {supportExtname} from '../constants'
1213

1314
const path = require('path')
1415

16+
// 如果存在.wx.js 那么 .js 的文件不用处理
17+
function hasWxFile(srcpath) {
18+
const extname = path.extname(srcpath)
19+
if (supportExtname.has(extname)) {
20+
const wxSrcpath = srcpath.replace(extname, `.wx${extname}`)
21+
if (fse.existsSync(wxSrcpath)) {
22+
return true
23+
}
24+
}
25+
26+
return false
27+
}
28+
1529
/**
1630
* 新增一个文件的处理,按照如下规则
1731
* 1. isFileIgnore为true的直接忽略
@@ -37,14 +51,13 @@ export default async function addFile(filepath) {
3751
return []
3852
}
3953

40-
// 如果存在.wx.js 那么 .js 的文件不用处理
41-
if (srcpath.endsWith('.js')) {
42-
const wxSrcpath = srcpath.replace('.js', '.wx.js')
43-
if (fse.existsSync(wxSrcpath)) {
44-
return []
45-
}
54+
const hasWx = hasWxFile(srcpath)
55+
if (hasWx) {
56+
console.log(`${srcpath.replace(global.execArgs.INPUT_DIR, '')} 检测到有wx后缀文件存在,忽略!`.info)
57+
return []
4658
}
4759

60+
4861
// 如果 xx@3x.png 存在,则忽略xx@2x.png, xx.png
4962
if (isStaticRes(srcpath)) {
5063
if (srcpath.includes('@3x')) {

src/filewatch/changeFile.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
8+
import npath from 'path'
99
import addFile from './addFile';
1010
import getFiles from "./getFiles";
1111
import fse from "fs-extra";
12+
import {supportExtname} from '../constants'
1213

1314
export default async function changeFile(path) {
1415
const { INPUT_DIR, OUT_DIR } = global.execArgs;
1516
const targetPath = path.replace(INPUT_DIR, OUT_DIR);
1617
let oldFiles = [];
17-
if (path.endsWith('.js')) {
18-
oldFiles = await getFiles(targetPath, '.js');
18+
19+
if (supportExtname.has(npath.extname(path))) {
20+
oldFiles = await getFiles(targetPath, '.js')
1921
}
22+
2023
let newFiles = await addFile(path);
2124

2225
getDeleteFiles(oldFiles, newFiles).map(async (file) => {

src/filewatch/unlinkFile.js

+37-21
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,29 @@
99
import fse from 'fs-extra';
1010
import addFile from './addFile';
1111
import { isStaticRes } from "../util/util";
12+
import {supportExtname} from '../constants'
1213
import getFiles from './getFiles';
1314

1415
const path = require('path');
1516

17+
18+
function isHandleFile(srcPath) {
19+
const extname = path.extname(srcPath)
20+
21+
return supportExtname.has(extname)
22+
}
23+
24+
function isWxFile(srcPath) {
25+
if (isHandleFile(srcPath)) {
26+
const extname = path.extname(srcPath)
27+
return srcPath.replace(extname, '').endsWith('.wx')
28+
}
29+
30+
return false
31+
}
32+
33+
34+
1635
/**
1736
* 删除一个文件的处理,按照如下规则
1837
* 1. isFileIgnore为true的直接忽略
@@ -50,7 +69,6 @@ const path = require('path');
5069
* @param filePath
5170
* @returns {Promise<void>}
5271
*/
53-
5472
export default async function unlinkFile(filePath) {
5573
const { INPUT_DIR, OUT_DIR, configObj } = global.execArgs;
5674

@@ -81,27 +99,25 @@ export default async function unlinkFile(filePath) {
8199
//删除目标文件
82100
await resolveStaticRes(srcPath, targetPath, 0);
83101
}
84-
} else if (srcPath.endsWith('.js')) {
85-
86-
let allFiles = [];
87-
if (srcPath.endsWith('.wx.js')) {
88-
//删除的是 .wx.js
89-
allFiles = await getFiles(targetPath, '.wx.js');
90-
await removeFiles(allFiles);
91-
if (fse.existsSync(srcPath.replace('.wx.js', '.js'))) {
92-
//是否存在同名 .js 文件
93-
await addFile(srcPath.replace('.wx.js', '.js'));
94-
}
102+
} else if (isWxFile(srcPath)) {
103+
//删除的是 .wx.js
104+
const extname = path.extname(srcPath)
105+
const allFiles = await getFiles(targetPath, `.wx${extname}`);
106+
await removeFiles(allFiles);
107+
if (fse.existsSync(srcPath.replace(`.wx${extname}`, extname))) {
108+
//是否存在同名 .js 文件
109+
await addFile(srcPath.replace(`.wx${extname}`, extname));
110+
}
111+
} else if (isHandleFile(srcPath)) {
112+
//删除的是 .js
113+
const extname = path.extname(srcPath)
114+
const wxsrcPath = srcPath.replace(extname, `.wx${extname}`);
115+
if (fse.existsSync(wxsrcPath)) {
116+
//如果存在同名的 .wx.js
117+
return [];
95118
} else {
96-
//删除的是 .js
97-
const wxsrcPath = srcPath.replace('.js', '.wx.js');
98-
if (fse.existsSync(wxsrcPath)) {
99-
//如果存在同名的 .wx.js
100-
return [];
101-
} else {
102-
allFiles = await getFiles(targetPath, '.js');
103-
await removeFiles(allFiles);
104-
}
119+
const allFiles = await getFiles(targetPath, extname);
120+
await removeFiles(allFiles);
105121
}
106122
} else {
107123
await fse.remove(targetPath).catch(err => console.log(err));

src/struc/handleEntry.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import fse from 'fs-extra'
1010
import traverse from "@babel/traverse"
1111
import * as t from '@babel/types'
12-
import {isStaticRes} from '../util/util'
12+
import {isStaticRes, miscNameToJSName} from '../util/util'
1313
import { geneReactCode } from "../util/uast";
1414

1515
import basetran from '../basetran'
@@ -348,18 +348,18 @@ App({})
348348

349349

350350

351-
const entryCode = geneReactCode(ast)
351+
const entryCode = geneReactCode(ast, npath.extname(filepath))
352352
const dirname = npath.dirname(filepath)
353353
fse.mkdirsSync(dirname)
354+
355+
const realFilePath = miscNameToJSName(filepath)
354356
fse.writeFileSync(
355-
filepath,
357+
realFilePath,
356358
entryCode
357359
)
358-
359-
360-
360+
361361
return {
362-
filepath,
362+
realFilePath,
363363
allCompSet: new Set(Object.values(compImportMap).map(compPath => compPath.replace('.comp', '')))
364364
}
365365
}

0 commit comments

Comments
 (0)