提交 27bc27d7 编写于 作者: fxy060608's avatar fxy060608

refactor(v3): asset url

上级 11ceb637
......@@ -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) {
......
......@@ -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
......
......@@ -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 运行时,可以支持自定义输出目录
......
'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
}
......@@ -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')
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册