Skip to content

Commit

Permalink
feat(mini-runner): 增加compile的include和exclude配置 (#6226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj authored May 7, 2020
1 parent 0dd1d30 commit 6914f54
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 8 deletions.
74 changes: 68 additions & 6 deletions docs/config-detail.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,47 @@ copy: {

#### mini.compile.exclude

配置小程序编译过程中排除不需要经过 Taro 编译的文件,数组类型,写文件路径,文件路径必须以源码所在 `src` 目录开头:
配置小程序编译过程中排除不需要经过 Taro 编译的文件,数组类型,数组里面可以包含具体文件路径,也可以是判断函数,同 [Rule.exclude](https://webpack.js.org/configuration/module/#ruleexclude)

```jsx
mini: {
compile: {
exclude: ['src/components/ec-canvas/echarts.js']
例如,想要排除某个文件,可以如下配置要排除的文件具体路径:

```js
const config = {
mini: {
compile: {
exclude: [
path.resolve(__dirname, '..', 'src/pages/index/vod-wx-sdk-v2.js')
]
}
}
}
```

也可以配置判断函数,如下

```js
const config = {
mini: {
compile: {
exclude: [
function (modulePath) {
return modulePath.indexOf('vod-wx-sdk-v2') >= 0
}
]
}
}
}
```

#### mini.compile.incldue

配置额外需要经过 Taro 编译的文件,例如 Taro 默认不编译 `node_modules` 包中文件,可以通过这个配置让 Taro 编译 `node_modules` 包中文件,使用方式与 `mini.compile.exclude` 一致,同 [Rule.include](https://webpack.js.org/configuration/module/#ruleinclude)

### mini.webpackChain

自定义 Webpack 配置,接受函数形式的配置。

这个函数会收到两个参数,第一个参数是 webpackChain 对象,可参考 [webpack-chain](https://github.com/neutrinojs/webpack-chain) 的 api 进行修改;第二个参数是 `webpack` 实例。例如:
这个函数会收到三个参数,第一个参数是 webpackChain 对象,可参考 [webpack-chain](https://github.com/neutrinojs/webpack-chain) 的 api 进行修改;第二个参数是 `webpack` 实例;第三个参数 `PARSE_AST_TYPE` 是小程序编译时的文件类型集合。例如:

```jsx
// 这是一个添加 ts-loader 的例子,但事实上 taro 是默认支持 ts 的,并不需要这样做。
Expand Down Expand Up @@ -346,6 +372,42 @@ mini: {
}
```

注意:第三个参数的取值如下

```typescript
export enum PARSE_AST_TYPE {
ENTRY = 'ENTRY',
PAGE = 'PAGE',
COMPONENT = 'COMPONENT',
NORMAL = 'NORMAL',
STATIC = 'STATIC'
}
```

### mini.addChunkPages

> 2.0.5 开始支持
> `type addChunkPages = ((pages: Map<string, string[]>, pagesNames?: string[]) => void)`
在某些情况下,我们可能需要为某些页面单独指定需要引用的公共文件,例如,使用小程序分包的时候,为了减少主包大小,分包的页面希望引入自己的公共文件,而不希望直接放在主包内,那么我们首先可以通过配置 `mini.webpackChain` 来单独抽离分包的公共文件,然后通过 `mini.addChunkPages` 为分包页面配置引入子包公共文件,其使用方式如下:

`mini.addChunkPages` 配置为一个函数,接受两个参数

* `pages` 参数为 Map 类型,用于为页面添加公共文件
* `pagesNames` 参数为当前应用的所有页面标识列表,可以通过打印的方式进行查看页面的标识

例如,为 `pages/index/index` 页面添加 `eating``morning` 两个抽离的公共文件

```js
const config = {
mini: {
addChunkPages (pages, pagesNames) {
pages.set('pages/index/index', ['eating', 'morning'])
}
}
}
```

### mini.cssLoaderOption

css-loader 的附加配置。配置项参考[官方文档](https://github.com/webpack-contrib/css-loader),例如:
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as webpack from 'webpack'
import { BUILD_TYPES } from '@tarojs/runner-utils'
import { BUILD_TYPES, PARSE_AST_TYPE } from '@tarojs/runner-utils'

import { IBuildConfig } from './utils/types'
import { printBuildError, bindProdLogger, bindDevLogger } from './utils/logHelper'
Expand All @@ -10,7 +10,7 @@ import { makeConfig } from './webpack/chain'

const customizeChain = (chain, customizeFunc: Function) => {
if (customizeFunc instanceof Function) {
customizeFunc(chain, webpack)
customizeFunc(chain, webpack, PARSE_AST_TYPE)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
designWidth,
deviceRatio,
enableSourceMap,
compile: config.compile || {},

cssLoaderOption,
lessLoaderOption,
Expand Down
3 changes: 3 additions & 0 deletions packages/taro-mini-runner/src/webpack/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export const getModule = (appPath: string, {
buildAdapter,
// constantsReplaceList,
enableSourceMap,
compile,

cssLoaderOption,
lessLoaderOption,
Expand Down Expand Up @@ -368,6 +369,8 @@ export const getModule = (appPath: string, {
},
script: {
test: REG_SCRIPTS,
exclude: compile.exclude && compile.exclude.length ? compile.exclude : [filename => /node_modules/.test(filename)],
include: compile.include && compile.include.length ? compile.include : [],
use: {
babelLoader: getBabelLoader([])
}
Expand Down
9 changes: 9 additions & 0 deletions packages/taro-runner-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ export enum META_TYPE {
CONFIG = 'CONFIG'
}

export enum PARSE_AST_TYPE {
ENTRY = 'ENTRY',
PAGE = 'PAGE',
COMPONENT = 'COMPONENT',
NORMAL = 'NORMAL',
STATIC = 'STATIC',
EXPORTS = 'EXPORTS'
}

export const enum processTypeEnum {
START = 'start',
CREATE = 'create',
Expand Down
8 changes: 8 additions & 0 deletions packages/taro-runner-utils/types/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ export declare enum META_TYPE {
STATIC = "STATIC",
CONFIG = "CONFIG"
}
export declare enum PARSE_AST_TYPE {
ENTRY = 'ENTRY',
PAGE = 'PAGE',
COMPONENT = 'COMPONENT',
NORMAL = 'NORMAL',
STATIC = 'STATIC',
EXPORTS = 'EXPORTS'
}
export declare const enum processTypeEnum {
START = "start",
CREATE = "create",
Expand Down

0 comments on commit 6914f54

Please sign in to comment.