From 829df8272c89af1ccf416e725e68738b2279382c Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 10 Nov 2020 18:43:40 +0800 Subject: [PATCH] feat(cli): copy assets (uni_modules) --- lib/h5/uni.config.js | 10 +++++++--- packages/uni-app-plus/lib/uni.config.js | 3 +++ packages/uni-mp-alipay/lib/uni.config.js | 10 +++++++--- packages/uni-mp-baidu/lib/uni.config.js | 10 +++++++--- packages/uni-mp-kuaishou/lib/uni.config.js | 12 ++++++++---- packages/uni-mp-qq/lib/uni.config.js | 14 ++++++++++++-- packages/uni-mp-toutiao/lib/uni.config.js | 10 +++++++--- packages/uni-mp-weixin/lib/uni.config.js | 16 +++++++++++++--- .../lib/copy-webpack-options.js | 10 +++++++++- packages/vue-cli-plugin-uni/lib/env.js | 12 ++++++++++++ .../webpack-uni-pages-loader/lib/uni_modules.js | 6 +----- 11 files changed, 86 insertions(+), 27 deletions(-) diff --git a/lib/h5/uni.config.js b/lib/h5/uni.config.js index 46f6b98e26..60be35d01b 100644 --- a/lib/h5/uni.config.js +++ b/lib/h5/uni.config.js @@ -1,7 +1,7 @@ const fs = require('fs') const path = require('path') -function getTemplatePath (template) { +function getTemplatePath(template) { if (template) { const userTemplate = path.resolve(process.env.UNI_INPUT_DIR, template) if (fs.existsSync(userTemplate)) @@ -40,12 +40,16 @@ module.exports = { vue: '@dcloudio/vue-cli-plugin-uni/packages/h5-vue' }, copyWebpackOptions(platformOptions, vueOptions) { - return [{ + const copyOptions = [{ from: require.resolve('@dcloudio/uni-h5/dist/index.css'), to: getIndexCssPath(vueOptions.assetsDir, platformOptions.template), transform }, 'hybrid/html' ] + global.uniModules.forEach(module => { + copyOptions.push('uni_modules/' + module + '/hybrid/html') + }) + return copyOptions } -} +} diff --git a/packages/uni-app-plus/lib/uni.config.js b/packages/uni-app-plus/lib/uni.config.js index 665a0f4956..2241958b94 100644 --- a/packages/uni-app-plus/lib/uni.config.js +++ b/packages/uni-app-plus/lib/uni.config.js @@ -33,6 +33,9 @@ module.exports = { copyOptions.push(componentsCopyOption) } copyOptions.push('hybrid/html') + global.uniModules.forEach(module => { + copyOptions.push('uni_modules/' + module + '/hybrid/html') + }) if (process.env.UNI_USING_V3) { // TODO 将仅保留v3逻辑 copyOptions.push(path.resolve(__dirname, '../dist/view.css')) copyOptions.push(path.resolve(__dirname, '../dist/view.umd.min.js')) diff --git a/packages/uni-mp-alipay/lib/uni.config.js b/packages/uni-mp-alipay/lib/uni.config.js index 162b12418e..4baf785d6f 100644 --- a/packages/uni-mp-alipay/lib/uni.config.js +++ b/packages/uni-mp-alipay/lib/uni.config.js @@ -4,8 +4,8 @@ module.exports = { cssVars: { '--status-bar-height': '25px', '--window-top': '0px', - '--window-bottom': '0px', - '--window-left': '0px', + '--window-bottom': '0px', + '--window-left': '0px', '--window-right': '0px' }, extnames: { @@ -17,6 +17,10 @@ module.exports = { subPackages: true }, copyWebpackOptions (platformOptions, vueOptions) { - return ['mycomponents'] + const copyOptions = ['mycomponents'] + global.uniModules.forEach(module => { + copyOptions.push('uni_modules/' + module + '/mycomponents') + }) + return copyOptions } } diff --git a/packages/uni-mp-baidu/lib/uni.config.js b/packages/uni-mp-baidu/lib/uni.config.js index ad8a64c6d1..38afcfef3c 100644 --- a/packages/uni-mp-baidu/lib/uni.config.js +++ b/packages/uni-mp-baidu/lib/uni.config.js @@ -3,8 +3,8 @@ module.exports = { cssVars: { '--status-bar-height': '25px', '--window-top': '0px', - '--window-bottom': '0px', - '--window-left': '0px', + '--window-bottom': '0px', + '--window-left': '0px', '--window-right': '0px' }, extnames: { @@ -17,6 +17,10 @@ module.exports = { subPackages: true }, copyWebpackOptions (platformOptions, vueOptions) { - return ['swancomponents'] + const copyOptions = ['swancomponents'] + global.uniModules.forEach(module => { + copyOptions.push('uni_modules/' + module + '/swancomponents') + }) + return copyOptions } } diff --git a/packages/uni-mp-kuaishou/lib/uni.config.js b/packages/uni-mp-kuaishou/lib/uni.config.js index a18300748e..5af7798fab 100644 --- a/packages/uni-mp-kuaishou/lib/uni.config.js +++ b/packages/uni-mp-kuaishou/lib/uni.config.js @@ -3,8 +3,8 @@ module.exports = { cssVars: { '--status-bar-height': '25px', '--window-top': '0px', - '--window-bottom': '0px', - '--window-left': '0px', + '--window-bottom': '0px', + '--window-left': '0px', '--window-right': '0px' }, extnames: { @@ -14,6 +14,10 @@ module.exports = { project: 'project.ks.json' }, copyWebpackOptions (platformOptions, vueOptions) { - return ['kscomponents'] + const copyOptions = ['kscomponents'] + global.uniModules.forEach(module => { + copyOptions.push('uni_modules/' + module + '/kscomponents') + }) + return copyOptions } -} +} diff --git a/packages/uni-mp-qq/lib/uni.config.js b/packages/uni-mp-qq/lib/uni.config.js index fa69999c2c..96bbb194af 100644 --- a/packages/uni-mp-qq/lib/uni.config.js +++ b/packages/uni-mp-qq/lib/uni.config.js @@ -8,8 +8,8 @@ module.exports = { cssVars: { '--status-bar-height': '25px', '--window-top': '0px', - '--window-bottom': '0px', - '--window-left': '0px', + '--window-bottom': '0px', + '--window-left': '0px', '--window-right': '0px' }, extnames: { @@ -38,6 +38,16 @@ module.exports = { ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤 }) } + global.uniModules.forEach(module => { + const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, COMPONENTS_DIR_NAME) + if (fs.existsSync(wxcomponentsDir)) { + copyOptions.push({ + from: wxcomponentsDir, + to: 'uni_modules/' + module + '/' + COMPONENTS_DIR_NAME, + ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤 + }) + } + }) return copyOptions } } diff --git a/packages/uni-mp-toutiao/lib/uni.config.js b/packages/uni-mp-toutiao/lib/uni.config.js index cb2a821021..fd1eb36a50 100644 --- a/packages/uni-mp-toutiao/lib/uni.config.js +++ b/packages/uni-mp-toutiao/lib/uni.config.js @@ -3,8 +3,8 @@ module.exports = { cssVars: { '--status-bar-height': '25px', '--window-top': '0px', - '--window-bottom': '0px', - '--window-left': '0px', + '--window-bottom': '0px', + '--window-left': '0px', '--window-right': '0px' }, extnames: { @@ -14,6 +14,10 @@ module.exports = { project: 'project.tt.json' }, copyWebpackOptions (platformOptions, vueOptions) { - return ['ttcomponents'] + const copyOptions = ['ttcomponents'] + global.uniModules.forEach(module => { + copyOptions.push('uni_modules/' + module + '/ttcomponents') + }) + return copyOptions } } diff --git a/packages/uni-mp-weixin/lib/uni.config.js b/packages/uni-mp-weixin/lib/uni.config.js index 6dba971a3d..8cf180bc6c 100644 --- a/packages/uni-mp-weixin/lib/uni.config.js +++ b/packages/uni-mp-weixin/lib/uni.config.js @@ -8,8 +8,8 @@ module.exports = { cssVars: { '--status-bar-height': '25px', '--window-top': '0px', - '--window-bottom': '0px', - '--window-left': '0px', + '--window-bottom': '0px', + '--window-left': '0px', '--window-right': '0px' }, extnames: { @@ -23,7 +23,7 @@ module.exports = { darkmode: true }, copyWebpackOptions (platformOptions, vueOptions) { - const copyOptions = [ + const copyOptions = [ 'theme.json', 'sitemap.json', 'ext.json', @@ -40,6 +40,16 @@ module.exports = { ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤 }) } + global.uniModules.forEach(module => { + const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, COMPONENTS_DIR_NAME) + if (fs.existsSync(wxcomponentsDir)) { + copyOptions.push({ + from: wxcomponentsDir, + to: 'uni_modules/' + module + '/' + COMPONENTS_DIR_NAME, + ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤 + }) + } + }) return copyOptions } } diff --git a/packages/vue-cli-plugin-uni/lib/copy-webpack-options.js b/packages/vue-cli-plugin-uni/lib/copy-webpack-options.js index 3efe222e7a..bc4c0eac05 100644 --- a/packages/vue-cli-plugin-uni/lib/copy-webpack-options.js +++ b/packages/vue-cli-plugin-uni/lib/copy-webpack-options.js @@ -51,8 +51,16 @@ function getAssetsCopyOptions (assetsDir) { return copyOptions } +function getUniModulesAssetsCopyOptions (assetsDir) { + const copyOptions = [] + global.uniModules.forEach(module => { + copyOptions.push(...getAssetsCopyOptions('uni_modules/' + module + '/' + assetsDir)) + }) + return copyOptions +} + function getCopyWebpackPluginOptions (platformOptions, vueOptions) { - const copyOptions = getAssetsCopyOptions(assetsDir) + const copyOptions = getAssetsCopyOptions(assetsDir).concat(getUniModulesAssetsCopyOptions(assetsDir)) global.uniPlugin.copyWebpackOptions.forEach(copyWebpackOptions => { const platformCopyOptions = copyWebpackOptions(platformOptions, vueOptions, copyOptions) || [] platformCopyOptions.forEach(copyOption => { diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 2b81150edf..6363bdc08c 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -19,6 +19,7 @@ process.env.UNI_INPUT_DIR = process.env.UNI_INPUT_DIR || path.resolve(process.cw // 初始化全局插件对象 global.uniPlugin = require('@dcloudio/uni-cli-shared/lib/plugin').init() + const manifestJsonObj = require('@dcloudio/uni-cli-shared/lib/manifest').getManifestJson() const platformOptions = manifestJsonObj[process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM] || {} // 插件校验环境 @@ -103,6 +104,17 @@ if (process.env.NODE_ENV === 'production') { // 发行模式,不启用 cache delete process.env.UNI_USING_CACHE } +global.uniModules = [] +try { + global.uniModules = fs + .readdirSync(path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules')) + .filter(module => + fs.existsSync( + path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, 'package.json') + ) + ) +} catch (e) {} + const { normalizePath, isSupportSubPackages, diff --git a/packages/webpack-uni-pages-loader/lib/uni_modules.js b/packages/webpack-uni-pages-loader/lib/uni_modules.js index a1db448a2d..e2d27ee6b2 100644 --- a/packages/webpack-uni-pages-loader/lib/uni_modules.js +++ b/packages/webpack-uni-pages-loader/lib/uni_modules.js @@ -22,12 +22,8 @@ function normalizeUniModulesPagesJson (pagesJson, pluginId) { module.exports = function parsePages (content) { const uniModulesDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules') - let plugins = [] - try { - plugins = fs.readdirSync(uniModulesDir) - } catch (e) {} const pluginPagesJsons = [] - plugins.forEach(plugin => { + global.uniModules.forEach(plugin => { const pagesJsonPath = path.resolve(uniModulesDir, plugin, 'pages.json') if (fs.existsSync(pagesJsonPath)) { pluginPagesJsons.push( -- GitLab