uni.compiler.js 7.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5
'use strict';

var initMiniProgramPlugin = require('@dcloudio/uni-mp-vite');
var path = require('path');
var uniCliShared = require('@dcloudio/uni-cli-shared');
fxy060608's avatar
fxy060608 已提交
6
var compilerCore = require('@vue/compiler-core');
fxy060608's avatar
fxy060608 已提交
7
var shared = require('@vue/shared');
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var initMiniProgramPlugin__default = /*#__PURE__*/_interopDefaultLegacy(initMiniProgramPlugin);
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);

var component2 = true;
var enableAppxNg = true;
var source = {
	component2: component2,
	enableAppxNg: enableAppxNg
};

fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28
function transformRef(node, context) {
    if (!uniCliShared.isUserComponent(node, context)) {
        return;
    }
    addVueRef(node, context);
}
function addVueRef(node, context) {
    // 仅配置了 ref 属性的,才需要增补 vue-ref
fxy060608's avatar
fxy060608 已提交
29
    const refProp = compilerCore.findProp(node, 'ref');
fxy060608's avatar
fxy060608 已提交
30 31 32
    if (!refProp) {
        return;
    }
fxy060608's avatar
fxy060608 已提交
33
    const dataRef = 'u-' +
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
        (context.inVFor
            ? uniCliShared.VUE_REF_IN_FOR
            : uniCliShared.VUE_REF);
    if (refProp.type === 6 /* ATTRIBUTE */) {
        refProp.name = dataRef;
    }
    else {
        refProp.arg.content = dataRef;
    }
    const { props } = node;
    props.splice(props.indexOf(refProp), 0, uniCliShared.createAttributeNode('ref', '__r'));
}

fxy060608's avatar
fxy060608 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
const event = {
    format(name, { isCatch, isComponent }) {
        if (!isComponent && name === 'click') {
            name = 'tap';
        }
        name = eventMap[name] || name;
        return `${isCatch ? 'catch' : 'on'}${shared.capitalize(shared.camelize(name))}`;
    },
};
const eventMap = {
    touchstart: 'touchStart',
    touchmove: 'touchMove',
    touchend: 'touchEnd',
    touchcancel: 'touchCancel',
    longtap: 'longTap',
    longpress: 'longTap',
    transitionend: 'transitionEnd',
    animationstart: 'animationStart',
    animationiteration: 'animationIteration',
    animationend: 'animationEnd',
    firstappear: 'firstAppear',
    // map
    markertap: 'markerTap',
    callouttap: 'calloutTap',
    controltap: 'controlTap',
    regionchange: 'regionChange',
    paneltap: 'panelTap',
    // scroll-view
    scrolltoupper: 'scrollToUpper',
    scrolltolower: 'scrollToLower',
    // movable-view
    changeend: 'changeEnd',
    // video
    timeupdate: 'timeUpdate',
    waiting: 'loading',
    fullscreenchange: 'fullScreenChange',
    useraction: 'userAction',
    renderstart: 'renderStart',
    loadedmetadata: 'renderStart',
    // swiper
    animationfinish: 'animationEnd',
};

fxy060608's avatar
fxy060608 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
function transformOpenType(node) {
    var _a;
    if (node.type !== 1 /* ELEMENT */ || node.tag !== 'button') {
        return;
    }
    const openTypeProp = compilerCore.findProp(node, 'open-type');
    if (!openTypeProp) {
        return;
    }
    if (openTypeProp.type !== 6 /* ATTRIBUTE */ ||
        ((_a = openTypeProp.value) === null || _a === void 0 ? void 0 : _a.content) !== 'getPhoneNumber') {
        return;
    }
    openTypeProp.value.content = 'getAuthorize';
    const { props } = node;
    props.splice(props.indexOf(openTypeProp) + 1, 0, uniCliShared.createAttributeNode('scope', 'phoneNumber'));
    let getPhoneNumberMethodName = '';
    const getPhoneNumberPropIndex = props.findIndex((prop) => {
        if (prop.type === 7 /* DIRECTIVE */ && prop.name === 'on') {
            const { arg, exp } = prop;
            if ((arg === null || arg === void 0 ? void 0 : arg.type) === 4 /* SIMPLE_EXPRESSION */ &&
                (exp === null || exp === void 0 ? void 0 : exp.type) === 4 /* SIMPLE_EXPRESSION */ &&
                arg.isStatic &&
                arg.content === 'getphonenumber') {
                getPhoneNumberMethodName = exp.content;
                return true;
            }
        }
    });
    if (!getPhoneNumberMethodName) {
        return;
    }
    props.splice(getPhoneNumberPropIndex, 1);
    props.push(uniCliShared.createOnDirectiveNode('getAuthorize', `$onAliGetAuthorize('${getPhoneNumberMethodName}',$event)`));
    props.push(uniCliShared.createOnDirectiveNode('error', `$onAliAuthError('${getPhoneNumberMethodName}',$event)`));
}

fxy060608's avatar
fxy060608 已提交
127
const projectConfigFilename = 'mini.project.json';
fxy060608's avatar
fxy060608 已提交
128
const miniProgram = {
fxy060608's avatar
fxy060608 已提交
129
    event,
fxy060608's avatar
fxy060608 已提交
130 131 132 133
    class: {
        array: false,
    },
    slot: {
fxy060608's avatar
fxy060608 已提交
134
        $slots: false,
fxy060608's avatar
fxy060608 已提交
135
        // 支付宝 fallback 有 bug,当多个带默认 slot 组件嵌套使用时,所有的默认slot均会显示,如uni-file-picker(image)
fxy060608's avatar
fxy060608 已提交
136
        fallbackContent: true,
137
        dynamicSlotNames: true,
fxy060608's avatar
fxy060608 已提交
138 139
    },
    directive: 'a:',
fxy060608's avatar
fxy060608 已提交
140 141 142
    component: {
        getPropertySync: true,
    },
fxy060608's avatar
fxy060608 已提交
143 144 145
};
const nodeTransforms = [
    transformRef,
fxy060608's avatar
fxy060608 已提交
146
    transformOpenType,
fxy060608's avatar
fxy060608 已提交
147
    uniCliShared.transformMatchMedia,
fxy060608's avatar
fxy060608 已提交
148 149
    uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_ON_LINK, 6 /* ATTRIBUTE */),
];
fxy060608's avatar
fxy060608 已提交
150 151 152
const compilerOptions = {
    nodeTransforms,
};
fxy060608's avatar
fxy060608 已提交
153
const customElements = [
fxy060608's avatar
fxy060608 已提交
154 155 156 157 158 159 160 161 162 163 164 165
    'lifestyle',
    'life-follow',
    'contact-button',
    'spread',
    'error-view',
    'poster',
    'cashier',
    'ix-grid',
    'ix-native-grid',
    'ix-native-list',
    'mkt',
];
fxy060608's avatar
fxy060608 已提交
166
const options = {
fxy060608's avatar
fxy060608 已提交
167
    cdn: 2,
fxy060608's avatar
fxy060608 已提交
168 169 170 171 172 173 174 175 176
    vite: {
        inject: {
            uni: [path__default["default"].resolve(__dirname, 'uni.api.esm.js'), 'default'],
        },
        alias: {
            'uni-mp-runtime': path__default["default"].resolve(__dirname, 'uni.mp.esm.js'),
        },
        copyOptions: {
            assets: ['mycomponents'],
fxy060608's avatar
fxy060608 已提交
177
            targets: process.env.UNI_MP_PLUGIN ? [uniCliShared.copyMiniProgramPluginJson] : [],
fxy060608's avatar
fxy060608 已提交
178 179 180
        },
    },
    global: 'my',
fxy060608's avatar
fxy060608 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    json: {
        windowOptionsMap: {
            defaultTitle: 'navigationBarTitleText',
            pullRefresh: 'enablePullDownRefresh',
            allowsBounceVertical: 'allowsBounceVertical',
            titleBarColor: 'navigationBarBackgroundColor',
            optionMenu: 'optionMenu',
            backgroundColor: 'backgroundColor',
            usingComponents: 'usingComponents',
            navigationBarShadow: 'navigationBarShadow',
            titleImage: 'titleImage',
            transparentTitle: 'transparentTitle',
            titlePenetrate: 'titlePenetrate',
        },
        tabBarOptionsMap: {
            textColor: 'color',
            selectedColor: 'selectedColor',
            backgroundColor: 'backgroundColor',
            items: 'list',
        },
        tabBarItemOptionsMap: {
            pagePath: 'pagePath',
            name: 'text',
            icon: 'iconPath',
            activeIcon: 'selectedIconPath',
        },
    },
fxy060608's avatar
fxy060608 已提交
208 209 210
    app: {
        darkmode: false,
        subpackages: true,
fxy060608's avatar
fxy060608 已提交
211
        plugins: true,
fxy060608's avatar
fxy060608 已提交
212 213 214 215 216
    },
    project: {
        filename: projectConfigFilename,
        source,
    },
fxy060608's avatar
fxy060608 已提交
217
    template: Object.assign(Object.assign({}, miniProgram), { customElements, filter: {
fxy060608's avatar
fxy060608 已提交
218 219 220
            extname: '.sjs',
            lang: 'sjs',
            generate(filter, filename) {
fxy060608's avatar
fxy060608 已提交
221 222 223
                // TODO 标签内的 code 代码需要独立生成一个 sjs 文件
                // 暂不处理,让开发者自己全部使用 src 引入
                return `<import-sjs name="${filter.name}" from="${filename}.sjs"/>`;
fxy060608's avatar
fxy060608 已提交
224
            },
fxy060608's avatar
fxy060608 已提交
225
        }, extname: '.axml', compilerOptions }),
fxy060608's avatar
fxy060608 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239
    style: {
        extname: '.acss',
    },
};

const uniMiniProgramAlipayPlugin = {
    name: 'vite:uni-mp-alipay',
    config() {
        return {
            define: {
                __VUE_CREATED_DEFERRED__: false,
            },
            build: {
                assetsInlineLimit: 0,
fxy060608's avatar
fxy060608 已提交
240 241 242 243
                terserOptions: {
                    compress: false,
                    mangle: false,
                },
fxy060608's avatar
fxy060608 已提交
244 245 246 247 248 249 250
            },
        };
    },
};
var index = [uniMiniProgramAlipayPlugin, ...initMiniProgramPlugin__default["default"](options)];

module.exports = index;