Skip to content

Commit a031611

Browse files
authored
区分不同平台的 userAgent (#405)
* refactor(build): replace browserify with webpack * fix: stop using global.Buffer to prevent polyfill leancloud/leantodo-weapp#2 * chore: upload sourcemaps to cdn * feat: standardize user agent * chore: upload sourcemaps with correct names
1 parent dff8f70 commit a031611

22 files changed

+184
-197
lines changed

gulpfile.babel.js

Lines changed: 19 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,18 @@ import qiniu from 'qiniu';
88
import fs from 'fs';
99
import gulp from 'gulp';
1010
import clean from 'gulp-clean';
11-
import concat from 'gulp-concat';
12-
import rename from 'gulp-rename';
13-
import uglify from 'gulp-uglify';
14-
import source from 'vinyl-source-stream';
15-
import streamify from 'gulp-streamify';
16-
import browserify from 'browserify';
17-
import browserSync from 'browser-sync';
1811
import babel from 'gulp-babel';
12+
import shell from 'gulp-shell';
1913
import { version } from './package.json';
2014

21-
const reload = browserSync.reload;
22-
23-
// 获取当前版本号
24-
const getAVVersion = () => version;
25-
26-
const uploadCDN = (file, version, cb) => {
15+
const uploadCDN = (file) => {
2716
qiniu.conf.ACCESS_KEY = process.env.CDN_QINIU_KEY;
2817
qiniu.conf.SECRET_KEY = process.env.CDN_QINIU_SECRET;
2918
if (!qiniu.conf.ACCESS_KEY || !qiniu.conf.SECRET_KEY) {
3019
throw new Error('Need Qiniu CDN_QINIU_KEY and CDN_QINIU_SECRET');
3120
}
3221
const bucketname = 'paas_files';
33-
const key = 'static/js/' + path.basename(file, '.js') + '-' + version + '.js';
22+
const key = `static/js/${version}/${path.basename(file)}`;
3423
const putPolicy = new qiniu.rs.PutPolicy(bucketname + ':' + key);
3524
const uptoken = putPolicy.token();
3625
const extra = new qiniu.io.PutExtra();
@@ -42,8 +31,8 @@ const uploadCDN = (file, version, cb) => {
4231
} else {
4332
console.log(err);
4433
}
45-
cb();
4634
});
35+
return file;
4736
};
4837

4938
gulp.task('clean-dist', () => {
@@ -56,47 +45,14 @@ gulp.task('clean-dist', () => {
5645
});
5746

5847
// 编译浏览器版本
59-
gulp.task('bundle-browser', () => {
60-
return browserify({
61-
entries: './src/index.js',
62-
standalone: 'AV'
63-
}).bundle()
64-
.pipe(source('av.js'))
65-
// .pipe(sourcemaps.init())
66-
.pipe(streamify(babel({
67-
compact: false
68-
})))
69-
// .pipe(sourcemaps.write("."))
70-
.pipe(gulp.dest('dist'));
71-
});
48+
gulp.task('bundle-browser', shell.task('npm run build:browser'));
49+
gulp.task('bundle-rn', shell.task('npm run build:rn'));
50+
gulp.task('bundle-weapp', shell.task('npm run build:weapp'));
7251

73-
gulp.task('uglify', ['bundle-browser', 'bundle-weapp'], () => {
74-
return gulp.src([
75-
'dist/av.js',
76-
'dist/av-weapp.js'
77-
])
78-
.pipe(uglify())
79-
.pipe(rename((path) => {
80-
path.basename += '-min';
81-
}))
82-
.pipe(gulp.dest('dist'));
83-
84-
// return gulp.src(['dist/av-es5.js'])
85-
// .pipe(clean());
86-
});
87-
88-
gulp.task('bundle-weapp', () =>
89-
browserify({
90-
entries: './src/index-weapp.js',
91-
standalone: 'AV'
92-
})
93-
.bundle()
94-
.pipe(source('av-weapp.js'))
95-
.pipe(streamify(babel({
96-
compact: false
97-
})))
98-
.pipe(gulp.dest('dist'))
99-
)
52+
gulp.task('uglify', ['bundle-browser', 'bundle-weapp'], shell.task([
53+
'npm run uglify:browser',
54+
'npm run uglify:weapp',
55+
]));
10056

10157
gulp.task('clean-node', () => {
10258
return gulp.src(['dist/node/**/*.*'])
@@ -106,10 +62,7 @@ gulp.task('clean-node', () => {
10662
// 编译出 Node 版本
10763
gulp.task('babel-node', ['clean-node'], () => {
10864
return gulp.src('src/**/*.js')
109-
// .pipe(sourcemaps.init())
11065
.pipe(babel())
111-
// .pipe(concat('av.js'))
112-
// .pipe(sourcemaps.write("."))
11366
.pipe(gulp.dest('dist/node/'));
11467
});
11568

@@ -130,53 +83,21 @@ gulp.task('babel-demo', ['clean-demo'], () => {
13083

13184
// 上传到 CDN
13285
gulp.task('upload', () => {
133-
uploadCDN('./dist/av-min.js', getAVVersion(), () => {});
134-
uploadCDN('./dist/av-weapp-min.js', getAVVersion(), () => {});
135-
uploadCDN('./dist/av.js', getAVVersion(), () => {});
136-
uploadCDN('./dist/av-weapp.js', getAVVersion(), () => {});
86+
[
87+
'./dist/av-min.js',
88+
'./dist/av-weapp-min.js',
89+
'./dist/av.js',
90+
'./dist/av-weapp.js',
91+
].map(uploadCDN).map(file => `${file}.map`).map(uploadCDN);
13792
});
13893

13994
// 生成 release 文件
140-
gulp.task('release', [
141-
// 生成浏览器版本
95+
gulp.task('build', [
14296
'clean-dist',
14397
'bundle-browser',
98+
'bundle-rn',
14499
'bundle-weapp',
145100
'uglify',
146-
// 生成 node 版本
147101
'clean-node',
148102
'babel-node'
149103
]);
150-
151-
// 浏览器开发时使用
152-
gulp.task('dev', [
153-
'clean-dist',
154-
'bundle-browser',
155-
'babel-demo'
156-
], () => {
157-
browserSync({
158-
notify: false,
159-
port: 8888,
160-
server: {
161-
baseDir: ['demo'],
162-
routes: {
163-
'/dist': 'dist'
164-
}
165-
}
166-
});
167-
168-
gulp.watch('src/**/*.js', [
169-
'clean-dist',
170-
'browserify',
171-
'babel-browser',
172-
'uglify'
173-
]);
174-
175-
gulp.watch([
176-
'demo/*.html',
177-
'demo/*.js',
178-
'dist/*.js'
179-
], [
180-
'babel-demo'
181-
]).on('change', reload);
182-
});

package.json

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
"scripts": {
1111
"test": "NODE_ENV=test nyc --reporter lcov --reporter text mocha --timeout 300000 test/index.js",
1212
"docs": "jsdoc src README.md package.json -d docs -c .jsdocrc.json",
13-
"build": "gulp release"
13+
"build:node": "gulp babel-node",
14+
"build:browser": "PLATFORM=Browser webpack --config webpack/browser.js",
15+
"build:rn": "PLATFORM=ReactNative webpack --config webpack/rn.js",
16+
"build:weapp": "PLATFORM=Weapp webpack --config webpack/weapp.js",
17+
"uglify:browser": "cd dist; uglifyjs av.js -m -c -o av-min.js --in-source-map av.js.map --source-map av-min.js.map; cd ..;",
18+
"uglify:weapp": "cd dist; uglifyjs av-weapp.js -m -c -o av-weapp-min.js --in-source-map av-weapp.js.map --source-map av-weapp-min.js.map; cd ..;",
19+
"build": "gulp build"
1420
},
1521
"dependencies": {
1622
"debug": "^2.2.0",
@@ -23,11 +29,10 @@
2329
},
2430
"devDependencies": {
2531
"babel-core": "^6.4.0",
32+
"babel-loader": "^6.2.8",
2633
"babel-plugin-istanbul": "^2.0.0",
2734
"babel-preset-es2015": "^6.3.13",
2835
"babel-register": "^6.14.0",
29-
"browser-sync": "^2.2.1",
30-
"browserify": "^11.0.1",
3136
"docdash": "git+https://github.com/leeyeh/docdash.git#leancloud",
3237
"eslint": "^2.8.0",
3338
"eslint-config-airbnb": "^8.0.0",
@@ -38,49 +43,32 @@
3843
"gulp": "^3.8.10",
3944
"gulp-babel": "^6.1.1",
4045
"gulp-clean": "^0.3.1",
41-
"gulp-concat": "^2.4.3",
42-
"gulp-eslint": "^0.13.2",
43-
"gulp-rename": "^1.2.0",
44-
"gulp-streamify": "^1.0.2",
45-
"gulp-uglify": "^1.0.2",
46+
"gulp-shell": "^0.5.2",
4647
"jsdoc": "~3.4.0",
4748
"mocha": "^3.0.0",
4849
"nyc": "^8.1.0",
4950
"should": "^11.1.0",
50-
"vinyl-source-stream": "^1.1.0",
51-
"weapp-polyfill": "github:leancloud/weapp-polyfill#8efa131"
51+
"uglify-js": "git+https://github.com/Swaagie/UglifyJS2.git#fcb4f2f21584dc5b21af4c10e17733e1686135e4",
52+
"weapp-polyfill": "github:leancloud/weapp-polyfill#8efa131",
53+
"webpack": "^2.1.0-beta.27"
5254
},
5355
"license": "MIT",
5456
"author": {
5557
"name": "LeanCloud",
5658
"email": "[email protected]"
5759
},
5860
"browser": {
59-
"react-native": false,
6061
"./src/uploader/qiniu.js": "./src/uploader/qiniu-browser.js",
61-
"./src/browserify-wrapper/localStorage.js": "./src/browserify-wrapper/localstorage-browser.js",
62-
"./src/browserify-wrapper/parse-base64.js": "./src/browserify-wrapper/parse-base64-browser.js",
63-
"./dist/node/uploader/qiniu.js": "./dist/node/uploader/qiniu-browser.js",
64-
"./dist/node/browserify-wrapper/localStorage.js": "./dist/node/browserify-wrapper/localstorage-browser.js",
65-
"./dist/node/browserify-wrapper/parse-base64.js": "./dist/node/browserify-wrapper/parse-base64-browser.js"
62+
"./src/utils/localstorage.js": "./src/utils/localstorage-browser.js",
63+
"./src/utils/parse-base64.js": "./src/utils/parse-base64-browser.js",
64+
"./src/ua/comments.js": "./src/ua/comments-browser.js",
65+
"./dist/node/index.js": "./dist/av.js"
6666
},
6767
"react-native": {
68-
"react-native": "react-native"
68+
"./src/utils/localstorage.js": "./src/utils/localstorage-rn.js",
69+
"./dist/node/index.js": "./dist/av-rn.js"
6970
},
7071
"typings": "./storage.d.ts",
71-
"eslintConfig": {
72-
"env": {
73-
"es6": true,
74-
"node": true,
75-
"browser": true
76-
},
77-
"rules": {
78-
"quotes": [
79-
2,
80-
"single"
81-
]
82-
}
83-
},
8472
"nyc": {
8573
"require": [
8674
"babel-register"

src/av.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require('underscore');
2+
const userAgent = require('./ua');
23
const {
34
isNullOrUndefined,
45
} = require('./utils');
@@ -24,7 +25,7 @@ _.extend(AVConfig, {
2425
disableCurrentUser: false,
2526

2627
// Internal config can modifie the UserAgent
27-
userAgent: null,
28+
userAgent,
2829

2930
// set production environment or test environment
3031
// 1: production environment, 0: test environment, null: default environment

src/browserify-wrapper/localstorage-browser.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/file.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ module.exports = function(AV) {
342342
data = { base64: encodeBase64(data) };
343343
}
344344
if (data && data.base64) {
345-
var parseBase64 = require('./browserify-wrapper/parse-base64');
345+
var parseBase64 = require('./utils/parse-base64');
346346
var dataBase64 = parseBase64(data.base64, guessedType);
347347
this.attributes.base64 = dataURLToBase64(data.base64);
348348
this._source = Promise.resolve({ data: dataBase64, type: guessedType });
@@ -351,13 +351,12 @@ module.exports = function(AV) {
351351
data.blob.type = guessedType;
352352
}
353353
this._source = Promise.resolve({ data: data.blob, type: guessedType });
354-
} else if (typeof File !== "undefined" && data instanceof global.File) {
354+
} else if (typeof File !== "undefined" && data instanceof File) {
355355
if (data.size) {
356356
this.attributes.metaData.size = data.size;
357357
}
358358
this._source = Promise.resolve({ data, type: guessedType });
359-
} else if (typeof global.Buffer !== "undefined" && global.Buffer.isBuffer(data)) {
360-
// use global.Buffer to prevent browserify pack Buffer module
359+
} else if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
361360
this.attributes.metaData.size = data.length;
362361
this._source = Promise.resolve({ data, type: guessedType });
363362
} else if (_.isString(data)) {

src/localstorage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var _ = require('underscore');
22
var Promise = require('./promise');
3-
var localStorage = require('./browserify-wrapper/localStorage');
3+
var localStorage = require('./utils/localstorage');
44

55
var syncApiNames = [
66
'getItem',

src/request.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,7 @@ const setHeaders = (authOptions = {}) => {
124124
if (AV._config.applicationProduction !== null) {
125125
headers['X-LC-Prod'] = AV._config.applicationProduction;
126126
}
127-
if (!AV._config.isNode) {
128-
headers['X-LC-UA'] = `AV/${AV.version}`;
129-
} else {
130-
// LeanEngine need use AV._config.userAgent
131-
headers['User-Agent'] = AV._config.userAgent || `AV/${AV.version}; Node.js/${process.version}`;
132-
}
127+
headers[AV._config.isNode ? 'User-Agent' : 'X-LC-UA'] = AV._config.userAgent;
133128

134129
return Promise.resolve().then(() => {
135130
// Pass the session token

src/ua/comments-browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [];

src/ua/comments.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [`Node.js/${process.version}`];

src/ua/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const version = require('../version');
2+
const comments = [process.env.PLATFORM || 'Node.js'].concat(require('./comments'));
3+
4+
module.exports = `LeanCloud-JS-SDK/${version} (${comments.join('; ')})`;

0 commit comments

Comments
 (0)