Skip to content

Commit

Permalink
Merge pull request #106 from qican77/release-3.6.8-app
Browse files Browse the repository at this point in the history
fix: 修复taro convertor格式问题
  • Loading branch information
likailong180 authored Aug 22, 2023
2 parents aa4944c + ac9ed8c commit 399767a
Show file tree
Hide file tree
Showing 23 changed files with 2,524 additions and 2,277 deletions.
205 changes: 101 additions & 104 deletions packages/taro-cli-convertor/src/index.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/taro-cli-convertor/src/util/astConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as t from '@babel/types'
export function generateMinimalEscapeCode (ast: t.File) {
return generate(ast, {
jsescOption: {
minimal: true
}
minimal: true,
},
}).code
}
126 changes: 64 additions & 62 deletions packages/taro-cli-convertor/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
promoteRelativePath,
REG_SCRIPT,
REG_TYPESCRIPT,
resolveScriptPath
resolveScriptPath,
} from '@tarojs/helper'
import * as path from 'path'

Expand All @@ -19,33 +19,29 @@ export function getPkgVersion (): string {
return require(path.join(getRootPath(), 'package.json')).version
}

//Fix: 在小程序三方件中找到入口 index
function getModulePath(rootPath, modulePath) {
let parts = modulePath.split('/');
// Fix: 在小程序三方件中找到入口 index
function getModulePath (rootPath, modulePath) {
const parts = modulePath.split('/')
let moduleIndex = path.join(rootPath, 'node_modules') // node_modules文件夹
if (parts.at(-1) === 'index') {
parts.pop();
}
parts.push('index.js');
let npmIndex = path.join(rootPath, `miniprogram_npm/${modulePath}.js`); // 判断本身是否是一个完整的入口
parts.pop()
}
parts.push('index.js')
const npmIndex = path.join(rootPath, `miniprogram_npm/${modulePath}.js`) // 判断本身是否是一个完整的入口
if (fs.existsSync(npmIndex)) {
parts.pop();
let tempPart = parts.at(-1) + '.js';
parts.pop();
parts.push(tempPart);
parts.pop()
const tempPart = parts.at(-1) + '.js'
parts.pop()
parts.push(tempPart)
}
parts.forEach(part => {
const moduleFile = moduleIndex;
moduleIndex = searchFile(moduleFile, part);
parts.forEach((part) => {
const moduleFile = moduleIndex
moduleIndex = searchFile(moduleFile, part)
})
return moduleIndex;
return moduleIndex
}

function getRelativePath (
rootPath: string,
sourceFilePath: string,
oriPath: string
) {
function getRelativePath (rootPath: string, sourceFilePath: string, oriPath: string) {
// 处理以/开头的绝对路径,比如 /a/b
if (path.isAbsolute(oriPath)) {
if (oriPath.indexOf('/') !== 0) {
Expand All @@ -68,24 +64,28 @@ function getRelativePath (
if (fs.existsSync(vpath)) {
return './' + oriPath
}
const testParts = oriPath.split('/');
let testindex = path.join(rootPath, `node_modules/${testParts[0]}`); // 判断三方件是否在node_modules中
const testParts = oriPath.split('/')
const testindex = path.join(rootPath, `node_modules/${testParts[0]}`) // 判断三方件是否在node_modules中
if (!fs.existsSync(testindex)) {
if (oriPath.indexOf('/') !== -1 && oriPath.indexOf('@') === -1 && oriPath.lastIndexOf('.js') !== oriPath.length-3){
oriPath = oriPath + '.js' // 不在这里返回 utils/auth -> utils/auth.js
if (
oriPath.indexOf('/') !== -1 &&
oriPath.indexOf('@') === -1 &&
oriPath.lastIndexOf('.js') !== oriPath.length - 3
) {
oriPath = oriPath + '.js' // 不在这里返回 utils/auth -> utils/auth.js
}
if (fs.existsSync(oriPath)) {
oriPath = './' + oriPath
oriPath = './' + oriPath
}
return oriPath;
return oriPath
}
const realPath = getModulePath(rootPath, oriPath);
const realPath = getModulePath(rootPath, oriPath)
// 转成相对路径
let relativePath = path.relative(path.dirname(sourceFilePath), realPath);
const relativePath = path.relative(path.dirname(sourceFilePath), realPath)
if (relativePath.indexOf('.') !== 0) {
return './' + relativePath;
return './' + relativePath
}
return relativePath;
return relativePath
}
return oriPath
}
Expand Down Expand Up @@ -153,40 +153,42 @@ export const incrementId = () => {
return () => (n++).toString()
}

export function searchFile(moduleFile, indexPart) { //Fix: 递归遍历查找
const filePath = path.join(moduleFile, indexPart);
if(fs.existsSync(filePath)) {
return filePath;
export function searchFile (moduleFile, indexPart) {
// Fix: 递归遍历查找
const filePath = path.join(moduleFile, indexPart)
if (fs.existsSync(filePath)) {
return filePath
}
const folders = fs.readdirSync(moduleFile); //获取子目录
let resultFile;
const folders = fs.readdirSync(moduleFile) // 获取子目录
let resultFile
for (let i = 0; i < folders.length; i++) {
if (folders[i].indexOf('_dist') !== -1) { // 在先找到miniprogram_dist目录的情况下,查找同级子目录是否有源码(src/index.js)
for (let j = i+1; j < folders.length; j++) {
if (folders[j].indexOf('src') !== -1 || folders[j].indexOf('index.js') !== -1) {
i = j;
break;
}
if (folders[i].indexOf('_dist') !== -1) {
// 在先找到miniprogram_dist目录的情况下,查找同级子目录是否有源码(src/index.js)
for (let j = i + 1; j < folders.length; j++) {
if (folders[j].indexOf('src') !== -1 || folders[j].indexOf('index.js') !== -1) {
i = j
break
}
}
if (folders[i].indexOf('ali') !== -1) {
continue;
}
if (folders[i].indexOf('baidu') !== -1) {
continue;
}
if (folders[i].indexOf('qq') !== -1) {
continue;
}
if (folders[i].indexOf('toutiao') !== -1) {
continue;
}
const nextModule = path.join(moduleFile, folders[i]);
if (fs.lstatSync(nextModule).isDirectory()) {
resultFile = searchFile(nextModule, indexPart);
}
if (resultFile && fs.existsSync(resultFile)) {
return resultFile;
}
}
if (folders[i].indexOf('ali') !== -1) {
continue
}
if (folders[i].indexOf('baidu') !== -1) {
continue
}
if (folders[i].indexOf('qq') !== -1) {
continue
}
if (folders[i].indexOf('toutiao') !== -1) {
continue
}
const nextModule = path.join(moduleFile, folders[i])
if (fs.lstatSync(nextModule).isDirectory()) {
resultFile = searchFile(nextModule, indexPart)
}
if (resultFile && fs.existsSync(resultFile)) {
return resultFile
}
}
}
24 changes: 10 additions & 14 deletions packages/taro-transformer-wx/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint'
],
parserOptions: { },
extends: [
'eslint:recommended',
'standard',
'plugin:@typescript-eslint/recommended',
'prettier'
],
plugins: ['@typescript-eslint'],
parserOptions: {},
extends: ['eslint:recommended', 'standard', 'plugin:@typescript-eslint/recommended', 'prettier'],
rules: {
'@typescript-eslint/ban-ts-comment': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/indent': [2, 2],
'@typescript-eslint/member-delimiter-style': [1, { multiline: { delimiter: 'none' }, singleline: { delimiter: 'comma' } }],
'@typescript-eslint/member-delimiter-style': [
1,
{ multiline: { delimiter: 'none' }, singleline: { delimiter: 'comma' } },
],
'@typescript-eslint/no-empty-function': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-namespace': 0,
Expand All @@ -37,8 +33,8 @@ module.exports = {
'no-new': 'off',
'prefer-const': 'off',
'no-empty': 'off',
'no-unsafe-optional-chaining': 'off',
'no-unsafe-optional-chaining': 'off',
'no-prototype-builtins': 'off',
camelcase: 'off'
}
camelcase: 'off',
},
}
13 changes: 4 additions & 9 deletions packages/taro-transformer-wx/__tests__/base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import transform from '../src'
import {
baseOptions,
removeFrontBlank
} from './utils'
import { baseOptions, removeFrontBlank } from './utils'

describe('base', () => {
// 测试async、await转换后是否保持为async、await
test('asyncTrans', () => {
let originCode =
`async function fetchData() {
let originCode = `async function fetchData() {
try {
// 使用 await 关键字等待异步操作完成
const response = await fetch('https://api.example.com/data');
Expand All @@ -22,9 +18,8 @@ describe('base', () => {
}`
const { code } = transform({
...baseOptions,
code: originCode

code: originCode,
})
expect(removeFrontBlank(code)).toEqual(removeFrontBlank(originCode))
})
})
})
4 changes: 2 additions & 2 deletions packages/taro-transformer-wx/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export const baseOptions = {
sourcetDir: __dirname,
code: '',
isTyped: false,
isNormal: true
isNormal: true,
}

// 去除字符串每行前面的空格
export function removeFrontBlank(str: string | undefined) {
if (str === undefined) {
return ''
}
return str.replace(/^\s+/gm, '');
return str.replace(/^\s+/gm, '')
}
28 changes: 18 additions & 10 deletions packages/taro-transformer-wx/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const enum Adapters {
quickapp = 'quickapp',
tt = 'tt',
qq = 'qq',
jd = 'jd'
jd = 'jd',
}

interface Adapter {
Expand All @@ -27,7 +27,7 @@ const weixinAdapter: Adapter = {
forItem: 'wx:for-item',
forIndex: 'wx:for-index',
key: 'wx:key',
type: Adapters.weapp
type: Adapters.weapp,
}

const swanAdapter: Adapter = {
Expand All @@ -38,7 +38,7 @@ const swanAdapter: Adapter = {
forItem: 's-for-item',
forIndex: 's-for-index',
key: 's-key',
type: Adapters.swan
type: Adapters.swan,
}

const alipayAdapter: Adapter = {
Expand All @@ -49,7 +49,7 @@ const alipayAdapter: Adapter = {
forItem: 'a:for-item',
forIndex: 'a:for-index',
key: 'a:key',
type: Adapters.alipay
type: Adapters.alipay,
}

const ttAdapter: Adapter = {
Expand All @@ -60,7 +60,7 @@ const ttAdapter: Adapter = {
forItem: 'tt:for-item',
forIndex: 'tt:for-index',
key: 'tt:key',
type: Adapters.tt
type: Adapters.tt,
}

const quickappAdapter: Adapter = {
Expand All @@ -71,7 +71,7 @@ const quickappAdapter: Adapter = {
forItem: 'for-item',
forIndex: 'for-index',
key: 'key',
type: Adapters.quickapp
type: Adapters.quickapp,
}

const qqAdapter: Adapter = {
Expand All @@ -82,7 +82,7 @@ const qqAdapter: Adapter = {
forItem: 'qq:for-item',
forIndex: 'qq:for-index',
key: 'qq:key',
type: Adapters.qq
type: Adapters.qq,
}

const jdAdapter: Adapter = {
Expand All @@ -93,16 +93,24 @@ const jdAdapter: Adapter = {
forItem: 'jd:for-item',
forIndex: 'jd:for-index',
key: 'jd:key',
type: Adapters.jd
type: Adapters.jd,
}

export let Adapter: Adapter = weixinAdapter

export const isNewPropsSystem = () => {
return [Adapters.weapp, Adapters.swan, Adapters.tt, Adapters.qq, Adapters.alipay, Adapters.quickapp, Adapters.jd].includes(Adapter.type)
return [
Adapters.weapp,
Adapters.swan,
Adapters.tt,
Adapters.qq,
Adapters.alipay,
Adapters.quickapp,
Adapters.jd,
].includes(Adapter.type)
}

export function setAdapter (adapter: Adapters) {
export function setAdapter(adapter: Adapters) {
switch (adapter.toLowerCase()) {
case Adapters.swan:
Adapter = swanAdapter
Expand Down
Loading

0 comments on commit 399767a

Please sign in to comment.