diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8eddf00..a1a358e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -6,6 +6,9 @@ on: schedule: - cron: '0 0 * * *' +env: + LARAVEL_BYPASS_ENV_CHECK: 1 + jobs: tests: runs-on: ubuntu-latest diff --git a/src/index.ts b/src/index.ts index b890d99..1200794 100644 --- a/src/index.ts +++ b/src/index.ts @@ -98,6 +98,8 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug const env = loadEnv(mode, userConfig.envDir || process.cwd(), '') const assetUrl = env.ASSET_URL ?? '' + ensureCommandShouldRunInEnvironment(command, env) + return { base: command === 'build' ? resolveBase(pluginConfig, assetUrl) : '', publicDir: false, @@ -197,6 +199,31 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug } } +/** + * Validate the command can run in the given environment. + */ +function ensureCommandShouldRunInEnvironment(command: 'build'|'serve', env: Record): void { + if (command === 'build' || env.LARAVEL_BYPASS_ENV_CHECK === '1') { + return; + } + + if (typeof env.LARAVEL_VAPOR !== 'undefined') { + throw Error('You should not run the Vite HMR server on Vapor. You should build your assets for production instead.'); + } + + if (typeof env.LARAVEL_FORGE !== 'undefined') { + throw Error('You should not run the Vite HMR server in your Forge deployment script. You should build your assets for production instead.'); + } + + if (typeof env.LARAVEL_ENVOYER !== 'undefined') { + throw Error('You should not run the Vite HMR server in your Envoyer hook. You should build your assets for production instead.'); + } + + if (typeof env.CI !== 'undefined') { + throw Error('You should not run the Vite HMR server in CI environments. You should build your assets for production instead.'); + } +} + /** * The version of Laravel being run. */