Skip to content

Commit

Permalink
fix: 🐛 Package conversion errors in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Oct 17, 2024
1 parent 3073029 commit 74fba05
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
58 changes: 39 additions & 19 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,55 @@ import { pathToFileURL } from 'node:url'

import { extensionMap } from '../constants/index.js'

/**
* 检查给定内容中是否包含 <script> 标签。
*
* @param {string} content - 要检查的 HTML 内容。
* @returns {boolean} 如果内容中包含 <script> 标签,则返回 true;否则返回 false。
*/
export function hasScriptTag(content) {
const regex = /<script\b[^>]*>([\s\S]*?)<\/script>|<script\b[^>]*\/>/i

return regex.test(content)
}

/**
* 更新给定内容中所有 <script> 标签的属性。
*
* @param {string} content - 包含 <script> 标签的 HTML 内容。
* @param {Object} attrsToUpdate - 要更新的属性及其新值的对象,键为属性名,值为新值。
* @returns {string} 更新后的 HTML 内容,其中 <script> 标签的属性已被更新。
*/
export function updateScriptAttrs(content, attrsToUpdate) {
// 创建正则表达式以匹配 <script> 标签及其属性
const scriptRegex = /<script([^>]*)>/g

// 替换 <script> 标签中的属性
const updatedContent = content.replace(scriptRegex, (match, attrs) => {
// 将属性字符串分割为单个属性
const attributes = attrs.trim().split(/\s+/)

// 更新指定的属性
const updatedAttrs = attributes.map((attr) => {
const [attrName, attrValue] = attr.split('=')
if (attrsToUpdate[attrName]) {
return `${attrName}="${attrsToUpdate[attrName]}"` // 更新属性值
const scriptRegex = /<script\b([^>]*)>/gi

return content.replace(scriptRegex, (match, attrs) => {
const updatedAttrs = new Map(
attrs
.trim()
.split(/\s+/)
.map((attr) => {
const [name, ...valueParts] = attr.split('=')
const value = valueParts.join('=')
return [name, value ? value.replace(/^["']|["']$/g, '') : '']
}),
)

for (const [name, value] of Object.entries(attrsToUpdate)) {
if (value !== undefined) {
updatedAttrs.set(name, value)
}
else {
updatedAttrs.delete(name)
}
return attr // 保留未更新的属性
})
}

// 重新构建 <script> 标签
return `<script ${updatedAttrs.join(' ')}>`
})
const attrString = Array.from(updatedAttrs)
.map(([name, value]) => (value ? `${name}="${value}"` : name))
.join(' ')

return updatedContent
return `<script${attrString ? ` ${attrString}` : ''}>`
})
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Cleants {
this.progressCallback({ stage: 'copy', progress: 20 })

await this.processFiles()
this.progressCallback({ stage: 'convert', progress: 60 })
this.progressCallback({ stage: 'converting', progress: 60 })

await this.updateConfig()
await this.updatePackage()
Expand Down Expand Up @@ -294,8 +294,8 @@ class Cleants {

// 移除指定的依赖项
this.options.removeDependencies.forEach((dep) => {
delete packageJson.dependencies[dep]
delete packageJson.devDependencies[dep]
delete packageJson?.dependencies?.[dep]
delete packageJson?.devDependencies?.[dep]
})

// 更新 scripts
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const basePlugin = () => ({
extensions: Object.keys(extensionMap),

processFile: async (file, content, ctx) => {
const processFContent = ctx.transpileFile(content, path.extname(file))
const processContent = ctx.transpileFile(content, path.extname(file))

return processFContent
return processContent
},

writeFile: async (file, content, ctx) => {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/vue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const vuePlugin = () => ({

processContent = updateScriptAttrs(processContent, { lang: replaceLang })

// TODO 在 .vue 文件中需要去除ts编译器生成的冗余的导出
processContent = processContent.replaceAll('export {};', '')

return processContent
},

Expand Down

0 comments on commit 74fba05

Please sign in to comment.