Skip to content

Commit

Permalink
feat: expose process.env.BASE_URL
Browse files Browse the repository at this point in the history
Keeps it consistent with vue-cli.

BREAKING CHANGE: `__BASE__` special global has been removed. Use
`process.env.BASE_URL` instead.
  • Loading branch information
yyx990803 committed May 26, 2020
1 parent 301d7a3 commit 9503762
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions playground/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<code>__DEV__: {{ dev }}</code>
</p>
<p class="base">
<code>__BASE__: {{ base }}</code>
<code>process.env.BASE_URL: {{ base }}</code>
</p>
<TestEnv />
<h2>Async Component</h2>
Expand Down Expand Up @@ -46,7 +46,7 @@ import TestRewriteOptimized from "./rewrite-optimized/TestRewriteOptimized.vue";
export default {
data: () => ({
dev: __DEV__,
base: __BASE__
base: process.env.BASE_URL
}),
components: {
TestEnv,
Expand Down
4 changes: 2 additions & 2 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export async function build(options: BuildConfig): Promise<BuildResult> {
(id) => /\.(j|t)sx?$/.test(id),
{
...envReplacements,
'process.env.BASE_URL': JSON.stringify(publicBasePath),
'process.env.': `({}).`,
'import.meta.hot': `false`
},
Expand All @@ -246,8 +247,7 @@ export async function build(options: BuildConfig): Promise<BuildResult> {
id.startsWith('/vite') ||
(!id.includes('node_modules') && /\.(j|t)sx?$/.test(id)),
{
__DEV__: `false`,
__BASE__: JSON.stringify(publicBasePath)
__DEV__: `false`
},
sourcemap
),
Expand Down
4 changes: 2 additions & 2 deletions src/node/server/serverPluginHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export const htmlRewritePlugin: ServerPlugin = ({
`\n<script type="module">\n` +
`import "${hmrClientPublicPath}"\n` +
`window.__DEV__ = true\n` +
`window.__BASE__ = '/'\n` +
`window.process = { env: ${JSON.stringify({
...env,
NODE_ENV: mode
NODE_ENV: mode,
BASE_URL: '/'
})}}\n` +
`</script>\n`

Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('vite', () => {

test('env variables', async () => {
expect(await getText('.dev')).toMatch(`__DEV__: ${!isBuild}`)
expect(await getText('.base')).toMatch(`__BASE__: /`)
expect(await getText('.base')).toMatch(`process.env.BASE_URL: /`)
expect(await getText('.node-env')).toMatch(
`process.env.NODE_ENV: ${isBuild ? 'production' : 'development'}`
)
Expand Down

0 comments on commit 9503762

Please sign in to comment.