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

var initMiniProgramPlugin = require('@dcloudio/uni-mp-vite');
fxy060608's avatar
fxy060608 已提交
4
var path = require('path');
fxy060608's avatar
fxy060608 已提交
5
var uniCliShared = require('@dcloudio/uni-cli-shared');
fxy060608's avatar
fxy060608 已提交
6
var uniMpCompiler = require('@dcloudio/uni-mp-compiler');
fxy060608's avatar
fxy060608 已提交
7 8 9 10

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

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

var appid = "";
var host = "baiduboxapp";
var projectname = "";
var setting = {
	autoAudits: false,
	urlCheck: false
};
var source = {
	appid: appid,
	"compilation-args": {
	common: {
		babelSetting: {
			ignore: [
			]
		},
		enhance: true,
		ignorePrefixCss: false
	},
	selected: -3
},
	host: host,
	projectname: projectname,
	setting: setting
};

fxy060608's avatar
fxy060608 已提交
38
const transformFor = (node, context) => {
fxy060608's avatar
fxy060608 已提交
39 40 41
    if (!uniMpCompiler.isForElementNode(node)) {
        return;
    }
fxy060608's avatar
fxy060608 已提交
42 43
    const { vFor, props } = node;
    let sourceCode = vFor.valueAlias + ' in ' + vFor.sourceAlias;
fxy060608's avatar
fxy060608 已提交
44 45 46 47
    const keyProp = uniMpCompiler.findProp(node, 'key', true);
    if (keyProp) {
        const { exp } = keyProp;
        if (exp) {
fxy060608's avatar
fxy060608 已提交
48
            const key = uniMpCompiler.rewriteExpression(exp, context).content;
fxy060608's avatar
fxy060608 已提交
49 50
            sourceCode = sourceCode + ' trackBy ' + key;
            props.splice(props.indexOf(keyProp), 1);
fxy060608's avatar
fxy060608 已提交
51 52
        }
    }
fxy060608's avatar
fxy060608 已提交
53 54
    vFor.valueAlias = '';
    vFor.sourceCode = sourceCode;
fxy060608's avatar
fxy060608 已提交
55 56
};

fxy060608's avatar
fxy060608 已提交
57
/**
fxy060608's avatar
fxy060608 已提交
58
 * 百度小程序的自定义组件,不支持动态事件绑定
fxy060608's avatar
fxy060608 已提交
59
 */
fxy060608's avatar
fxy060608 已提交
60
const transformOn = uniCliShared.createTransformOn(uniMpCompiler.transformOn);
fxy060608's avatar
fxy060608 已提交
61

fxy060608's avatar
fxy060608 已提交
62 63 64
/**
 * 百度小程序的自定义组件,不支持动态事件绑定,故 v-model 也需要调整
 */
fxy060608's avatar
fxy060608 已提交
65
const transformModel = uniCliShared.createTransformModel(uniMpCompiler.transformModel);
fxy060608's avatar
fxy060608 已提交
66

fxy060608's avatar
fxy060608 已提交
67
const customElements = ['follow-swan', 'login', 'inline-payment-panel'];
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72
const nodeTransforms = [uniCliShared.transformRef, transformFor, uniCliShared.transformMatchMedia];
const directiveTransforms = {
    on: transformOn,
    model: transformModel,
};
fxy060608's avatar
fxy060608 已提交
73 74 75 76 77
const miniProgram = {
    class: {
        array: true,
    },
    slot: {
78
        fallbackContent: true,
fxy060608's avatar
fxy060608 已提交
79
        // https://github.com/baidu/san/discussions/601
80
        dynamicSlotNames: false,
fxy060608's avatar
fxy060608 已提交
81
    },
fxy060608's avatar
fxy060608 已提交
82
    directive: 's-',
fxy060608's avatar
fxy060608 已提交
83
};
fxy060608's avatar
fxy060608 已提交
84 85 86 87
const compilerOptions = {
    nodeTransforms,
    directiveTransforms,
};
fxy060608's avatar
fxy060608 已提交
88 89
const projectConfigFilename = 'project.swan.json';
const options = {
fxy060608's avatar
fxy060608 已提交
90
    cdn: 3,
fxy060608's avatar
fxy060608 已提交
91 92
    vite: {
        inject: {
fxy060608's avatar
fxy060608 已提交
93
            uni: [path__default["default"].resolve(__dirname, 'uni.api.esm.js'), 'default'],
fxy060608's avatar
fxy060608 已提交
94 95
        },
        alias: {
fxy060608's avatar
fxy060608 已提交
96
            'uni-mp-runtime': path__default["default"].resolve(__dirname, 'uni.mp.esm.js'),
fxy060608's avatar
fxy060608 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
        },
        copyOptions: {
            assets: ['swancomponents'],
        },
    },
    global: 'swan',
    app: {
        darkmode: false,
        subpackages: true,
    },
    project: {
        filename: projectConfigFilename,
        source,
    },
fxy060608's avatar
fxy060608 已提交
111
    template: Object.assign(Object.assign({}, miniProgram), { customElements, filter: {
fxy060608's avatar
fxy060608 已提交
112
            extname: '.sjs',
fxy060608's avatar
fxy060608 已提交
113 114 115 116 117 118
            lang: 'sjs',
            generate(filter, filename) {
                if (filename) {
                    return `<import-sjs src="${filename}.sjs" module="${filter.name}"/>`;
                }
                return `<import-sjs module="${filter.name}">
fxy060608's avatar
fxy060608 已提交
119 120
  ${filter.code}
  </import-sjs>`;
fxy060608's avatar
fxy060608 已提交
121
            },
fxy060608's avatar
fxy060608 已提交
122
        }, extname: '.swan', compilerOptions }),
fxy060608's avatar
fxy060608 已提交
123 124 125
    style: {
        extname: '.css',
    },
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131 132 133 134 135 136
};

const uniMiniProgramBaiduPlugin = {
    name: 'vite:uni-mp-baidu',
    config() {
        return {
            define: {
                __VUE_CREATED_DEFERRED__: false,
            },
        };
    },
fxy060608's avatar
fxy060608 已提交
137 138 139 140
};
var index = [uniMiniProgramBaiduPlugin, ...initMiniProgramPlugin__default["default"](options)];

module.exports = index;