fix(nuxt): remove side-effect imports from page metadata (#6376)

This commit is contained in:
Daniel Roe 2022-08-05 17:35:38 +01:00 committed by GitHub
parent 07fa104b46
commit 60656dfa2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -43,9 +43,18 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
return result() return result()
} }
const imports = findStaticImports(code)
// Purge all imports bringing side effects, such as CSS imports
for (const entry of imports) {
if (!entry.imports) {
s.remove(entry.start, entry.end)
}
}
// [webpack] Re-export any imports from script blocks in the components // [webpack] Re-export any imports from script blocks in the components
// with workaround for vue-loader bug: https://github.com/vuejs/vue-loader/pull/1911 // with workaround for vue-loader bug: https://github.com/vuejs/vue-loader/pull/1911
const scriptImport = findStaticImports(code).find(i => parseQuery(i.specifier.replace('?macro=true', '')).type === 'script') const scriptImport = imports.find(i => parseQuery(i.specifier.replace('?macro=true', '')).type === 'script')
if (scriptImport) { if (scriptImport) {
// https://github.com/vuejs/vue-loader/pull/1911 // https://github.com/vuejs/vue-loader/pull/1911
// https://github.com/vitejs/vite/issues/8473 // https://github.com/vitejs/vite/issues/8473

View File

@ -372,7 +372,7 @@ describe('dynamic paths', () => {
const html = await $fetch('/assets') const html = await $fetch('/assets')
const urls = Array.from(html.matchAll(/(href|src)="(.*?)"/g)).map(m => m[2]) const urls = Array.from(html.matchAll(/(href|src)="(.*?)"/g)).map(m => m[2])
const cssURL = urls.find(u => /_nuxt\/entry.*\.css$/.test(u)) const cssURL = urls.find(u => /_nuxt\/assets.*\.css$/.test(u))
expect(cssURL).toBeDefined() expect(cssURL).toBeDefined()
const css = await $fetch(cssURL) const css = await $fetch(cssURL)
const imageUrls = Array.from(css.matchAll(/url\(([^)]*)\)/g)).map(m => m[1].replace(/[-.][\w]{8}\./g, '.')) const imageUrls = Array.from(css.matchAll(/url\(([^)]*)\)/g)).map(m => m[1].replace(/[-.][\w]{8}\./g, '.'))