fix(nuxt): normalise rollup opts in island transform w/o nuxt (#26589)

This commit is contained in:
Julien Huang 2024-04-03 12:36:15 +02:00 committed by GitHub
parent 59b58b8485
commit 2dc4505c8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@ import MagicString from 'magic-string'
import { ELEMENT_NODE, parse, walk } from 'ultrahtml' import { ELEMENT_NODE, parse, walk } from 'ultrahtml'
import { hash } from 'ohash' import { hash } from 'ohash'
import { resolvePath } from '@nuxt/kit' import { resolvePath } from '@nuxt/kit'
import defu from 'defu'
import { isVue } from '../core/utils' import { isVue } from '../core/utils'
interface ServerOnlyComponentTransformPluginOptions { interface ServerOnlyComponentTransformPluginOptions {
@ -146,7 +147,7 @@ export const islandsTransform = createUnplugin((options: ServerOnlyComponentTran
* extract attributes from a node * extract attributes from a node
*/ */
function extractAttributes (attributes: Record<string, string>, names: string[]) { function extractAttributes (attributes: Record<string, string>, names: string[]) {
const extracted:Record<string, string> = {} const extracted: Record<string, string> = {}
for (const name of names) { for (const name of names) {
if (name in attributes) { if (name in attributes) {
extracted[name] = attributes[name] extracted[name] = attributes[name]
@ -182,15 +183,27 @@ export const componentsChunkPlugin = createUnplugin((options: ComponentChunkOpti
vite: { vite: {
async config (config) { async config (config) {
const components = options.getComponents() const components = options.getComponents()
config.build = config.build || {}
config.build.rollupOptions = config.build.rollupOptions || {} config.build = defu(config.build, {
config.build.rollupOptions.output = config.build.rollupOptions.output || {} rollupOptions: {
config.build.rollupOptions.input = config.build.rollupOptions.input || {} input: {},
output: {}
}
})
const rollupOptions = config.build.rollupOptions!
if (typeof rollupOptions.input === 'string') {
rollupOptions.input = { entry: rollupOptions.input }
} else if (typeof rollupOptions.input === 'object' && Array.isArray(rollupOptions.input)) {
rollupOptions.input = rollupOptions.input.reduce<{ [key: string]: string }>((acc, input) => { acc[input] = input; return acc }, {})
}
// don't use 'strict', this would create another "facade" chunk for the entry file, causing the ssr styles to not detect everything // don't use 'strict', this would create another "facade" chunk for the entry file, causing the ssr styles to not detect everything
config.build.rollupOptions.preserveEntrySignatures = 'allow-extension' rollupOptions.preserveEntrySignatures = 'allow-extension'
for (const component of components) { for (const component of components) {
if (component.mode === 'client' || component.mode === 'all') { if (component.mode === 'client' || component.mode === 'all') {
(config.build.rollupOptions.input as Record<string, string>)[component.pascalName] = await resolvePath(component.filePath) rollupOptions.input![component.pascalName] = await resolvePath(component.filePath)
} }
} }
}, },