diff --git a/packages/uni-cli-shared/lib/pages.js b/packages/uni-cli-shared/lib/pages.js index a2666e8c99728bf5c441facc2535592a58c2c8ca..a13820a4035d951f07c03bab7de90e630a1fbcdd 100644 --- a/packages/uni-cli-shared/lib/pages.js +++ b/packages/uni-cli-shared/lib/pages.js @@ -334,7 +334,7 @@ let uniAutoImportComponents = [] let uniAutoImportScanComponents = [] -function initAutoImportScanComponents () { +function initAutoImportScanComponents () { const componentsPath = path.resolve(process.env.UNI_INPUT_DIR, 'components') const components = {} try { @@ -347,12 +347,22 @@ function initAutoImportScanComponents () { }) } catch (e) {} - uniAutoImportScanComponents = parseUsingAutoImportComponents(components) - + uniAutoImportScanComponents = parseUsingAutoImportComponents(components) + refreshAutoComponentMap() } -function initAutoImportComponents (usingAutoImportComponents = {}) { +const _toString = Object.prototype.toString + +function isPlainObject (obj) { + return _toString.call(obj) === '[object Object]' +} + +function initAutoImportComponents (easycom = {}) { + let usingAutoImportComponents = easycom.custom || easycom || {} + if (!isPlainObject(usingAutoImportComponents)) { + usingAutoImportComponents = {} + } // 目前仅 mp-weixin 内置支持 page-meta 等组件 if (process.env.UNI_PLATFORM !== 'mp-weixin') { if (!usingAutoImportComponents['^page-meta$']) { @@ -415,10 +425,13 @@ function parseUsingAutoImportComponents (usingAutoImportComponents) { const autoImportComponents = [] if (usingAutoImportComponents) { Object.keys(usingAutoImportComponents).forEach(pattern => { - autoImportComponents.push({ - pattern: new RegExp(pattern), - replacement: usingAutoImportComponents[pattern] - }) + const replacement = usingAutoImportComponents[pattern] + if (replacement && typeof replacement === 'string') { + autoImportComponents.push({ + pattern: new RegExp(pattern), + replacement: replacement + }) + } }) } return autoImportComponents @@ -432,7 +445,7 @@ module.exports = { parsePagesJson, pagesJsonJsFileName, getAutoComponents, - initAutoImportComponents, + initAutoImportComponents, initAutoImportScanComponents, addPageUsingComponents, getUsingComponentsCode, diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 193b7a6af8659e006997e20ea20cacaf4a3029b9..c24d39d87f3c7b8d91ec9c6bfcfad64edc8245a9 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -312,12 +312,15 @@ if ( } const { - initAutoImportComponents, + initAutoImportComponents, initAutoImportScanComponents -} = require('@dcloudio/uni-cli-shared/lib/pages') - -initAutoImportScanComponents() -initAutoImportComponents(pagesJsonObj.easycom) +} = require('@dcloudio/uni-cli-shared/lib/pages') + +process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJsonObj.easycom && pagesJsonObj.easycom.autoscan === false) +initAutoImportComponents(pagesJsonObj.easycom) +if (process.UNI_AUTO_SCAN_COMPONENTS) { + initAutoImportScanComponents() +} runByHBuilderX && console.log(`正在编译中...`) diff --git a/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js index f1a22aa208fd53df28880cfc0d8748213e502642..a05b203d81e6ea03a73e41400724ab016cb81d26 100644 --- a/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js +++ b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js @@ -13,7 +13,9 @@ class WebpackUniAppPlugin { compiler.hooks.invalid.tap('webpack-uni-app-invalid', (fileName, changeTime) => { if (fileName && typeof fileName === 'string') { if (fileName.indexOf('.vue') !== -1 || fileName.indexOf('.nvue') !== -1) { - initAutoImportScanComponents() + if (process.UNI_AUTO_SCAN_COMPONENTS) { + initAutoImportScanComponents() + } } } }) diff --git a/packages/webpack-uni-pages-loader/lib/index-new.js b/packages/webpack-uni-pages-loader/lib/index-new.js index dee90251e02f246d85d8b2ea09c07c5e196f161b..57d258403b8cd3cd02846fda361066c7b36b0040 100644 --- a/packages/webpack-uni-pages-loader/lib/index-new.js +++ b/packages/webpack-uni-pages-loader/lib/index-new.js @@ -56,6 +56,7 @@ module.exports = function (content) { }) // 组件自动导入配置 + process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJson.easycom && pagesJson.easycom.autoscan === false) initAutoImportComponents(pagesJson.easycom) // TODO 与 usingComponents 放在一块读取设置 @@ -128,4 +129,4 @@ module.exports = function (content) { } return '' -} +}