Skip to content

Commit

Permalink
Merge pull request #7253 from NervJS/feat/mini-options
Browse files Browse the repository at this point in the history
feat(mini-runner): 为小程序端新增若干编译配置项
  • Loading branch information
Chen-jj authored Aug 21, 2020
2 parents 781683e + ca7d34a commit 4c691f5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
7 changes: 7 additions & 0 deletions packages/taro-cli/src/doctor/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const schema = Joi.object().keys({
commonChunks: Joi.alternatives(Joi.func(), Joi.array().items(Joi.string())),
addChunkPages: Joi.func(),
output: Joi.object(),
enableSourceMap: Joi.bool(),
sourceMapType: Joi.string(),
debugReact: Joi.bool(),
minifyXML: Joi.object().keys({
collapseWhitespace: Joi.bool()
}),
postcss: Joi.object().pattern(
Joi.string(),
Joi.object().keys({
Expand Down Expand Up @@ -115,6 +121,7 @@ const schema = Joi.object().keys({
Joi.func()
),
enableSourceMap: Joi.bool(),
sourceMapType: Joi.string(),
enableExtract: Joi.bool(),
cssLoaderOption: Joi.object(), // 第三方配置
styleLoaderOption: Joi.object(), // 第三方配置
Expand Down
1 change: 1 addition & 0 deletions packages/taro-mini-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"csso-webpack-plugin": "^1.0.0-beta.12",
"file-loader": "^6.0.0",
"fs-extra": "^8.0.1",
"html-minifier": "^4.0.0",
"jsdom": "^15.2.1",
"less": "^3.10.3",
"less-loader": "^5.0.0",
Expand Down
17 changes: 15 additions & 2 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as NodeSourcePlugin from 'webpack/lib/node/NodeSourcePlugin'
import * as LoaderTargetPlugin from 'webpack/lib/LoaderTargetPlugin'
import { ConcatSource } from 'webpack-sources'
import { urlToRequest } from 'loader-utils'
import { minify } from 'html-minifier'
import { AppConfig, Config } from '@tarojs/taro'
import { RecursiveTemplate, UnRecursiveTemplate } from '@tarojs/shared'
import {
Expand Down Expand Up @@ -41,6 +42,9 @@ interface ITaroMiniPluginOptions {
prerender?: PrerenderConfig
addChunkPages?: AddPageChunks
isBuildQuickapp: boolean
minifyXML?: {
collapseWhitespace?: boolean
}
fileType: IFileType
template: RecursiveTemplate | UnRecursiveTemplate
modifyBuildAssets?: Function
Expand Down Expand Up @@ -109,7 +113,8 @@ export default class TaroMiniPlugin {
script: '.js',
templ: '.wxml',
xs: '.wxs'
}
},
minifyXML: {}
}, options)

const { template, baseLevel } = this.options
Expand Down Expand Up @@ -679,8 +684,16 @@ export default class TaroMiniPlugin {
}

generateTemplateFile (compilation: webpack.compilation.Compilation, filePath: string, templateFn: (...args) => string, ...options) {
const templStr = templateFn(...options)
let templStr = templateFn(...options)
const fileTemplName = this.getTemplatePath(this.getComponentName(filePath))

if (this.options.minifyXML?.collapseWhitespace) {
templStr = minify(templStr, {
collapseWhitespace: true,
keepClosingSlash: true
})
}

compilation.assets[fileTemplName] = {
size: () => templStr.length,
source: () => templStr
Expand Down
15 changes: 13 additions & 2 deletions packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
designWidth = 750,
deviceRatio,
enableSourceMap = process.env.NODE_ENV !== 'production',
sourceMapType,
debugReact = false,
baseLevel = 16,
framework = 'nerv',
prerender,
minifyXML = {},

defineConstants = {},
env = {},
Expand Down Expand Up @@ -97,6 +100,13 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
alias[taroJsComponents + '$'] = `${taroJsComponents}/mini`
if (framework === 'react') {
alias['react-dom'] = '@tarojs/react'
if (process.env.NODE_ENV !== 'production' && !debugReact) {
alias['react-reconciler'] = 'react-reconciler/cjs/react-reconciler.production.min.js'
// eslint-disable-next-line dot-notation
alias['react'] = 'react/cjs/react.production.min.js'
// eslint-disable-next-line dot-notation
alias['scheduler'] = 'scheduler/cjs/scheduler.production.min.js'
}
}
if (framework === 'nerv') {
alias['react-dom'] = 'nervjs'
Expand Down Expand Up @@ -139,7 +149,8 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
prerender,
addChunkPages,
modifyMiniConfigs,
modifyBuildAssets
modifyBuildAssets,
minifyXML
})

plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{
Expand Down Expand Up @@ -175,7 +186,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {

chain.merge({
mode,
devtool: getDevtool(enableSourceMap),
devtool: getDevtool(enableSourceMap, sourceMapType),
entry: entryRes!.entry,
output: getOutput(appPath, [{
outputRoot,
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-mini-runner/src/webpack/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,6 @@ export function getOutput (appPath: string, [{ outputRoot, publicPath, globalObj
}
}

export function getDevtool (enableSourceMap) {
return enableSourceMap ? 'source-map' : 'none'
export function getDevtool (enableSourceMap, sourceMapType = 'cheap-module-source-map') {
return enableSourceMap ? sourceMapType : 'none'
}
5 changes: 5 additions & 0 deletions packages/taro/types/compile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export interface ICompileOption {
export interface IMiniAppConfig {
appOutput?: boolean,
enableSourceMap?: boolean,
sourceMapType?: string,
debugReact?: boolean,
minifyXML?: {
collapseWhitespace?: boolean
},

webpackChain?: (chain: any, webpack: any, PARSE_AST_TYPE: any) => void,
entry?: webpack.Entry,
Expand Down

0 comments on commit 4c691f5

Please sign in to comment.