diff --git a/playground/App.vue b/playground/App.vue index 50f8706394e93b..3681bce493e952 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -3,6 +3,9 @@

__DEV__: {{ dev }}

+

+ __BASE__: {{ base }} +

process.env.NODE_ENV: {{ env }}

@@ -42,6 +45,7 @@ import TestTransform from './TestTransform.vue' export default { data: () => ({ dev: __DEV__, + base: __BASE__, env: process.env.NODE_ENV }), components: { diff --git a/src/node/build/index.ts b/src/node/build/index.ts index 67d277a9427c8e..14fca6ec452c1c 100644 --- a/src/node/build/index.ts +++ b/src/node/build/index.ts @@ -128,7 +128,8 @@ export async function build(options: BuildConfig = {}): Promise { createReplacePlugin( { 'process.env.NODE_ENV': '"production"', - __DEV__: 'false' + __DEV__: 'false', + __BASE__: JSON.stringify(publicBasePath) }, sourcemap ), diff --git a/src/node/server/serverPluginModuleRewrite.ts b/src/node/server/serverPluginModuleRewrite.ts index ce11a9c5b6594b..8c42d2e5321641 100644 --- a/src/node/server/serverPluginModuleRewrite.ts +++ b/src/node/server/serverPluginModuleRewrite.ts @@ -56,6 +56,7 @@ export const moduleRewritePlugin: ServerPlugin = ({ `\n\n` diff --git a/test/test.js b/test/test.js index 6fcd5c34b1b0dd..43674724d5d8d2 100644 --- a/test/test.js +++ b/test/test.js @@ -88,6 +88,7 @@ describe('vite', () => { test('env variables', async () => { expect(await getText('.dev')).toMatch(`__DEV__: ${!isBuild}`) + expect(await getText('.base')).toMatch(`__BASE__: /`) expect(await getText('.node_env')).toMatch( `process.env.NODE_ENV: ${isBuild ? 'production' : 'development'}` )