1 | "use strict";
|
2 | var __rest = (this && this.__rest) || function (s, e) {
|
3 | var t = {};
|
4 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
5 | t[p] = s[p];
|
6 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
7 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
8 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
9 | t[p[i]] = s[p[i]];
|
10 | }
|
11 | return t;
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.getPostcssPlugins = exports.getDefaultPostcssConfig = void 0;
|
15 | const helper_1 = require("@tarojs/helper");
|
16 | const path = require("path");
|
17 | const resolve_1 = require("resolve");
|
18 | const platform = 'h5';
|
19 | const defaultAutoprefixerOption = {
|
20 | enable: true,
|
21 | config: {
|
22 | flexbox: 'no-2009'
|
23 | }
|
24 | };
|
25 | const defaultPxtransformOption = {
|
26 | enable: true,
|
27 | config: {
|
28 | platform
|
29 | }
|
30 | };
|
31 | const defaultConstparseOption = {
|
32 | constants: [
|
33 | {
|
34 | key: 'taro-tabbar-height',
|
35 | val: '50PX'
|
36 | }
|
37 | ],
|
38 | platform
|
39 | };
|
40 | const defaultHtmltransformOption = {
|
41 | enable: true,
|
42 | config: {
|
43 | platform,
|
44 | removeCursorStyle: false
|
45 | }
|
46 | };
|
47 | const defaultUrlOption = {
|
48 | enable: false,
|
49 | config: {
|
50 | url: 'inline'
|
51 | }
|
52 | };
|
53 | const plugins = [];
|
54 | const getDefaultPostcssConfig = function ({ designWidth, deviceRatio, option = {} }) {
|
55 | const { autoprefixer, pxtransform, htmltransform, url } = option, options = __rest(option, ["autoprefixer", "pxtransform", "htmltransform", "url"]);
|
56 | if (designWidth) {
|
57 | defaultPxtransformOption.config.designWidth = designWidth;
|
58 | }
|
59 | if (deviceRatio) {
|
60 | defaultPxtransformOption.config.deviceRatio = deviceRatio;
|
61 | }
|
62 | const autoprefixerOption = (0, helper_1.recursiveMerge)({}, defaultAutoprefixerOption, autoprefixer);
|
63 | const pxtransformOption = (0, helper_1.recursiveMerge)({}, defaultPxtransformOption, pxtransform);
|
64 | const htmltransformOption = (0, helper_1.recursiveMerge)({}, defaultHtmltransformOption, htmltransform);
|
65 | const urlOption = (0, helper_1.recursiveMerge)({}, defaultUrlOption, url);
|
66 | return [
|
67 | ['postcss-import', {}, require('postcss-import')],
|
68 | ['autoprefixer', autoprefixerOption, require('autoprefixer')],
|
69 | ['postcss-pxtransform', pxtransformOption, require('postcss-pxtransform')],
|
70 | ['postcss-html-transform', htmltransformOption, require('postcss-html-transform')],
|
71 | ['postcss-plugin-constparse', defaultConstparseOption, require('postcss-plugin-constparse')],
|
72 | ['postcss-url', urlOption, require('postcss-url')],
|
73 | ...Object.entries(options)
|
74 | ];
|
75 | };
|
76 | exports.getDefaultPostcssConfig = getDefaultPostcssConfig;
|
77 | const getPostcssPlugins = function (appPath, option = {}) {
|
78 | option.forEach(([pluginName, pluginOption, pluginPkg]) => {
|
79 | if (!pluginOption)
|
80 | return;
|
81 | if (Object.hasOwnProperty.call(pluginOption, 'enable') && !pluginOption.enable)
|
82 | return;
|
83 | if (pluginPkg) {
|
84 | plugins.push(pluginPkg(pluginOption.config || {}));
|
85 | return;
|
86 | }
|
87 | if (!(0, helper_1.isNpmPkg)(pluginName)) {
|
88 |
|
89 | pluginName = path.join(appPath, pluginName);
|
90 | }
|
91 | try {
|
92 | const pluginPath = (0, resolve_1.sync)(pluginName, { basedir: appPath });
|
93 | plugins.push(require(pluginPath)(pluginOption.config || {}));
|
94 | }
|
95 | catch (e) {
|
96 | const msg = e.code === 'MODULE_NOT_FOUND' ? `缺少 postcss 插件 "${pluginName}", 已忽略` : e;
|
97 | console.log(msg);
|
98 | }
|
99 | });
|
100 | return plugins;
|
101 | };
|
102 | exports.getPostcssPlugins = getPostcssPlugins;
|
103 |
|
\ | No newline at end of file |