├── .gitignore ├── .npmignore ├── Makefile ├── README.md ├── examples └── github-users │ ├── app.js │ ├── egoist.html │ └── index.html ├── package.json ├── rollup.config.js ├── src ├── index.js └── router.js ├── webpack.config.dev.js ├── webpack.config.js └── webpack.config.prod.js /.gitignore: -------------------------------------------------------------------------------- 1 | # app 2 | /dist 3 | /index.js 4 | 5 | # Logs 6 | logs 7 | *.log 8 | 9 | # Runtime data 10 | pids 11 | *.pid 12 | *.seed 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directory 30 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 31 | node_modules 32 | .DS_Store 33 | .AppleDouble 34 | .LSOverride 35 | 36 | # Icon must end with two \r 37 | Icon 38 | 39 | 40 | # Thumbnails 41 | ._* 42 | 43 | # Files that might appear on external disk 44 | .Spotlight-V100 45 | .Trashes 46 | 47 | # Directories potentially created on remote AFP share 48 | .AppleDB 49 | .AppleDesktop 50 | Network Trash Folder 51 | Temporary Items 52 | .apdisk 53 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /test.js 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | publish: 2 | npm run build 3 | npm publish 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-simple-router 2 | 3 | In fact, it's not simple at all, it's just made for **Non-Single-Page** applications. 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install vue-simple-router --save-dev 9 | ``` 10 | 11 | ## Example 12 | 13 | ```javascript 14 | import router from 'vue-simple-router'; 15 | 16 | // define a view for homepage: 17 | // first define a footer element: 18 | const footer = { 19 | el: '#footer', 20 | data () { 21 | return { year: 2015 }; 22 | } 23 | }; 24 | const home = { 25 | data () { 26 | return { siteName: 'Home' }; 27 | }, 28 | kids: [footer], 29 | // It works with `Webpack` and `Vue-loader` 30 | // which means you can write single-file Vue component and use it in Vue Simple Router. 31 | components: { 32 | clock: require('./components/clock.vue') 33 | } 34 | }; 35 | 36 | // use a plugin, like vue-resource 37 | router.use(VueResource); 38 | // or 39 | router.Vue.use(VueResource); 40 | 41 | // map routes, support /user/* style minimatch 42 | router.reg('/home*', { 43 | view: home 44 | }); 45 | // or Regex 46 | router.reg(/^\/home$/, { 47 | view: home 48 | }); 49 | 50 | // initial router and bind it to body 51 | router.init(); 52 | // or bind to anywhere 53 | router.init('#app'); 54 | ``` 55 | 56 | ## License 57 | 58 | MIT © [EGOIST](https://github.com/egoist). 59 | -------------------------------------------------------------------------------- /examples/github-users/app.js: -------------------------------------------------------------------------------- 1 | System.config({ 2 | map: { 3 | } 4 | }); 5 | var home = { 6 | data: { 7 | user: {} 8 | }, 9 | ready: function () { 10 | $.getJSON('https://api.github.com/users/egoist').then(function (data) { 11 | this.user = data; 12 | }.bind(this)); 13 | } 14 | }; 15 | 16 | var sox = { 17 | el: '#sox', 18 | data: { 19 | name: 'sox' 20 | } 21 | }; 22 | 23 | var egoist = { 24 | data: { 25 | repos: [] 26 | }, 27 | ready: function () { 28 | $.getJSON('https://api.github.com/users/egoist/repos').then(function (data) { 29 | this.repos = data; 30 | }.bind(this)); 31 | }, 32 | kids: [sox] 33 | }; 34 | 35 | System.import('../../dist/vsr.js') 36 | .then(function (router) { 37 | return router.default; 38 | }) 39 | .then(function (router) { 40 | router.reg(/^\/examples\/github-users\/$/, { 41 | view: home 42 | }) 43 | router.reg(/^\/examples\/github-users\/([\w])+\.html/, { 44 | view: egoist 45 | }) 46 | router.init('body'); 47 | }); 48 | -------------------------------------------------------------------------------- /examples/github-users/egoist.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | EGOIST - GitHub users 6 | 32 | 33 | 34 |

35 | EGOIST 36 |

37 |
38 |
loading...
39 |
40 |

41 |
42 |
43 |
{{ name }}
44 |
45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /examples/github-users/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | GitHub users 6 | 23 | 24 | 25 |
26 |
27 | 28 |
29 |
30 | loading... 31 |
32 |
33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-simple-router", 3 | "version": "1.0.0", 4 | "description": "Vue router for Non-Single-Page applications.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "make test", 8 | "unit-test": "mocha test.js --recursive", 9 | "dev": "webpack --config webpack.config.dev.js --watch", 10 | "build": "npm run rollup && webpack -p --config webpack.config.prod.js", 11 | "rollup": "rollup -c -o index.js", 12 | "serve": "serve" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/egoist/vue-simple-router.git" 17 | }, 18 | "keywords": [ 19 | "vue", 20 | "router" 21 | ], 22 | "author": "EGOIST <0x142857@gmail.com>", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/egoist/vue-simple-router/issues" 26 | }, 27 | "homepage": "https://github.com/egoist/vue-simple-router#readme", 28 | "devDependencies": { 29 | "babel-core": "^6.3.15", 30 | "babel-loader": "^6.2.0", 31 | "babel-preset-es2015": "^6.3.13", 32 | "babel-preset-es2015-rollup": "^1.0.0", 33 | "babel-preset-stage-0": "^6.3.13", 34 | "rollup": "^0.21.2", 35 | "rollup-plugin-babel": "^1.0.0", 36 | "webpack": "^1.12.9" 37 | }, 38 | "dependencies": { 39 | "minimatch": "^3.0.0", 40 | "vue": "^1.0.10" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from 'rollup-plugin-babel'; 2 | 3 | export default { 4 | entry: './src/index', 5 | plugins: [ 6 | babel({ 7 | exclude: 'node_modules/**' 8 | }) 9 | ], 10 | format: 'cjs' 11 | }; 12 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import Router from './router'; 2 | export default new Router(); 3 | -------------------------------------------------------------------------------- /src/router.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue'; 2 | import minimatch from 'minimatch'; 3 | 4 | const isDev = (typeof __DEV__ !== 'undefined') && __DEV__; 5 | if (isDev) { 6 | Vue.config.debug = true; 7 | } 8 | 9 | export default class Router { 10 | constructor () { 11 | this.routes = []; 12 | this.Vue = Vue; 13 | this.includeQuery = false; 14 | } 15 | use (name, options = {}) { 16 | this.Vue.use(name, options); 17 | return this; 18 | } 19 | set (name, value) { 20 | this[name] = value; 21 | return this; 22 | } 23 | reg (name, route) { 24 | if (isDev && (!route || !route.view)) { 25 | throw new ReferenceError('[VSR] Could not reference route view'); 26 | } 27 | if ('default' in route.view) { 28 | route.view = route.view.default; 29 | } 30 | this.routes.push({ 31 | pattern: name, 32 | view: route.view 33 | }); 34 | return this; 35 | } 36 | init (bindTo = 'body') { 37 | this.bindTo = bindTo; 38 | const fullPath = this.includeQuery ? location.pathname + location.search : location.pathname; 39 | for (const route of this.routes) { 40 | const { pattern, view } = route; 41 | const isMiniMatch = (typeof pattern === 'string') && (pattern === fullPath); 42 | if (isMiniMatch || pattern.test(fullPath)) { 43 | view.el = this.bindTo; 44 | // we initial its kids before itself 45 | // for the scope matter 46 | // you cannot initial a parent instance 47 | // then initial its child element 48 | // same, the parent instance would not own the control of its children 49 | this.initKids(view.kids); 50 | new this.Vue(view); 51 | } 52 | } 53 | return this; 54 | } 55 | initKids (kids) { 56 | if (kids && Array.isArray(kids)) { 57 | kids.forEach(kid => { 58 | this.initKids(kid.kids); 59 | new this.Vue(kid); 60 | }); 61 | } 62 | } 63 | }; 64 | -------------------------------------------------------------------------------- /webpack.config.dev.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack') 2 | var config = require('./webpack.config') 3 | var path = require('path') 4 | 5 | config.entry = './src/index' 6 | config.debug = true 7 | config.devtool = 'source-map' 8 | config.plugins = [ 9 | new webpack.NoErrorsPlugin(), 10 | new webpack.DefinePlugin({ 11 | '__DEV__': true, 12 | 'process.env': JSON.stringify('development') 13 | }) 14 | ] 15 | 16 | module.exports = config 17 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | entry: ['./index'], 3 | output: { 4 | path: __dirname + '/dist', 5 | filename: 'vsr.js', 6 | libraryTarget: 'umd', 7 | library: 'router' 8 | }, 9 | resolve: { 10 | extensions: ['', '.js'] 11 | }, 12 | module: { 13 | loaders: [ 14 | { test: /\.js$/, loaders: ['babel'], exclude: [/node_modules/] } 15 | ] 16 | }, 17 | babel: { 18 | presets: ["es2015", "stage-0"] 19 | }, 20 | plugins: [] 21 | }; 22 | -------------------------------------------------------------------------------- /webpack.config.prod.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack') 2 | var config = require('./webpack.config') 3 | var path = require('path') 4 | 5 | config.plugins = [ 6 | new webpack.optimize.OccurenceOrderPlugin(), 7 | new webpack.DefinePlugin({ 8 | '__DEV__': false, 9 | 'process.env': { 10 | 'NODE_ENV': JSON.stringify('production') 11 | } 12 | }), 13 | new webpack.optimize.UglifyJsPlugin({ 14 | compressor: { 15 | warnings: false 16 | }, 17 | output: { 18 | comments: false 19 | } 20 | }) 21 | ] 22 | 23 | module.exports = config 24 | --------------------------------------------------------------------------------