Files
OpenMaopaoya/frontend-uni/node_modules/@dcloudio/uni-cli-shared/dist/postcss/plugins/uniapp.js
T

170 lines
6.2 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterPrefersColorScheme = void 0;
const shared_1 = require("@vue/shared");
const postcss_selector_parser_1 = __importDefault(require("postcss-selector-parser"));
const uni_shared_1 = require("@dcloudio/uni-shared");
const constants_1 = require("./constants");
const alipayPageBackground_1 = require("./alipayPageBackground");
const x_1 = require("../../x");
const defaultUniAppCssProcessorOptions = (0, shared_1.extend)({}, uni_shared_1.defaultRpx2Unit);
function transform(selector, state, { rewriteTag, vapor }) {
if (selector.type !== 'tag') {
return;
}
const { value } = selector;
if (vapor && value !== 'page') {
rewriteVaporTag(selector);
return;
}
selector.value = rewriteTag(value);
if (value === 'page' && selector.value === 'uni-page-body') {
state.body = true;
}
}
function createBodyRule(origRule) {
const bodyDecls = [];
origRule.walkDecls((decl) => {
if (constants_1.BG_PROPS.indexOf(decl.prop) !== -1) {
bodyDecls.push(decl.clone());
}
// css变量也要提升到body
if (decl.prop.startsWith('--')) {
bodyDecls.push(decl.clone());
}
});
if (bodyDecls.length) {
const { rule } = require('postcss');
origRule.after(rule({ selector: 'body' }).append(bodyDecls));
}
}
function rewriteVaporTag(selector) {
// 显式命名空间属于原生 CSS,不将 svg|image 等当成 uni 组件。
if (selector.namespace !== undefined)
return;
let tag = selector.value.replace(/^v-uni-/, '');
if (tag === 'cover-view')
tag = 'view';
if (tag === 'cover-image')
tag = 'image';
if (!(0, uni_shared_1.isBuiltInComponent)(tag) && !(0, uni_shared_1.isDom2WebVueComponentTag)(tag))
return;
let host = 'div';
if (tag === 'text')
host = 'span';
else if (tag === 'image')
host = 'img';
else if (tag === 'canvas')
host = 'canvas';
// Button、Navigator 会使用 View 或 Text 根,切换分支后仍须匹配。
else if (tag === 'button' || tag === 'navigator')
host = ':is(div,span)';
// 框架样式及本插件产物可能再次经过此处。统一识别紧邻的纯属性
// :where,已有身份时只转换宿主;不推导分支、后代或否定选择器。
const marker = `uni-${tag}`;
const next = selector.next();
const hasMarker = next?.type === 'pseudo' &&
next.value === ':where' &&
next.nodes.length === 1 &&
next.nodes[0].nodes.every((node) => node.type === 'attribute') &&
next.nodes[0].nodes.some((node) => node.type === 'attribute' &&
node.attribute === marker &&
node.namespace === undefined &&
!node.operator);
// 用 div:where([uni-view]) 而不是 [uni-view]:保留原标签的 (0,0,1)
// 优先级,避免属性选择器的 (0,1,0) 改变与 class、scoped 样式的覆盖关系。
// :is(div,span) 同样只有标签权重;已有标识直接参与匹配,不排除复合组件根。
const nodes = (0, postcss_selector_parser_1.default)().astSync(host + (hasMarker ? '' : `:where([${marker}])`)).first.nodes;
nodes[0].spaces.before = selector.spaces.before;
nodes[nodes.length - 1].spaces.after = selector.spaces.after;
selector.replaceWith(...nodes);
}
function walkRules(options) {
return (rule) => {
const state = { body: false };
rule.selector = (0, postcss_selector_parser_1.default)((selectors) => selectors.walk((selector) => transform(selector, state, options))).processSync(rule.selector);
state.body && createBodyRule(rule);
};
}
function walkDecls(rpx2unit) {
return (decl) => {
const { value } = decl;
if (value.indexOf('rpx') === -1 && value.indexOf('upx') === -1) {
return;
}
decl.value = rpx2unit(decl.value);
};
}
function filterPrefersColorScheme(root, force = false) {
if (process.env.VUE_APP_DARK_MODE !== 'true') {
const filePath = root.source?.input.file;
if (force || (filePath && filePath.includes('@dcloudio'))) {
root.walkAtRules((rule) => {
if (/prefers-color-scheme\s*:\s*dark/.test(rule.params)) {
rule.remove();
}
});
}
}
}
exports.filterPrefersColorScheme = filterPrefersColorScheme;
const baiduTags = {
navigator: 'nav',
};
function rewriteBaiduTags(tag) {
return baiduTags[tag] || tag;
}
function rewriteUniH5Tags(tag) {
if (tag === 'page') {
return 'uni-page-body';
}
if ((0, uni_shared_1.isBuiltInComponent)(tag)) {
return uni_shared_1.COMPONENT_SELECTOR_PREFIX + tag;
}
return tag;
}
function rewriteUniAppTags(tag) {
if (tag === 'page') {
return 'body';
}
if ((0, uni_shared_1.isBuiltInComponent)(tag)) {
return uni_shared_1.COMPONENT_SELECTOR_PREFIX + tag;
}
return tag;
}
const transforms = {
h5: rewriteUniH5Tags,
app: rewriteUniAppTags,
'app-harmony': rewriteUniAppTags,
'mp-baidu': rewriteBaiduTags,
};
const uniapp = (opts) => {
const platform = process.env.UNI_PLATFORM;
const { unit, unitRatio, unitPrecision } = (0, shared_1.extend)({}, defaultUniAppCssProcessorOptions, opts);
const rpx2unit = (0, uni_shared_1.createRpx2Unit)(unit, unitRatio, unitPrecision);
return {
postcssPlugin: 'uni-app',
prepare() {
return {
OnceExit(root) {
root.walkDecls(walkDecls(rpx2unit));
const rewriteTag = transforms[platform];
filterPrefersColorScheme(root);
(0, alipayPageBackground_1.adaptAlipayPageBackground)(root);
if (rewriteTag) {
root.walkRules(walkRules({
rewriteTag,
vapor: (0, x_1.isUniAppXWebVapor)(),
}));
}
},
};
},
};
};
uniapp.postcss = true;
exports.default = uniapp;