From 7ec7c0ac86a76f7f5d03abbafa33056fa45dd36b Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 15 Sep 2022 23:23:58 +1000 Subject: [PATCH] [0.6.x] Fail running HMR in known environments (#128) * fail in known environments * Allow bypassing env validation * update the Forge env * bypass env check for tests * naming * rename vapor env * fail on envoyer --- .github/workflows/tests.yml | 3 +++ src/index.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) 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 30babbd..586c7a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -105,6 +105,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, @@ -202,6 +204,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. */