Skip to content

Commit 81ee104

Browse files
authored
Merge pull request #18 from laravel/ssr-no-external
Prevent SSR build from externalizing Inertia helpers
2 parents 9b74d44 + cd235cb commit 81ee104

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/index.ts

Lines changed: 33 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) {
@@ -277,6 +280,9 @@ function resolveOutDir(config: Required<PluginConfig>, ssr: boolean): string|und
277280
return path.join(config.publicDirectory, config.buildDirectory)
278281
}
279282

283+
/**
284+
* Resolve the Vite manifest config from the configuration.
285+
*/
280286
function resolveManifestConfig(config: ResolvedConfig): string|false
281287
{
282288
const manifestConfig = config.build.ssr
@@ -293,3 +299,28 @@ function resolveManifestConfig(config: ResolvedConfig): string|false
293299

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

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)