From 6e2e7815e598b0e4a638cd8a160a031c7aa2cc95 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 20 Aug 2020 13:48:36 +0800 Subject: [PATCH] feat(cli): support match-media --- packages/uni-cli-shared/lib/pages.js | 30 +++++++++++++------ packages/uni-template-compiler/lib/util.js | 14 +++++++-- .../lib/plugin/generate-component.js | 11 +++---- .../webpack-uni-mp-loader/lib/script-new.js | 13 ++++---- .../webpack-uni-mp-loader/lib/template.js | 9 +++--- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/packages/uni-cli-shared/lib/pages.js b/packages/uni-cli-shared/lib/pages.js index 42346ad58..ff1d05ba6 100644 --- a/packages/uni-cli-shared/lib/pages.js +++ b/packages/uni-cli-shared/lib/pages.js @@ -111,7 +111,7 @@ function isNVuePage (page, root = '') { } function isValidPage (page, root = '') { - if (typeof page === 'string' || !page.path) { // 不合法的配置 + if (typeof page === 'string' || !page.path) { // 不合法的配置 console.warn('pages.json 页面配置错误, 已被忽略, 查看文档: https://uniapp.dcloud.io/collocation/pages?id=pages') return false } @@ -392,14 +392,13 @@ function initAutoImportComponents (easycom = {}) { } // 目前仅 mp-weixin 内置支持 page-meta 等组件 if (process.env.UNI_PLATFORM !== 'mp-weixin') { - if (!usingAutoImportComponents['^page-meta$']) { - usingAutoImportComponents['^page-meta$'] = - '@dcloudio/uni-cli-shared/components/page-meta.vue' - } - if (!usingAutoImportComponents['^navigation-bar$']) { - usingAutoImportComponents['^navigation-bar$'] = - '@dcloudio/uni-cli-shared/components/navigation-bar.vue' - } + BUILT_IN_COMPONENTS.forEach(name => { + const easycomName = `^${name}$` + if (!usingAutoImportComponents[easycomName]) { + usingAutoImportComponents[easycomName] = + '@dcloudio/uni-cli-shared/components/' + name + '.vue' + } + }) } const newUsingAutoImportComponentsJson = JSON.stringify(usingAutoImportComponents) @@ -463,7 +462,20 @@ function parseUsingAutoImportComponents (usingAutoImportComponents) { } return autoImportComponents } + +const BUILT_IN_COMPONENTS = ['page-meta', 'navigation-bar', 'match-media'] + +function isBuiltInComponent (name) { + return BUILT_IN_COMPONENTS.includes(name) +} + +function isBuiltInComponentPath (modulePath) { + return !!BUILT_IN_COMPONENTS.find(name => modulePath.includes(name)) +} + module.exports = { + isBuiltInComponent, + isBuiltInComponentPath, getMainEntry, getNVueMainEntry, parsePages, diff --git a/packages/uni-template-compiler/lib/util.js b/packages/uni-template-compiler/lib/util.js index 41b3d3a86..fc4cbb22a 100644 --- a/packages/uni-template-compiler/lib/util.js +++ b/packages/uni-template-compiler/lib/util.js @@ -181,6 +181,10 @@ function hasOwn (obj, key) { const tags = require('@dcloudio/uni-cli-shared/lib/tags') +const { + isBuiltInComponent +} = require('@dcloudio/uni-cli-shared/lib/pages') + const { getTagName } = require('./h5') @@ -196,7 +200,7 @@ function isComponent (tagName) { } // mp-weixin 底层支持 page-meta,navigation-bar if (process.env.UNI_PLATFORM === 'mp-weixin') { - if (tagName === 'page-meta' || tagName === 'navigation-bar') { + if (isBuiltInComponent(tagName)) { return false } } @@ -219,7 +223,11 @@ function makeMap (str, expectsLowerCase) { * @param {*} node */ function isSimpleObjectExpression (node) { - return t.isObjectExpression(node) && !node.properties.find(({ key, value }) => !t.isIdentifier(key) || !(t.isIdentifier(value) || t.isStringLiteral(value) || t.isBooleanLiteral(value) || t.isNumericLiteral(value) || t.isNullLiteral(value))) + return t.isObjectExpression(node) && !node.properties.find(({ + key, + value + }) => !t.isIdentifier(key) || !(t.isIdentifier(value) || t.isStringLiteral(value) || t.isBooleanLiteral(value) || + t.isNumericLiteral(value) || t.isNullLiteral(value))) } module.exports = { @@ -253,4 +261,4 @@ module.exports = { processMemberExpression, getForIndexIdentifier, isSimpleObjectExpression -} +} diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js index fc89fa61e..2db5fd673 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js @@ -10,6 +10,10 @@ const { getJsonFile } = require('@dcloudio/uni-cli-shared/lib/cache') +const { + isBuiltInComponentPath +} = require('@dcloudio/uni-cli-shared/lib/pages') + const { restoreNodeModules } = require('../shared') @@ -88,10 +92,7 @@ module.exports = function generateComponent (compilation) { if ( /^win/.test(process.platform) && modulePath.includes('@dcloudio') && - ( - modulePath.includes('page-meta') || - modulePath.includes('navigation-bar') - ) + isBuiltInComponentPath(modulePath) ) { resource = normalizePath(path.resolve(process.env.UNI_CLI_CONTEXT, modulePath)) } @@ -233,4 +234,4 @@ function removeUnusedComponent (name) { fs.renameSync(path.join(process.env.UNI_OUTPUT_DIR, name + '.json'), path.join(process.env.UNI_OUTPUT_DIR, name + '.bak.json')) } catch (e) {} -} +} diff --git a/packages/webpack-uni-mp-loader/lib/script-new.js b/packages/webpack-uni-mp-loader/lib/script-new.js index 6f3eadf4a..335552e44 100644 --- a/packages/webpack-uni-mp-loader/lib/script-new.js +++ b/packages/webpack-uni-mp-loader/lib/script-new.js @@ -12,7 +12,11 @@ const { const { getBabelParserOptions -} = require('@dcloudio/uni-cli-shared/lib/platform') +} = require('@dcloudio/uni-cli-shared/lib/platform') + +const { + isBuiltInComponentPath +} = require('@dcloudio/uni-cli-shared/lib/pages') const { updateUsingComponents @@ -61,11 +65,8 @@ module.exports = function (content, map) { if ( // windows 上 page-meta, navigation-bar 可能在不同盘上 /^win/.test(process.platform) && - path.isAbsolute(resourcePath) && - ( - resourcePath.indexOf('page-meta') !== -1 || - resourcePath.indexOf('navigation-bar') !== -1 - ) + path.isAbsolute(resourcePath) && + isBuiltInComponentPath(resourcePath) ) { resourcePath = normalizePath(path.relative(process.env.UNI_CLI_CONTEXT, resourcePath)) } diff --git a/packages/webpack-uni-mp-loader/lib/template.js b/packages/webpack-uni-mp-loader/lib/template.js index 36dbff254..6539148f7 100644 --- a/packages/webpack-uni-mp-loader/lib/template.js +++ b/packages/webpack-uni-mp-loader/lib/template.js @@ -19,6 +19,10 @@ const { updateUsingGlobalComponents } = require('@dcloudio/uni-cli-shared/lib/cache') +const { + isBuiltInComponentPath +} = require('@dcloudio/uni-cli-shared/lib/pages') + const { getPlatformFilterTag } = require('@dcloudio/uni-cli-shared/lib/platform') @@ -49,10 +53,7 @@ module.exports = function (content, map) { if ( // windows 上 page-meta, navigation-bar 可能在不同盘上 /^win/.test(process.platform) && path.isAbsolute(resourcePath) && - ( - resourcePath.indexOf('page-meta') !== -1 || - resourcePath.indexOf('navigation-bar') !== -1 - ) + isBuiltInComponentPath(resourcePath) ) { resourcePath = normalizePath(path.relative(process.env.UNI_CLI_CONTEXT, resourcePath)) } -- GitLab