From 4940383c96734ba3bdee7c40d9ca2e08b9774711 Mon Sep 17 00:00:00 2001 From: yintyuan Date: Wed, 6 Dec 2023 15:45:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20taroize=E8=A1=A5=E5=85=85plugin?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E8=BD=AC=E6=8D=A2=E7=9A=84=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=EF=BC=8C=E4=BB=A5=E5=8F=8A=E5=AF=B9script.te?= =?UTF-8?q?st.ts=E6=B5=8B=E8=AF=95=E7=9A=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__snapshots__/script.test.ts.snap | 52 +++++-- packages/taroize/__tests__/script.test.ts | 129 +++++++++++------- 2 files changed, 120 insertions(+), 61 deletions(-) diff --git a/packages/taroize/__tests__/__snapshots__/script.test.ts.snap b/packages/taroize/__tests__/__snapshots__/script.test.ts.snap index a9b8bc9e6b83..502f856141b1 100644 --- a/packages/taroize/__tests__/__snapshots__/script.test.ts.snap +++ b/packages/taroize/__tests__/__snapshots__/script.test.ts.snap @@ -20,7 +20,7 @@ module.exports = { } }; Component({}); -@withWeapp(cacheOptions.getOptionsFromCache(), true) +@withWeapp(cacheOptions.getOptionsFromCache()) class _C extends React.Component { render() { return {wxs_date.date}; @@ -30,7 +30,7 @@ module.exports = _C; `; exports[`parseScript app.js 1`] = ` -import "@tarojs/components"; +import { Block } from "@tarojs/components"; import React from "react"; import Taro from "@tarojs/taro"; const { @@ -51,7 +51,7 @@ export default App; `; exports[`parseScript getAPP || getCurrentPages 1`] = ` -import "@tarojs/components"; +import { Block } from "@tarojs/components"; import React from "react"; import Taro from "@tarojs/taro"; const { @@ -72,6 +72,29 @@ class App extends React.Component { export default App; `; +exports[`parseScript page页面通过插件url跳转到插件页面 1`] = ` +import { Block, Button } from "@tarojs/components"; +import React from "react"; +import Taro from "@tarojs/taro"; +const { + default: withWeapp +} = require("@tarojs/with-weapp"); +Page({ + pluginUrl() { + Taro.navigateTo({ + url: "/hello-plugin/pages/hello-page" + }); + } +}); +@withWeapp(cacheOptions.getOptionsFromCache()) +class _C extends React.Component { + render() { + return ; + } +} +export default _C; +`; + exports[`parseScript wxml expression 1`] = ` import { Block, View } from "@tarojs/components"; import React from "react"; @@ -79,18 +102,21 @@ import Taro from "@tarojs/taro"; const { default: withWeapp } = require("@tarojs/with-weapp"); -App({ +Page({ data: { srt: 'Hello Word!' } }); -@withWeapp(cacheOptions.getOptionsFromCache(), true) -class App extends React.Component { +@withWeapp(cacheOptions.getOptionsFromCache()) +class _C extends React.Component { render() { - return this.props.children; + const { + str + } = this.data; + return {str}; } } -export default App; +export default _C; `; exports[`parseScript wxml jsxText 1`] = ` @@ -100,12 +126,12 @@ import Taro from "@tarojs/taro"; const { default: withWeapp } = require("@tarojs/with-weapp"); -App({}); -@withWeapp(cacheOptions.getOptionsFromCache(), true) -class App extends React.Component { +Page({}); +@withWeapp(cacheOptions.getOptionsFromCache()) +class _C extends React.Component { render() { - return this.props.children; + return 123; } } -export default App; +export default _C; `; diff --git a/packages/taroize/__tests__/script.test.ts b/packages/taroize/__tests__/script.test.ts index 02a59284bc51..e5b5e33e4bb7 100644 --- a/packages/taroize/__tests__/script.test.ts +++ b/packages/taroize/__tests__/script.test.ts @@ -1,7 +1,4 @@ -import * as t from '@babel/types' - -import { parseScript } from '../src/script' -import { parseWXML } from '../src/wxml' +import { parse } from '../src' import { generateMinimalEscapeCode, removeBackslashesSerializer } from './util' expect.addSnapshotSerializer(removeBackslashesSerializer) @@ -11,27 +8,23 @@ jest.mock('fs', () => ({ appendFile: jest.fn(), })) -interface Option { - script: string - scriptPath: string - wxml: object - wxses: [] - refIds: Set - isApp: boolean -} +describe('parseScript', () => { + let option: any -const option: Option = { - script: '', - scriptPath: '', - wxml: {}, - wxses: [], - refIds: new Set(), - isApp: false -} + beforeAll(() => { + option = { + script: '', + scriptPath: '', + wxml: '', + path: '', + rootPath: '', + framework: 'react', + isApp: false, + logFilePath: '', + pluginInfo: {} + } + }) -describe('parseScript', () => { - // 调用 parseWXML 需要用到的参数,因parseWXML会先获取缓存,所有每个用例的path需要保持其唯一性 - let path = '' // 解析app.js test('app.js', () => { option.script = ` @@ -41,10 +34,12 @@ describe('parseScript', () => { } }) ` - option.wxml = { type: 'NullLiteral' } - const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true) + // app.js 没有对应的wxml文件 + option.wxml = undefined + option.path = 'app_js' + option.isApp = true + const { ast } = parse(option) const code = generateMinimalEscapeCode(ast) - expect(ast).toBeTruthy() expect(code).toMatchSnapshot() }) @@ -58,38 +53,39 @@ describe('parseScript', () => { } }) ` - option.wxml = { type: 'NullLiteral' } - const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true) + // app.js 没有对应的wxml文件 + option.wxml = undefined + option.path = 'getApp_or_getCurrentPages' + option.isApp = true + const { ast } = parse(option) const code = generateMinimalEscapeCode(ast) expect(code).toMatchSnapshot() }) - // 当wxml是纯文本 parseWXML的返回值 { wxml } 影响js转换 + // 当wxml是纯文本 test('wxml jsxText', () => { - option.script = `App({})` - const wxmlStr = `123` - // 调用 parseWXML 会首先获取缓存,所以第一个参数path建议保持唯一 - path = 'wxml_jsxText' - const { wxml } = parseWXML(path, wxmlStr) - const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true) + option.script = `Page({})` + option.wxml = `123` + option.path = 'wxml_jsxText' + option.isApp = false + const { ast } = parse(option) const code = generateMinimalEscapeCode(ast) expect(code).toMatchSnapshot() }) - // 当wxml存在变量 parseWXML的返回值 { wxml } 影响js转换 + // 当wxml存在变量 test('wxml expression', () => { option.script = ` - App({ + Page({ data:{ srt:'Hello Word!', } }) ` - const wxmlStr = `{{str}}` - // 调用 parseWXML 会首先获取缓存,所以第一个参数path建议保持唯一 - path = 'wxml_expression' - const { wxml } = parseWXML(path, wxmlStr) - const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true) + option.wxml = '{{str}}' + option.path = 'wxml_expression' + option.isApp = false + const { ast } = parse(option) const code = generateMinimalEscapeCode(ast) expect(code).toMatchSnapshot() }) @@ -101,11 +97,11 @@ describe('parseScript', () => { date:'2023-11-11' } */ - const wxmlStr = ` + option.wxml = ` {{wxs_date.date}} ` - path = 'CommonJS_module_exports' + option.path = 'CommonJS_module_exports' option.script = ` module.exports = { data: { @@ -116,12 +112,49 @@ describe('parseScript', () => { }, } ` - const { wxml, wxses }:any = parseWXML(path,wxmlStr) - option.wxml = wxml - option.wxses = wxses - const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true) + option.isApp = false + const { ast } = parse(option) const code = generateMinimalEscapeCode(ast) expect(ast).toBeTruthy() expect(code).toMatchSnapshot() }) + + test('page页面通过插件url跳转到插件页面', () => { + /** + * plugin的目录结构以及内容 + * /plugin/plugin.json:"{ + "pages": { + "hello-page": "pages/hello-page" + }, + "main": "index.js" + }", + /plugin/pages/hello-page.wxml:"This is a plugin page!", + /plugin/pages/hello-page.js:"Page({})", + */ + // 插件的解析在 taro-cli-convertor 中进行 + option.pluginInfo = { + pluginRoot: '/wxProject/plugin', + pluginName: 'hello-plugin', + pages: new Set(['pages/hello-page']), + pagesMap: new Map([['hello-page', 'pages/hello-page']]), + publicComponents: {}, + entryFilePath: '/wxProject/plugin/index.js', + } + option.path = '/wxProject/miniprogram/pages/index' + option.rootPath = '/wxProject/miniprogram' + option.script = ` + Page({ + pluginUrl() { + wx.navigateTo({ + url: 'plugin://hello-plugin/hello-page', + }) + } + })` + option.scriptPath = '/wxProject/miniprogram/pages/index/index.js' + option.wxml = `` + option.isApp = false + const { ast } = parse(option) + const code = generateMinimalEscapeCode(ast) + expect(code).toMatchSnapshot() + }) })