Evan You 11 år sedan
förälder
incheckning
1667736a20

+ 0 - 5
benchmarks/bench.js

@@ -1,5 +0,0 @@
-require('./observer').run(function () {
-  require('./instantiation').run(function () {
-    require('./expression')
-  })
-})

+ 0 - 36
benchmarks/dir.js

@@ -1,36 +0,0 @@
-console.log('\nDirective Parser\n')
-
-var Cache = require('../src/cache')
-var parse = require('../src/parse/directive').parse
-
-Cache.prototype.get = Cache.prototype.put = function () {}
-
-function bench (id, fn) {
-  var s = Date.now()
-  var max = i = 100000
-  while (i--) {
-    fn()
-  }
-  var used = Date.now() - s
-  var ops = Math.round(16 / (used / max))
-  console.log(id + ': ' + ops + ' ops/frame')
-}
-
-
-var side
-
-bench('simple path', function () {
-  side = parse('a.b.c')
-})
-
-bench('filters', function () {
-  side = parse('a.b.c | a | b | c')
-})
-
-bench('multi', function () {
-  side = parse('abc:ddd, bcd:eee, fsef:fff')
-})
-
-bench('complex exp', function () {
-  side = parse('test:a.b + { c: d} + "abced{fsefesf}ede"')
-})

+ 0 - 35
benchmarks/expression.js

@@ -1,35 +0,0 @@
-console.log('\Expression Parser\n')
-
-var Cache = require('../src/cache')
-var parse = require('../src/parse/expression').parse
-
-Cache.prototype.get = Cache.prototype.put = function () {}
-
-function bench (id, fn) {
-  var s = Date.now()
-  var max = i = 10000
-  while (i--) {
-    fn()
-  }
-  var used = Date.now() - s
-  var ops = Math.round(16 / (used / max))
-  console.log(id + ': ' + ops + ' ops/frame')
-}
-
-var side
-
-bench('simple path', function () {
-  side = parse('a.b.c')
-})
-
-bench('complex path', function () {
-  side = parse('a["b"].c')
-})
-
-bench('simple exp', function () {
-  side = parse('a.b + c')
-})
-
-bench('complex exp', function () {
-  side = parse('a.b + { c: d} + "abced{fsefesf}ede"')
-})

+ 0 - 146
benchmarks/instantiation.js

@@ -1,146 +0,0 @@
-console.log('\nInstantiation\n')
-
-var done = null
-var OldVue = require('../../vue')
-var Vue = require('../src/vue')
-var sideEffect = null
-var parent = new Vue({
-  data: { a: 1 }
-})
-var oldParent = new OldVue({
-  data: { a: 1 }
-})
-
-function now () {
-  return window.performence
-    ? window.performence.now()
-    : Date.now()
-}
-
-// warm up
-for (var i = 0; i < 1000; i++) {
-  sideEffect = new Vue()
-}
-
-var queue = []
-
-function bench (desc, n, fn) {
-  queue.push(function () {
-    var s = now()
-    for (var i = 0; i < n; i++) {
-      fn()
-    }
-    var time = now() - s
-    var opf = (16 / (time / n)).toFixed(2)
-    console.log(desc + ' ' + n + ' times - ' + opf + ' ops/frame')
-  })
-}
-
-function run () {
-  queue.shift()()
-  if (queue.length) {
-    setTimeout(run, 0)
-  } else {
-    done && done()
-  }
-}
-
-function simpleInstance () {
-  sideEffect = new Vue({
-    el: document.createElement('div'),
-    data: {a: 1}
-  })
-}
-
-function oldSimpleInstance () {
-  sideEffect = new OldVue({
-    data: {a: 1}
-  })
-}
-
-function simpleInstanceWithInheritance () {
-  sideEffect = new Vue({
-    el: document.createElement('div'),
-    parent: parent,
-    data: { b:2 }
-  })
-}
-
-function oldSimpleInstanceWithInheritance () {
-  sideEffect = new OldVue({
-    parent: oldParent,
-    data: { b:2 }
-  })
-}
-
-function complexInstance () {
-  sideEffect = new Vue({
-    el: document.createElement('div'),
-    data: {
-      a: {
-        b: {
-          c: 1
-        }
-      },
-      c: {
-        b: {
-          c: { a:1 },
-          d: 2,
-          e: 3,
-          d: 4
-        }
-      },
-      e: [{a:1}, {a:2}, {a:3}]
-    }
-  })
-}
-
-function oldComplexInstance () {
-  sideEffect = new OldVue({
-    data: {
-      a: {
-        b: {
-          c: 1
-        }
-      },
-      c: {
-        b: {
-          c: { a:1 },
-          d: 2,
-          e: 3,
-          d: 4
-        }
-      },
-      e: [{a:1}, {a:2}, {a:3}]
-    }
-  })
-}
-
-bench('Simple instance', 10, simpleInstance)
-bench('Simple instance', 100, simpleInstance)
-bench('Simple instance', 1000, simpleInstance)
-
-bench('Simple instance (old)', 10, oldSimpleInstance)
-bench('Simple instance (old)', 100, oldSimpleInstance)
-bench('Simple instance (old)', 1000, oldSimpleInstance)
-
-bench('Simple instance with inheritance', 10, simpleInstanceWithInheritance)
-bench('Simple instance with inheritance', 100, simpleInstanceWithInheritance)
-bench('Simple instance with inheritance', 1000, simpleInstanceWithInheritance)
-
-bench('Simple instance with inheritance (old)', 10, oldSimpleInstanceWithInheritance)
-bench('Simple instance with inheritance (old)', 100, oldSimpleInstanceWithInheritance)
-bench('Simple instance with inheritance (old)', 1000, oldSimpleInstanceWithInheritance)
-
-bench('Complex instance', 10, complexInstance)
-bench('Complex instance', 100, complexInstance)
-bench('Complex instance', 1000, complexInstance)
-
-bench('Complex instance (old)', 10, oldComplexInstance)
-bench('Complex instance (old)', 100, oldComplexInstance)
-bench('Complex instance (old)', 1000, oldComplexInstance)
-
-exports.run = function (cb) {
-  done = cb
-  run()
-}

+ 0 - 378
benchmarks/observer.js

@@ -1,378 +0,0 @@
-console.log('\nObserver\n')
-
-var done = null
-var Observer = require('../src/observe/observer')
-var Emitter = require('../src/emitter')
-var OldObserver = require('../../vue/src/observer')
-var sideEffect = true
-var runs = 1000
-function cb () {
-  sideEffect = !sideEffect
-}
-
-function now () {
-  return window.performence
-    ? window.performence.now()
-    : Date.now()
-}
-
-var queue = []
-
-function bench (desc, fac, run) {
-  queue.push(function () {
-    var objs = []
-    for (var i = 0; i < runs; i++) {
-      objs.push(fac(i))
-    }
-    var s = now()
-    for (var i = 0; i < runs; i++) {
-      run(objs[i])
-    }
-    var passed = now() - s
-    console.log(desc + ' - ' + (16 / (passed / runs)).toFixed(2) + ' ops/frame')
-  })  
-}
-
-function run () {
-  queue.shift()()
-  if (queue.length) {
-    setTimeout(run, 0)
-  } else {
-    done && done()
-  }
-}
-
-bench(
-  'observe (simple object)        ',
-  function (i) {
-    return {a:i}
-  },
-  function (o) {
-    new Observer().observe('', o)
-  }
-)
-
-bench(
-  'observe (simple object) old    ',
-  function (i) {
-    return {a:i}
-  },
-  function (o) {
-    OldObserver.observe(o, '', new Emitter())
-  }
-)
-
-bench(
-  'observe (3 nested objects)     ',
-  function (i) {
-    return {a:{b:{c:i}}}
-  },
-  function (o) {
-    new Observer().observe('', o)
-  }
-)
-
-bench(
-  'observe (3 nested objects) old ',
-  function (i) {
-    return {a:{b:{c:i}}}
-  },
-  function (o) {
-    OldObserver.observe(o, '', new Emitter())
-  }
-)
-
-bench(
-  'observe (array, 3 objects)     ',
-  function (i) {
-    return [{a:i}, {a:i+1}, {a:i+2}]
-  },
-  function (o) {
-    new Observer().observe('', o)
-  }
-)
-
-bench(
-  'observe (array, 3 objects) old ',
-  function (i) {
-    return [{a:i}, {a:i+1}, {a:i+2}]
-  },
-  function (o) {
-    OldObserver.observe(o, '', new Emitter())
-  }
-)
-
-bench(
-  'observe (array, 30 objects)    ',
-  function () {
-    var a = [], i = 30
-    while (i--) {
-      a.push({a:i})
-    }
-    return a
-  },
-  function (o) {
-    new Observer().observe('', o)
-  }
-)
-
-bench(
-  'observe (array, 30 objects) old',
-  function () {
-    var a = [], i = 30
-    while (i--) {
-      a.push({a:i})
-    }
-    return a
-  },
-  function (o) {
-    OldObserver.observe(o, '', new Emitter())
-  }
-)
-
-Observer.emitGet = true
-OldObserver.shouldGet = true
-
-bench(
-  'simple get    ',
-  function () {
-    var a = {a:1}
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('get', cb)
-    return a
-  },
-  function (o) {
-    var v = o.a
-  }
-)
-
-bench(
-  'simple get old',
-  function () {
-    var a = {a:1}
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('get', cb)
-    return a
-  },
-  function (o) {
-    var v = o.a
-  }
-)
-
-bench(
-  'nested get    ',
-  function () {
-    var a = {a:{b:{c:1}}}
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('get', cb)
-    return a
-  },
-  function (o) {
-    var v = o.a.b.c
-  }
-)
-
-bench(
-  'nested get old',
-  function () {
-    var a = {a:{b:{c:1}}}
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('get', cb)
-    return a
-  },
-  function (o) {
-    var v = o.a.b.c
-  }
-)
-
-Observer.emitGet = false
-OldObserver.shouldGet = false
-
-bench(
-  'simple set    ',
-  function () {
-    var a = {a:1}
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('set', cb)
-    return a
-  },
-  function (o) {
-    o.a = 12345
-  }
-)
-
-bench(
-  'simple set old',
-  function () {
-    var a = {a:1}
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('set', cb)
-    return a
-  },
-  function (o) {
-    o.a = 12345
-  }
-)
-
-bench(
-  'nested set    ',
-  function () {
-    var a = {a:{b:{c:1}}}
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('set', cb)
-    return a
-  },
-  function (o) {
-    o.a.b.c = 2
-  }
-)
-
-bench(
-  'nested set old',
-  function () {
-    var a = {a:{b:{c:1}}}
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('set', cb)
-    return a
-  },
-  function (o) {
-    o.a.b.c = 2
-  }
-)
-
-bench(
-  'swap set      ',
-  function () {
-    var a = {a:{b:{c:1}}}
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('set', cb)
-    return a
-  },
-  function (o) {
-    o.a = {b:{c:2}}
-  }
-)
-
-bench(
-  'swap set old  ',
-  function () {
-    var a = {a:{b:{c:1}}}
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('set', cb)
-    return a
-  },
-  function (o) {
-    o.a = {b:{c:2}}
-  }
-)
-
-bench(
-  'array push    ',
-  function () {
-    var a = []
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('mutation', cb)
-    return a
-  },
-  function (o) {
-    o.push({a:1})
-  }
-)
-
-bench(
-  'array push old',
-  function () {
-    var a = []
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('mutation', cb)
-    return a
-  },
-  function (o) {
-    o.push({a:1})
-  }
-)
-
-bench(
-  'array reverse (5 objects)     ',
-  function () {
-    var a = [], i = 5
-    while (i--) {
-      a.push({a:i})
-    }
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('mutation', cb)
-    return a
-  },
-  function (o) {
-    o.reverse()
-  }
-)
-
-bench(
-  'array reverse (5 objects) old ',
-  function () {
-    var a = [], i = 5
-    while (i--) {
-      a.push({a:i})
-    }
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('mutation', cb)
-    return a
-  },
-  function (o) {
-    o.reverse()
-  }
-)
-
-bench(
-  'array reverse (50 objects)    ',
-  function () {
-    var a = [], i = 50
-    while (i--) {
-      a.push({a:i})
-    }
-    var ob = new Observer()
-    ob.observe('', a)
-    ob.on('mutation', cb)
-    return a
-  },
-  function (o) {
-    o.reverse()
-  }
-)
-
-bench(
-  'array reverse (50 objects) old',
-  function () {
-    var a = [], i = 50
-    while (i--) {
-      a.push({a:i})
-    }
-    var ob = new Emitter()
-    OldObserver.observe(a, '', ob)
-    ob.on('mutation', cb)
-    return a
-  },
-  function (o) {
-    o.reverse()
-  }
-)
-
-exports.run = function (cb) {
-  done = cb
-  run()
-}

+ 0 - 10
benchmarks/runner.html

@@ -1,10 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-  <head>
-    <meta charset="utf-8">
-    <title></title>
-  </head>
-  <body>
-    <script src="browser.js"></script>
-  </body>
-</html>

+ 62 - 0
grunt/shared-build.js

@@ -0,0 +1,62 @@
+/**
+ * Shared build function
+ */
+
+var fs = require('fs')
+var resolve = require('component-resolver')
+var build = require('component-builder')
+
+module.exports = function (grunt, cb) {
+  var banner = grunt.config.get('banner')
+  // build with component-builder
+  resolve(process.cwd(), {}, function (err, tree) {
+    build.scripts(tree)
+      .use('scripts', build.plugins.js())
+      .end(function (err, js) {
+        // wrap with umd
+        js = umd(js)
+        // replace require paths with numbers for file size
+        js = shortenPaths(js)
+        // add banner
+        js = banner + js
+        // done
+        cb(js)
+      })
+  })
+}
+
+/**
+ * component's umd wrapper throws error in strict mode
+ * so we have to roll our own
+ */
+
+function umd (js) {
+  return '\n;(function(){\n\n'
+    + '"use strict"\n\n'
+    + build.scripts.require
+    + js
+    + 'if (typeof exports == "object") {\n'
+    + '  module.exports = require("vue");\n'
+    + '} else if (typeof define == "function" && define.amd) {\n'
+    +'  define([], function(){ return require("vue"); });\n'
+    + '} else {\n'
+    + '  window.Vue = require("vue");\n'
+    + '}\n'
+    + '})()\n';
+}
+
+/**
+ * Shorten require() paths for smaller file size
+ */
+
+function shortenPaths (js) {
+  var seen = {}
+  var count = 0
+  return js.replace(/'vue\/src\/(.+?)'|"vue\/src\/(.+?)"/g, function (path) {
+    path = path.slice(1, -1)
+    if (!seen[path]) {
+      seen[path] = ++count
+    }
+    return seen[path]
+  })
+}

+ 15 - 0
grunt/tasks/build-test.js

@@ -0,0 +1,15 @@
+module.exports = function (grunt) {
+  grunt.registerTask('build-test', function () {
+    var done = this.async()
+    var fs = require('fs')
+    var browserify = require('browserify')
+    var files = grunt.file.expand(['test/unit/specs/**/*.js'])
+      .map(function (file) {
+        return './' + file
+      })
+    browserify(files)
+      .bundle()
+      .pipe(fs.createWriteStream('test/unit/specs.js'))
+      .on('close', done)
+  })
+}

+ 57 - 0
grunt/tasks/build.js

@@ -0,0 +1,57 @@
+module.exports = function (grunt) {
+  grunt.registerTask('build', function () {
+
+    var done = this.async()
+    var fs = require('fs')
+    var zlib = require('zlib')
+    var build = require('../shared-build')
+    var uglifyjs = require('uglify-js')
+    
+    // update component.json first
+    var jsRE = /\.js$/
+    var component = grunt.file.readJSON('component.json')
+    component.scripts = []
+    grunt.file.recurse('src', function (file) {
+      if (jsRE.test(file)) {
+        component.scripts.push(file)
+      }
+    })
+    grunt.file.write('component.json', JSON.stringify(component, null, 2))
+
+    // then build
+    build(grunt, function (js) {
+      write('dist/vue.js', js)
+      // uglify
+      var min = uglifyjs.minify(js, {
+        fromString: true,
+        compress: {
+          pure_funcs: [
+            '_.log',
+            '_.warn',
+            '_.assertAsset',
+            'enableDebug'
+          ]
+        }
+      }).code
+      min = grunt.config.get('banner') + min
+      write('dist/vue.min.js', min)
+      // report gzip size
+      zlib.gzip(min, function (err, buf) {
+        write('dist/vue.min.js.gz', buf)
+        done()
+      })
+    })
+
+    function write (path, file) {
+      fs.writeFileSync(path, file)
+      console.log(
+        blue(path + ': ') +
+        (file.length / 1024).toFixed(2) + 'kb'
+      )
+    }
+
+    function blue (str) {
+      return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
+    }
+  })
+}

+ 0 - 0
tasks/casper.js → grunt/tasks/casper.js


+ 11 - 0
grunt/tasks/dev.js

@@ -0,0 +1,11 @@
+module.exports = function (grunt) {
+  grunt.registerTask('dev', function () {
+    var done = this.async()
+    var fs = require('fs')
+    var build = require('../shared-build')
+    build(grunt, function (js) {
+      fs.writeFileSync('dist/vue.js', js)
+      done()
+    })
+  })
+}

+ 0 - 0
tasks/release.js → grunt/tasks/release.js


+ 14 - 59
gruntfile.js

@@ -10,6 +10,8 @@ module.exports = function (grunt) {
 
   grunt.initConfig({
 
+    banner: banner,
+
     jshint: {
       options: {
         reporter: require('jshint-stylish'),
@@ -26,6 +28,16 @@ module.exports = function (grunt) {
       }
     },
 
+    watch: {
+      options: {
+        nospawn: true
+      },
+      dev: {
+        files: ['src/**/*.js'],
+        tasks: ['dev']
+      }
+    },
+
     karma: {
       options: {
         frameworks: ['jasmine', 'commonjs'],
@@ -61,79 +73,22 @@ module.exports = function (grunt) {
           }
         }
       }
-    },
-
-    browserify: {
-      build: {
-        src: ['src/vue.js'],
-        dest: 'dist/vue.js',
-        options: {
-          bundleOptions: {
-            standalone: 'Vue'
-          },
-          postBundleCB: function (err, src, next) {
-            next(err, banner + src)
-          }
-        }
-      },
-      watch: {
-        src: ['src/vue.js'],
-        dest: 'dist/vue.js',
-        options: {
-          watch: true,
-          keepAlive: true,
-          bundleOptions: {
-            standalone: 'Vue'
-          }
-        }
-      },
-      bench: {
-        src: ['benchmarks/bench.js'],
-        dest: 'benchmarks/browser.js'
-      },
-      test: {
-        src: ['test/unit/specs/**/*.js'],
-        dest: 'test/unit/specs.js'
-      }
-    },
-
-    uglify: {
-      build: {
-        options: {
-          banner: banner,
-          compress: {
-            pure_funcs: [
-              '_.log',
-              '_.warn',
-              '_.assertAsset',
-              '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-contrib-watch')
   grunt.loadNpmTasks('grunt-karma')
-  grunt.loadNpmTasks('grunt-browserify')
 
   // load custom tasks
-  grunt.file.recurse('tasks', function (path) {
+  grunt.file.recurse('grunt/tasks', function (path) {
     require('./' + path)(grunt)
   })
 
   grunt.registerTask('unit', ['karma:browsers'])
   grunt.registerTask('cover', ['karma:phantom'])
-  grunt.registerTask('bench', ['browserify:bench'])
-  grunt.registerTask('watch', ['browserify:watch'])
-  grunt.registerTask('build', ['component', 'browserify:build', 'uglify:build', 'size'])
   grunt.registerTask('test', ['unit', 'cover'])
   grunt.registerTask('default', ['jshint', 'test', 'build'])
 

+ 7 - 4
package.json

@@ -20,20 +20,23 @@
     "test": "grunt ci"
   },
   "devDependencies": {
-    "browserify": "^4.2.0",
+    "browserify": "^5.11.0",
+    "component-builder": "^1.1.10",
+    "component-resolver": "^1.1.8",
     "grunt": "^0.4.5",
-    "grunt-browserify": "^2.1.3",
     "grunt-contrib-jshint": "^0.10.0",
-    "grunt-contrib-uglify": "^0.5.1",
+    "grunt-contrib-watch": "^0.6.1",
     "grunt-karma": "^0.8.3",
     "jshint-stylish": "^0.3.0",
     "karma": "^0.12.16",
-    "karma-browserify": "^0.2.1",
     "karma-chrome-launcher": "^0.1.4",
     "karma-commonjs": "^0.0.10",
     "karma-coverage": "^0.2.5",
     "karma-firefox-launcher": "^0.1.3",
     "karma-jasmine": "^0.2.2",
     "karma-phantomjs-launcher": "^0.1.4"
+  },
+  "dependencies": {
+    "uglify-js": "^2.4.15"
   }
 }

+ 0 - 1
src/directives/model/text.js

@@ -94,7 +94,6 @@ module.exports = {
   },
 
   update: function (value) {
-    console.log(value)
     this.el.value = value
   },
 

+ 0 - 19
tasks/component.js

@@ -1,19 +0,0 @@
-// automatically fill in component.json's script field
-
-module.exports = function (grunt) {
-  grunt.registerTask('component', function () {
-
-    var jsRE = /\.js$/
-    var component = grunt.file.readJSON('component.json')
-    component.scripts = []
-
-    grunt.file.recurse('src', function (file) {
-      if (jsRE.test(file)) {
-        component.scripts.push(file)
-      }
-    })
-
-    grunt.file.write('component.json', JSON.stringify(component, null, 2))
-
-  })
-}

+ 0 - 12
tasks/size.js

@@ -1,12 +0,0 @@
-var zlib = require('zlib')
-
-module.exports = function (grunt) {
-  grunt.registerTask('size', function () {
-    var done = this.async()
-    zlib.gzip(grunt.file.read('dist/vue.min.js'), function (err, buf) {
-      var size = (buf.length / 1024).toFixed(2)
-      console.log('gzipped size: ' + size + 'kb')
-      done()
-    })
-  })
-}

+ 0 - 1
test/unit/specs/parse/text_spec.js

@@ -88,7 +88,6 @@ describe('Text Parser', function () {
 
   it('tokens to expression', function () {
     var tokens = textParser.parse('view-{{test}}-test-{{ok}}')
-    console.log(tokens)
     var exp = textParser.tokensToExp(tokens)
     expect(exp).toBe('"view-"+test+"-test-"+ok')
   })