Skip to content

Commit

Permalink
feat: expose base path as __BASE__
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 10, 2020
1 parent 02491a4 commit 97ae7c3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions playground/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<p class="dev">
<code>__DEV__: {{ dev }}</code>
</p>
<p class="base">
<code>__BASE__: {{ base }}</code>
</p>
<p class="node_env">
<code>process.env.NODE_ENV: {{ env }}</code>
</p>
Expand Down Expand Up @@ -42,6 +45,7 @@ import TestTransform from './TestTransform.vue'
export default {
data: () => ({
dev: __DEV__,
base: __BASE__,
env: process.env.NODE_ENV
}),
components: {
Expand Down
3 changes: 2 additions & 1 deletion src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
createReplacePlugin(
{
'process.env.NODE_ENV': '"production"',
__DEV__: 'false'
__DEV__: 'false',
__BASE__: JSON.stringify(publicBasePath)
},
sourcemap
),
Expand Down
1 change: 1 addition & 0 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const moduleRewritePlugin: ServerPlugin = ({
`\n<script type="module">` +
`import "${hmrClientPublicPath}"\n` +
`window.__DEV__ = true\n` +
`window.__BASE__ = '/'\n` +
`window.process = { env: { NODE_ENV: 'development' }}\n` +
`</script>\n`

Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}`
)
Expand Down

0 comments on commit 97ae7c3

Please sign in to comment.