瀏覽代碼

more util refactor, add build banner

Evan You 11 年之前
父節點
當前提交
69e915401e
共有 5 個文件被更改,包括 184 次插入155 次删除
  1. 31 1
      gruntfile.js
  2. 1 0
      package.json
  3. 40 0
      src/util/debug.js
  4. 6 154
      src/util/index.js
  5. 106 0
      src/util/option.js

+ 31 - 1
gruntfile.js

@@ -1,5 +1,13 @@
 module.exports = function (grunt) {
 
+  var version = grunt.file.readJSON('package.json').version
+  var banner =
+    '/**\n' +
+    ' * Vue.js v' + version + '\n' +
+    ' * (c) ' + new Date().getFullYear() + ' Evan You\n' +
+    ' * Released under the MIT License.\n' +
+    ' */\n'
+
   grunt.initConfig({
 
     jshint: {
@@ -52,6 +60,9 @@ module.exports = function (grunt) {
         options: {
           bundleOptions: {
             standalone: 'Vue'
+          },
+          postBundleCB: function (err, src, next) {
+            next(err, banner + src)
           }
         }
       },
@@ -70,12 +81,31 @@ module.exports = function (grunt) {
         src: ['benchmarks/*.js', '!benchmarks/browser.js'],
         dest: 'benchmarks/browser.js'
       }
+    },
+
+    uglify: {
+      build: {
+        options: {
+          banner: banner,
+          compress: {
+            pure_funcs: [
+                '_.log',
+                '_.warn',
+                'enableDebug'
+            ]
+          }
+        },
+        files: {
+          'dist/vue.min.js': ['dist/vue.js']
+        }
+      }
     }
 
   })
   
   // load npm tasks
   grunt.loadNpmTasks('grunt-contrib-jshint')
+  grunt.loadNpmTasks('grunt-contrib-uglify')
   grunt.loadNpmTasks('grunt-karma')
   grunt.loadNpmTasks('grunt-browserify')
 
@@ -87,6 +117,6 @@ module.exports = function (grunt) {
   grunt.registerTask('unit', ['karma:browsers'])
   grunt.registerTask('phantom', ['karma:phantom'])
   grunt.registerTask('watch', ['browserify:watch'])
-  grunt.registerTask('build', ['browserify:build'])
+  grunt.registerTask('build', ['browserify:build', 'uglify:build'])
 
 }

+ 1 - 0
package.json

@@ -25,6 +25,7 @@
     "grunt": "^0.4.5",
     "grunt-browserify": "^2.1.3",
     "grunt-contrib-jshint": "^0.10.0",
+    "grunt-contrib-uglify": "^0.5.1",
     "grunt-karma": "^0.8.3",
     "jshint-stylish": "^0.3.0",
     "karma": "^0.12.16",

+ 40 - 0
src/util/debug.js

@@ -0,0 +1,40 @@
+var config = require('../config')
+
+/**
+ * Enable debug utilities. The enableDebug() function and all
+ * _.log() & _.warn() calls will be dropped in the minified
+ * production build.
+ */
+
+enableDebug()
+
+function enableDebug () {
+  var hasConsole = typeof console !== 'undefined'
+  
+  /**
+   * Log a message.
+   *
+   * @param {String} msg
+   */
+
+  exports.log = function (msg) {
+    if (hasConsole && config.debug) {
+      console.log(msg)
+    }
+  }
+
+  /**
+   * We've got a problem here.
+   *
+   * @param {String} msg
+   */
+
+  exports.warn = function (msg) {
+    if (hasConsole && !config.silent) {
+      console.warn(msg)
+      if (config.debug && console.trace) {
+        console.trace(msg)
+      }
+    }
+  }
+}

+ 6 - 154
src/util/index.js

@@ -1,156 +1,8 @@
-var _ = exports
-var config = require('../config')
-var env = require('./env')
-var lang = require('./lang')
-var dom = require('./dom')
+var lang   = require('./lang')
 var extend = lang.extend
 
-extend(_, env)
-extend(_, lang)
-extend(_, dom)
-
-/**
- * Option overwriting strategies are functions that handle
- * how to merge a parent option value and a child option
- * value into the final value.
- *
- * All strategy functions follow the same signature:
- *
- * @param {*} parentVal
- * @param {*} childVal
- * @param {Vue} [vm]
- */
-
-var strats = {}
-
-/**
- * Hooks and param attributes are merged as arrays.
- */
-
-strats.created =
-strats.ready =
-strats.attached =
-strats.detached =
-strats.beforeDestroy =
-strats.afterDestroy =
-strats.paramAttributes = function (parentVal, childVal) {
-  return (parentVal || []).concat(childVal || [])
-}
-
-/**
- * Assets
- *
- * When a vm is present (instance creation), we need to do a
- * 3-way merge for assets: constructor assets, instance assets,
- * and instance scope assets.
- */
-
-strats.directives =
-strats.filters =
-strats.partials =
-strats.effects =
-strats.components = function (parentVal, childVal, key, vm) {
-  var ret = Object.create(vm.$parent
-    ? vm.$parent.$options[key]
-    : null)
-  extend(ret, parentVal) 
-  extend(ret, childVal)
-  return ret
-}
-
-/**
- * Methods and computed properties
- */
-
-strats.methods =
-strats.computed = function (parentVal, childVal) {
-  var ret = Object.create(parentVal || null)
-  extend(ret, childVal)
-  return ret
-}
-
-/**
- * Default strategy
- */
-
-var defaultStrat = function (parentVal, childVal) {
-  return childVal === undefined
-    ? parentVal
-    : childVal
-}
-
-/**
- * Merge two option objects into a new one.
- * Core utility used in both instantiation and inheritance.
- *
- * @param {Object} parent
- * @param {Object} child
- * @param {Vue} [vm] - if vm is present, indicates this is
- *                     an instantiation merge.
- */
-
-_.mergeOptions = function (parent, child, vm) {
-  var options = {}
-  var key
-  for (key in parent) {
-    merge(key)
-  }
-  for (key in child) {
-    if (!(key in parent)) {
-      merge(key)
-    }
-  }
-  function merge (key) {
-    if (!vm && (key === 'el' || key === 'data' || key === 'parent')) {
-      _.warn(
-        'The "' + key + '" option can only be used as an instantiation ' +
-        'option and will be ignored in Vue.extend().'
-      )
-      return
-    }
-    var strat = strats[key] || defaultStrat
-    options[key] = strat(parent[key], child[key], key, vm)
-  }
-  return options
-}
-
-/**
- * Enable debug utilities. The enableDebug() function and all
- * _.log() & _.warn() calls will be dropped in the minified
- * production build.
- */
-
-enableDebug()
-
-function enableDebug () {
-
-  var hasConsole = typeof console !== 'undefined'
-  
-  /**
-   * Log a message.
-   *
-   * @param {String} msg
-   */
-
-  _.log = function (msg) {
-    if (hasConsole && config.debug) {
-      console.log(msg)
-    }
-  }
-
-  /**
-   * We've got a problem here.
-   *
-   * @param {String} msg
-   */
-
-  _.warn = function (msg) {
-    if (hasConsole && !config.silent) {
-      console.warn(msg)
-      if (config.debug && console.trace) {
-        console.trace(msg)
-      }
-    }
-  }
-
-}
+extend(exports, lang)
+extend(exports, require('./env'))
+extend(exports, require('./dom'))
+extend(exports, require('./option'))
+extend(exports, require('./debug'))

+ 106 - 0
src/util/option.js

@@ -0,0 +1,106 @@
+var extend = require('./lang').extend
+
+/**
+ * Option overwriting strategies are functions that handle
+ * how to merge a parent option value and a child option
+ * value into the final value.
+ *
+ * All strategy functions follow the same signature:
+ *
+ * @param {*} parentVal
+ * @param {*} childVal
+ * @param {Vue} [vm]
+ */
+
+var strats = {}
+
+/**
+ * Hooks and param attributes are merged as arrays.
+ */
+
+strats.created =
+strats.ready =
+strats.attached =
+strats.detached =
+strats.beforeDestroy =
+strats.afterDestroy =
+strats.paramAttributes = function (parentVal, childVal) {
+  return (parentVal || []).concat(childVal || [])
+}
+
+/**
+ * Assets
+ *
+ * When a vm is present (instance creation), we need to do a
+ * 3-way merge for assets: constructor assets, instance assets,
+ * and instance scope assets.
+ */
+
+strats.directives =
+strats.filters =
+strats.partials =
+strats.effects =
+strats.components = function (parentVal, childVal, key, vm) {
+  var ret = Object.create(vm.$parent
+    ? vm.$parent.$options[key]
+    : null)
+  extend(ret, parentVal) 
+  extend(ret, childVal)
+  return ret
+}
+
+/**
+ * Methods and computed properties
+ */
+
+strats.methods =
+strats.computed = function (parentVal, childVal) {
+  var ret = Object.create(parentVal || null)
+  extend(ret, childVal)
+  return ret
+}
+
+/**
+ * Default strategy
+ */
+
+var defaultStrat = function (parentVal, childVal) {
+  return childVal === undefined
+    ? parentVal
+    : childVal
+}
+
+/**
+ * Merge two option objects into a new one.
+ * Core utility used in both instantiation and inheritance.
+ *
+ * @param {Object} parent
+ * @param {Object} child
+ * @param {Vue} [vm] - if vm is present, indicates this is
+ *                     an instantiation merge.
+ */
+
+exports.mergeOptions = function (parent, child, vm) {
+  var options = {}
+  var key
+  for (key in parent) {
+    merge(key)
+  }
+  for (key in child) {
+    if (!(key in parent)) {
+      merge(key)
+    }
+  }
+  function merge (key) {
+    if (!vm && (key === 'el' || key === 'data' || key === 'parent')) {
+      _.warn(
+        'The "' + key + '" option can only be used as an instantiation ' +
+        'option and will be ignored in Vue.extend().'
+      )
+      return
+    }
+    var strat = strats[key] || defaultStrat
+    options[key] = strat(parent[key], child[key], key, vm)
+  }
+  return options
+}