diff --git a/docs/config-detail.md b/docs/config-detail.md index 1e144950705b..c5e4f083d522 100644 --- a/docs/config-detail.md +++ b/docs/config-detail.md @@ -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 的,并不需要这样做。 @@ -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, 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),例如: diff --git a/packages/taro-mini-runner/src/index.ts b/packages/taro-mini-runner/src/index.ts index 52b21876e76e..8b797751aecd 100644 --- a/packages/taro-mini-runner/src/index.ts +++ b/packages/taro-mini-runner/src/index.ts @@ -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' @@ -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) } } diff --git a/packages/taro-mini-runner/src/webpack/build.conf.ts b/packages/taro-mini-runner/src/webpack/build.conf.ts index f2f67f58eecc..b16bb5910f20 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -180,6 +180,7 @@ export default (appPath: string, mode, config: Partial): any => { designWidth, deviceRatio, enableSourceMap, + compile: config.compile || {}, cssLoaderOption, lessLoaderOption, diff --git a/packages/taro-mini-runner/src/webpack/chain.ts b/packages/taro-mini-runner/src/webpack/chain.ts index 3101d3a1d36c..cca081c71e53 100644 --- a/packages/taro-mini-runner/src/webpack/chain.ts +++ b/packages/taro-mini-runner/src/webpack/chain.ts @@ -186,6 +186,7 @@ export const getModule = (appPath: string, { buildAdapter, // constantsReplaceList, enableSourceMap, + compile, cssLoaderOption, lessLoaderOption, @@ -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([]) } diff --git a/packages/taro-runner-utils/src/constants.ts b/packages/taro-runner-utils/src/constants.ts index edb202ca70b7..bac958287b44 100644 --- a/packages/taro-runner-utils/src/constants.ts +++ b/packages/taro-runner-utils/src/constants.ts @@ -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', diff --git a/packages/taro-runner-utils/types/constants.d.ts b/packages/taro-runner-utils/types/constants.d.ts index 42ae9435863d..87f0ebad0184 100644 --- a/packages/taro-runner-utils/types/constants.d.ts +++ b/packages/taro-runner-utils/types/constants.d.ts @@ -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",