fix(sites): fix error relative path in playground (#959)

* fix(sites): fix error relative path in playground

* fix(sites): fix error relative path in playground

* fix(sites): fix error relative path in playground
This commit is contained in:
gimmyhehe 2023-11-30 14:43:42 +08:00 committed by GitHub
parent 4247eab395
commit b8810cc059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -28,7 +28,7 @@
import { ref } from 'vue'
import Svgs from '@opentiny/vue-icon'
import { Modal, Input as TinyInput } from '@opentiny/vue'
import { iconGroups } from './iconGroups'
import { iconGroups } from './iconGroups.js'
const all = Object.values(iconGroups).flat()
iconGroups.Others = Object.keys(Svgs).filter((name) => !all.includes(name) && name[0] === 'I')

View File

@ -27,7 +27,7 @@
<script>
import Svgs from '@opentiny/vue-icon'
import { Modal, Input } from '@opentiny/vue'
import { iconGroups } from './iconGroups'
import { iconGroups } from './iconGroups.js'
export default {
components: {

View File

@ -37,7 +37,8 @@ const $pub = (url) => {
* @returns
*/
const fetchDemosFile = (path) => {
return fetch(baseUrl + path, {
const filePath = baseUrl + path
return fetch(filePath, {
method: 'GET',
headers: {
'Content-Type': 'text/plain;charset=UTF-8'
@ -45,13 +46,33 @@ const fetchDemosFile = (path) => {
}).then((res) => {
if (res.ok) {
return res.text().then((text) => {
return text.replace(/(\$\{)?import\.meta\.env\.VITE_APP_BUILD_BASE_URL(\})?/g, (str) => {
// 处理相对路径引入
text = text.replace(/import.+('|")((\.\/|\.\.\/).+)('|")/g, (...args) => {
const [matchStr, , relativePath, type] = args
const pathArr = filePath.split('/')
if (type === './') {
const relativeFilename = relativePath.replace('./', '')
pathArr.splice(-1, 1, relativeFilename)
return matchStr.replace(relativePath, pathArr.join('/'))
} else if (type === '../') {
const relativePathArr = relativePath.split('../')
const len = relativePathArr.length
pathArr.splice(-len)
pathArr.push(relativePathArr[len - 1])
return matchStr.replace(relativePath, pathArr.join('/'))
} else {
return matchStr
}
})
// 处理静态资源路径
text = text.replace(/(\$\{)?import\.meta\.env\.VITE_APP_BUILD_BASE_URL(\})?/g, (str) => {
if (str.startsWith('$')) {
return import.meta.env.VITE_APP_BUILD_BASE_URL
} else {
return `'${import.meta.env.VITE_APP_BUILD_BASE_URL}'`
}
})
return text
})
} else {
throw new Error(res.statusText)