Skip to content

Commit

Permalink
Merge pull request #18 from laravel/ssr-no-external
Browse files Browse the repository at this point in the history
Prevent SSR build from externalizing Inertia helpers
  • Loading branch information
timacdonald authored Jun 16, 2022
2 parents 9b74d44 + cd235cb commit 81ee104
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import { AddressInfo } from 'net'
import path from 'path'
import colors from 'picocolors'
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig } from 'vite'
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig, SSROptions } from 'vite'

interface PluginConfig {
/**
Expand Down Expand Up @@ -99,7 +99,10 @@ export default function laravel(config: string|string[]|PluginConfig): LaravelPl
...defaultAliases,
...userConfig.resolve?.alias,
}
}
},
ssr: {
noExternal: noExternalInertiaHelpers(userConfig),
},
}
},
configResolved(config) {
Expand Down Expand Up @@ -277,6 +280,9 @@ function resolveOutDir(config: Required<PluginConfig>, ssr: boolean): string|und
return path.join(config.publicDirectory, config.buildDirectory)
}

/**
* Resolve the Vite manifest config from the configuration.
*/
function resolveManifestConfig(config: ResolvedConfig): string|false
{
const manifestConfig = config.build.ssr
Expand All @@ -293,3 +299,28 @@ function resolveManifestConfig(config: ResolvedConfig): string|false

return manifestConfig
}

/**
* Add the Interia helpers to the list of SSR dependencies that aren't externalized.
*
* @see https://vitejs.dev/guide/ssr.html#ssr-externals
*/
function noExternalInertiaHelpers(config: UserConfig): true|Array<string|RegExp> {
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
/* @ts-ignore */
const userNoExternal = (config.ssr as SSROptions|undefined)?.noExternal
const pluginNoExternal = ['laravel-vite-plugin']

if (userNoExternal === true) {
return true
}

if (typeof userNoExternal === 'undefined') {
return pluginNoExternal
}

return [
...(Array.isArray(userNoExternal) ? userNoExternal : [userNoExternal]),
...pluginNoExternal,
]
}
24 changes: 24 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,28 @@ describe('laravel-vite-plugin', () => {
delete process.env.LARAVEL_SAIL
delete process.env.VITE_PORT
})

it('prevents the Inertia helpers from being externalized', () => {
/* eslint-disable @typescript-eslint/ban-ts-comment */
const plugin = laravel('resources/js/app.js')

const noSsrConfig = plugin.config({ build: { ssr: true } }, { command: 'build', mode: 'production' })
/* @ts-ignore */
expect(noSsrConfig.ssr.noExternal).toEqual(['laravel-vite-plugin'])

/* @ts-ignore */
const nothingExternalConfig = plugin.config({ ssr: { noExternal: true }, build: { ssr: true } }, { command: 'build', mode: 'production' })
/* @ts-ignore */
expect(nothingExternalConfig.ssr.noExternal).toBe(true)

/* @ts-ignore */
const arrayNoExternalConfig = plugin.config({ ssr: { noExternal: ['foo'] }, build: { ssr: true } }, { command: 'build', mode: 'production' })
/* @ts-ignore */
expect(arrayNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])

/* @ts-ignore */
const stringNoExternalConfig = plugin.config({ ssr: { noExternal: 'foo' }, build: { ssr: true } }, { command: 'build', mode: 'production' })
/* @ts-ignore */
expect(stringNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])
})
})

0 comments on commit 81ee104

Please sign in to comment.