Skip to content

Commit d97782d

Browse files
authored
Merge pull request #243 from NervJS/feat/4.x-cli-vite
Feat/4.x cli vite
2 parents 7ff3985 + 7177db7 commit d97782d

File tree

8 files changed

+255
-86
lines changed

8 files changed

+255
-86
lines changed

docs/cli.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,24 @@ Taro Doctor 就像一个医生一样,可以诊断项目的依赖、设置、
5151

5252
### 快速创建新页面
5353

54-
Taro create --name [页面名称] 能够在当前项目的 pages 目录下快速生成新的页面文件,并填充基础代码,是一个提高开发效率的利器。
54+
Taro create --name [页面名称] --dir [路径] --subpkg [分包路径] 能够在当前项目的指定目录下快速生成新的页面文件,并填充基础代码,是一个提高开发效率的利器。
5555

56-
> taro 会尝试同步修改 `app.config.js` 配置文件中的 `pages` 字段。
56+
> taro 会尝试同步修改 `app.config.js` 配置文件中的 `pages` 或者 `subPackages` 字段。
57+
58+
#### 例子
59+
假设当前当前跟路径为 `/project/root`
60+
61+
执行下面命令创建主包页面:
62+
```bash
63+
taro create newPage --dir pages/mydir
64+
```
65+
那么会在 `/project/root/src/pages/mydir` 目录下生成新的页面,并且在 `app.config.js` 中自动补齐 `pages` 字段。
66+
67+
执行下面命令创建分包页面:
68+
```bash
69+
taro create newPage --subpkg mysubpages
70+
```
71+
那么会在 `/project/root/src/mysubpages` 目录下生成新的页面,并且在 `app.config.js` 中自动补齐 `subPackages` 字段。
5772

5873
### 快速创建插件模版
5974

docs/config-detail.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,21 +504,24 @@ module.exports = {
504504
505505
:::info
506506
Taro v3.5 开始支持。
507+
508+
509+
Taro v4.0.0 开始支持 `vite` 值。
507510
:::
508511
509512
`string | object`
510513
511514
默认值:`'webpack4'`
512515
513-
使用的编译工具。可选值:`webpack4``webpack5`
516+
使用的编译工具。可选值:`webpack4``webpack5``vite`
514517
515518
取值也可以是对象,此时可以对针对特定的编译器作额外的配置:
516519
517520
### compiler.type
518521
519522
`string`
520523
521-
使用的编译工具。可选值:`webpack4``webpack5`
524+
使用的编译工具。可选值:`webpack4``webpack5``vite`
522525
523526
### compiler.errorLevel
524527
@@ -609,6 +612,14 @@ webpack 编译过程中的错误类型主要如下两类:1、致命的 wepback
609612
610613
不需要执行预编译的依赖。
611614
615+
### compiler.vitePlugins
616+
617+
> 只有 vite 支持
618+
619+
`array`
620+
621+
vite 插件
622+
612623
## cache
613624
614625
:::info
@@ -917,13 +928,13 @@ module.exports = {
917928
selectorBlackList: ['body'],
918929
},
919930
},
920-
// 小程序端样式引用本地资源内联
921-
url: {
922-
enable: true,
923-
config: {
924-
maxSize: 10, // 设定转换尺寸上限(单位:kbytes)
925-
},
926-
},
931+
// 小程序端样式引用本地资源内联 该属性在 v4.0.0 版本已废弃,小程序端默认全部转换
932+
// url: {
933+
// enable: true,
934+
// config: {
935+
// maxSize: 10, // 设定转换尺寸上限(单位:kbytes)
936+
// },
937+
// },
927938
// css modules 功能开关与相关配置
928939
cssModules: {
929940
enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
@@ -1268,7 +1279,10 @@ module.exports = {
12681279

12691280
`object`
12701281

1271-
可用于修改、拓展 Webpack 的 **output** 选项,配置项参考[官方文档](https://webpack.js.org/configuration/output/)。
1282+
可用于修改、拓展 Webpack 的 **output** 选项,配置项参考[webpack官方文档](https://webpack.js.org/configuration/output/)。
1283+
1284+
vite 编译环境下用于修改、扩展 rollup 的 output,目前仅适配 chunkFileNames 和 assetFileNames 两个配置,修改其他配置请使用 vite 插件进行修改。配置想参考[官方文档](https://rollupjs.org/configuration-options/)
1285+
12721286

12731287
```js
12741288
module.exports = {
@@ -1888,6 +1902,18 @@ module.exports = {
18881902

18891903
针对 `woff | woff2 | eot | ttf | otf` 文件的 `url-loader` 配置。配置项参考[官方文档](https://github.com/webpack-contrib/url-loader)。
18901904

1905+
### h5.legacy
1906+
1907+
:::info
1908+
Taro v4.0.0 开始支持。
1909+
:::
1910+
1911+
`boolean`
1912+
1913+
默认值 `false`
1914+
1915+
选择 `vite` 编译器的情况下才会使用到该字段。生成的代码是否要兼容旧版浏览器,值为 true 时,会去读取 package.json 的 browserslist 字段。
1916+
18911917
## rn
18921918

18931919
专属于 RN 的配置。

docs/config.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ const config = {
4949
autoprefixer: {
5050
enable: true,
5151
},
52-
// 小程序端样式引用本地资源内联配置
53-
url: {
54-
enable: true,
55-
config: {
56-
limit: 10240,
57-
},
58-
},
5952
cssModules: {
6053
enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
6154
config: {
@@ -102,6 +95,8 @@ module.exports = function (merge) {
10295
:::info
10396
Taro v3.6.9 开始支持
10497

98+
Taro v4.0.0 之后支持范型,可以传入编译器类型 'webpack4 | 'webpack5 | 'vite'
99+
105100
react native 暂不支持
106101
:::
107102

@@ -113,7 +108,7 @@ react native 暂不支持
113108
// config/index.ts
114109
import { defineConfig } from '@tarojs/cli'
115110

116-
export default defineConfig({
111+
export default defineConfig<T>({
117112
// ...Taro 配置
118113
})
119114
```
@@ -126,7 +121,7 @@ import type { UserConfigExport } from '@tarojs/cli'
126121

127122
export default {
128123
// ...Taro 配置
129-
} satisfies UserConfigExport
124+
} satisfies UserConfigExport<T>
130125
```
131126

132127
### 异步配置

docs/template.md

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,35 @@ zip 包解压出单文件夹,文件夹内包含若干模板。
8383

8484
很多情况下需要为模板加入一些逻辑,从而根据不同的环境生成不同的模板内容。
8585

86-
开发者可以在模板根目录加入 **template_creator.js** 文件,文件对外 exports 包含 handler basePageFiles 字段的对象:
86+
开发者可以在模板根目录加入 **template_creator.js** 文件,文件对外 exports 包含 handler basePageFiles 与 compiler 字段的对象:
8787

8888
```js {5,16} title="template_creator.js"
89-
function createWhenTs(params) {
89+
const path = require('path')
90+
91+
function normalizePath (path) {
92+
return path.replace(/\\/g, '/').replace(/\/{2,}/g, '/')
93+
}
94+
95+
function createWhenTs(err, params) {
9096
return params.typescript ? true : false
9197
}
98+
const SOURCE_ENTRY = '/src'
99+
const PAGES_ENTRY = '/src/pages'
92100

93101
const handler = {
94102
'/global.d.ts': createWhenTs,
95103
'/tsconfig.json': createWhenTs,
96-
'/src/pages/index/index.jsx'({ pageName }) {
97-
return { setPageName: `/src/pages/${pageName}/${pageName}.jsx` }
104+
'/src/pages/index/index.jsx' (err, { pageName = '', pageDir = '', subPkg = '' }) {
105+
return {
106+
setPageName: normalizePath(path.join(PAGES_ENTRY, pageDir, pageName, 'index.jsx')),
107+
setSubPkgName: normalizePath(path.join(SOURCE_ENTRY, subPkg, pageDir, pageName, 'index.jsx'))
108+
}
98109
},
99-
'/src/pages/index/index.css'({ pageName }) {
100-
return { setPageName: `/src/pages/${pageName}/${pageName}.css` }
110+
'/src/pages/index/index.css' (err, { pageName = '', pageDir = '', subPkg = '' }) {
111+
return {
112+
setPageName: normalizePath(path.join(PAGES_ENTRY, pageDir, pageName, 'index.css')),
113+
setSubPkgName: normalizePath(path.join(SOURCE_ENTRY, subPkg, pageDir, pageName, 'index.css'))
114+
}
101115
},
102116
}
103117

@@ -106,12 +120,13 @@ const basePageFiles = ['/src/pages/index/index.jsx', '/src/pages/index/index.css
106120
module.exports = {
107121
handler,
108122
basePageFiles,
123+
compiler: ['Webpack5', 'Webpack4', 'Wite']
109124
}
110125
```
111126

112127
#### 模板语言
113128

114-
请使用 [ejs](https://ejs.co/) 作为模板语言,各模板文件都将接收到全局模板参数。
129+
请使用 [Handlebars](https://handlebarsjs.com/) 作为模板语言,各模板文件都将接收到全局模板参数。
115130

116131
##### 默认全局模板参数(模板中可直接使用的变量)
117132

@@ -121,22 +136,18 @@ module.exports = {
121136
| description | string | 项目描述 |
122137
| version | string | Taro CLI 版本 |
123138
| date | string | 模板创建时间戳 |
124-
| css | 'none' or 'sass' or 'stylus' or 'less' | 样式预处理工具 |
139+
| css | 'None' or 'Sass' or 'Stylus' or 'Less' | 样式预处理工具 |
125140
| cssExt | string | 样式文件后缀 |
126141
| typescript | boolean | 是否使用 TS |
127142
| pageName | string | `taro create` 时传入的页面名称,默认 'index' |
128143
| template | string | 模板名称 |
129-
144+
| framework | 'React' or 'Preact' or 'Vue' or 'Vue3' | 框架名称 |
145+
| compiler | 'Webpack4' or 'Webpack5' or 'Vite' | 编译模式名称 |
130146
##### 例子
131147

132-
```ejs title="index.js"
133-
<%if (typescript) {-%>
134-
import Taro, { Component, Config } from '@tarojs/taro'
135-
<%} else { -%>
136-
import Taro, { Component } from '@tarojs/taro'
137-
<%}-%>
138-
import { View, Text } from '@tarojs/components'
139-
import './<%= pageName %>.<%= cssExt %>'
148+
```handlebars
149+
import { defineConfig{{#if typescript }}, type UserConfigExport{{/if}} } from '@tarojs/cli'
150+
{{#if typescript }}import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'{{/if}}
140151
```
141152

142153
#### handler 字段
@@ -155,6 +166,12 @@ handler 用于控制是否生成某文件,或给文件传入特定参数。
155166

156167
params: object
157168

169+
:::info
170+
`params.pageDir` Taro v4.0.0+ 开始支持
171+
172+
`params.subPkg` Taro v4.0.0+ 开始支持
173+
:::
174+
158175
| 属性 | 类型 | 说明 |
159176
| :----------- | :------------------------------------- | :-------------------------------------------- |
160177
| projectName | string | 项目名 |
@@ -164,13 +181,19 @@ params: object
164181
| css | 'none' or 'sass' or 'stylus' or 'less' | 样式预处理工具 |
165182
| typescript | boolean | 是否使用 TS |
166183
| pageName | string | 页面名称 |
184+
| pageDir | string | 页面路径(相对于「页面目录」的相对路径) taro create 时 --dir 传入的值|
185+
| subPkg | string | 分包页面路径(相对于「src目录」的相对路径) taro create 时 --subpkg 传入的值|
167186
| template | string | 模板名称 |
168187
| templatePath | string | 模板路径 |
169188
| projectPath | string | 目标路径 |
170189
| period | 'createApp' or 'createPage' | `taro init` 创建项目或 `taro create` 创建页面 |
171190

172191
return: boolean/object
173192

193+
:::info
194+
`object.setSubPkgName` Taro v4.0.0+ 开始支持
195+
:::
196+
174197
返回值说明
175198

176199
| 取值 | 说明 |
@@ -191,7 +214,7 @@ return: boolean/object
191214
当用户选择了使用 typescript 时,才生成 **global.d.ts****tsconfig.json** 文件。
192215

193216
```js title="template_creator.js"
194-
function createWhenTs(params) {
217+
function createWhenTs(err, params) {
195218
return params.typescript ? true : false
196219
}
197220

@@ -214,12 +237,24 @@ basePageFiles 告诉 CLI,当用户使用 `taro create` 命令创建页面时
214237
当用户使用命令 `taro create --page=detail` 时,会创建 **/src/pages/detail/detail.jsx****/src/pages/detail/detail.css** 两个文件。
215238

216239
```js title="template_creator.js"
240+
const path = require('path')
241+
242+
function normalizePath (path) {
243+
return path.replace(/\\/g, '/').replace(/\/{2,}/g, '/')
244+
}
245+
217246
const handler = {
218-
'/src/pages/index/index.jsx'({ pageName }) {
219-
return { setPageName: `/src/pages/${pageName}/${pageName}.jsx` }
247+
'/src/pages/index/index.jsx' (err, { pageName = '', pageDir = '', subPkg = '' }) {
248+
return {
249+
setPageName: normalizePath(path.join(PAGES_ENTRY, pageDir, pageName, 'index.jsx')),
250+
setSubPkgName: normalizePath(path.join(SOURCE_ENTRY, subPkg, pageDir, pageName, 'index.jsx'))
251+
}
220252
},
221-
'/src/pages/index/index.css'({ pageName }) {
222-
return { setPageName: `/src/pages/${pageName}/${pageName}.css` }
253+
'/src/pages/index/index.css' (err, { pageName = '', pageDir = '', subPkg = '' }) {
254+
return {
255+
setPageName: normalizePath(path.join(PAGES_ENTRY, pageDir, pageName, 'index.css')),
256+
setSubPkgName: normalizePath(path.join(SOURCE_ENTRY, subPkg, pageDir, pageName, 'index.css'))
257+
}
223258
},
224259
}
225260

@@ -230,3 +265,11 @@ module.exports = {
230265
basePageFiles,
231266
}
232267
```
268+
269+
### compiler 字段
270+
271+
:::info
272+
Taro v4.0.0+ 开始支持
273+
:::
274+
275+
compiler 告诉 cli 当前模版支持的编译器类型,该值是一个 `string[]`,目前 taro 支持的编译器类型有 `Webpack4、Webpack5、Vite`

versioned_docs/version-4.x/cli.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,24 @@ Taro Doctor 就像一个医生一样,可以诊断项目的依赖、设置、
6262

6363
### 快速创建新页面
6464

65-
Taro create --name [页面名称] 能够在当前项目的 pages 目录下快速生成新的页面文件,并填充基础代码,是一个提高开发效率的利器。
65+
Taro create --name [页面名称] --dir [路径] --subpkg [分包路径] 能够在当前项目的指定目录下快速生成新的页面文件,并填充基础代码,是一个提高开发效率的利器。
6666

67-
> taro 会尝试同步修改 `app.config.js` 配置文件中的 `pages` 字段。
67+
> taro 会尝试同步修改 `app.config.js` 配置文件中的 `pages` 或者 `subPackages` 字段。
68+
69+
#### 例子
70+
假设当前当前跟路径为 `/project/root`
71+
72+
执行下面命令创建主包页面:
73+
```bash
74+
taro create newPage --dir pages/mydir
75+
```
76+
那么会在 `/project/root/src/pages/mydir` 目录下生成新的页面,并且在 `app.config.js` 中自动补齐 `pages` 字段。
77+
78+
执行下面命令创建分包页面:
79+
```bash
80+
taro create newPage --subpkg mysubpages
81+
```
82+
那么会在 `/project/root/src/mysubpages` 目录下生成新的页面,并且在 `app.config.js` 中自动补齐 `subPackages` 字段。
6883

6984
### 快速创建插件模版
7085

0 commit comments

Comments
 (0)