UNPKG

6.02 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 = 'src', outputRoot = 'dist', publicPath = '/', staticDirectory = 'static', chunkDirectory = 'chunk', router = {}, designWidth = 750, deviceRatio, enableSourceMap = true, sourceMapType, enableExtract = false, defineConstants = {}, env = {}, styleLoaderOption = {}, cssLoaderOption = {}, sassLoaderOption = {}, lessLoaderOption = {}, stylusLoaderOption = {}, mediaUrlLoaderOption = {}, fontUrlLoaderOption = {}, imageUrlLoaderOption = {}, miniCssExtractPluginOption = {}, esnextModules = [], compile = {}, postcss = {}, htmlPluginOption = {}, 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 mode = 'development';
95 chain.merge({
96 mode,
97 devtool: (0, chain_1.getDevtool)({ enableSourceMap, sourceMapType }),
98 entry,
99 output: (0, chain_1.getOutput)(appPath, [{
100 outputRoot,
101 publicPath: ['', 'auto'].includes(publicPath) ? publicPath : (0, utils_1.addTrailingSlash)(publicPath),
102 chunkDirectory
103 }, output]),
104 resolve: { alias },
105 module: { rule },
106 plugin,
107 optimization: {
108 noEmitOnErrors: true,
109 splitChunks: {
110 chunks: 'initial',
111 minSize: 0,
112 cacheGroups: {
113 common: {
114 name: 'common',
115 minChunks: 2,
116 priority: 1
117 },
118 vendors: {
119 name: 'vendors',
120 minChunks: 2,
121 test: module => /[\\/]node_modules[\\/]/.test(module.resource),
122 priority: 10
123 },
124 taro: {
125 name: 'taro',
126 test: module => /@tarojs[\\/][a-z]+/.test(module.context),
127 priority: 100
128 }
129 }
130 }
131 }
132 });
133 return chain;
134}
135exports.default = default_1;
136//# sourceMappingURL=dev.conf.js.map
\No newline at end of file