Skip to content

Commit

Permalink
fix: parse template name error (opentiny#880)
Browse files Browse the repository at this point in the history
* fix: parse template name error

* fix: add detection for test env
  • Loading branch information
chilingling committed Dec 4, 2024
1 parent 1b38d3b commit b5ba46c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function genTemplatePlugin(options = {}) {
return
}

const template = context?.template || 'default'
const template = options?.template || 'default'

if (!template) {
return
Expand Down
33 changes: 26 additions & 7 deletions packages/vue-generator/src/templates/vue-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,31 @@ const getTemplate = (schema, str) => {
const base64ToBlob = (base64Data) => {
// Split base64
const arr = base64Data.split(',')

// Get MIME type
const mimeMatch = arr[0].match(/:(.*?);/)

if (!mimeMatch) {
throw new Error('Invalid base64 data')
}

const mime = mimeMatch[1]
// Decode base64 string
let raw

try {
raw = window.atob(arr[1])
} catch (e) {
throw new Error('Failed to decode base64 string')
}

const rawLength = raw.length
// Convert to Blob
const uInt8Array = new Uint8Array(rawLength)
for (let i = 0; i < rawLength; i++) {
uInt8Array[i] = raw.charCodeAt(i)
}

return new Blob([uInt8Array], { type: mime })
}

Expand All @@ -68,7 +74,7 @@ const base64ToBlob = (base64Data) => {
* @returns
*/
export function generateTemplate(schema) {
return [
const res = [
{
fileType: 'md',
fileName: 'README.md',
Expand Down Expand Up @@ -98,12 +104,6 @@ export function generateTemplate(schema) {
path: '.',
fileContent: getTemplate(schema, entryHTMLFile)
},
{
fileType: 'image/x-icon',
fileName: 'favicon.ico',
path: './public',
fileContent: base64ToBlob(logoImage)
},
{
fileType: 'js',
fileName: 'main.js',
Expand Down Expand Up @@ -159,4 +159,23 @@ export function generateTemplate(schema) {
fileContent: httpEntryFile
}
]

// FIXME: vitest 测试的时候得到的并不是 base64data,所以这里需要跳过文件的出码
if (process.env?.NODE_ENV !== 'test') {
try {
const faviconData = base64ToBlob(logoImage)

res.push({
fileType: 'image/x-icon',
fileName: 'favicon.ico',
path: './public',
fileContent: faviconData
})
} catch (error) {
// eslint-disable-next-line no-console
console.error('generate favicon.ico error', error)
}
}

return res
}

0 comments on commit b5ba46c

Please sign in to comment.