Skip to content

Commit e431a27

Browse files
committed
Prevent SSR build from externalizing Inertia helpers
1 parent 9b74d44 commit e431a27

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import { AddressInfo } from 'net'
33
import path from 'path'
44
import colors from 'picocolors'
5-
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig } from 'vite'
5+
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig, SSROptions } from 'vite'
66

77
interface PluginConfig {
88
/**
@@ -99,7 +99,10 @@ export default function laravel(config: string|string[]|PluginConfig): LaravelPl
9999
...defaultAliases,
100100
...userConfig.resolve?.alias,
101101
}
102-
}
102+
},
103+
ssr: {
104+
noExternal: noExternalInertiaHelpers(userConfig),
105+
},
103106
}
104107
},
105108
configResolved(config) {
@@ -293,3 +296,28 @@ function resolveManifestConfig(config: ResolvedConfig): string|false
293296

294297
return manifestConfig
295298
}
299+
300+
/**
301+
* Add the Interia helpers to the list of SSR dependencies that aren't externalized.
302+
*
303+
* @see https://vitejs.dev/guide/ssr.html#ssr-externals
304+
*/
305+
function noExternalInertiaHelpers(config: UserConfig): true|Array<string|RegExp> {
306+
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
307+
/* @ts-ignore */
308+
const userNoExternal = (config.ssr as SSROptions|undefined)?.noExternal
309+
const pluginNoExternal = ['laravel-vite-plugin']
310+
311+
if (userNoExternal === true) {
312+
return true
313+
}
314+
315+
if (typeof userNoExternal === 'undefined') {
316+
return pluginNoExternal
317+
}
318+
319+
return [
320+
...(Array.isArray(userNoExternal) ? userNoExternal : [userNoExternal]),
321+
...pluginNoExternal,
322+
]
323+
}

tests/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,28 @@ describe('laravel-vite-plugin', () => {
218218
delete process.env.LARAVEL_SAIL
219219
delete process.env.VITE_PORT
220220
})
221+
222+
it('prevents the Inertia helpers from being externalized', () => {
223+
/* eslint-disable @typescript-eslint/ban-ts-comment */
224+
const plugin = laravel('resources/js/app.js')
225+
226+
const noSsrConfig = plugin.config({ build: { ssr: true } }, { command: 'build', mode: 'production' })
227+
/* @ts-ignore */
228+
expect(noSsrConfig.ssr.noExternal).toEqual(['laravel-vite-plugin'])
229+
230+
/* @ts-ignore */
231+
const nothingExternalConfig = plugin.config({ ssr: { noExternal: true }, build: { ssr: true } }, { command: 'build', mode: 'production' })
232+
/* @ts-ignore */
233+
expect(nothingExternalConfig.ssr.noExternal).toBe(true)
234+
235+
/* @ts-ignore */
236+
const arrayNoExternalConfig = plugin.config({ ssr: { noExternal: ['foo'] }, build: { ssr: true } }, { command: 'build', mode: 'production' })
237+
/* @ts-ignore */
238+
expect(arrayNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])
239+
240+
/* @ts-ignore */
241+
const stringNoExternalConfig = plugin.config({ ssr: { noExternal: 'foo' }, build: { ssr: true } }, { command: 'build', mode: 'production' })
242+
/* @ts-ignore */
243+
expect(stringNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])
244+
})
221245
})

0 commit comments

Comments
 (0)