From 27bc27d75a01692a7ba395d55eed49f072710e98 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 11 May 2020 13:47:35 +0800 Subject: [PATCH] refactor(v3): asset url --- packages/uni-cli-shared/lib/url-loader.js | 6 ---- .../uni-template-compiler/lib/asset-url.js | 20 ++++------- packages/vue-cli-plugin-uni/index.js | 9 +++-- .../vue-cli-plugin-uni/lib/app-plus/getUrl.js | 33 +++++++++++++++++++ packages/vue-cli-plugin-uni/lib/env.js | 7 ++-- 5 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 packages/vue-cli-plugin-uni/lib/app-plus/getUrl.js diff --git a/packages/uni-cli-shared/lib/url-loader.js b/packages/uni-cli-shared/lib/url-loader.js index e4ce16a7d..8d06a6929 100644 --- a/packages/uni-cli-shared/lib/url-loader.js +++ b/packages/uni-cli-shared/lib/url-loader.js @@ -10,12 +10,6 @@ const defaultOptions = { loader: 'file-loader', options: { publicPath (url, resourcePath, context) { - if ( - process.env.UNI_PLATFORM === 'app-plus' && - process.env.UNI_USING_V3 - ) { // app-plus v3 下路径不能以/开头 - return normalizePath(path.relative(process.env.UNI_INPUT_DIR, resourcePath)) - } return '/' + normalizePath(path.relative(process.env.UNI_INPUT_DIR, resourcePath)) }, outputPath (url, resourcePath, context) { diff --git a/packages/uni-template-compiler/lib/asset-url.js b/packages/uni-template-compiler/lib/asset-url.js index e689fd88f..653e38959 100644 --- a/packages/uni-template-compiler/lib/asset-url.js +++ b/packages/uni-template-compiler/lib/asset-url.js @@ -43,14 +43,14 @@ function urlToRequire (url) { * @param urlString an url as a string */ function parseUriParts (urlString) { - // initialize return value + // initialize return value /* eslint-disable node/no-deprecated-api */ const returnValue = url.parse('') if (urlString) { // A TypeError is thrown if urlString is not a string // @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost if (typeof urlString === 'string') { - // check is an uri + // check is an uri /* eslint-disable node/no-deprecated-api */ return url.parse(urlString) // take apart the uri } @@ -68,18 +68,12 @@ function rewrite (attr, name, options) { .replace('"@/', '"/') .replace('"~@/', '"/') } - // v3,h5 - const needRequire = options.service || options.view || options.h5 - if (needRequire) { + // h5 + if (options.h5) { attr.value = urlToRequire(attr.value.slice(1, -1)) - if (attr.value.startsWith('require("')) { // require - // v3 需要增加前缀 / - if (options.service || options.view) { - attr.value = `"/"+${attr.value}` - } else if (options.h5 && options.publicPath === './') { - // h5 且 publicPath 为 ./ (仅生产模式可能为./) - attr.value = `(${attr.value}).substr(1)` - } + if (attr.value.startsWith('require("') && options.publicPath === './') { // require + // h5 且 publicPath 为 ./ (仅生产模式可能为./) + attr.value = `(${attr.value}).substr(1)` } } return true diff --git a/packages/vue-cli-plugin-uni/index.js b/packages/vue-cli-plugin-uni/index.js index 4d62acda9..a037f49ab 100644 --- a/packages/vue-cli-plugin-uni/index.js +++ b/packages/vue-cli-plugin-uni/index.js @@ -53,8 +53,13 @@ module.exports = (api, options) => { vueConfig = vueConfig(options, api) } - if (options.pages) { // 允许 vue.config.js pages 覆盖 - delete vueConfig.pages + if (options.pages) { + // h5平台 允许 vue.config.js pages 覆盖,其他平台移除 pages 配置 + if (process.env.UNI_PLATFORM === 'h5') { + delete vueConfig.pages + } else { + delete options.pages + } } Object.assign(options, { // TODO 考虑非 HBuilderX 运行时,可以支持自定义输出目录 diff --git a/packages/vue-cli-plugin-uni/lib/app-plus/getUrl.js b/packages/vue-cli-plugin-uni/lib/app-plus/getUrl.js new file mode 100644 index 000000000..a743d8ca7 --- /dev/null +++ b/packages/vue-cli-plugin-uni/lib/app-plus/getUrl.js @@ -0,0 +1,33 @@ +'use strict' + +module.exports = function (url, options) { + if (!options) { + // eslint-disable-next-line no-param-reassign + options = {} + } // eslint-disable-next-line no-underscore-dangle, no-param-reassign + + url = url && url.__esModule ? url.default : url + + if (typeof url !== 'string') { + return url + } // If url is already wrapped in quotes, remove them + + if (/^['"].*['"]$/.test(url)) { + // eslint-disable-next-line no-param-reassign + url = url.slice(1, -1) + } + + if (options.hash) { + // eslint-disable-next-line no-param-reassign + url += options.hash + } // Should url be wrapped? + // See https://drafts.csswg.org/css-values-3/#urls + + if (/["'() \t\n]/.test(url) || options.needQuotes) { + return '"'.concat(url.replace(/"/g, '\\"').replace(/\n/g, '\\n'), '"') + } + if (url.indexOf('/') === 0) { + return url.substr(1) + } + return url +} diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 5cbfa1f17..58b651348 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -2,8 +2,8 @@ const fs = require('fs') const path = require('path') const mkdirp = require('mkdirp') const loaderUtils = require('loader-utils') - -require('./error-reporting') + +require('./error-reporting') const hasOwnProperty = Object.prototype.hasOwnProperty @@ -335,7 +335,8 @@ moduleAlias.addAlias('mpvue-template-compiler', '@dcloudio/vue-cli-plugin-uni/pa // vue-loader moduleAlias.addAlias('vue-loader', '@dcloudio/vue-cli-plugin-uni/packages/vue-loader') -if (process.env.UNI_USING_V3 && process.env.UNI_PLATFORM === 'app-plus') { +if (process.env.UNI_USING_V3 && process.env.UNI_PLATFORM === 'app-plus') { + moduleAlias.addAlias('../runtime/getUrl.js', '@dcloudio/vue-cli-plugin-uni/lib/app-plus/getUrl.js') moduleAlias.addAlias('vue-style-loader', '@dcloudio/vue-cli-plugin-uni/packages/app-vue-style-loader') } -- GitLab