Skip to content

Commit 4becebc

Browse files
committed
init
0 parents  commit 4becebc

File tree

240 files changed

+5435
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

240 files changed

+5435
-0
lines changed

.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"sourceMap": "inline",
3+
"presets": ["es2015"],
4+
"plugins": [
5+
"add-module-exports"
6+
]
7+
}
8+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dest

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

Readme.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# wept-build
2+
3+
提够构建 WEPT 使用小程序代码的 API,支持 cache 以及按需更新指定文件。
4+
5+
## API
6+
7+
### new Builder(option)
8+
9+
* `option` 为 builder 设置:
10+
* `option.cache` 为 cache 对象,需提供 get, del, set 方法,默认为 null
11+
* `option.root` 为程序根路径,默认为 `process.cwd()`
12+
13+
```
14+
new builder({
15+
root: './demo',
16+
cache: {
17+
get: function(key) {
18+
return myCache.get(key)
19+
},
20+
del, function(key) {
21+
return myCache.del(key)
22+
}
23+
set: function(key, value) {
24+
myCache.set(key, value)
25+
}
26+
}
27+
})
28+
```
29+
30+
### .buildAll()
31+
32+
编译 root 目录下所有小程序文件,返回 promise 或者错误,用于提前编译发现错误
33+
34+
35+
### .buildConfig()
36+
37+
返回所有配置信息合并后对象
38+
39+
* 返回为 Promise,回调携带 `app.json` `wept.json` 合并的 config 对象或者错误
40+
* 返回 config 对象同时携带了所有 page.json 文件内的信息
41+
42+
### builder.buildWxml(path)
43+
### builder.buildWxss(path)
44+
45+
编译对应页面的 wxml 和 wxss 文件
46+
47+
* `path` 为文件相对 root 的路径或绝对路径,可以携带或者不带文件后缀名
48+
* 返回为 Promise,编译错误或者文件不存在返回错误
49+
* 对于 wxml 返回的是用于插入 html 的 JavaScript 代码字符串
50+
* 对于 wxss 返回的是用于通过 `deviceWidth` 和 `devicePixelRatio` 生成 css 的函数
51+
52+
### builder.buildJavascript([option])
53+
54+
* `option.ignore` 可指定为忽略文件路径的正则,默认为 `/node_modules/`
55+
56+
57+
### builder.rebuild(file, option)
58+
59+
更新编译指定 `root` 下指定文件, 仅在使用缓存时有效
60+
61+
* `file` 为文件对 root 的相对路径或绝对路径
62+
* `option` 为 buildJavascript 使用的 option
63+
* 返回 Promise
64+
65+
66+
## LICENSE
67+
68+
Copyright 2016 [email protected]
69+
70+
Permission is hereby granted, free of charge, to any person obtaining
71+
a copy of this software and associated documentation files (the "Software"),
72+
to deal in the Software without restriction, including without limitation
73+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
74+
and/or sell copies of the Software, and to permit persons to whom the
75+
Software is furnished to do so, subject to the following conditions:
76+
77+
The above copyright notice and this permission notice shall be included
78+
in all copies or substantial portions of the Software.
79+
80+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
81+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
82+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
83+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
84+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
85+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
86+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bin/wcc

1.53 MB
Binary file not shown.

bin/wcc.exe

1.45 MB
Binary file not shown.

bin/wcsc

1.15 MB
Binary file not shown.

bin/wcsc.exe

1.26 MB
Binary file not shown.

gulpfile.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var gulp = require('gulp')
2+
var babel = require('gulp-babel')
3+
var sourcemaps = require('gulp-sourcemaps');
4+
5+
gulp.task('build', function () {
6+
return gulp.src('src/*.js')
7+
.pipe(sourcemaps.init())
8+
.pipe(babel())
9+
.pipe(sourcemaps.write())
10+
.pipe(gulp.dest('dest'))
11+
})
12+
13+
gulp.task('watch', function () {
14+
gulp.watch('src/*.js', ['build'])
15+
})
16+
17+
18+
gulp.task('default', ['build', 'watch'])

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dest/index')

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "wept-build",
3+
"version": "0.1.0",
4+
"description": "builder for weapp",
5+
"main": "index.js",
6+
"scripts": {
7+
"prepublish": "gulp build",
8+
"test": "ava"
9+
},
10+
"ava": {
11+
"files": [
12+
"test/*.js"
13+
],
14+
"concurrency": 20
15+
},
16+
"author": "[email protected]",
17+
"license": "MIT",
18+
"devDependencies": {
19+
"ava": "^0.17.0",
20+
"babel-plugin-add-module-exports": "^0.2.1",
21+
"gulp": "^3.9.1",
22+
"gulp-babel": "^6.1.2",
23+
"gulp-sourcemaps": "^2.2.0",
24+
"gulp-util": "^3.0.8"
25+
},
26+
"dependencies": {
27+
"babel-core": "^6.21.0",
28+
"babel-preset-es2015": "^6.18.0",
29+
"chalk": "^1.1.3",
30+
"concat-with-sourcemaps": "^1.0.4",
31+
"convert-source-map": "^1.3.0",
32+
"glob": "^7.1.1",
33+
"merge": "^1.2.0",
34+
"node-parallel": "^0.1.6",
35+
"source-map": "^0.5.6"
36+
}
37+
}

src/builder.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export {default as buildWxml} from './wxml'
2+
export {default as buildWxss} from './wxss'
3+
export {default as buildConfig} from './config'
4+
export {default as buildJavascript} from './javascript'

src/config.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import merge from 'merge'
2+
import Parallel from 'node-parallel'
3+
import path from 'path'
4+
import {readFile, logError} from './util'
5+
6+
let default_config = {
7+
"debug": false,
8+
"appname": "debug",
9+
"window":{
10+
"backgroundTextStyle":"light",
11+
"navigationBarBackgroundColor": "#fff",
12+
"navigationBarTitleText": "WeChat",
13+
"navigationBarTextStyle":"black"
14+
},
15+
"projectConfig": {
16+
"Network": {
17+
"RequestDomain": [],
18+
"WsRequestDomain": [],
19+
"UploadDomain": [],
20+
"DownloadDomain": []
21+
},
22+
"Setting": {
23+
"MaxLocalstorageSize": 10,
24+
"MaxCodeSize": 5,
25+
"MaxWebviewDepth": 5,
26+
"MaxBackgroundLifespan": 300,
27+
"MaxRequestConcurrent": 5,
28+
"MaxUploadConcurrent": 1,
29+
"MaxDownloadConcurrent": 5
30+
}
31+
},
32+
"appserviceConfig": {
33+
"AppserviceMaxDataSize": 1048576,
34+
"HTTPSetting": {
35+
"HTTPHeaderMode": "BlackList",
36+
"HeaderBlackList": [
37+
"User-Agent"
38+
],
39+
"HeaderWhiteList": [],
40+
"UploadMaxTimeoutMS": 60000,
41+
"DownloadMaxTimeoutMS": 60000,
42+
"WebsocketMaxTimeoutMS": 60000,
43+
"RequestMaxTimeoutMS": 60000,
44+
"HTTPHeaderReferer": "servicewechat.com"
45+
},
46+
"CDNBaseURL": "https://res.wx.qq.com/weapp",
47+
"AppMaxRunningCount": 5
48+
},
49+
"apphash": 70475629,
50+
"userInfo": {
51+
"headUrl": "https://s-media-cache-ak0.pinimg.com/136x136/7f/f7/b9/7ff7b921190bc4c05a1f3c11ff2ce086.jpg",
52+
"city": "Chaoyang",
53+
"gender": 1,
54+
"nickName": "测试帐号",
55+
"province" : "Beijing"
56+
}
57+
}
58+
59+
export default function (root) {
60+
root = root || process.cwd()
61+
62+
return new Promise(function (resolve, reject) {
63+
let p = new Parallel()
64+
p.add(done => {
65+
readFile('./app.json', root).then(content => {
66+
try {
67+
let config = JSON.parse(content)
68+
if (!config.pages || !config.pages.length) return done(new Error('No pages found'))
69+
config.root = config.root || config.pages[0]
70+
done(null, config)
71+
} catch(e) {
72+
return done(new Error(`Parse error of ${path.resolve(root, './app.json')}`))
73+
}
74+
}, done)
75+
})
76+
p.add(done => {
77+
readFile('./wept.json', root).then(content => {
78+
try {
79+
let config = JSON.parse(content)
80+
done(null, config)
81+
} catch(e) {
82+
return done(new Error(`Parse error of ${path.resolve(root, './wept.json')}`))
83+
}
84+
}, err => {
85+
if (/not\sfound/.test(err.message)) return done()
86+
done(err)
87+
})
88+
})
89+
p.done((err,results) => {
90+
if (err) return reject(err)
91+
let appConfig = results[0]
92+
let weptConfig = results[1]
93+
let config = merge.recursive(true, default_config, appConfig)
94+
config.directory = root
95+
config.babel = true
96+
config['window'] = config['window'] || {}
97+
config = merge.recursive(true, config, weptConfig)
98+
config.appid = config.appid || 'touristappid'
99+
config.projectConfig.appid = config.appid
100+
config.isTourist = config.appid == 'touristappid'
101+
loadJsonFiles(config.pages, root).then(obj => {
102+
config['window'].pages = obj
103+
resolve(config)
104+
}, reject)
105+
})
106+
})
107+
}
108+
109+
function loadJsonFiles(pages, root) {
110+
let p = new Parallel()
111+
let res = {}
112+
return new Promise((resolve, reject) => {
113+
for (let page of pages) {
114+
p.add(cb => {
115+
let p = `${page}.json`
116+
readFile(p, root).then(content => {
117+
try {
118+
res[page] = JSON.parse(content)
119+
} catch (e) {
120+
logError(`${path.resolve(root, p)} JSON 解析失败,请检查`)
121+
}
122+
return cb()
123+
}, () => {
124+
// ignore not exist or couldn't open error
125+
cb()
126+
})
127+
})
128+
}
129+
p.done(err => {
130+
if (err) return reject(err)
131+
resolve(res)
132+
})
133+
})
134+
}

0 commit comments

Comments
 (0)