fix(nuxt): use case-insensitive regexp for <script> blocks

This commit is contained in:
Daniel Roe 2024-09-11 11:41:47 +01:00
parent 3ede01cd4e
commit 4fd24381c2
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
3 changed files with 4 additions and 4 deletions

View File

@ -24,7 +24,7 @@ interface ComponentChunkOptions {
buildDir: string buildDir: string
} }
const SCRIPT_RE = /<script[^>]*>/g const SCRIPT_RE = /<script[^>]*>/gi
const HAS_SLOT_OR_CLIENT_RE = /<slot[^>]*>|nuxt-client/ const HAS_SLOT_OR_CLIENT_RE = /<slot[^>]*>|nuxt-client/
const TEMPLATE_RE = /<template>([\s\S]*)<\/template>/ const TEMPLATE_RE = /<template>([\s\S]*)<\/template>/
const NUXTCLIENT_ATTR_RE = /\s:?nuxt-client(="[^"]*")?/g const NUXTCLIENT_ATTR_RE = /\s:?nuxt-client(="[^"]*")?/g

View File

@ -43,7 +43,7 @@ export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptio
}, },
transform (code, id) { transform (code, id) {
if (!KEYED_FUNCTIONS_RE.test(code)) { return } if (!KEYED_FUNCTIONS_RE.test(code)) { return }
const { 0: script = code, index: codeIndex = 0 } = code.match(/(?<=<script[^>]*>)[\s\S]*?(?=<\/script>)/) || { index: 0, 0: code } const { 0: script = code, index: codeIndex = 0 } = code.match(/(?<=<script[^>]*>)[\s\S]*?(?=<\/script>)/i) || { index: 0, 0: code }
const s = new MagicString(code) const s = new MagicString(code)
// https://github.com/unjs/unplugin/issues/90 // https://github.com/unjs/unplugin/issues/90
let imports: Set<string> | undefined let imports: Set<string> | undefined

View File

@ -697,8 +697,8 @@ describe('nuxt composables', () => {
expect(id1).toBeTruthy() expect(id1).toBeTruthy()
const matches = [ const matches = [
html.match(/<script[^>]*>\(\(\)=>\{console.log\(window\)\}\)\(\)<\/script>/), html.match(/<script[^>]*>\(\(\)=>\{console.log\(window\)\}\)\(\)<\/script>/),
html.match(new RegExp(`<script[^>]*>document.querySelectorAll\\('\\[data-prehydrate-id\\*=":${id1}:"]'\\).forEach\\(o=>{console.log\\(o.outerHTML\\)}\\)</script>`)), html.match(new RegExp(`<script[^>]*>document.querySelectorAll\\('\\[data-prehydrate-id\\*=":${id1}:"]'\\).forEach\\(o=>{console.log\\(o.outerHTML\\)}\\)</script>`, 'i')),
html.match(new RegExp(`<script[^>]*>document.querySelectorAll\\('\\[data-prehydrate-id\\*=":${id2}:"]'\\).forEach\\(o=>{console.log\\("other",o.outerHTML\\)}\\)</script>`)), html.match(new RegExp(`<script[^>]*>document.querySelectorAll\\('\\[data-prehydrate-id\\*=":${id2}:"]'\\).forEach\\(o=>{console.log\\("other",o.outerHTML\\)}\\)</script>`, 'i')),
] ]
// This tests we inject all scripts correctly, and only have one occurrence of multiple calls of a composable // This tests we inject all scripts correctly, and only have one occurrence of multiple calls of a composable