From e380df20a55e87580b560a860d98b0ae61c765ae Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 16 Apr 2020 10:21:06 +0800 Subject: [PATCH] feat(v3): Add scoped attribute to style tag for custom components --- .../vue-cli-plugin-uni/lib/app-plus/index.js | 8 +++++++ packages/vue-cli-plugin-uni/lib/env.js | 2 ++ .../webpack-uni-app-loader/view/style.js | 24 +++++++++++++++++++ .../lib/platforms/app-plus/define-pages.js | 2 +- 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/style.js diff --git a/packages/vue-cli-plugin-uni/lib/app-plus/index.js b/packages/vue-cli-plugin-uni/lib/app-plus/index.js index ec47a78e24..796e6a063f 100644 --- a/packages/vue-cli-plugin-uni/lib/app-plus/index.js +++ b/packages/vue-cli-plugin-uni/lib/app-plus/index.js @@ -236,6 +236,14 @@ const v3 = { } if (isAppView) { + if (process.env.UNI_USING_V3_SCOPED) { + webpackConfig.module + .rule('vue') + .use('uniapp-app-style-scoped') + .loader(path.resolve(__dirname, + '../../packages/webpack-uni-app-loader/view/style')) + } + if (process.env.NODE_ENV === 'production') { require('../h5/cssnano-options')(webpackConfig) } diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index bb288bd38b..1c7ca9a7e8 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -25,6 +25,8 @@ global.uniPlugin.validate.forEach(validate => { }) process.UNI_MANIFEST = manifestJsonObj + +process.env.UNI_USING_V3_SCOPED = true process.UNI_CLOUD = false process.UNI_CLOUD_TCB = false diff --git a/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/style.js b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/style.js new file mode 100644 index 0000000000..ace6f647f8 --- /dev/null +++ b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/style.js @@ -0,0 +1,24 @@ +const path = require('path') +const loaderUtils = require('loader-utils') + +const isWin = /^win/.test(process.platform) +const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path) + +const AppPath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, 'App.vue')) +module.exports = function(content) { + this.cacheable && this.cacheable() + + if (this.resourceQuery) { + const params = loaderUtils.parseQuery(this.resourceQuery) + if (params.mpType === 'page') { + return content + } + } + if (normalizePath(this.resourcePath) === AppPath) { + return content + } + if (content.indexOf('platform="mp-weixin"') !== -1) { + return content + } + return content.replace(/(<]*)>/ig, '$1 scoped>') +} diff --git a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js index 1e4694522e..47b8b27967 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js @@ -3,7 +3,7 @@ function generatePageCode (pages, pageOptions) { if (pageOptions[pagePath].nvue) { return '' } - return `__definePage('${pagePath}',function(){return Vue.extend(require('${pagePath}.vue').default)})` + return `__definePage('${pagePath}',function(){return Vue.extend(require('${pagePath}.vue?mpType=page').default)})` }).join('\n') } -- GitLab