Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(vite): don't fail builds for virtual modules that don't support i…
Browse files Browse the repository at this point in the history
…nlining (#7440)
  • Loading branch information
danielroe authored Sep 12, 2022
1 parent 13dc0b9 commit 9382b42
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/vite/src/plugins/ssr-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function ssrStylesPlugin (options: SSRStylePluginOptions): Plugin {

const relativeToSrcDir = (path: string) => relative(options.srcDir, path)

const warnCache = new Set<string>()

return {
name: 'ssr-styles',
generateBundle (outputOptions) {
Expand Down Expand Up @@ -104,6 +106,13 @@ export function ssrStylesPlugin (options: SSRStylePluginOptions): Plugin {

const resolved = await this.resolve(i.specifier, id)
if (!resolved) { continue }
if (!(await this.resolve(resolved.id + '?inline&used'))) {
if (!warnCache.has(resolved.id)) {
warnCache.add(resolved.id)
this.warn(`[nuxt] Cannot extract styles for \`${i.specifier}\`. Its styles will not be inlined when server-rendering.`)
}
continue
}

const ref = this.emitFile({
type: 'chunk',
Expand Down
24 changes: 22 additions & 2 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineNuxtConfig } from 'nuxt'
import { addComponent } from '@nuxt/kit'
import { addComponent, addVitePlugin, addWebpackPlugin } from '@nuxt/kit'
import { createUnplugin } from 'unplugin'

export default defineNuxtConfig({
app: {
Expand Down Expand Up @@ -32,7 +33,26 @@ export default defineNuxtConfig({
privateRuntimeConfig: {
privateConfig: 'secret_key'
},
modules: ['~/modules/example'],
modules: [
'~/modules/example',
function (_, nuxt) {
if (process.env.TEST_WITH_WEBPACK) { return }

nuxt.options.css.push('virtual.css')
nuxt.options.build.transpile.push('virtual.css')
const plugin = createUnplugin(() => ({
name: 'virtual',
resolveId (id) {
if (id === 'virtual.css') { return 'virtual.css' }
},
load (id) {
if (id === 'virtual.css') { return ':root { --virtual: red }' }
}
}))
addVitePlugin(plugin.vite())
addWebpackPlugin(plugin.webpack())
}
],
hooks: {
'modules:done' () {
addComponent({
Expand Down

0 comments on commit 9382b42

Please sign in to comment.