Skip to content

Commit 797e72e

Browse files
committed
build: use __DEV__ flag
1 parent 72aed6a commit 797e72e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+145
-206
lines changed

scripts/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const banner =
1616
' */'
1717

1818
const aliases = require('./alias')
19-
const resolve = (p) => {
19+
const resolve = p => {
2020
const base = p.split('/')[0]
2121
if (aliases[base]) {
2222
return path.resolve(aliases[base], p.slice(base.length + 1))
@@ -241,12 +241,13 @@ function genConfig(name) {
241241
__VERSION__: version
242242
}
243243
// feature flags
244-
Object.keys(featureFlags).forEach((key) => {
244+
Object.keys(featureFlags).forEach(key => {
245245
vars[`process.env.${key}`] = featureFlags[key]
246246
})
247247
// build-specific env
248248
if (opts.env) {
249249
vars['process.env.NODE_ENV'] = JSON.stringify(opts.env)
250+
vars.__DEV__ = opts.env !== 'production'
250251
}
251252

252253
vars.preventAssignment = true

src/compiler/codegen/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function genOnce(el: ASTElement, state: CodegenState): string {
137137
parent = parent.parent
138138
}
139139
if (!key) {
140-
process.env.NODE_ENV !== 'production' &&
140+
__DEV__ &&
141141
state.warn(
142142
`v-once can only be used inside v-for that is keyed. `,
143143
el.rawAttrsMap['v-once']
@@ -201,7 +201,7 @@ export function genFor(
201201
const iterator2 = el.iterator2 ? `,${el.iterator2}` : ''
202202

203203
if (
204-
process.env.NODE_ENV !== 'production' &&
204+
__DEV__ &&
205205
state.maybeComponent(el) &&
206206
el.tag !== 'slot' &&
207207
el.tag !== 'template' &&
@@ -345,10 +345,7 @@ function genInlineTemplate(
345345
state: CodegenState
346346
): string | undefined {
347347
const ast = el.children[0]
348-
if (
349-
process.env.NODE_ENV !== 'production' &&
350-
(el.children.length !== 1 || ast.type !== 1)
351-
) {
348+
if (__DEV__ && (el.children.length !== 1 || ast.type !== 1)) {
352349
state.warn(
353350
'Inline-template components must have exactly one child element.',
354351
{ start: el.start }

src/compiler/create-compiler.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ export function createCompilerCreator(baseCompile: Function): Function {
2121
}
2222

2323
if (options) {
24-
if (
25-
process.env.NODE_ENV !== 'production' &&
26-
options.outputSourceRange
27-
) {
24+
if (__DEV__ && options.outputSourceRange) {
2825
// $flow-disable-line
2926
const leadingSpaceLength = template.match(/^\s*/)![0].length
3027

@@ -69,7 +66,7 @@ export function createCompilerCreator(baseCompile: Function): Function {
6966
finalOptions.warn = warn
7067

7168
const compiled = baseCompile(template.trim(), finalOptions)
72-
if (process.env.NODE_ENV !== 'production') {
69+
if (__DEV__) {
7370
detectErrors(compiled.ast, warn)
7471
}
7572
compiled.errors = errors

src/compiler/directives/on.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { warn } from 'core/util/index'
22

33
export default function on(el: ASTElement, dir: ASTDirective) {
4-
if (process.env.NODE_ENV !== 'production' && dir.modifiers) {
4+
if (__DEV__ && dir.modifiers) {
55
warn(`v-on without argument does not support modifiers.`)
66
}
77
el.wrapListeners = (code: string) => `_g(${code},${dir.value})`

src/compiler/helpers.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,7 @@ export function addHandler(
101101
modifiers = modifiers || emptyObject
102102
// warn prevent and passive modifier
103103
/* istanbul ignore if */
104-
if (
105-
process.env.NODE_ENV !== 'production' &&
106-
warn &&
107-
modifiers.prevent &&
108-
modifiers.passive
109-
) {
104+
if (__DEV__ && warn && modifiers.prevent && modifiers.passive) {
110105
warn(
111106
"passive and prevent can't be used together. " +
112107
"Passive handler can't prevent default event.",

src/compiler/parser/html-parser.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ export function parseHTML(html, options) {
181181

182182
if (html === last) {
183183
options.chars && options.chars(html)
184-
if (
185-
process.env.NODE_ENV !== 'production' &&
186-
!stack.length &&
187-
options.warn
188-
) {
184+
if (__DEV__ && !stack.length && options.warn) {
189185
options.warn(`Mal-formatted tag at end of template: "${html}"`, {
190186
start: index + html.length
191187
})
@@ -258,7 +254,7 @@ export function parseHTML(html, options) {
258254
name: args[1],
259255
value: decodeAttr(value, shouldDecodeNewlines)
260256
}
261-
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
257+
if (__DEV__ && options.outputSourceRange) {
262258
attrs[i].start = args.start + args[0].match(/^\s*/).length
263259
attrs[i].end = args.end
264260
}
@@ -301,11 +297,7 @@ export function parseHTML(html, options) {
301297
if (pos >= 0) {
302298
// Close all the open elements, up the stack
303299
for (let i = stack.length - 1; i >= pos; i--) {
304-
if (
305-
process.env.NODE_ENV !== 'production' &&
306-
(i > pos || !tagName) &&
307-
options.warn
308-
) {
300+
if (__DEV__ && (i > pos || !tagName) && options.warn) {
309301
options.warn(`tag <${stack[i].tag}> has no matching end tag.`, {
310302
start: stack[i].start,
311303
end: stack[i].end

src/compiler/parser/index.ts

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
119119
if (!stack.length && element !== root) {
120120
// allow root elements with v-if, v-else-if and v-else
121121
if (root.if && (element.elseif || element.else)) {
122-
if (process.env.NODE_ENV !== 'production') {
122+
if (__DEV__) {
123123
checkRootConstraints(element)
124124
}
125125
addIfCondition(root, {
126126
exp: element.elseif,
127127
block: element
128128
})
129-
} else if (process.env.NODE_ENV !== 'production') {
129+
} else if (__DEV__) {
130130
warnOnce(
131131
`Component template should contain exactly one root element. ` +
132132
`If you are using v-if on multiple elements, ` +
@@ -229,7 +229,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
229229
element.ns = ns
230230
}
231231

232-
if (process.env.NODE_ENV !== 'production') {
232+
if (__DEV__) {
233233
if (options.outputSourceRange) {
234234
element.start = start
235235
element.end = end
@@ -254,7 +254,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
254254

255255
if (isForbiddenTag(element) && !isServerRendering()) {
256256
element.forbidden = true
257-
process.env.NODE_ENV !== 'production' &&
257+
__DEV__ &&
258258
warn(
259259
'Templates should only be responsible for mapping the state to the ' +
260260
'UI. Avoid placing tags with side-effects in your templates, such as ' +
@@ -289,7 +289,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
289289

290290
if (!root) {
291291
root = element
292-
if (process.env.NODE_ENV !== 'production') {
292+
if (__DEV__) {
293293
checkRootConstraints(root)
294294
}
295295
}
@@ -307,15 +307,15 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
307307
// pop stack
308308
stack.length -= 1
309309
currentParent = stack[stack.length - 1]
310-
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
310+
if (__DEV__ && options.outputSourceRange) {
311311
element.end = end
312312
}
313313
closeElement(element)
314314
},
315315

316316
chars(text: string, start: number, end: number) {
317317
if (!currentParent) {
318-
if (process.env.NODE_ENV !== 'production') {
318+
if (__DEV__) {
319319
if (text === template) {
320320
warnOnce(
321321
'Component template requires a root element, rather than just text.',
@@ -382,10 +382,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
382382
}
383383
}
384384
if (child) {
385-
if (
386-
process.env.NODE_ENV !== 'production' &&
387-
options.outputSourceRange
388-
) {
385+
if (__DEV__ && options.outputSourceRange) {
389386
child.start = start
390387
child.end = end
391388
}
@@ -402,10 +399,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
402399
text,
403400
isComment: true
404401
}
405-
if (
406-
process.env.NODE_ENV !== 'production' &&
407-
options.outputSourceRange
408-
) {
402+
if (__DEV__ && options.outputSourceRange) {
409403
child.start = start
410404
child.end = end
411405
}
@@ -465,7 +459,7 @@ export function processElement(element: ASTElement, options: CompilerOptions) {
465459
function processKey(el) {
466460
const exp = getBindingAttr(el, 'key')
467461
if (exp) {
468-
if (process.env.NODE_ENV !== 'production') {
462+
if (__DEV__) {
469463
if (el.tag === 'template') {
470464
warn(
471465
`<template> cannot be keyed. Place the key on real elements instead.`,
@@ -508,7 +502,7 @@ export function processFor(el: ASTElement) {
508502
const res = parseFor(exp)
509503
if (res) {
510504
extend(el, res)
511-
} else if (process.env.NODE_ENV !== 'production') {
505+
} else if (__DEV__) {
512506
warn(`Invalid v-for expression: ${exp}`, el.rawAttrsMap['v-for'])
513507
}
514508
}
@@ -566,7 +560,7 @@ function processIfConditions(el, parent) {
566560
exp: el.elseif,
567561
block: el
568562
})
569-
} else if (process.env.NODE_ENV !== 'production') {
563+
} else if (__DEV__) {
570564
warn(
571565
`v-${el.elseif ? 'else-if="' + el.elseif + '"' : 'else'} ` +
572566
`used on element <${el.tag}> without corresponding v-if.`,
@@ -581,7 +575,7 @@ function findPrevElement(children: Array<any>): ASTElement | void {
581575
if (children[i].type === 1) {
582576
return children[i]
583577
} else {
584-
if (process.env.NODE_ENV !== 'production' && children[i].text !== ' ') {
578+
if (__DEV__ && children[i].text !== ' ') {
585579
warn(
586580
`text "${children[i].text.trim()}" between v-if and v-else(-if) ` +
587581
`will be ignored.`,
@@ -614,7 +608,7 @@ function processSlotContent(el) {
614608
if (el.tag === 'template') {
615609
slotScope = getAndRemoveAttr(el, 'scope')
616610
/* istanbul ignore if */
617-
if (process.env.NODE_ENV !== 'production' && slotScope) {
611+
if (__DEV__ && slotScope) {
618612
warn(
619613
`the "scope" attribute for scoped slots have been deprecated and ` +
620614
`replaced by "slot-scope" since 2.5. The new "slot-scope" attribute ` +
@@ -627,7 +621,7 @@ function processSlotContent(el) {
627621
el.slotScope = slotScope || getAndRemoveAttr(el, 'slot-scope')
628622
} else if ((slotScope = getAndRemoveAttr(el, 'slot-scope'))) {
629623
/* istanbul ignore if */
630-
if (process.env.NODE_ENV !== 'production' && el.attrsMap['v-for']) {
624+
if (__DEV__ && el.attrsMap['v-for']) {
631625
warn(
632626
`Ambiguous combined usage of slot-scope and v-for on <${el.tag}> ` +
633627
`(v-for takes higher priority). Use a wrapper <template> for the ` +
@@ -659,7 +653,7 @@ function processSlotContent(el) {
659653
// v-slot on <template>
660654
const slotBinding = getAndRemoveAttrByRegex(el, slotRE)
661655
if (slotBinding) {
662-
if (process.env.NODE_ENV !== 'production') {
656+
if (__DEV__) {
663657
if (el.slotTarget || el.slotScope) {
664658
warn(`Unexpected mixed usage of different slot syntaxes.`, el)
665659
}
@@ -680,7 +674,7 @@ function processSlotContent(el) {
680674
// v-slot on component, denotes default slot
681675
const slotBinding = getAndRemoveAttrByRegex(el, slotRE)
682676
if (slotBinding) {
683-
if (process.env.NODE_ENV !== 'production') {
677+
if (__DEV__) {
684678
if (!maybeComponent(el)) {
685679
warn(
686680
`v-slot can only be used on components or <template>.`,
@@ -729,7 +723,7 @@ function getSlotName(binding) {
729723
if (!name) {
730724
if (binding.name[0] !== '#') {
731725
name = 'default'
732-
} else if (process.env.NODE_ENV !== 'production') {
726+
} else if (__DEV__) {
733727
warn(`v-slot shorthand syntax requires a slot name.`, binding)
734728
}
735729
}
@@ -744,7 +738,7 @@ function getSlotName(binding) {
744738
function processSlotOutlet(el) {
745739
if (el.tag === 'slot') {
746740
el.slotName = getBindingAttr(el, 'name')
747-
if (process.env.NODE_ENV !== 'production' && el.key) {
741+
if (__DEV__ && el.key) {
748742
warn(
749743
`\`key\` does not work on <slot> because slots are abstract outlets ` +
750744
`and can possibly expand into multiple elements. ` +
@@ -791,10 +785,7 @@ function processAttrs(el) {
791785
if (isDynamic) {
792786
name = name.slice(1, -1)
793787
}
794-
if (
795-
process.env.NODE_ENV !== 'production' &&
796-
value.trim().length === 0
797-
) {
788+
if (__DEV__ && value.trim().length === 0) {
798789
warn(
799790
`The value for a v-bind expression cannot be empty. Found in "v-bind:${name}"`
800791
)
@@ -885,13 +876,13 @@ function processAttrs(el) {
885876
modifiers,
886877
list[i]
887878
)
888-
if (process.env.NODE_ENV !== 'production' && name === 'model') {
879+
if (__DEV__ && name === 'model') {
889880
checkForAliasModel(el, value)
890881
}
891882
}
892883
} else {
893884
// literal attribute
894-
if (process.env.NODE_ENV !== 'production') {
885+
if (__DEV__) {
895886
const res = parseText(value, delimiters)
896887
if (res) {
897888
warn(
@@ -942,12 +933,7 @@ function parseModifiers(name: string): Object | void {
942933
function makeAttrsMap(attrs: Array<Record<string, any>>): Record<string, any> {
943934
const map = {}
944935
for (let i = 0, l = attrs.length; i < l; i++) {
945-
if (
946-
process.env.NODE_ENV !== 'production' &&
947-
map[attrs[i].name] &&
948-
!isIE &&
949-
!isEdge
950-
) {
936+
if (__DEV__ && map[attrs[i].name] && !isIE && !isEdge) {
951937
warn('duplicate attribute: ' + attrs[i].name, attrs[i])
952938
}
953939
map[attrs[i].name] = attrs[i].value

src/compiler/to-function.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function createCompileToFunctionFn(compile: Function): Function {
3030
delete options.warn
3131

3232
/* istanbul ignore if */
33-
if (process.env.NODE_ENV !== 'production') {
33+
if (__DEV__) {
3434
// detect possible CSP restriction
3535
try {
3636
new Function('return 1')
@@ -59,7 +59,7 @@ export function createCompileToFunctionFn(compile: Function): Function {
5959
const compiled = compile(template, options)
6060

6161
// check compilation errors/tips
62-
if (process.env.NODE_ENV !== 'production') {
62+
if (__DEV__) {
6363
if (compiled.errors && compiled.errors.length) {
6464
if (options.outputSourceRange) {
6565
compiled.errors.forEach(e => {
@@ -99,7 +99,7 @@ export function createCompileToFunctionFn(compile: Function): Function {
9999
// this should only happen if there is a bug in the compiler itself.
100100
// mostly for codegen development use
101101
/* istanbul ignore if */
102-
if (process.env.NODE_ENV !== 'production') {
102+
if (__DEV__) {
103103
if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) {
104104
warn(
105105
`Failed to generate render function:\n\n` +

src/core/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ export default {
4545
/**
4646
* Show production mode tip message on boot?
4747
*/
48-
productionTip: process.env.NODE_ENV !== 'production',
48+
productionTip: __DEV__,
4949

5050
/**
5151
* Whether to enable devtools
5252
*/
53-
devtools: process.env.NODE_ENV !== 'production',
53+
devtools: __DEV__,
5454

5555
/**
5656
* Whether to record perf

0 commit comments

Comments
 (0)