UNPKG

6.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const helper_1 = require("@tarojs/helper");
4const lodash_1 = require("lodash");
5const path = require("path");
6const utils_1 = require("../utils");
7const chain_1 = require("../utils/chain");
8const base_conf_1 = require("./base.conf");
9function default_1(appPath, config, appHelper) {
10 const chain = (0, base_conf_1.default)(appPath, config);
11 const { alias = {}, copy, entry = {}, entryFileName = 'app', output = {}, sourceRoot = '', outputRoot = 'dist', publicPath = '/', staticDirectory = 'static', chunkDirectory = 'chunk', router = {}, designWidth = 750, deviceRatio, enableSourceMap = false, sourceMapType, enableExtract = true, defineConstants = {}, env = {}, styleLoaderOption = {}, cssLoaderOption = {}, sassLoaderOption = {}, lessLoaderOption = {}, stylusLoaderOption = {}, mediaUrlLoaderOption = {}, fontUrlLoaderOption = {}, imageUrlLoaderOption = {}, miniCssExtractPluginOption = {}, esnextModules = [], compile = {}, postcss = {}, htmlPluginOption = {}, csso, uglify, terser, useDeprecatedAdapterComponent = false } = config;
12 const sourceDir = path.join(appPath, sourceRoot);
13 const isMultiRouterMode = (0, lodash_1.get)(router, 'mode') === 'multi';
14 const { rule, postcssOption } = (0, chain_1.parseModule)(appPath, {
15 designWidth,
16 deviceRatio,
17 enableExtract,
18 enableSourceMap,
19 styleLoaderOption,
20 cssLoaderOption,
21 lessLoaderOption,
22 sassLoaderOption,
23 stylusLoaderOption,
24 fontUrlLoaderOption,
25 imageUrlLoaderOption,
26 mediaUrlLoaderOption,
27 esnextModules,
28 compile,
29 postcss,
30 sourceDir,
31 staticDirectory
32 });
33 const [, pxtransformOption] = postcssOption.find(([name]) => name === 'postcss-pxtransform') || [];
34 const plugin = {};
35 plugin.mainPlugin = (0, chain_1.getMainPlugin)({
36 /** paths */
37 sourceDir,
38 entryFileName,
39 /** config & message */
40 framework: config.framework,
41 frameworkExts: config.frameworkExts,
42 routerConfig: router,
43 runtimePath: config.runtimePath,
44 pxTransformConfig: (pxtransformOption === null || pxtransformOption === void 0 ? void 0 : pxtransformOption.config) || {},
45 /** building mode */
46 isBuildNativeComp: config.isBuildNativeComp,
47 /** hooks & methods */
48 onCompilerMake: config.onCompilerMake,
49 onParseCreateElement: config.onParseCreateElement,
50 });
51 if (enableExtract) {
52 plugin.miniCssExtractPlugin = (0, chain_1.getMiniCssExtractPlugin)([
53 {
54 filename: 'css/[name].css',
55 chunkFilename: 'css/[name].css'
56 },
57 miniCssExtractPluginOption
58 ]);
59 }
60 if (copy) {
61 plugin.copyWebpackPlugin = (0, chain_1.getCopyWebpackPlugin)({ copy, appPath });
62 }
63 const htmlScript = (0, utils_1.parseHtmlScript)(pxtransformOption);
64 if (process.env.NODE_ENV !== 'production' && htmlScript !== undefined && Object.hasOwnProperty.call(htmlPluginOption, 'script')) {
65 console.warn(helper_1.chalk.yellowBright('配置文件覆盖 htmlPluginOption.script 参数会导致 pxtransform 脚本失效,请慎重使用!'));
66 }
67 const template = path.join(sourceDir, 'index.html');
68 if (helper_1.fs.existsSync(template)) {
69 if (isMultiRouterMode) {
70 delete entry[entryFileName];
71 appHelper.pagesConfigList.forEach((page, index) => {
72 entry[index] = [page];
73 });
74 (0, lodash_1.merge)(plugin, (0, lodash_1.mapValues)(entry, (_filePath, entryName) => {
75 return (0, chain_1.getHtmlWebpackPlugin)([(0, helper_1.recursiveMerge)({
76 filename: `${entryName}.html`,
77 script: htmlScript,
78 template,
79 chunks: [entryName]
80 }, htmlPluginOption)]);
81 }));
82 }
83 else {
84 plugin.htmlWebpackPlugin = (0, chain_1.getHtmlWebpackPlugin)([(0, helper_1.recursiveMerge)({
85 filename: 'index.html',
86 script: htmlScript,
87 template,
88 }, htmlPluginOption)]);
89 }
90 }
91 env.SUPPORT_DINGTALK_NAVIGATE = env.SUPPORT_DINGTALK_NAVIGATE || '"disabled"';
92 defineConstants.DEPRECATED_ADAPTER_COMPONENT = JSON.stringify(!!useDeprecatedAdapterComponent);
93 plugin.definePlugin = (0, chain_1.getDefinePlugin)([(0, chain_1.processEnvOption)(env), defineConstants]);
94 const isCssoEnabled = !(csso && csso.enable === false);
95 if (isCssoEnabled) {
96 plugin.cssoWebpackPlugin = (0, chain_1.getCssoWebpackPlugin)([csso ? csso.config : {}]);
97 }
98 const mode = 'production';
99 const minimizer = [];
100 const uglifyConfig = uglify || terser;
101 const isUglifyEnabled = !(uglifyConfig && uglifyConfig.enable === false);
102 if (isUglifyEnabled) {
103 minimizer.push((0, chain_1.getTerserPlugin)([
104 enableSourceMap,
105 uglifyConfig ? uglifyConfig.config : {}
106 ]));
107 }
108 chain.merge({
109 mode,
110 devtool: (0, chain_1.getDevtool)({ enableSourceMap, sourceMapType }),
111 entry,
112 output: (0, chain_1.getOutput)(appPath, [{
113 outputRoot,
114 publicPath: ['', 'auto'].includes(publicPath) ? publicPath : (0, utils_1.addTrailingSlash)(publicPath),
115 chunkDirectory
116 }, output]),
117 resolve: { alias },
118 module: { rule },
119 plugin,
120 optimization: {
121 minimizer,
122 splitChunks: {
123 name: false,
124 chunks: 'initial',
125 minSize: 0,
126 cacheGroups: {
127 default: false,
128 common: {
129 name: false,
130 minChunks: 2,
131 priority: 1
132 },
133 vendors: {
134 name: false,
135 minChunks: 2,
136 test: module => /[\\/]node_modules[\\/]/.test(module.resource),
137 priority: 10
138 },
139 taro: {
140 name: false,
141 test: module => /@tarojs[\\/][a-z]+/.test(module.context),
142 priority: 100
143 }
144 }
145 }
146 }
147 });
148 return chain;
149}
150exports.default = default_1;
151//# sourceMappingURL=prod.conf.js.map
\No newline at end of file