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 5bec3efcb1
commit 5a61bc52f2
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
}
const SCRIPT_RE = /<script[^>]*>/g
const SCRIPT_RE = /<script[^>]*>/gi
const HAS_SLOT_OR_CLIENT_RE = /<slot[^>]*>|nuxt-client/
const TEMPLATE_RE = /<template>([\s\S]*)<\/template>/
const NUXTCLIENT_ATTR_RE = /\s:?nuxt-client(="[^"]*")?/g

View File

@ -43,7 +43,7 @@ export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptio
},
transform (code, id) {
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)
// https://github.com/unjs/unplugin/issues/90
let imports: Set<string> | undefined

View File

@ -699,8 +699,8 @@ describe('nuxt composables', () => {
expect(id1).toBeTruthy()
const matches = [
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\\*=":${id2}:"]'\\).forEach\\(o=>{console.log\\("other",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>`, 'i')),
]
// This tests we inject all scripts correctly, and only have one occurrence of multiple calls of a composable