2025/2/8第一次更新

This commit is contained in:
爱吃咸鱼小猫咪
2025-02-08 18:50:38 +08:00
commit d7af560866
26519 changed files with 5046029 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
export * from './transformRef';
export * from './transformPageHead';
export * from './transformComponent';
export * from './transformEvent';
export * from './transformTag';
export * from './transformUTSComponent';
export * from './transformRefresherSlot';
export { createAssetUrlTransformWithOptions } from './templateTransformAssetUrl';
export { createSrcsetTransformWithOptions } from './templateTransformSrcset';
export { STRINGIFY_JSON, ATTR_DATASET_EVENT_OPTS, createTransformOn, defaultMatch as matchTransformOn, } from './vOn';
export { createTransformModel, defaultMatch as matchTransformModel, } from './vModel';
export declare const transformH5BuiltInComponents: import("@vue/compiler-core").NodeTransform;
export declare const transformMatchMedia: import("@vue/compiler-core").NodeTransform;
export declare const transformTapToClick: import("@vue/compiler-core").NodeTransform;
export declare const transformComponentLink: (node: import("@vue/compiler-core").RootNode | import("@vue/compiler-core").TemplateChildNode, context: import("@vue/compiler-core").TransformContext) => void;
export * from './x/transformMPBuiltInTag';
export * from './x/transformDirection';
+57
View File
@@ -0,0 +1,57 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformComponentLink = exports.transformTapToClick = exports.transformMatchMedia = exports.transformH5BuiltInComponents = exports.matchTransformModel = exports.createTransformModel = exports.matchTransformOn = exports.createTransformOn = exports.ATTR_DATASET_EVENT_OPTS = exports.STRINGIFY_JSON = exports.createSrcsetTransformWithOptions = exports.createAssetUrlTransformWithOptions = void 0;
const uni_shared_1 = require("@dcloudio/uni-shared");
const transformTag_1 = require("./transformTag");
const transformEvent_1 = require("./transformEvent");
const transformComponent_1 = require("./transformComponent");
const constants_1 = require("../../mp/constants");
__exportStar(require("./transformRef"), exports);
__exportStar(require("./transformPageHead"), exports);
__exportStar(require("./transformComponent"), exports);
__exportStar(require("./transformEvent"), exports);
__exportStar(require("./transformTag"), exports);
__exportStar(require("./transformUTSComponent"), exports);
__exportStar(require("./transformRefresherSlot"), exports);
var templateTransformAssetUrl_1 = require("./templateTransformAssetUrl");
Object.defineProperty(exports, "createAssetUrlTransformWithOptions", { enumerable: true, get: function () { return templateTransformAssetUrl_1.createAssetUrlTransformWithOptions; } });
var templateTransformSrcset_1 = require("./templateTransformSrcset");
Object.defineProperty(exports, "createSrcsetTransformWithOptions", { enumerable: true, get: function () { return templateTransformSrcset_1.createSrcsetTransformWithOptions; } });
var vOn_1 = require("./vOn");
Object.defineProperty(exports, "STRINGIFY_JSON", { enumerable: true, get: function () { return vOn_1.STRINGIFY_JSON; } });
Object.defineProperty(exports, "ATTR_DATASET_EVENT_OPTS", { enumerable: true, get: function () { return vOn_1.ATTR_DATASET_EVENT_OPTS; } });
Object.defineProperty(exports, "createTransformOn", { enumerable: true, get: function () { return vOn_1.createTransformOn; } });
Object.defineProperty(exports, "matchTransformOn", { enumerable: true, get: function () { return vOn_1.defaultMatch; } });
var vModel_1 = require("./vModel");
Object.defineProperty(exports, "createTransformModel", { enumerable: true, get: function () { return vModel_1.createTransformModel; } });
Object.defineProperty(exports, "matchTransformModel", { enumerable: true, get: function () { return vModel_1.defaultMatch; } });
exports.transformH5BuiltInComponents = (0, transformTag_1.createTransformTag)(uni_shared_1.BUILT_IN_TAG_NAMES.reduce((tags, tag) => ((tags[tag] = uni_shared_1.COMPONENT_PREFIX + tag), tags), {}));
exports.transformMatchMedia = (0, transformTag_1.createTransformTag)({
'match-media': 'uni-match-media',
});
exports.transformTapToClick = (0, transformEvent_1.createTransformEvent)({
tap: (node) => {
// 地图组件有自己特定的 tap 事件
if (node.tag === 'map' || node.tag === 'v-uni-map') {
return 'tap';
}
return 'click';
},
});
exports.transformComponentLink = (0, transformComponent_1.createTransformComponentLink)(constants_1.COMPONENT_BIND_LINK);
__exportStar(require("./x/transformMPBuiltInTag"), exports);
__exportStar(require("./x/transformDirection"), exports);
@@ -0,0 +1,33 @@
import { type NodeTransform } from '@vue/compiler-core';
export interface AssetURLTagConfig {
[name: string]: string[];
}
export interface AssetURLOptions {
/**
* If base is provided, instead of transforming relative asset urls into
* imports, they will be directly rewritten to absolute urls.
*/
base?: string | null;
/**
* If true, also processes absolute urls.
*/
includeAbsolute?: boolean;
tags?: AssetURLTagConfig;
}
export declare const defaultAssetUrlOptions: Required<AssetURLOptions>;
export declare const normalizeOptions: (options: AssetURLOptions | AssetURLTagConfig) => Required<AssetURLOptions>;
export declare const createAssetUrlTransformWithOptions: (options: Required<AssetURLOptions>) => NodeTransform;
/**
* A `@vue/compiler-core` plugin that transforms relative asset urls into
* either imports or absolute urls.
*
* ``` js
* // Before
* createVNode('img', { src: './logo.png' })
*
* // After
* import _imports_0 from './logo.png'
* createVNode('img', { src: _imports_0 })
* ```
*/
export declare const transformAssetUrl: NodeTransform;
@@ -0,0 +1,150 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformAssetUrl = exports.createAssetUrlTransformWithOptions = exports.normalizeOptions = exports.defaultAssetUrlOptions = void 0;
const path_1 = __importDefault(require("path"));
const compiler_core_1 = require("@vue/compiler-core");
const templateUtils_1 = require("./templateUtils");
const shared_1 = require("@vue/shared");
exports.defaultAssetUrlOptions = {
base: null,
includeAbsolute: false,
tags: {
video: ['src', 'poster'],
source: ['src'],
img: ['src'],
image: ['xlink:href', 'href'],
use: ['xlink:href', 'href'],
},
};
const normalizeOptions = (options) => {
if (Object.keys(options).some((key) => (0, shared_1.isArray)(options[key]))) {
// legacy option format which directly passes in tags config
return {
...exports.defaultAssetUrlOptions,
tags: options,
};
}
return {
...exports.defaultAssetUrlOptions,
...options,
};
};
exports.normalizeOptions = normalizeOptions;
const createAssetUrlTransformWithOptions = (options) => {
return (node, context) => exports.transformAssetUrl(node, context, options);
};
exports.createAssetUrlTransformWithOptions = createAssetUrlTransformWithOptions;
/**
* A `@vue/compiler-core` plugin that transforms relative asset urls into
* either imports or absolute urls.
*
* ``` js
* // Before
* createVNode('img', { src: './logo.png' })
*
* // After
* import _imports_0 from './logo.png'
* createVNode('img', { src: _imports_0 })
* ```
*/
const transformAssetUrl = (node, context, options = exports.defaultAssetUrlOptions) => {
if (node.type === compiler_core_1.NodeTypes.ELEMENT) {
if (!node.props.length) {
return;
}
const tags = options.tags || exports.defaultAssetUrlOptions.tags;
const attrs = tags[node.tag];
const wildCardAttrs = tags['*'];
if (!attrs && !wildCardAttrs) {
return;
}
// 策略:
// h5 平台保留原始策略
// 非 h5 平台
// - 绝对路径 static 资源不做转换
// - 相对路径 static 资源转换为绝对路径
// - 非 static 资源转换为 import
const assetAttrs = (attrs || []).concat(wildCardAttrs || []);
node.props.forEach((attr, index) => {
if (attr.type !== compiler_core_1.NodeTypes.ATTRIBUTE ||
!assetAttrs.includes(attr.name) ||
!attr.value ||
(0, templateUtils_1.isExternalUrl)(attr.value.content) ||
(0, templateUtils_1.isDataUrl)(attr.value.content) ||
attr.value.content[0] === '#') {
return;
}
// fixed by xxxxxx 区分 static 资源
const isStaticAsset = attr.value.content.indexOf('/static/') > -1;
// 绝对路径的静态资源不作处理
if (isStaticAsset && !(0, templateUtils_1.isRelativeUrl)(attr.value.content)) {
return;
}
const url = (0, templateUtils_1.parseUrl)(attr.value.content);
if (options.base && attr.value.content[0] === '.' && isStaticAsset) {
// explicit base - directly rewrite relative urls into absolute url
// to avoid generating extra imports
// Allow for full hostnames provided in options.base
const base = (0, templateUtils_1.parseUrl)(options.base);
const protocol = base.protocol || '';
const host = base.host ? protocol + '//' + base.host : '';
const basePath = base.path || '/';
// when packaged in the browser, path will be using the posix-
// only version provided by rollup-plugin-node-builtins.
attr.value.content =
host +
(path_1.default.posix || path_1.default).join(basePath, url.path + (url.hash || ''));
return;
}
// otherwise, transform the url into an import.
// this assumes a bundler will resolve the import into the correct
// absolute url (e.g. webpack file-loader)
const exp = getImportsExpressionExp(url.path, url.hash, attr.loc, context);
node.props[index] = {
type: compiler_core_1.NodeTypes.DIRECTIVE,
name: 'bind',
arg: (0, compiler_core_1.createSimpleExpression)(attr.name, true, attr.loc),
exp,
modifiers: [],
loc: attr.loc,
};
});
}
};
exports.transformAssetUrl = transformAssetUrl;
function getImportsExpressionExp(path, hash, loc, context) {
if (path) {
let name;
let exp;
const existingIndex = context.imports.findIndex((i) => i.path === path);
if (existingIndex > -1) {
name = `_imports_${existingIndex}`;
exp = context.imports[existingIndex].exp;
}
else {
name = `_imports_${context.imports.length}`;
exp = (0, compiler_core_1.createSimpleExpression)(name, false, loc, compiler_core_1.ConstantTypes.CAN_HOIST);
context.imports.push({ exp, path });
}
if (!hash) {
return exp;
}
const hashExp = `${name} + '${hash}'`;
const existingHoistIndex = context.hoists.findIndex((h) => {
return (h &&
h.type === compiler_core_1.NodeTypes.SIMPLE_EXPRESSION &&
!h.isStatic &&
h.content === hashExp);
});
if (existingHoistIndex > -1) {
return (0, compiler_core_1.createSimpleExpression)(`_hoisted_${existingHoistIndex + 1}`, false, loc, compiler_core_1.ConstantTypes.CAN_HOIST);
}
return context.hoist((0, compiler_core_1.createSimpleExpression)(hashExp, false, loc, compiler_core_1.ConstantTypes.CAN_HOIST));
}
else {
return (0, compiler_core_1.createSimpleExpression)(`''`, false, loc, compiler_core_1.ConstantTypes.CAN_HOIST);
}
}
@@ -0,0 +1,4 @@
import { type NodeTransform } from '@vue/compiler-core';
import { type AssetURLOptions } from './templateTransformAssetUrl';
export declare const createSrcsetTransformWithOptions: (options: Required<AssetURLOptions>) => NodeTransform;
export declare const transformSrcset: NodeTransform;
@@ -0,0 +1,122 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformSrcset = exports.createSrcsetTransformWithOptions = void 0;
const path_1 = __importDefault(require("path"));
const compiler_core_1 = require("@vue/compiler-core");
const templateUtils_1 = require("./templateUtils");
const templateTransformAssetUrl_1 = require("./templateTransformAssetUrl");
const srcsetTags = ['img', 'source'];
// http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g;
const createSrcsetTransformWithOptions = (options) => {
return (node, context) => exports.transformSrcset(node, context, options);
};
exports.createSrcsetTransformWithOptions = createSrcsetTransformWithOptions;
const transformSrcset = (node, context, options = templateTransformAssetUrl_1.defaultAssetUrlOptions) => {
if (node.type === compiler_core_1.NodeTypes.ELEMENT) {
if (srcsetTags.includes(node.tag) && node.props.length) {
node.props.forEach((attr, index) => {
if (attr.name === 'srcset' && attr.type === compiler_core_1.NodeTypes.ATTRIBUTE) {
if (!attr.value)
return;
const value = attr.value.content;
if (!value)
return;
const imageCandidates = value
.split(',')
.map((s) => {
// The attribute value arrives here with all whitespace, except
// normal spaces, represented by escape sequences
const [url, descriptor] = s
.replace(escapedSpaceCharacters, ' ')
.trim()
.split(' ', 2);
return { url, descriptor };
});
// data urls contains comma after the ecoding so we need to re-merge
// them
for (let i = 0; i < imageCandidates.length; i++) {
const { url } = imageCandidates[i];
if ((0, templateUtils_1.isDataUrl)(url)) {
imageCandidates[i + 1].url =
url + ',' + imageCandidates[i + 1].url;
imageCandidates.splice(i, 1);
}
}
const hasQualifiedUrl = imageCandidates.some(({ url }) => {
return (!(0, templateUtils_1.isExternalUrl)(url) &&
!(0, templateUtils_1.isDataUrl)(url) &&
(options.includeAbsolute || (0, templateUtils_1.isRelativeUrl)(url)));
});
// When srcset does not contain any qualified URLs, skip transforming
if (!hasQualifiedUrl) {
return;
}
if (options.base) {
const base = options.base;
const set = [];
imageCandidates.forEach(({ url, descriptor }) => {
descriptor = descriptor ? ` ${descriptor}` : ``;
if ((0, templateUtils_1.isRelativeUrl)(url)) {
set.push((path_1.default.posix || path_1.default).join(base, url) + descriptor);
}
else {
set.push(url + descriptor);
}
});
attr.value.content = set.join(', ');
return;
}
const compoundExpression = (0, compiler_core_1.createCompoundExpression)([], attr.loc);
imageCandidates.forEach(({ url, descriptor }, index) => {
if (!(0, templateUtils_1.isExternalUrl)(url) &&
!(0, templateUtils_1.isDataUrl)(url) &&
(options.includeAbsolute || (0, templateUtils_1.isRelativeUrl)(url))) {
const { path } = (0, templateUtils_1.parseUrl)(url);
let exp;
if (path) {
const existingImportsIndex = context.imports.findIndex((i) => i.path === path);
if (existingImportsIndex > -1) {
exp = (0, compiler_core_1.createSimpleExpression)(`_imports_${existingImportsIndex}`, false, attr.loc, compiler_core_1.ConstantTypes.CAN_HOIST);
}
else {
exp = (0, compiler_core_1.createSimpleExpression)(`_imports_${context.imports.length}`, false, attr.loc, compiler_core_1.ConstantTypes.CAN_HOIST);
context.imports.push({ exp, path });
}
compoundExpression.children.push(exp);
}
}
else {
const exp = (0, compiler_core_1.createSimpleExpression)(`"${url}"`, false, attr.loc, compiler_core_1.ConstantTypes.CAN_HOIST);
compoundExpression.children.push(exp);
}
const isNotLast = imageCandidates.length - 1 > index;
if (descriptor && isNotLast) {
compoundExpression.children.push(` + ' ${descriptor}, ' + `);
}
else if (descriptor) {
compoundExpression.children.push(` + ' ${descriptor}'`);
}
else if (isNotLast) {
compoundExpression.children.push(` + ', ' + `);
}
});
const hoisted = context.hoist(compoundExpression);
hoisted.constType = compiler_core_1.ConstantTypes.CAN_HOIST;
node.props[index] = {
type: compiler_core_1.NodeTypes.DIRECTIVE,
name: 'bind',
arg: (0, compiler_core_1.createSimpleExpression)('srcset', true, attr.loc),
exp: hoisted,
modifiers: [],
loc: attr.loc,
};
}
});
}
}
};
exports.transformSrcset = transformSrcset;
@@ -0,0 +1,9 @@
/// <reference types="node" />
import { type UrlWithStringQuery } from 'url';
export declare function isRelativeUrl(url: string): boolean;
export declare function isExternalUrl(url: string): boolean;
export declare function isDataUrl(url: string): boolean;
/**
* Parses string url into URL object.
*/
export declare function parseUrl(url: string): UrlWithStringQuery;
@@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseUrl = exports.isDataUrl = exports.isExternalUrl = exports.isRelativeUrl = void 0;
const url_1 = require("url");
const shared_1 = require("@vue/shared");
function isRelativeUrl(url) {
const firstChar = url.charAt(0);
return firstChar === '.' || firstChar === '~' || firstChar === '@';
}
exports.isRelativeUrl = isRelativeUrl;
const externalRE = /^(https?:)?\/\//;
function isExternalUrl(url) {
return externalRE.test(url);
}
exports.isExternalUrl = isExternalUrl;
const dataUrlRE = /^\s*data:/i;
function isDataUrl(url) {
return dataUrlRE.test(url);
}
exports.isDataUrl = isDataUrl;
/**
* Parses string url into URL object.
*/
function parseUrl(url) {
const firstChar = url.charAt(0);
if (firstChar === '~') {
const secondChar = url.charAt(1);
url = url.slice(secondChar === '/' ? 2 : 1);
}
return parseUriParts(url);
}
exports.parseUrl = parseUrl;
/**
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
* @param urlString an url as a string
*/
function parseUriParts(urlString) {
// A TypeError is thrown if urlString is not a string
// @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
return (0, url_1.parse)((0, shared_1.isString)(urlString) ? urlString : '', false, true);
}
@@ -0,0 +1,3 @@
import { NodeTypes, type RootNode, type TemplateChildNode, type TransformContext } from '@vue/compiler-core';
import type { COMPONENT_BIND_LINK, COMPONENT_ON_LINK } from '../../mp/constants';
export declare function createTransformComponentLink(name: typeof COMPONENT_BIND_LINK | typeof COMPONENT_ON_LINK, type?: NodeTypes.ATTRIBUTE | NodeTypes.DIRECTIVE): (node: RootNode | TemplateChildNode, context: TransformContext) => void;
@@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTransformComponentLink = void 0;
const compiler_core_1 = require("@vue/compiler-core");
const utils_1 = require("../utils");
function createTransformComponentLink(name, type = compiler_core_1.NodeTypes.DIRECTIVE) {
return function transformComponentLink(node, context) {
if (!(0, utils_1.isUserComponent)(node, context)) {
return;
}
// 新版本的 vue,识别 template 有差异,可能认为是自定义组件
if (node.tag === 'template') {
return;
}
if (type === compiler_core_1.NodeTypes.DIRECTIVE) {
node.props.push({
type: compiler_core_1.NodeTypes.DIRECTIVE,
name: 'on',
modifiers: [],
loc: compiler_core_1.locStub,
arg: (0, compiler_core_1.createSimpleExpression)(name, true),
exp: (0, compiler_core_1.createSimpleExpression)('__l', true),
});
}
else {
node.props.push((0, utils_1.createAttributeNode)(name, '__l'));
}
};
}
exports.createTransformComponentLink = createTransformComponentLink;
@@ -0,0 +1,2 @@
import type { DirectiveNode, ElementNode, NodeTransform } from '@vue/compiler-core';
export declare function createTransformEvent(options: Record<string, string | ((node: ElementNode, dir: DirectiveNode) => string)>): NodeTransform;
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTransformEvent = void 0;
const shared_1 = require("@vue/shared");
const ast_1 = require("../../vite/utils/ast");
function createTransformEvent(options) {
return function transformEvent(node) {
if (!(0, ast_1.isElementNode)(node)) {
return;
}
node.props.forEach((prop) => {
const { name, arg } = prop;
if (name === 'on' && arg && (0, ast_1.isSimpleExpressionNode)(arg)) {
const eventType = options[arg.content];
if (eventType) {
// e.g tap => click
if ((0, shared_1.isFunction)(eventType)) {
arg.content = eventType(node, prop);
}
else {
arg.content = eventType;
}
}
}
});
};
}
exports.createTransformEvent = createTransformEvent;
@@ -0,0 +1,2 @@
import type { NodeTransform } from '@vue/compiler-core';
export declare const transformPageHead: NodeTransform;
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformPageHead = void 0;
const compiler_core_1 = require("@vue/compiler-core");
const utils_1 = require("../../utils");
const transformPageHead = (node, context) => {
// 发现是page-meta下的head,直接remove该节点
if ((0, utils_1.checkElementNodeTag)(node, 'head') &&
(0, utils_1.checkElementNodeTag)(context.parent, 'page-meta')) {
;
node.tag = 'page-meta-head';
node.tagType = compiler_core_1.ElementTypes.COMPONENT;
}
};
exports.transformPageHead = transformPageHead;
@@ -0,0 +1,2 @@
import { type RootNode, type TemplateChildNode, type TransformContext } from '@vue/compiler-core';
export declare function transformRef(node: RootNode | TemplateChildNode, context: TransformContext): void;
@@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformRef = void 0;
const compiler_core_1 = require("@vue/compiler-core");
const utils_1 = require("../utils");
function transformRef(node, context) {
if (!(0, utils_1.isUserComponent)(node, context)) {
return;
}
addVueRef(node, context);
}
exports.transformRef = transformRef;
function addVueRef(node, context) {
// 仅配置了 ref 属性的,才需要增补 vue-ref
const refProp = (0, compiler_core_1.findProp)(node, 'ref');
if (!refProp) {
return;
}
if (refProp.type === compiler_core_1.NodeTypes.ATTRIBUTE) {
refProp.name = 'u-' + utils_1.VUE_REF;
}
else {
;
refProp.arg.content = 'u-' + utils_1.VUE_REF;
}
return (0, utils_1.addStaticClass)(node,
// ref-in-for
// ref
context.inVFor
? utils_1.VUE_REF_IN_FOR
: utils_1.VUE_REF);
}
@@ -0,0 +1,8 @@
import { type RootNode, type TemplateChildNode } from '@vue/compiler-core';
/**
* 将scroll-view、list-view内的<view slot="refresher">转为vue支持的用法,此transform需要再较早时机执行,暂时放在transformTag前。此时node.tag还没有加上v-uni-前缀
* @param node
* @param context
* @returns
*/
export declare function transformRefresherSlot(node: RootNode | TemplateChildNode): void;
@@ -0,0 +1,58 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformRefresherSlot = void 0;
const compiler_core_1 = require("@vue/compiler-core");
const ast_1 = require("../../vite/utils/ast");
/**
* 将scroll-view、list-view内的<view slot="refresher">转为vue支持的用法,此transform需要再较早时机执行,暂时放在transformTag前。此时node.tag还没有加上v-uni-前缀
* @param node
* @param context
* @returns
*/
function transformRefresherSlot(node) {
if (!(0, ast_1.isElementNode)(node)) {
return;
}
if (node.tag !== 'scroll-view' && node.tag !== 'list-view') {
return;
}
let refresher = null, refresherIndex = -1;
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
if (!(0, ast_1.isElementNode)(child)) {
continue;
}
if (child.props.find((prop) => (0, ast_1.isAttributeNode)(prop) &&
prop.name === 'slot' &&
prop.value?.content === 'refresher')) {
refresher = child;
refresherIndex = i;
break;
}
}
if (!refresher) {
return;
}
node.children.splice(refresherIndex, 1, {
type: compiler_core_1.NodeTypes.ELEMENT,
tag: 'template',
tagType: compiler_core_1.ElementTypes.TEMPLATE,
props: [
{
type: compiler_core_1.NodeTypes.DIRECTIVE,
name: 'slot',
loc: compiler_core_1.locStub,
arg: {
loc: compiler_core_1.locStub,
type: compiler_core_1.NodeTypes.SIMPLE_EXPRESSION,
content: 'refresher',
isStatic: true,
constType: compiler_core_1.ConstantTypes.CAN_STRINGIFY,
},
},
],
children: [refresher],
loc: compiler_core_1.locStub,
});
}
exports.transformRefresherSlot = transformRefresherSlot;
@@ -0,0 +1,2 @@
import type { NodeTransform } from '@vue/compiler-core';
export declare function createTransformTag(opts: Record<string, string>): NodeTransform;
@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTransformTag = void 0;
const ast_1 = require("../../vite/utils/ast");
function createTransformTag(opts) {
return function transformTag(node, context) {
if (!(0, ast_1.isElementNode)(node)) {
return;
}
const oldTag = node.tag;
const newTag = opts[oldTag];
if (!newTag) {
return;
}
node.tag = newTag;
};
}
exports.createTransformTag = createTransformTag;
@@ -0,0 +1,8 @@
import type { NodeTransform } from '@vue/compiler-core';
/**
* 将uts组件保存到自定义组件列表中
* @param node
* @param context
* @returns
*/
export declare const transformUTSComponent: NodeTransform;
@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformUTSComponent = void 0;
const ast_1 = require("../../vite/utils/ast");
const utsUtils_1 = require("../../utsUtils");
/**
* 将uts组件保存到自定义组件列表中
* @param node
* @param context
* @returns
*/
const transformUTSComponent = (node, context) => {
if (!(0, ast_1.isElementNode)(node)) {
return;
}
if ((0, utsUtils_1.matchUTSComponent)(node.tag)) {
if (!context.root.components.includes(node.tag)) {
context.components.add(node.tag);
}
}
};
exports.transformUTSComponent = transformUTSComponent;
+12
View File
@@ -0,0 +1,12 @@
import type { DirectiveTransform, ElementNode, TransformContext } from '@vue/compiler-core';
export declare function defaultMatch(node: ElementNode, context: TransformContext): boolean;
interface CreateTransformModelOptions {
match: typeof defaultMatch;
}
/**
* 百度、快手小程序的自定义组件,不支持动态事件绑定,故 v-model 也需要调整
* @param baseTransformModel
* @returns
*/
export declare function createTransformModel(baseTransformModel: DirectiveTransform, { match }?: CreateTransformModelOptions): DirectiveTransform;
export {};
+33
View File
@@ -0,0 +1,33 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTransformModel = exports.defaultMatch = void 0;
const utils_1 = require("../utils");
const vOn_1 = require("./vOn");
function defaultMatch(node, context) {
return (0, utils_1.isUserComponent)(node, context);
}
exports.defaultMatch = defaultMatch;
/**
* 百度、快手小程序的自定义组件,不支持动态事件绑定,故 v-model 也需要调整
* @param baseTransformModel
* @returns
*/
function createTransformModel(baseTransformModel, { match } = {
match: defaultMatch,
}) {
return (dir, node, context, augmentor) => {
const res = baseTransformModel(dir, node, context, augmentor);
if (!match(node, context)) {
return res;
}
const props = res.props;
if (props[1]) {
// input,textarea 的 v-model 事件可能会被合并到已有的 input 中
const { arg, exp } = props[1];
(0, vOn_1.addEventOpts)(arg.content, exp, node, context);
props[1].exp = (0, vOn_1.createCustomEventExpr)();
}
return res;
};
}
exports.createTransformModel = createTransformModel;
+16
View File
@@ -0,0 +1,16 @@
import { type DirectiveTransform, type ElementNode, type ExpressionNode, type TransformContext } from '@vue/compiler-core';
export declare function defaultMatch(name: string, node: ElementNode, context: TransformContext): boolean;
interface CreateTransformOnOptions {
match: typeof defaultMatch;
}
/**
* 百度、快手小程序的自定义组件,不支持动态事件绑定,故转换为静态事件 + dataset
* @param baseTransformOn
* @returns
*/
export declare function createTransformOn(baseTransformOn: DirectiveTransform, { match }?: CreateTransformOnOptions): DirectiveTransform;
export declare function createCustomEventExpr(): import("@vue/compiler-core").SimpleExpressionNode;
export declare function addEventOpts(event: string, value: ExpressionNode, node: ElementNode, context: TransformContext): void;
export declare const ATTR_DATASET_EVENT_OPTS = "data-e-o";
export declare const STRINGIFY_JSON: unique symbol;
export {};
+97
View File
@@ -0,0 +1,97 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.STRINGIFY_JSON = exports.ATTR_DATASET_EVENT_OPTS = exports.addEventOpts = exports.createCustomEventExpr = exports.createTransformOn = exports.defaultMatch = void 0;
const uni_shared_1 = require("@dcloudio/uni-shared");
const compiler_core_1 = require("@vue/compiler-core");
const utils_1 = require("../utils");
function defaultMatch(name, node, context) {
return isCustomEvent(name) && (0, utils_1.isUserComponent)(node, context);
}
exports.defaultMatch = defaultMatch;
/**
* 百度、快手小程序的自定义组件,不支持动态事件绑定,故转换为静态事件 + dataset
* @param baseTransformOn
* @returns
*/
function createTransformOn(baseTransformOn, { match } = {
match: defaultMatch,
}) {
return (dir, node, context, augmentor) => {
const res = baseTransformOn(dir, node, context, augmentor);
const { name, arg, exp } = dir;
if (name !== 'on' || !arg || !exp || !(0, compiler_core_1.isStaticExp)(arg)) {
return res;
}
if (!match(arg.content, node, context)) {
return res;
}
const value = res.props[0].value;
res.props[0].value = createCustomEventExpr();
addEventOpts(node.tagType === compiler_core_1.ElementTypes.COMPONENT
? (0, uni_shared_1.customizeEvent)(arg.content)
: arg.content, value, node, context);
return res;
};
}
exports.createTransformOn = createTransformOn;
function createCustomEventExpr() {
return (0, compiler_core_1.createSimpleExpression)('__e', true);
}
exports.createCustomEventExpr = createCustomEventExpr;
function addEventOpts(event, value, node, context) {
const attrName = node.tagType === compiler_core_1.ElementTypes.COMPONENT
? ATTR_DATA_EVENT_OPTS
: exports.ATTR_DATASET_EVENT_OPTS;
const opts = (0, compiler_core_1.findProp)(node, attrName, true);
if (!opts) {
node.props.push(createDataEventOptsProp(attrName, event, value, context));
}
else {
const children = opts.exp.children;
children.splice(children.length - 2, 0, createDataEventOptsProperty(event, value));
}
}
exports.addEventOpts = addEventOpts;
const ATTR_DATA_EVENT_OPTS = 'eO';
exports.ATTR_DATASET_EVENT_OPTS = 'data-e-o';
function createDataEventOptsProperty(event, exp) {
return (0, compiler_core_1.createCompoundExpression)([`'${event}'`, ': ', exp, ',']);
}
exports.STRINGIFY_JSON = Symbol(`stringifyJson`);
function createDataEventOptsProp(name, event, exp, context) {
const children = [];
const stringify = name === ATTR_DATA_EVENT_OPTS;
if (stringify) {
children.push(context.helperString(exports.STRINGIFY_JSON) + '(');
}
children.push('{', createDataEventOptsProperty(event, exp), '}');
if (stringify) {
children.push(')');
}
return {
type: compiler_core_1.NodeTypes.DIRECTIVE,
name: 'bind',
loc: compiler_core_1.locStub,
modifiers: [],
arg: (0, compiler_core_1.createSimpleExpression)(name, true),
exp: (0, compiler_core_1.createCompoundExpression)(children),
};
}
const builtInEvents = [
'__l', // 快手使用了该事件
'tap',
'longtap',
'longpress',
'touchstart',
'touchmove',
'touchcancel',
'touchend',
'touchforcechange',
'transitionend',
'animationstart',
'animationiteration',
'animationend',
];
function isCustomEvent(name) {
return !builtInEvents.includes(name);
}
@@ -0,0 +1,6 @@
import { type RootNode, type TemplateChildNode, type TransformContext } from '@vue/compiler-core';
/**
* 将direction属性转化为scroll-x和scroll-y
* 注意transformMPBuiltInTag内会讲list-view转化为scroll-view,所以此transform应该在transformMPBuiltInTag之后执行
*/
export declare const transformDirection: (node: RootNode | TemplateChildNode, context: TransformContext) => void;
@@ -0,0 +1,52 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformDirection = void 0;
const vite_1 = require("../../../vite");
const utils_1 = require("../../utils");
const compiler_core_1 = require("@vue/compiler-core");
/**
* 将direction属性转化为scroll-x和scroll-y
* 注意transformMPBuiltInTag内会讲list-view转化为scroll-view,所以此transform应该在transformMPBuiltInTag之后执行
*/
const transformDirection = function (node, context) {
if (!(0, vite_1.isElementNode)(node)) {
return;
}
if (node.tag !== 'scroll-view') {
return;
}
const directionPropIndex = node.props.findIndex((prop) => (0, utils_1.isPropNameEquals)(prop, 'direction'));
const scrollXPropIndex = node.props.findIndex((prop) => (0, utils_1.isPropNameEquals)(prop, 'scrollX'));
const scrollYPropIndex = node.props.findIndex((prop) => (0, utils_1.isPropNameEquals)(prop, 'scrollY'));
if (directionPropIndex === -1 ||
(scrollXPropIndex !== -1 && scrollYPropIndex !== -1)) {
node.props.push((0, utils_1.createAttributeNode)('scroll-y', 'true'));
return;
}
const directionProp = node.props[directionPropIndex];
if (directionProp.type === compiler_core_1.NodeTypes.ATTRIBUTE) {
const directionValue = directionProp.value?.content;
const scrollX = directionValue === 'horizontal' || directionValue === 'all';
const scrollY = !directionValue ||
directionValue === 'vertical' ||
directionValue === 'all';
node.props.splice(directionPropIndex, 1);
scrollX && node.props.push((0, utils_1.createAttributeNode)('scroll-x', '' + scrollX));
scrollY && node.props.push((0, utils_1.createAttributeNode)('scroll-y', '' + scrollY));
}
else if (directionProp.type === compiler_core_1.NodeTypes.DIRECTIVE) {
if (!directionProp.arg ||
!(0, compiler_core_1.isStaticExp)(directionProp.arg) ||
!directionProp.exp ||
!(0, compiler_core_1.isStaticExp)(directionProp.exp)) {
return;
}
const exp = directionProp.exp.content;
const scrollX = `(${exp}) === 'horizontal' || (${exp}) === 'all'`;
const scrollY = `!(${exp}) || (${exp}) === 'vertical' || (${exp}) === 'all'`;
node.props.splice(directionPropIndex, 1);
scrollX && node.props.push((0, utils_1.createBindDirectiveNode)('scroll-x', scrollX));
scrollY && node.props.push((0, utils_1.createBindDirectiveNode)('scroll-y', scrollY));
}
};
exports.transformDirection = transformDirection;
@@ -0,0 +1,12 @@
import { type RootNode, type TemplateChildNode, type TransformContext } from '@vue/compiler-core';
export interface TransformMPBuiltInTagOptions {
propRename?: Record<string, Record<string, string>>;
propAdd?: Record<string, {
name: string;
value: string;
}[]>;
tagRename?: Record<string, string>;
}
export declare const defaultTransformMPBuiltInTagOptions: TransformMPBuiltInTagOptions;
export declare function createMPBuiltInTagTransform(options: TransformMPBuiltInTagOptions): (node: RootNode | TemplateChildNode, context: TransformContext) => void;
export declare const transformMPBuiltInTag: (node: RootNode | TemplateChildNode, context: TransformContext) => void;
@@ -0,0 +1,94 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformMPBuiltInTag = exports.createMPBuiltInTagTransform = exports.defaultTransformMPBuiltInTagOptions = void 0;
const shared_1 = require("@vue/shared");
const vite_1 = require("../../../vite");
const utils_1 = require("../../utils");
const compiler_core_1 = require("@vue/compiler-core");
exports.defaultTransformMPBuiltInTagOptions = {
propRename: {
checkbox: {
// "backgroundColor": "",
// "borderColor": "",
// "activeBackgroundColor": "",
// "activeBorderColor": "",
foreColor: 'color',
},
radio: {
// "backgroundColor": "",
// "borderColor": "",
activeBackgroundColor: 'color',
// "activeBorderColor": "",
// "foreColor": ""
},
slider: {
backgroundColor: 'backgroundColor',
activeBackgroundColor: 'activeColor',
foreColor: 'block-color',
},
switch: {
// "backgroundColor": "",
activeBackgroundColor: 'color',
// "foreColor": "",
// "activeForeColor": ""
},
},
propAdd: {
canvas: [
{
name: 'type',
value: '2d',
},
],
'scroll-view': [
{
name: 'enable-flex',
value: 'true',
},
],
},
tagRename: {
'list-view': 'scroll-view',
},
};
function createMPBuiltInTagTransform(options) {
return function (node, context) {
if (!(0, vite_1.isElementNode)(node)) {
return;
}
if (options.tagRename && node.tag in options.tagRename) {
node.tag = options.tagRename[node.tag];
}
if (options.propRename && node.tag in options.propRename) {
const propMap = options.propRename[node.tag];
node.props.forEach((prop) => {
if (prop.type === compiler_core_1.NodeTypes.ATTRIBUTE) {
const propName = (0, shared_1.camelize)(prop.name);
if (propName in propMap && propMap[propName]) {
(0, utils_1.renameProp)(propMap[propName], prop);
}
}
else if (prop.type === compiler_core_1.NodeTypes.DIRECTIVE) {
if (!prop.rawName || !prop.arg || !(0, compiler_core_1.isStaticExp)(prop.arg)) {
return;
}
const propName = (0, shared_1.camelize)(prop.rawName.slice(1));
if (propName in propMap && propMap[propName]) {
(0, utils_1.renameProp)(propMap[propName], prop);
}
}
});
}
if (options.propAdd && node.tag in options.propAdd) {
const add = options.propAdd[node.tag];
add.forEach(({ name, value }) => {
if (node.props.some((item) => (0, utils_1.isPropNameEquals)(item, name))) {
return;
}
node.props.push((0, utils_1.createAttributeNode)(name, value));
});
}
};
}
exports.createMPBuiltInTagTransform = createMPBuiltInTagTransform;
exports.transformMPBuiltInTag = createMPBuiltInTagTransform(exports.defaultTransformMPBuiltInTagOptions);