Skip to content

Commit 0d3b264

Browse files
committed
adjust build setup for better production minification
1 parent 69edc1a commit 0d3b264

34 files changed

+176
-111
lines changed

build/banner.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var version =
2+
process.env.VUE_VERSION ||
3+
require('../package.json').version
4+
5+
module.exports =
6+
'Vue.js v' + version + '\n' +
7+
'(c) ' + new Date().getFullYear() + ' Evan You\n' +
8+
'Released under the MIT License.'

build/grunt-tasks/build.js

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Build, update component.json, uglify, and report size.
2+
* Build and report size.
33
*/
44

55
module.exports = function (grunt) {
@@ -9,66 +9,34 @@ module.exports = function (grunt) {
99
var fs = require('fs')
1010
var zlib = require('zlib')
1111
var webpack = require('webpack')
12-
var uglifyjs = require('uglify-js')
12+
var devConfig = require('../webpack.build.dev.config')
13+
var prodConfig = require('../webpack.build.prod.config')
1314

14-
var banner =
15-
'/**\n' +
16-
' * Vue.js v' + grunt.config.get('version') + '\n' +
17-
' * (c) ' + new Date().getFullYear() + ' Evan You\n' +
18-
' * Released under the MIT License.\n' +
19-
' */\n'
20-
21-
// build
22-
webpack({
23-
entry: './src/vue',
24-
output: {
25-
path: './dist',
26-
filename: 'vue.js',
27-
library: 'Vue',
28-
libraryTarget: 'umd'
29-
},
30-
plugins: [
31-
new webpack.BannerPlugin(banner, { raw: true })
32-
]
33-
}, function (err, stats) {
15+
webpack(devConfig, function (err, stats) {
3416
if (err) return done(err)
35-
minify()
17+
report('dist/vue.js')
18+
webpack(prodConfig, function (err, stats) {
19+
if (err) return done(err)
20+
report('dist/vue.min.js')
21+
zip()
22+
})
3623
})
3724

38-
function minify () {
39-
var js = fs.readFileSync('dist/vue.js', 'utf-8')
40-
report('dist/vue.js', js)
41-
// uglify
42-
var result = uglifyjs.minify(js, {
43-
fromString: true,
44-
output: {
45-
comments: /License/
46-
},
47-
compress: {
48-
pure_funcs: [
49-
'require',
50-
'_.log',
51-
'_.warn',
52-
'_.assertAsset',
53-
'enableDebug'
54-
]
55-
}
56-
})
57-
// var min = grunt.config.get('banner') + result.code
58-
write('dist/vue.min.js', result.code)
59-
// report gzip size
60-
zlib.gzip(result.code, function (err, buf) {
61-
write('dist/vue.min.js.gz', buf)
62-
done(err)
25+
function zip () {
26+
fs.readFile('dist/vue.min.js', function (err, buf) {
27+
if (err) return done(err)
28+
zlib.gzip(buf, function (err, buf) {
29+
if (err) return done(err)
30+
report('dist/vue.min.js.gz', buf)
31+
done()
32+
})
6333
})
6434
}
6535

66-
function write (path, file) {
67-
fs.writeFileSync(path, file)
68-
report(path, file)
69-
}
70-
7136
function report (path, file) {
37+
if (!file) {
38+
file = fs.readFileSync(path)
39+
}
7240
console.log(
7341
blue(path + ': ') +
7442
(file.length / 1024).toFixed(2) + 'kb'

build/grunt-tasks/release.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function (grunt) {
99
*/
1010

1111
grunt.registerTask('version', function (version) {
12-
var manifests = ['package', 'bower', 'component']
12+
var manifests = ['package', 'component']
1313
manifests.forEach(function (file) {
1414
file = file + '.json'
1515
var json = grunt.file.read(file)
@@ -62,7 +62,8 @@ module.exports = function (grunt) {
6262
}).question('Releasing version ' + next + '. Continue? (Y/n)', function (answer) {
6363
if (!answer || answer.toLowerCase() === 'y') {
6464
console.log(blue('Releasing: ' + next))
65-
grunt.config.set('version', next)
65+
// set version in env
66+
process.env.VUE_VERSION = next
6667
grunt.task.run([
6768
'eslint',
6869
'cover',
File renamed without changes.

build/webpack.build.dev.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var webpack = require('webpack')
2+
var banner = require('./banner')
3+
4+
module.exports = {
5+
entry: './src/vue',
6+
output: {
7+
path: './dist',
8+
filename: 'vue.js',
9+
library: 'Vue',
10+
libraryTarget: 'umd'
11+
},
12+
plugins: [
13+
new webpack.BannerPlugin(banner),
14+
new webpack.DefinePlugin({
15+
'process.env': {
16+
NODE_ENV: '"development"'
17+
}
18+
})
19+
]
20+
}

build/webpack.build.prod.config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var webpack = require('webpack')
2+
var banner = require('./banner')
3+
4+
module.exports = {
5+
entry: './src/vue',
6+
output: {
7+
path: './dist',
8+
filename: 'vue.min.js',
9+
library: 'Vue',
10+
libraryTarget: 'umd'
11+
},
12+
plugins: [
13+
new webpack.BannerPlugin(banner),
14+
new webpack.DefinePlugin({
15+
'process.env': {
16+
NODE_ENV: '"production"'
17+
}
18+
}),
19+
new webpack.optimize.UglifyJsPlugin({
20+
compress: {
21+
warnings: false
22+
}
23+
})
24+
]
25+
}
File renamed without changes.
File renamed without changes.

gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var sauceConfig = require('./build/saucelabs-config')
1+
var sauceConfig = require('./build/saucelabs.config.js')
22

33
module.exports = function (grunt) {
44

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818
"homepage": "http://vuejs.org",
1919
"scripts": {
2020
"test": "grunt ci",
21-
"dev": "webpack --watch --config build/webpack-dev-config.js & webpack --watch --config build/webpack-test-config.js"
21+
"dev": "webpack --watch --config build/webpack.dev.config.js & webpack --watch --config build/webpack.test.config.js"
22+
},
23+
"dependencies": {
24+
"envify": "^3.4.0"
25+
},
26+
"browserify": {
27+
"transform": [
28+
"envify"
29+
]
2230
},
2331
"devDependencies": {
2432
"casperjs": "^1.1.0-beta3",

src/api/lifecycle.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ var compiler = require('../compiler')
1313

1414
exports.$mount = function (el) {
1515
if (this._isCompiled) {
16-
_.warn('$mount() should be called only once.')
16+
process.env.NODE_ENV !== 'production' && _.warn(
17+
'$mount() should be called only once.'
18+
)
1719
return
1820
}
1921
el = _.query(el)

src/batcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ exports.push = function (job) {
7171
has[id]++
7272
// detect possible infinite update loops
7373
if (has[id] > config._maxUpdateCount) {
74-
_.warn(
74+
process.env.NODE_ENV !== 'production' && _.warn(
7575
'You may have an infinite update loop for the ' +
7676
'watcher with expression: "' + job.expression + '".'
7777
)

src/compiler/compile-props.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ module.exports = function compileProps (el, propOptions) {
3030
// so we need to camelize the path here
3131
path = _.camelize(name.replace(dataAttrRE, ''))
3232
if (!identRE.test(path)) {
33-
_.warn(
33+
process.env.NODE_ENV !== 'production' && _.warn(
3434
'Invalid prop key: "' + name + '". Prop keys ' +
3535
'must be valid identifiers.'
3636
)
37+
continue
3738
}
3839
value = el.getAttribute(_.hyphenate(name))
3940
// create a prop descriptor
@@ -68,15 +69,17 @@ module.exports = function compileProps (el, propOptions) {
6869
if (settablePathRE.test(prop.parentPath)) {
6970
prop.mode = propBindingModes.TWO_WAY
7071
} else {
71-
_.warn(
72+
process.env.NODE_ENV !== 'production' && _.warn(
7273
'Cannot bind two-way prop with non-settable ' +
7374
'parent path: ' + prop.parentPath
7475
)
7576
}
7677
}
7778
}
7879
} else if (options && options.required) {
79-
_.warn('Missing required prop: ' + name)
80+
process.env.NODE_ENV !== 'production' && _.warn(
81+
'Missing required prop: ' + name
82+
)
8083
}
8184
props.push(prop)
8285
}
@@ -120,7 +123,7 @@ function makePropsLinkFn (props) {
120123
vm._bindDir('prop', el, prop, propDef)
121124
}
122125
} else {
123-
_.warn(
126+
process.env.NODE_ENV !== 'production' && _.warn(
124127
'Cannot bind dynamic prop on a root instance' +
125128
' with no parent: ' + prop.name + '="' +
126129
prop.raw + '"'

src/compiler/compile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,9 @@ function compileDirectives (elOrAttrs, options) {
522522
if (name.indexOf(config.prefix) === 0) {
523523
dirName = name.slice(config.prefix.length)
524524
dirDef = resolveAsset(options, 'directives', dirName)
525-
_.assertAsset(dirDef, 'directive', dirName)
525+
if (process.env.NODE_ENV !== 'production') {
526+
_.assertAsset(dirDef, 'directive', dirName)
527+
}
526528
if (dirDef) {
527529
dirs.push({
528530
name: dirName,

src/compiler/transclude.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ exports.transclude = function (el, options) {
5959
function transcludeTemplate (el, options) {
6060
var template = options.template
6161
var frag = templateParser.parse(template, true)
62-
if (!frag) {
63-
_.warn('Invalid template option: ' + template)
64-
} else {
62+
if (frag) {
6563
var replacer = frag.firstChild
6664
var tag = replacer.tagName && replacer.tagName.toLowerCase()
6765
if (options.replace) {
6866
/* istanbul ignore if */
6967
if (el === document.body) {
70-
_.warn(
68+
process.env.NODE_ENV !== 'production' && _.warn(
7169
'You are mounting an instance with a template to ' +
7270
'<body>. This will replace <body> entirely. You ' +
7371
'should probably use `replace: false` here.'
@@ -96,6 +94,10 @@ function transcludeTemplate (el, options) {
9694
el.appendChild(frag)
9795
return el
9896
}
97+
} else {
98+
process.env.NODE_ENV !== 'production' && _.warn(
99+
'Invalid template option: ' + template
100+
)
99101
}
100102
}
101103

src/directives/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
this.transMode = this._checkParam('transition-mode')
5151
}
5252
} else {
53-
_.warn(
53+
process.env.NODE_ENV !== 'production' && _.warn(
5454
'Do not create a component that only contains ' +
5555
'a single other component - they will be mounted to ' +
5656
'the same element and cause conflict. Wrap it with ' +

src/directives/if.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ module.exports = {
2525
true
2626
)
2727
} else {
28-
this.invalid = true
29-
_.warn(
28+
process.env.NODE_ENV !== 'production' && _.warn(
3029
'v-if="' + this.expression + '" cannot be ' +
3130
'used on an instance root element.'
3231
)
32+
this.invalid = true
3333
}
3434
},
3535

src/directives/model/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
// friendly warning...
3030
this.checkFilters()
3131
if (this.hasRead && !this.hasWrite) {
32-
_.warn(
32+
process.env.NODE_ENV !== 'production' && _.warn(
3333
'It seems you are using a read-only filter with ' +
3434
'v-model. You might want to use a two-way filter ' +
3535
'to ensure correct behavior.'
@@ -45,7 +45,9 @@ module.exports = {
4545
} else if (tag === 'TEXTAREA') {
4646
handler = handlers.text
4747
} else {
48-
_.warn('v-model does not support element type: ' + tag)
48+
process.env.NODE_ENV !== 'production' && _.warn(
49+
'v-model does not support element type: ' + tag
50+
)
4951
return
5052
}
5153
handler.bind.call(this)

src/directives/model/select.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ function initOptions (expression) {
8282
buildOptions(self.el, value)
8383
self.forceUpdate()
8484
} else {
85-
_.warn('Invalid options value for v-model: ' + value)
85+
process.env.NODE_ENV !== 'production' && _.warn(
86+
'Invalid options value for v-model: ' + value
87+
)
8688
}
8789
}
8890
this.optionWatcher = new Watcher(

src/directives/on.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121

2222
update: function (handler) {
2323
if (typeof handler !== 'function') {
24-
_.warn(
24+
process.env.NODE_ENV !== 'production' && _.warn(
2525
'Directive "v-on:' + this.expression + '" ' +
2626
'expects a function value.'
2727
)

src/directives/ref.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
bind: function () {
88
var vm = this.el.__vue__
99
if (!vm) {
10-
_.warn(
10+
process.env.NODE_ENV !== 'production' && _.warn(
1111
'v-ref should only be used on a component root element.'
1212
)
1313
return

0 commit comments

Comments
 (0)