diff --git a/config/loaders/wxml-loader.js b/config/loaders/wxml-loader.js index 092c3a8..d4444d5 100644 --- a/config/loaders/wxml-loader.js +++ b/config/loaders/wxml-loader.js @@ -77,6 +77,7 @@ function traverse(doc, callback) { const isSrc = (name) => name === 'src'; const isDynamicSrc = (src) => /\{\{/.test(src); +const oakMessage = 'oak-message'; const oakRegex = /(\/*[a-zA-Z0-9_-])*\/app\/|(\\*[a-zA-Z0-9_-])*\\app\\/; const localRegex = /(\/*[a-zA-Z0-9_-])*\/src+\/|(\\*[a-zA-Z0-9_-])*\\src+\\/; const TranslationFunction = 't'; @@ -117,6 +118,15 @@ function getWxsCode() { return [code, runner].join('\n'); } +function getAppJson(context) { + const JSON_PATH = require.resolve(`${context}/app.json`); + if (!fs.existsSync(JSON_PATH)) { + return; + } + const data = fs.readFileSync(JSON_PATH, 'utf8'); + return JSON.parse(data); +} + module.exports = async function (content) { // loader的缓存功能 // this.cacheable && this.cacheable(); @@ -175,9 +185,16 @@ module.exports = async function (content) { } // 注入全局message组件 if (/pages/.test(context)) { - source = - source + - ``; + const appJson = getAppJson(projectContext); + if ( + appJson && + appJson.usingComponents && + appJson.usingComponents[oakMessage] + ) { + source = + source + + `\n <${oakMessage} show="{{!!oakError}}" type="{{oakError.type || ''}}" content="{{oakError.msg || ''}}" >`; + } } const doc = new DOMParser({