Skip to content

Commit

Permalink
fix: set NODE_ENV for build
Browse files Browse the repository at this point in the history
fix #1445, fix #1452
  • Loading branch information
yyx990803 committed Jan 9, 2021
1 parent dce2456 commit d7ceabe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/playground/vue/Main.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<div class="comments"><!--hello--></div>
<h1>Vue SFCs</h1>
<pre>{{ time }}</pre>
<Hmr />
Expand Down
4 changes: 4 additions & 0 deletions packages/playground/vue/__tests__/vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ test('template/script latest syntax support', async () => {
expect(await page.textContent('.syntax')).toBe('baz')
})

test('should remove comments in prod', async () => {
expect(await page.innerHTML('.comments')).toBe(isBuild ? `` : `<!--hello-->`)
})

describe('pre-processors', () => {
test('pug', async () => {
expect(await page.textContent('p.pug')).toMatch(
Expand Down
18 changes: 15 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,15 @@ export async function resolveConfig(
defaultMode = 'development'
): Promise<ResolvedConfig> {
let config = inlineConfig
let mode = defaultMode
let mode = inlineConfig.mode || defaultMode

// some dependencies e.g. @vue/compiler-* relies on NODE_ENV for getting
// production-specific behavior, so set it here even though we haven't
// resolve the final mode yet
if (mode === 'production') {
process.env.NODE_ENV = 'production'
}

let { configFile } = config
if (configFile !== false) {
const loadResult = await loadConfigFromFile(
Expand Down Expand Up @@ -185,8 +193,12 @@ export async function resolveConfig(
// Note it is possible for user to have a custom mode, e.g. `staging` where
// production-like behavior is expected. This is indicated by NODE_ENV=production
// loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
const resolvedMode = process.env.VITE_USER_NODE_ENV || mode
const isProduction = resolvedMode === 'production'
const isProduction = (process.env.VITE_USER_NODE_ENV || mode) === 'production'
if (isProduction) {
// in case default mode was not production and is overwritten
process.env.NODE_ENV = 'production'
}

const resolvedBuildOptions = resolveBuildOptions(config.build)

// resolve optimizer cache directory
Expand Down
12 changes: 4 additions & 8 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ import { OutputBundle, OutputChunk } from 'rollup'
import { cleanUrl, isExternalUrl, isDataUrl, generateCodeFrame } from '../utils'
import { ResolvedConfig } from '../config'
import slash from 'slash'
import {
AttributeNode,
NodeTransform,
NodeTypes,
parse,
transform
} from '@vue/compiler-dom'
import MagicString from 'magic-string'
import { checkPublicFile, assetUrlRE, urlToBuiltUrl } from './asset'
import { isCSSRequest, chunkToEmittedCssFileMap } from './css'
import { polyfillId } from './dynamicImportPolyfill'
import { AttributeNode, NodeTransform, NodeTypes } from '@vue/compiler-dom'

const htmlProxyRE = /\?html-proxy&index=(\d+)\.js$/
export const isHTMLProxy = (id: string) => htmlProxyRE.test(id)
Expand Down Expand Up @@ -95,11 +89,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
return e
}

// lazy load compiler-dom
const { parse, transform } = await import('@vue/compiler-dom')
// @vue/compiler-core doesn't like lowercase doctypes
html = html.replace(/<!doctype\s/i, '<!DOCTYPE ')
let ast
try {
ast = parse(html)
ast = parse(html, { comments: true })
} catch (e) {
this.error(formatError(e))
}
Expand Down

0 comments on commit d7ceabe

Please sign in to comment.