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

var initMiniProgramPlugin = require('@dcloudio/uni-mp-vite');
var uniCliShared = require('@dcloudio/uni-cli-shared');
var path = require('path');
var uniMpCompiler = require('@dcloudio/uni-mp-compiler');
var compilerCore = require('@vue/compiler-core');

fxy060608's avatar
fxy060608 已提交
9
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
fxy060608's avatar
fxy060608 已提交
10

fxy060608's avatar
fxy060608 已提交
11 12
var initMiniProgramPlugin__default = /*#__PURE__*/_interopDefault(initMiniProgramPlugin);
var path__default = /*#__PURE__*/_interopDefault(path);
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

var setting = {
	urlCheck: false,
	es6: true,
	postcss: false,
	minified: false,
	newFeature: true
};
var appid = "testAppId";
var projectname = "";
var condition = {
	miniprogram: {
		current: -1
	}
};
var source = {
	setting: setting,
	appid: appid,
	projectname: projectname,
	condition: condition
};

fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
function transformSwiper(node) {
    if (node.type !== 1 /* NodeTypes.ELEMENT */ || node.tag !== 'swiper') {
        return;
    }
    const disableTouchProp = compilerCore.findProp(node, 'disable-touch', false, true);
    if (!disableTouchProp) {
        return;
    }
    const { props } = node;
    if (disableTouchProp.type === 6 /* NodeTypes.ATTRIBUTE */) {
        // <swiper disable-touch/> => <swiper :touchable="false"/>
        props.splice(props.indexOf(disableTouchProp), 1, uniCliShared.createBindDirectiveNode('touchable', 'false'));
    }
    else {
        if (disableTouchProp.exp) {
            // <swiper :disable-touch="true"/> => <swiper :touchable="!(true)"/>
            let touchable = '';
            if (disableTouchProp.exp.type === 4 /* NodeTypes.SIMPLE_EXPRESSION */) {
                if (disableTouchProp.exp.content === 'true') {
                    touchable = 'false';
                }
                else if (disableTouchProp.exp.content === 'false') {
                    touchable = 'true';
                }
            }
            props.splice(props.indexOf(disableTouchProp), 1, uniCliShared.createBindDirectiveNode('touchable', touchable || `!(${uniMpCompiler.genExpr(disableTouchProp.exp)})`));
        }
    }
fxy060608's avatar
fxy060608 已提交
63 64
}

fxy060608's avatar
fxy060608 已提交
65 66 67 68 69 70
const customElements = [
    'aweme-data',
    'consume-card',
    'pay-button',
    'rate-button',
    'member-button',
fxy060608's avatar
fxy060608 已提交
71 72 73 74
    'confirm-receipt-button',
    'live-preview',
    'aweme-live-book',
    'aweme-user-card',
75
    'rtc-room',
fxy060608's avatar
fxy060608 已提交
76
];
fxy060608's avatar
fxy060608 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140
const projectConfigFilename = 'project.config.json';
const nodeTransforms = [
    uniCliShared.transformRef,
    transformSwiper,
    uniCliShared.transformMatchMedia,
    uniCliShared.transformComponentLink,
];
const compilerOptions = {
    nodeTransforms,
};
const COMPONENTS_DIR = 'ttcomponents';
const miniProgram = {
    class: {
        array: false,
    },
    slot: {
        fallbackContent: true,
        dynamicSlotNames: true,
    },
    directive: 'tt:',
    component: {
        dir: COMPONENTS_DIR,
        vShow: uniCliShared.COMPONENT_CUSTOM_HIDDEN_BIND,
    },
};
const options = {
    cdn: 4,
    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: [COMPONENTS_DIR],
            targets: [
                {
                    src: ['ext.json'],
                    get dest() {
                        return process.env.UNI_OUTPUT_DIR;
                    },
                },
            ],
        },
    },
    global: 'tt',
    app: {
        darkmode: false,
        subpackages: true,
        usingComponents: false,
    },
    project: {
        filename: projectConfigFilename,
        config: ['project.tt.json'],
        source,
    },
    template: Object.assign(Object.assign({}, miniProgram), { customElements, filter: {
            extname: '.sjs',
            lang: 'sjs',
            generate(filter, filename) {
                if (filename) {
                    return `<sjs src="${filename}.sjs" module="${filter.name}"/>`;
                }
fxy060608's avatar
fxy060608 已提交
141 142
                return `<sjs module="${filter.name}">
${filter.code}
fxy060608's avatar
fxy060608 已提交
143 144 145 146 147 148
</sjs>`;
            },
        }, extname: '.ttml', compilerOptions }),
    style: {
        extname: '.ttss',
    },
fxy060608's avatar
fxy060608 已提交
149 150
};

fxy060608's avatar
fxy060608 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
const uniMiniProgramToutiaoPlugin = {
    name: 'uni:mp-lark',
    config() {
        return {
            define: {
                __VUE_CREATED_DEFERRED__: true,
            },
            build: {
                // css 中不支持引用本地资源
                assetsInlineLimit: uniCliShared.ASSETS_INLINE_LIMIT,
            },
        };
    },
};
options.vite.copyOptions.targets = [
    ...(options.vite.copyOptions.targets || []),
    ...uniCliShared.copyMiniProgramThemeJson(),
];
options.app.darkmode = true;
options.cdn = 10;
options.template.slot.fallbackContent = false;
options.project.config = ['project.lark.json'];
fxy060608's avatar
fxy060608 已提交
173
var index = [uniMiniProgramToutiaoPlugin, ...initMiniProgramPlugin__default.default(options)];
fxy060608's avatar
fxy060608 已提交
174 175

module.exports = index;