Skip to content

Commit 87348be

Browse files
committed
add getViewScript and getServiceScript
1 parent 86a0c05 commit 87348be

File tree

12 files changed

+13734
-0
lines changed

12 files changed

+13734
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
dest
2+
*.diff
3+
.offcial

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
src
2+
*.map
3+
.offcial

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
diff:
2+
@colordiff -r static .offcial
3+
4+
update:
5+
@cp ~/Library/Application\ Support/微信web开发者工具/WeappVendor/* ./.offcial
6+
@cp /Applications/wechatwebdevtools.app/Contents/Resources/app.nw/app/dist/inject/jweixindebug.js ./.offcial
7+
@cp /Applications/wechatwebdevtools.app/Contents/Resources/app.nw/app/dist/weapp/appservice/asdebug.js ./.offcial
8+
@find ./.offcial/ -type f -name '*.js' -exec js-beautify -r -s 2 -p -f '{}' \;
9+
10+
.PHONY: update diff

Readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@
6363
6464
编译 root 目录下所有小程序文件,返回 promise 或者错误,用于提前编译 & 发现错误
6565
66+
### Builder.getViewScript([option])
67+
### Builder.getServiceScript([option])
68+
69+
获取 view 层和 service 层小程序底层代码,返回 content 和 verion,例如:
70+
71+
``` js
72+
{
73+
content: '...' // script content
74+
version: '2016122200'
75+
}
76+
```
77+
78+
* `option.sourceMap` 启用后开启 source map 支持。
79+
6680
## LICENSE
6781

6882
Copyright 2016 [email protected]

src/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'path'
55
import * as builder from './builder'
66
import fs from 'fs'
77
import EventEmitter from 'events'
8+
import {readFile, concatFiles} from './util'
89

910
export default class Builder extends EventEmitter {
1011

@@ -175,4 +176,46 @@ export default class Builder extends EventEmitter {
175176
return Promise.all(arr)
176177
})
177178
}
179+
180+
static loadVersion() {
181+
return readFile(path.resolve(__dirname, '../static/version.json')).then(content => {
182+
try {
183+
var obj = JSON.parse(content)
184+
return obj
185+
} catch(e) {
186+
console.error(e.stack)
187+
return Promise.reject(e)
188+
}
189+
})
190+
}
191+
192+
static getViewScript(opt = {}) {
193+
return Builder.loadVersion().then(obj => {
194+
let paths = [
195+
path.resolve(__dirname, '../static/jweixindebug.js'),
196+
path.resolve(__dirname, '../static/WAWebview.js')
197+
]
198+
return concatFiles(paths, opt.sourceMap, 'view.js').then(content => {
199+
return {
200+
content: content,
201+
version: obj['WAWebview.js']
202+
}
203+
})
204+
})
205+
}
206+
207+
static getServiceScript(opt = {}) {
208+
return Builder.loadVersion().then(obj => {
209+
let paths = [
210+
path.resolve(__dirname, '../static/asdebug.js'),
211+
path.resolve(__dirname, '../static/WAService.js')
212+
]
213+
return concatFiles(paths, opt.sourceMap, 'service.js').then(content => {
214+
return {
215+
content: content,
216+
version: obj['WAService.js']
217+
}
218+
})
219+
})
220+
}
178221
}

src/util.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import convert from 'convert-source-map'
12
import Concat from 'concat-with-sourcemaps'
23
import babel from 'babel-core'
34
import Parallel from 'node-parallel'
@@ -32,6 +33,31 @@ export function readFile(p, root) {
3233
})
3334
}
3435

36+
export function concatFiles(paths, sourceMap, name) {
37+
const p = new Parallel()
38+
for (let f of paths) {
39+
p.add(done => {
40+
readFile(f, '').then(c => {
41+
done(null, c)
42+
}, done)
43+
})
44+
}
45+
return new Promise((resolve, reject) => {
46+
p.done((err, results) => {
47+
if (err) return reject(err)
48+
if (!sourceMap) {
49+
return resolve(results.join('\n'))
50+
}
51+
let concat = new Concat(true, name, '\n')
52+
results.forEach((c, i) => {
53+
concat.add(paths[i], c)
54+
})
55+
let str = concat.content + "\n" + convert.fromJSON(concat.sourceMap).toComment()
56+
resolve(str)
57+
})
58+
})
59+
}
60+
3561
export function logError(message) {
3662
console.log(chalk.red(message))
3763
}

0 commit comments

Comments
 (0)