Skip to content

Commit 4738b55

Browse files
committed
feat(main): first version
BREAKING CHANGE: 支持 组可见或即将可见时懒加载
0 parents  commit 4738b55

39 files changed

+11843
-0
lines changed

.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
["es2015", { "modules": false }]
4+
],
5+
"plugins": [
6+
"external-helpers"
7+
]
8+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules/
3+
npm-debug.log

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 xunlei
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Vue Lazy Component
2+
3+
> Vue.js 2.0 组件级懒加载方案
4+
5+
![Preview](https://github.com/binggg/vue-lazy-component/blob/master/demo/assets/demo.jpeg?raw=true)
6+
7+
## 安装
8+
```
9+
npm install @xunlei/vue-lazy-component
10+
```
11+
12+
## 在线Demo
13+
14+
https://xunleif2e.github.io/vue-lazy-component/demo/dist
15+
16+
## 使用
17+
18+
### 1. 注册组件
19+
20+
#### 方式1 利用插件方式全局注册
21+
22+
```javascript
23+
import VueContextMenu from '@xunlei/vue-lazy-component'
24+
import Vue from 'vue'
25+
26+
Vue.use(VueContextMenu)
27+
```
28+
#### 方式2 局部注册
29+
30+
```javascript
31+
import { component as VueContextMenu } from '@xunlei/vue-lazy-component'
32+
33+
export default {
34+
// ...
35+
components: {
36+
'vue-lazy-component': VueContextMenu
37+
}
38+
}
39+
```
40+
41+
#### 方式3 独立版本引入,自动全局注册
42+
> 前提是 vue 也是独立版本通过script标签引入
43+
44+
```html
45+
<script src="./node_modules/dist/vue-lazy-component.js"></script>
46+
```
47+
48+
### 2. 模版语法
49+
```html
50+
<context-menu class="right-menu"
51+
:target="contextMenuTarget"
52+
:show="contextMenuVisible"
53+
@update:show="(show) => contextMenuVisible = show">
54+
<a href="javascript:;" @click="copyMsg">复制</a>
55+
<a href="javascript:;" @click="quoteMsg">引用</a>
56+
<a href="javascript:;" @click="deleteMsg">删除</a>
57+
</context-menu>
58+
```
59+
60+
## Props
61+
62+
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
63+
|-------------------------|-------|------|--------|--------|
64+
| target | 触发右键事件的元素 | Element | - | - |
65+
| show | 是否显示右键菜单 | Boolean | - | false |
66+
67+
68+
## Events
69+
70+
| 事件名 | 说明 | 事件参数
71+
|-------------------------|-------|------|
72+
| update:show | 右键菜单显示/隐藏时触发 | 是否显示 |
73+
74+
75+
## 注意
76+
77+
如果target是某个兄弟元素,可以使用 `$refs`来访问,但是注意请在父组件mounted 之后获取。
78+
79+
参考 https://cn.vuejs.org/v2/guide/components.html#子组件索引
80+
81+
82+
## ChangeLog
83+
- [1.0.1] 2017-07-10
84+
- 修复 target 为空时可能出错的bug
85+
86+
- [1.0.0] 2017-06-23
87+
- 实现右键菜单基本功能
88+
89+
## Development Setup
90+
91+
``` bash
92+
# install deps
93+
npm install
94+
95+
# serve demo at localhost:8080
96+
npm run dev
97+
98+
# build library and demo
99+
npm run build
100+
101+
# build library
102+
npm run build:library
103+
104+
# build demo
105+
npm run build:demo
106+
```
107+
108+
## License
109+
110+
[MIT](http://opensource.org/licenses/MIT)
111+
112+
Copyright (c) 2017 赵兵

build/build.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require('./check-versions')()
2+
3+
process.env.NODE_ENV = 'production'
4+
5+
var ora = require('ora')
6+
var rm = require('rimraf')
7+
var path = require('path')
8+
var chalk = require('chalk')
9+
var webpack = require('webpack')
10+
var config = require('../config')
11+
var webpackConfig = require('./webpack.prod.conf')
12+
13+
var spinner = ora('building for production...')
14+
spinner.start()
15+
16+
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
17+
if (err) throw err
18+
webpack(webpackConfig, function (err, stats) {
19+
spinner.stop()
20+
if (err) throw err
21+
process.stdout.write(stats.toString({
22+
colors: true,
23+
modules: false,
24+
children: false,
25+
chunks: false,
26+
chunkModules: false
27+
}) + '\n\n')
28+
29+
console.log(chalk.cyan(' Build complete.\n'))
30+
console.log(chalk.yellow(
31+
' Tip: built files are meant to be served over an HTTP server.\n' +
32+
' Opening index.html over file:// won\'t work.\n'
33+
))
34+
})
35+
})

build/build.rollup.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
var chalk = require('chalk')
4+
var rollup = require('rollup')
5+
var babel = require('rollup-plugin-babel')
6+
var uglify = require('rollup-plugin-uglify')
7+
8+
var version = process.env.VERSION || require('../package.json').version
9+
var author = process.env.VERSION || require('../package.json').author
10+
var license = process.env.VERSION || require('../package.json').license
11+
12+
var banner =
13+
'/**\n' +
14+
' * vue-context-menu v' + version + '\n' +
15+
' * (c) ' + new Date().getFullYear() + ' ' + author + '\n' +
16+
' * @license ' + license + '\n' +
17+
' */\n'
18+
19+
rollup.rollup({
20+
entry: path.resolve(__dirname, '..', 'src/vue-context-menu.js'),
21+
plugins: [
22+
babel(),
23+
uglify()
24+
]
25+
})
26+
.then(bundle => {
27+
return write(path.resolve(__dirname, '../dist/vue-context-menu.js'), bundle.generate({
28+
format: 'umd',
29+
moduleName: 'vueContextMenu'
30+
}).code)
31+
})
32+
.then(() => {
33+
console.log(chalk.green('\nAwesome! vue-context-menu v' + version + ' builded.\n'))
34+
})
35+
.catch(console.log)
36+
37+
function getSize (code) {
38+
return (code.length / 1024).toFixed(2) + 'kb'
39+
}
40+
41+
function write (dest, code) {
42+
return new Promise(function (resolve, reject) {
43+
code = banner + code
44+
fs.writeFile(dest, code, function (err) {
45+
if (err) return reject(err)
46+
console.log(chalk.blue(dest) + ' ' + getSize(code))
47+
resolve()
48+
})
49+
})
50+
}

build/check-versions.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var chalk = require('chalk')
2+
var semver = require('semver')
3+
var packageConfig = require('../package.json')
4+
var shell = require('shelljs')
5+
function exec (cmd) {
6+
return require('child_process').execSync(cmd).toString().trim()
7+
}
8+
9+
var versionRequirements = [
10+
{
11+
name: 'node',
12+
currentVersion: semver.clean(process.version),
13+
versionRequirement: packageConfig.engines.node
14+
},
15+
]
16+
17+
if (shell.which('npm')) {
18+
versionRequirements.push({
19+
name: 'npm',
20+
currentVersion: exec('npm --version'),
21+
versionRequirement: packageConfig.engines.npm
22+
})
23+
}
24+
25+
module.exports = function () {
26+
var warnings = []
27+
for (var i = 0; i < versionRequirements.length; i++) {
28+
var mod = versionRequirements[i]
29+
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
30+
warnings.push(mod.name + ': ' +
31+
chalk.red(mod.currentVersion) + ' should be ' +
32+
chalk.green(mod.versionRequirement)
33+
)
34+
}
35+
}
36+
37+
if (warnings.length) {
38+
console.log('')
39+
console.log(chalk.yellow('To use this template, you must update following to modules:'))
40+
console.log()
41+
for (var i = 0; i < warnings.length; i++) {
42+
var warning = warnings[i]
43+
console.log(' ' + warning)
44+
}
45+
console.log()
46+
process.exit(1)
47+
}
48+
}

build/dev-client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable */
2+
require('eventsource-polyfill')
3+
var hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true')
4+
5+
hotClient.subscribe(function (event) {
6+
if (event.action === 'reload') {
7+
window.location.reload()
8+
}
9+
})

build/dev-server.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require('./check-versions')()
2+
3+
var config = require('../config')
4+
if (!process.env.NODE_ENV) {
5+
process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
6+
}
7+
8+
var opn = require('opn')
9+
var path = require('path')
10+
var express = require('express')
11+
var webpack = require('webpack')
12+
var proxyMiddleware = require('http-proxy-middleware')
13+
var webpackConfig = require('./webpack.dev.conf')
14+
15+
// default port where dev server listens for incoming traffic
16+
var port = process.env.PORT || config.dev.port
17+
// automatically open browser, if not set will be false
18+
var autoOpenBrowser = !!config.dev.autoOpenBrowser
19+
// Define HTTP proxies to your custom API backend
20+
// https://github.com/chimurai/http-proxy-middleware
21+
var proxyTable = config.dev.proxyTable
22+
23+
var app = express()
24+
var compiler = webpack(webpackConfig)
25+
26+
var devMiddleware = require('webpack-dev-middleware')(compiler, {
27+
publicPath: webpackConfig.output.publicPath,
28+
quiet: true
29+
})
30+
31+
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
32+
log: () => {}
33+
})
34+
// force page reload when html-webpack-plugin template changes
35+
compiler.plugin('compilation', function (compilation) {
36+
compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
37+
hotMiddleware.publish({ action: 'reload' })
38+
cb()
39+
})
40+
})
41+
42+
// proxy api requests
43+
Object.keys(proxyTable).forEach(function (context) {
44+
var options = proxyTable[context]
45+
if (typeof options === 'string') {
46+
options = { target: options }
47+
}
48+
app.use(proxyMiddleware(options.filter || context, options))
49+
})
50+
51+
// handle fallback for HTML5 history API
52+
app.use(require('connect-history-api-fallback')())
53+
54+
// serve webpack bundle output
55+
app.use(devMiddleware)
56+
57+
// enable hot-reload and state-preserving
58+
// compilation error display
59+
app.use(hotMiddleware)
60+
61+
// serve pure static assets
62+
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
63+
app.use(staticPath, express.static('./demo/static'))
64+
65+
var uri = 'http://localhost:' + port
66+
67+
var _resolve
68+
var readyPromise = new Promise(resolve => {
69+
_resolve = resolve
70+
})
71+
72+
console.log('> Starting dev server...')
73+
devMiddleware.waitUntilValid(() => {
74+
console.log('> Listening at ' + uri + '\n')
75+
// when env is testing, don't need open it
76+
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
77+
opn(uri)
78+
}
79+
_resolve()
80+
})
81+
82+
var server = app.listen(port)
83+
84+
module.exports = {
85+
ready: readyPromise,
86+
close: () => {
87+
server.close()
88+
}
89+
}

0 commit comments

Comments
 (0)