Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.5] Vite 3 support #77

Merged
merged 11 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ If you prefer to build your assets on deploy instead of committing them to your
You may start the SSR server using `node`:

```sh
node bootstrap/ssr/ssr.js
node bootstrap/ssr/ssr.mjs
```

### Optional: Expose Vite port when using Laravel Sail
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
"eslint": "^8.14.0",
"picocolors": "^1.0.0",
"typescript": "^4.6.4",
"vite": "^2.9.6",
"vite": "^3.0.0",
"vitest": "^0.12.4"
},
"peerDependencies": {
"vite": "^2.9.9"
"vite": "^3.0.0"
},
"engines": {
"node": ">=14"
Expand Down
78 changes: 8 additions & 70 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, SSROptions, normalizePath, PluginOption } from 'vite'
import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig, SSROptions, PluginOption } from 'vite'
import fullReload, { Config as FullReloadConfig } from 'vite-plugin-full-reload'

interface PluginConfig {
Expand Down Expand Up @@ -85,7 +85,6 @@ export default function laravel(config: string|string[]|PluginConfig): [LaravelP
function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlugin {
let viteDevServerUrl: DevServerUrl
let resolvedConfig: ResolvedConfig
const cssManifest: Manifest = {}

const defaultAliases: Record<string, string> = {
'@': '/resources/js',
Expand Down Expand Up @@ -159,9 +158,9 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
fs.writeFileSync(hotFile, viteDevServerUrl)

setTimeout(() => {
server.config.logger.info(colors.red(`\n Laravel ${laravelVersion()} `))
server.config.logger.info(`\n > APP_URL: ` + colors.cyan(appUrl))
})
server.config.logger.info(colors.red(`\n ${colors.bold('Laravel')} ${laravelVersion()} `))
jessarcher marked this conversation as resolved.
Show resolved Hide resolved
server.config.logger.info(` ${colors.green('➜')} ${colors.bold('APP_URL')}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`)
}, 100)
jessarcher marked this conversation as resolved.
Show resolved Hide resolved
}
})

Expand Down Expand Up @@ -199,50 +198,6 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug

next()
})
},

// The following two hooks are a workaround to help solve a "flash of unstyled content" with Blade.
// They add any CSS entry points into the manifest because Vite does not currently do this.
renderChunk(_, chunk) {
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`
const cssLangRE = new RegExp(cssLangs)

if (! chunk.isEntry || chunk.facadeModuleId === null || ! cssLangRE.test(chunk.facadeModuleId)) {
return null
}

const relativeChunkPath = normalizePath(path.relative(resolvedConfig.root, chunk.facadeModuleId))

cssManifest[relativeChunkPath] = {
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
/* @ts-ignore */
file: Array.from(chunk.viteMetadata.importedCss)[0] ?? chunk.fileName,
src: relativeChunkPath,
isEntry: true,
}

return null
},
writeBundle() {
const manifestConfig = resolveManifestConfig(resolvedConfig)

if (manifestConfig === false) {
return;
}

const manifestPath = path.resolve(resolvedConfig.root, resolvedConfig.build.outDir, manifestConfig)

if (! fs.existsSync(manifestPath)) {
// The manifest does not exist yet when first writing the legacy asset bundle.
return;
}

const manifest = JSON.parse(fs.readFileSync(manifestPath).toString())
const newManifest = {
...manifest,
...cssManifest,
}
fs.writeFileSync(manifestPath, JSON.stringify(newManifest, null, 2))
}
}
}
Expand Down Expand Up @@ -339,26 +294,6 @@ 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
? config.build.ssrManifest
: config.build.manifest;

if (manifestConfig === false) {
return false
}

if (manifestConfig === true) {
return config.build.ssr ? 'ssr-manifest.json' : 'manifest.json'
}

return manifestConfig
}

function resolveFullReloadConfig({ refresh: config }: Required<PluginConfig>): PluginOption[]{
if (typeof config === 'boolean') {
return [];
Expand Down Expand Up @@ -413,7 +348,10 @@ 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']
const pluginNoExternal = [
'laravel-vite-plugin/inertia-helpers',
'@inertiajs/server'
timacdonald marked this conversation as resolved.
Show resolved Hide resolved
]

if (userNoExternal === true) {
return true
Expand Down
10 changes: 5 additions & 5 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('laravel-vite-plugin', () => {

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

/* @ts-ignore */
const nothingExternalConfig = plugin.config({ ssr: { noExternal: true }, build: { ssr: true } }, { command: 'build', mode: 'production' })
Expand All @@ -229,12 +229,12 @@ describe('laravel-vite-plugin', () => {
/* @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'])
expect(arrayNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin/inertia-helpers'])

/* @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'])
expect(stringNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin/inertia-helpers'])
})

it('does not configure full reload when configuration it not an object', () => {
Expand Down Expand Up @@ -360,8 +360,8 @@ describe('inertia-helpers', () => {
expect(file.default).toBe('Dummy File')
})

it('pass globEager value to resolvePageComponent', async () => {
const file = await resolvePageComponent<{ default: string }>(path, import.meta.globEager('./__data__/*.ts'))
it('pass eagerly globed value to resolvePageComponent', async () => {
const file = await resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts', { eager: true }))
expect(file.default).toBe('Dummy File')
})
})