From 87e3da93b30ff98b44539709e596a70a1e482a09 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 8 Apr 2020 15:17:52 +0800 Subject: [PATCH] fix(h5): compatible with relative public path --- .../uni-template-compiler/lib/asset-url.js | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/uni-template-compiler/lib/asset-url.js b/packages/uni-template-compiler/lib/asset-url.js index cb9621a32..a5c572af6 100644 --- a/packages/uni-template-compiler/lib/asset-url.js +++ b/packages/uni-template-compiler/lib/asset-url.js @@ -27,13 +27,13 @@ function urlToRequire (url) { } const uriParts = parseUriParts(url) if (!uriParts.hash) { // fixed by xxxxxx (v3 template中需要加/) - return `"/"+require("${url}")` + return `require("${url}")` } else { // fixed by xxxxxx (v3 template中需要加/) // support uri fragment case by excluding it from // the require and instead appending it as string; // assuming that the path part is sufficient according to // the above caseing(t.i. no protocol-auth-host parts expected) - return `"/"+require("${uriParts.path}") + "${uriParts.hash}"` + return `require("${uriParts.path}") + "${uriParts.hash}"` } } return returnValue @@ -66,14 +66,18 @@ function rewrite (attr, name, options) { .replace('"@/', '"/') .replace('"~@/', '"/') } - if (options.service || options.view || options.h5) { // v3,h5 + // v3,h5 + const needRequire = options.service || options.view || options.h5 + if (needRequire) { attr.value = urlToRequire(attr.value.slice(1, -1)) - // h5 且 publicPath 为 ./ (仅生产模式可能为./) - if ( - options.h5 && - options.publicPath === './' - ) { - attr.value = `(${attr.value}).substr(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)` + } } } return true -- GitLab