1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __rest = (this && this.__rest) || function (s, e) {
|
12 | var t = {};
|
13 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
14 | t[p] = s[p];
|
15 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
16 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
17 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
18 | t[p[i]] = s[p[i]];
|
19 | }
|
20 | return t;
|
21 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | exports.getRuntimeConstants = exports.getDevtool = exports.getOutput = exports.getEntry = exports.getModule = exports.getProviderPlugin = exports.getBuildNativePlugin = exports.getMiniSplitChunksPlugin = exports.getMiniPlugin = exports.getCopyWebpackPlugin = exports.getCssoWebpackPlugin = exports.getTerserPlugin = exports.getDefinePlugin = exports.getMiniCssExtractPlugin = exports.getResolveUrlLoader = exports.getMiniTemplateLoader = exports.getFileLoader = exports.getUrlLoader = exports.getStylusLoader = exports.getLessLoader = exports.getSassLoader = exports.getPostcssLoader = exports.getCssLoader = exports.processEnvOption = exports.mergeOption = exports.makeConfig = void 0;
|
24 | const helper_1 = require("@tarojs/helper");
|
25 | const runner_utils_1 = require("@tarojs/runner-utils");
|
26 | const CopyWebpackPlugin = require("copy-webpack-plugin");
|
27 | const csso_webpack_plugin_1 = require("csso-webpack-plugin");
|
28 | const lodash_1 = require("lodash");
|
29 | const fp_1 = require("lodash/fp");
|
30 | const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
31 | const path = require("path");
|
32 | const sass = require("sass");
|
33 | const TerserPlugin = require("terser-webpack-plugin");
|
34 | const webpack = require("webpack");
|
35 | const terserOptions_1 = require("../config/terserOptions");
|
36 | const BuildNativePlugin_1 = require("../plugins/BuildNativePlugin");
|
37 | const MiniPlugin_1 = require("../plugins/MiniPlugin");
|
38 | const MiniSplitChunksPlugin_1 = require("../plugins/MiniSplitChunksPlugin");
|
39 | const postcss_conf_1 = require("./postcss.conf");
|
40 | const makeConfig = (buildConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
41 | const sassLoaderOption = yield (0, runner_utils_1.getSassLoaderOption)(buildConfig);
|
42 | return Object.assign(Object.assign({}, buildConfig), { sassLoaderOption, frameworkExts: buildConfig.frameworkExts || helper_1.SCRIPT_EXT });
|
43 | });
|
44 | exports.makeConfig = makeConfig;
|
45 | const defaultCSSCompressOption = {
|
46 | mergeRules: false,
|
47 | mergeIdents: false,
|
48 | reduceIdents: false,
|
49 | discardUnused: false,
|
50 | minifySelectors: false
|
51 | };
|
52 | const defaultMediaUrlLoaderOption = {
|
53 | limit: 10240,
|
54 | esModule: false
|
55 | };
|
56 | const defaultFontUrlLoaderOption = {
|
57 | limit: 10240,
|
58 | esModule: false
|
59 | };
|
60 | const defaultImageUrlLoaderOption = {
|
61 | limit: 2046,
|
62 | esModule: false
|
63 | };
|
64 | const defaultCssModuleOption = {
|
65 | enable: false,
|
66 | config: {
|
67 | namingPattern: 'global',
|
68 | generateScopedName: '[name]__[local]___[hash:base64:5]'
|
69 | }
|
70 | };
|
71 | const defaultUrlOption = {
|
72 | enable: true,
|
73 | config: {
|
74 | limit: 10240
|
75 | }
|
76 | };
|
77 | const getLoader = (loaderName, options) => {
|
78 | return {
|
79 | loader: require.resolve(loaderName),
|
80 | options: options || {}
|
81 | };
|
82 | };
|
83 | const listify = listOrItem => {
|
84 | if (Array.isArray(listOrItem)) {
|
85 | return listOrItem;
|
86 | }
|
87 | return [listOrItem];
|
88 | };
|
89 | const getPlugin = (plugin, args) => {
|
90 | return {
|
91 | plugin,
|
92 | args
|
93 | };
|
94 | };
|
95 | const mergeOption = ([...options]) => {
|
96 | return (0, helper_1.recursiveMerge)({}, ...options);
|
97 | };
|
98 | exports.mergeOption = mergeOption;
|
99 | const styleModuleReg = /(.*\.module).*\.(css|s[ac]ss|less|styl)\b/;
|
100 | const styleGlobalReg = /(.*\.global).*\.(css|s[ac]ss|less|styl)\b/;
|
101 | exports.processEnvOption = (0, lodash_1.partial)(fp_1.mapKeys, (key) => `process.env.${key}`);
|
102 | exports.getCssLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'css-loader'));
|
103 | exports.getPostcssLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'postcss-loader'));
|
104 | exports.getSassLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'sass-loader'));
|
105 | exports.getLessLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'less-loader'));
|
106 | exports.getStylusLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'stylus-loader'));
|
107 | exports.getUrlLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'url-loader'));
|
108 | exports.getFileLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'file-loader'));
|
109 | exports.getMiniTemplateLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, path.resolve(__dirname, '../loaders/miniTemplateLoader')));
|
110 | exports.getResolveUrlLoader = (0, fp_1.pipe)(exports.mergeOption, (0, lodash_1.partial)(getLoader, 'resolve-url-loader'));
|
111 | const getExtractCssLoader = () => {
|
112 | return {
|
113 | loader: MiniCssExtractPlugin.loader
|
114 | };
|
115 | };
|
116 | exports.getMiniCssExtractPlugin = (0, fp_1.pipe)(exports.mergeOption, listify, (0, lodash_1.partial)(getPlugin, MiniCssExtractPlugin));
|
117 | exports.getDefinePlugin = (0, fp_1.pipe)(exports.mergeOption, listify, (0, lodash_1.partial)(getPlugin, webpack.DefinePlugin));
|
118 | const getTerserPlugin = ([enableSourceMap, terserOptions]) => {
|
119 | return new TerserPlugin({
|
120 | cache: true,
|
121 | parallel: true,
|
122 | sourceMap: enableSourceMap,
|
123 | terserOptions: (0, helper_1.recursiveMerge)({}, terserOptions_1.default, terserOptions)
|
124 | });
|
125 | };
|
126 | exports.getTerserPlugin = getTerserPlugin;
|
127 | const getCssoWebpackPlugin = ([cssoOption]) => {
|
128 | return (0, fp_1.pipe)(listify, (0, lodash_1.partial)(getPlugin, csso_webpack_plugin_1.default))([(0, exports.mergeOption)([defaultCSSCompressOption, cssoOption]), helper_1.REG_STYLE]);
|
129 | };
|
130 | exports.getCssoWebpackPlugin = getCssoWebpackPlugin;
|
131 | const getCopyWebpackPlugin = ({ copy, appPath }) => {
|
132 | const args = [
|
133 | copy.patterns.map((_a) => {
|
134 | var { from, to } = _a, extra = __rest(_a, ["from", "to"]);
|
135 | return Object.assign({ from, to: path.resolve(appPath, to), context: appPath }, extra);
|
136 | }),
|
137 | copy.options
|
138 | ];
|
139 | return (0, lodash_1.partial)(getPlugin, CopyWebpackPlugin)(args);
|
140 | };
|
141 | exports.getCopyWebpackPlugin = getCopyWebpackPlugin;
|
142 | const getMiniPlugin = args => {
|
143 | return (0, lodash_1.partial)(getPlugin, MiniPlugin_1.default)([args]);
|
144 | };
|
145 | exports.getMiniPlugin = getMiniPlugin;
|
146 | const getMiniSplitChunksPlugin = (args) => {
|
147 | return (0, lodash_1.partial)(getPlugin, MiniSplitChunksPlugin_1.default)([args]);
|
148 | };
|
149 | exports.getMiniSplitChunksPlugin = getMiniSplitChunksPlugin;
|
150 | const getBuildNativePlugin = args => {
|
151 | return (0, lodash_1.partial)(getPlugin, BuildNativePlugin_1.default)([args]);
|
152 | };
|
153 | exports.getBuildNativePlugin = getBuildNativePlugin;
|
154 | const getProviderPlugin = args => {
|
155 | return (0, lodash_1.partial)(getPlugin, webpack.ProvidePlugin)([args]);
|
156 | };
|
157 | exports.getProviderPlugin = getProviderPlugin;
|
158 | const getModule = (appPath, { sourceDir, designWidth, deviceRatio, buildAdapter, isBuildQuickapp,
|
159 |
|
160 | enableSourceMap, compile, cssLoaderOption, lessLoaderOption, sassLoaderOption, stylusLoaderOption, fontUrlLoaderOption, imageUrlLoaderOption, mediaUrlLoaderOption, postcss, fileType }) => {
|
161 | const postcssOption = postcss || {};
|
162 | const cssModuleOptions = (0, helper_1.recursiveMerge)({}, defaultCssModuleOption, postcssOption.cssModules);
|
163 | const { namingPattern, generateScopedName } = cssModuleOptions.config;
|
164 | const cssOptions = [
|
165 | {
|
166 | importLoaders: 1,
|
167 | sourceMap: enableSourceMap,
|
168 | modules: false
|
169 | },
|
170 | cssLoaderOption
|
171 | ];
|
172 | const cssOptionsWithModule = [
|
173 | Object.assign({
|
174 | importLoaders: 1,
|
175 | sourceMap: enableSourceMap,
|
176 | modules: {
|
177 | mode: namingPattern === 'module' ? 'local' : 'global'
|
178 | }
|
179 | }, {
|
180 | modules: typeof generateScopedName === 'function'
|
181 | ? { getLocalIdent: (context, _, localName) => generateScopedName(localName, context.resourcePath) }
|
182 | : { localIdentName: generateScopedName }
|
183 | }),
|
184 | cssLoaderOption
|
185 | ];
|
186 | const extractCssLoader = getExtractCssLoader();
|
187 | const miniTemplateLoader = (0, exports.getMiniTemplateLoader)([{
|
188 | buildAdapter
|
189 | }]);
|
190 | const cssLoader = (0, exports.getCssLoader)(cssOptions);
|
191 | const baseSassOptions = {
|
192 | sourceMap: true,
|
193 | implementation: sass,
|
194 | sassOptions: {
|
195 | outputStyle: 'expanded',
|
196 | fiber: false,
|
197 | importer(url, prev, done) {
|
198 |
|
199 | const extname = path.extname(url);
|
200 |
|
201 | if (extname === '.scss' || extname === '.sass' || extname === '.css' || !extname) {
|
202 | return null;
|
203 | }
|
204 | else {
|
205 | const filePath = path.resolve(path.dirname(prev), url);
|
206 | helper_1.fs.access(filePath, helper_1.fs.constants.F_OK, (err) => {
|
207 | if (err) {
|
208 | console.log(err);
|
209 | return null;
|
210 | }
|
211 | else {
|
212 | helper_1.fs.readFile(filePath)
|
213 | .then(res => {
|
214 | done({ contents: res.toString() });
|
215 | })
|
216 | .catch(err => {
|
217 | console.log(err);
|
218 | return null;
|
219 | });
|
220 | }
|
221 | });
|
222 | }
|
223 | }
|
224 | }
|
225 | };
|
226 | const sassLoader = (0, exports.getSassLoader)([baseSassOptions, {
|
227 | sassOptions: {
|
228 | indentedSyntax: true
|
229 | }
|
230 | }, sassLoaderOption]);
|
231 | const scssLoader = (0, exports.getSassLoader)([baseSassOptions, sassLoaderOption]);
|
232 | const resolveUrlLoader = (0, exports.getResolveUrlLoader)([{}]);
|
233 | const postcssLoader = (0, exports.getPostcssLoader)([
|
234 | { sourceMap: enableSourceMap },
|
235 | {
|
236 | postcssOptions: {
|
237 | plugins: (0, postcss_conf_1.getPostcssPlugins)(appPath, {
|
238 | isBuildQuickapp,
|
239 | designWidth,
|
240 | deviceRatio,
|
241 | postcssOption
|
242 | })
|
243 | }
|
244 | }
|
245 | ]);
|
246 | const lessLoader = (0, exports.getLessLoader)([{ sourceMap: enableSourceMap }, lessLoaderOption]);
|
247 | const stylusLoader = (0, exports.getStylusLoader)([{ sourceMap: enableSourceMap }, stylusLoaderOption]);
|
248 | const cssLoaders = [{
|
249 | use: [
|
250 | extractCssLoader,
|
251 | cssLoader,
|
252 | postcssLoader
|
253 | ]
|
254 | }];
|
255 | if (cssModuleOptions.enable) {
|
256 | const cssLoaderWithModule = (0, exports.getCssLoader)(cssOptionsWithModule);
|
257 | let cssModuleCondition;
|
258 | if (cssModuleOptions.config.namingPattern === 'module') {
|
259 |
|
260 | cssModuleCondition = styleModuleReg;
|
261 |
|
262 | cssLoaders.unshift({
|
263 | resourceQuery: /module=/,
|
264 | use: [
|
265 | extractCssLoader,
|
266 | cssLoaderWithModule,
|
267 | postcssLoader
|
268 | ]
|
269 | });
|
270 | }
|
271 | else {
|
272 | cssModuleCondition = {
|
273 | and: [
|
274 | { exclude: styleGlobalReg },
|
275 | { exclude: [helper_1.isNodeModule] }
|
276 | ]
|
277 | };
|
278 | }
|
279 | cssLoaders.unshift({
|
280 | include: [cssModuleCondition],
|
281 | use: [
|
282 | extractCssLoader,
|
283 | cssLoaderWithModule,
|
284 | postcssLoader
|
285 | ]
|
286 | });
|
287 | }
|
288 | const urlOptions = (0, helper_1.recursiveMerge)({}, defaultUrlOption, postcssOption.url);
|
289 | let postcssUrlOption;
|
290 | if (urlOptions.enable) {
|
291 | postcssUrlOption = urlOptions.config;
|
292 | }
|
293 | function addCssLoader(cssLoaders, ...loader) {
|
294 | const cssLoadersCopy = (0, lodash_1.cloneDeep)(cssLoaders);
|
295 | cssLoadersCopy.forEach(item => {
|
296 | if (item.use) {
|
297 | item.use = [...item.use, ...loader];
|
298 | }
|
299 | });
|
300 | return cssLoadersCopy;
|
301 | }
|
302 | const scriptRule = {
|
303 | test: helper_1.REG_SCRIPTS,
|
304 | use: {
|
305 | babelLoader: {
|
306 | loader: require.resolve('babel-loader')
|
307 | }
|
308 | }
|
309 | };
|
310 | if (compile.exclude && compile.exclude.length) {
|
311 | scriptRule.exclude = [
|
312 | ...compile.exclude,
|
313 | filename => /css-loader/.test(filename) || (/node_modules/.test(filename) && !(/taro/.test(filename)))
|
314 | ];
|
315 | }
|
316 | else if (compile.include && compile.include.length) {
|
317 | scriptRule.include = [
|
318 | ...compile.include,
|
319 | sourceDir,
|
320 | filename => /taro/.test(filename)
|
321 | ];
|
322 | }
|
323 | else {
|
324 | scriptRule.exclude = [filename => /css-loader/.test(filename) || (/node_modules/.test(filename) && !(/taro/.test(filename)))];
|
325 | }
|
326 | const rule = {
|
327 | sass: {
|
328 | test: helper_1.REG_SASS_SASS,
|
329 | oneOf: addCssLoader(cssLoaders, resolveUrlLoader, sassLoader)
|
330 | },
|
331 | scss: {
|
332 | test: helper_1.REG_SASS_SCSS,
|
333 | oneOf: addCssLoader(cssLoaders, resolveUrlLoader, scssLoader)
|
334 | },
|
335 | less: {
|
336 | test: helper_1.REG_LESS,
|
337 | oneOf: addCssLoader(cssLoaders, lessLoader)
|
338 | },
|
339 | stylus: {
|
340 | test: helper_1.REG_STYLUS,
|
341 | oneOf: addCssLoader(cssLoaders, stylusLoader)
|
342 | },
|
343 | nomorlCss: {
|
344 | test: helper_1.REG_CSS,
|
345 | oneOf: cssLoaders
|
346 | },
|
347 | script: scriptRule,
|
348 | template: {
|
349 | test: helper_1.REG_TEMPLATE,
|
350 | use: [(0, exports.getFileLoader)([{
|
351 | useRelativePath: true,
|
352 | name: `[path][name]${fileType.templ}`,
|
353 | context: sourceDir
|
354 | }]), miniTemplateLoader]
|
355 | },
|
356 | media: {
|
357 | test: helper_1.REG_MEDIA,
|
358 | use: {
|
359 | urlLoader: (0, exports.getUrlLoader)([defaultMediaUrlLoaderOption, Object.assign(Object.assign({ name: '[path][name].[ext]', useRelativePath: true, context: sourceDir }, (postcssUrlOption || {})), mediaUrlLoaderOption)])
|
360 | }
|
361 | },
|
362 | font: {
|
363 | test: helper_1.REG_FONT,
|
364 | use: {
|
365 | urlLoader: (0, exports.getUrlLoader)([defaultFontUrlLoaderOption, Object.assign(Object.assign({ name: '[path][name].[ext]', useRelativePath: true, context: sourceDir }, (postcssUrlOption || {})), fontUrlLoaderOption)])
|
366 | }
|
367 | },
|
368 | image: {
|
369 | test: helper_1.REG_IMAGE,
|
370 | use: {
|
371 | urlLoader: (0, exports.getUrlLoader)([defaultImageUrlLoaderOption, Object.assign(Object.assign({ name: '[path][name].[ext]', useRelativePath: true, context: sourceDir }, (postcssUrlOption || {})), imageUrlLoaderOption)])
|
372 | }
|
373 | }
|
374 | };
|
375 | return { rule };
|
376 | };
|
377 | exports.getModule = getModule;
|
378 | const getEntry = ({ sourceDir, entry, isBuildPlugin }) => {
|
379 | if (!isBuildPlugin) {
|
380 | return {
|
381 | entry
|
382 | };
|
383 | }
|
384 | const pluginDir = path.join(sourceDir, 'plugin');
|
385 | if (!helper_1.fs.existsSync(pluginDir)) {
|
386 | console.log(helper_1.chalk.red('插件目录不存在,请检查!'));
|
387 | return;
|
388 | }
|
389 | const pluginConfigPath = path.join(pluginDir, 'plugin.json');
|
390 | if (!helper_1.fs.existsSync(pluginConfigPath)) {
|
391 | console.log(helper_1.chalk.red('缺少插件配置文件,请检查!'));
|
392 | return;
|
393 | }
|
394 | const pluginConfig = helper_1.fs.readJSONSync(pluginConfigPath);
|
395 | const entryObj = {};
|
396 | let pluginMainEntry;
|
397 | Object.keys(pluginConfig).forEach(key => {
|
398 | if (key === 'main') {
|
399 | const filePath = path.join(pluginDir, pluginConfig[key]);
|
400 | const fileName = path.basename(filePath).replace(path.extname(filePath), '');
|
401 | pluginMainEntry = `plugin/${fileName}`;
|
402 | entryObj[pluginMainEntry] = [(0, helper_1.resolveMainFilePath)(filePath.replace(path.extname(filePath), ''))];
|
403 | }
|
404 | else if (key === 'publicComponents' || key === 'pages') {
|
405 | Object.keys(pluginConfig[key]).forEach(subKey => {
|
406 | const filePath = path.join(pluginDir, pluginConfig[key][subKey]);
|
407 | entryObj[`plugin/${pluginConfig[key][subKey]}`] = [(0, helper_1.resolveMainFilePath)(filePath.replace(path.extname(filePath), ''))];
|
408 | });
|
409 | }
|
410 | });
|
411 | return {
|
412 | entry: entryObj,
|
413 | pluginConfig,
|
414 | pluginMainEntry
|
415 | };
|
416 | };
|
417 | exports.getEntry = getEntry;
|
418 | function getOutput(appPath, [{ outputRoot, publicPath, globalObject }, customOutput]) {
|
419 | return Object.assign({ path: path.resolve(appPath, outputRoot), publicPath, filename: '[name].js', chunkFilename: '[name].js', globalObject }, customOutput);
|
420 | }
|
421 | exports.getOutput = getOutput;
|
422 | function getDevtool(enableSourceMap, sourceMapType = 'cheap-module-source-map') {
|
423 | return enableSourceMap ? sourceMapType : 'none';
|
424 | }
|
425 | exports.getDevtool = getDevtool;
|
426 | function getRuntimeConstants(runtime) {
|
427 | var _a, _b, _c, _d, _e, _f, _g;
|
428 | const constants = {};
|
429 | constants.ENABLE_INNER_HTML = (_a = runtime.enableInnerHTML) !== null && _a !== void 0 ? _a : true;
|
430 | constants.ENABLE_ADJACENT_HTML = (_b = runtime.enableAdjacentHTML) !== null && _b !== void 0 ? _b : false;
|
431 | constants.ENABLE_SIZE_APIS = (_c = runtime.enableSizeAPIs) !== null && _c !== void 0 ? _c : false;
|
432 | constants.ENABLE_TEMPLATE_CONTENT = (_d = runtime.enableTemplateContent) !== null && _d !== void 0 ? _d : false;
|
433 | constants.ENABLE_CLONE_NODE = (_e = runtime.enableCloneNode) !== null && _e !== void 0 ? _e : false;
|
434 | constants.ENABLE_CONTAINS = (_f = runtime.enableContains) !== null && _f !== void 0 ? _f : false;
|
435 | constants.ENABLE_MUTATION_OBSERVER = (_g = runtime.enableMutationObserver) !== null && _g !== void 0 ? _g : false;
|
436 | return constants;
|
437 | }
|
438 | exports.getRuntimeConstants = getRuntimeConstants;
|
439 |
|
\ | No newline at end of file |