Skip to content

Commit

Permalink
fix: handle script tags with attributes (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 13, 2021
1 parent b3c5366 commit 19f9d5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/loaders/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const vueLoader: Loader = async (input, { loadFile }) => {
}

const contents = await input.getContents()
const [scriptBlock, attrs = '', script] = contents.match(/<script(\s[^>\s]*)*>([\S\s.]*?)<\/script>/) || []
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [scriptBlock, attrs = '', _lastAttr, script] = contents.match(/<script((\s[^>\s]*)*)>([\S\s.]*?)<\/script>/) || []
if (!scriptBlock || !script) {
return
}
Expand All @@ -26,10 +27,12 @@ export const vueLoader: Loader = async (input, { loadFile }) => {
return
}

const newAttrs = attrs.replace(new RegExp(`\\s?lang="${lang}"`), '')

return [
{
path: input.path,
contents: contents.replace(scriptBlock, `<script>\n${scriptFile.contents}</script>`)
contents: contents.replace(scriptBlock, `<script${newAttrs}>\n${scriptFile.contents}</script>`)
},
...files.filter(f => f !== scriptFile)
]
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ describe('createLoader', () => {
expect(results).toMatchObject([{ raw: true }])
})

it('vueLoader handles script tags with attributes', async () => {
const { loadFile } = createLoader({
loaders: [vueLoader, jsLoader]
})
const results = await loadFile({
extension: '.vue',
getContents: () => '<script setup lang="ts">Test</script>',
path: 'test.vue'
})
expect(results).toMatchObject([{ contents: ['<script setup>', 'Test;', '</script>'].join('\n') }])
})

it('vueLoader will generate dts file', async () => {
const { loadFile } = createLoader({
loaders: [vueLoader, jsLoader],
Expand Down

0 comments on commit 19f9d5d

Please sign in to comment.