diff --git a/packages/taro-cli/src/doctor/configSchema.ts b/packages/taro-cli/src/doctor/configSchema.ts index 322fb8f098de..2c2ccb17e3f5 100644 --- a/packages/taro-cli/src/doctor/configSchema.ts +++ b/packages/taro-cli/src/doctor/configSchema.ts @@ -51,6 +51,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({ @@ -114,6 +120,7 @@ const schema = Joi.object().keys({ Joi.func() ), enableSourceMap: Joi.bool(), + sourceMapType: Joi.string(), enableExtract: Joi.bool(), cssLoaderOption: Joi.object(), // 第三方配置 styleLoaderOption: Joi.object(), // 第三方配置 diff --git a/packages/taro-mini-runner/package.json b/packages/taro-mini-runner/package.json index 6a174a80ced7..93a4ee7f57af 100644 --- a/packages/taro-mini-runner/package.json +++ b/packages/taro-mini-runner/package.json @@ -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", diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 10e6d81eeaec..a332a64417d9 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -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 { resolveMainFilePath, @@ -45,6 +46,9 @@ interface ITaroMiniPluginOptions { isBuildQuickapp: boolean isSupportRecursive: boolean isSupportXS: boolean + minifyXML?: { + collapseWhitespace?: boolean + } fileType: IFileType templateAdapter: IAdapter modifyBuildAssets?: Function, @@ -117,7 +121,8 @@ export default class TaroMiniPlugin { templ: '.wxml', xs: '.wxs' }, - templateAdapter: weixinAdapter + templateAdapter: weixinAdapter, + minifyXML: {} }, options) setAdapter(this.options.templateAdapter) } @@ -665,8 +670,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 diff --git a/packages/taro-mini-runner/src/webpack/build.conf.ts b/packages/taro-mini-runner/src/webpack/build.conf.ts index 427f8601e649..769b363189fa 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -45,9 +45,12 @@ export default (appPath: string, mode, config: Partial): any => { designWidth = 750, deviceRatio, enableSourceMap = process.env.NODE_ENV !== 'production', + sourceMapType, + debugReact = false, baseLevel = 16, framework = 'nerv', prerender, + minifyXML = {}, defineConstants = {}, env = {}, @@ -100,6 +103,13 @@ export default (appPath: string, mode, config: Partial): 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' @@ -145,7 +155,8 @@ export default (appPath: string, mode, config: Partial): any => { prerender, addChunkPages, modifyMiniConfigs, - modifyBuildAssets + modifyBuildAssets, + minifyXML }) plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{ @@ -181,7 +192,7 @@ export default (appPath: string, mode, config: Partial): any => { chain.merge({ mode, - devtool: getDevtool(enableSourceMap), + devtool: getDevtool(enableSourceMap, sourceMapType), entry: entryRes!.entry, output: getOutput(appPath, [{ outputRoot, diff --git a/packages/taro-mini-runner/src/webpack/chain.ts b/packages/taro-mini-runner/src/webpack/chain.ts index ebf83a39b48c..31550b151ecd 100644 --- a/packages/taro-mini-runner/src/webpack/chain.ts +++ b/packages/taro-mini-runner/src/webpack/chain.ts @@ -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' } diff --git a/packages/taro/types/compile.d.ts b/packages/taro/types/compile.d.ts index 6e92d5168112..9c51d1425dcf 100644 --- a/packages/taro/types/compile.d.ts +++ b/packages/taro/types/compile.d.ts @@ -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,