Skip to content

Commit a80bacb

Browse files
authored
refactor: remove AV._config.isNode (#421)
use conditional compile instead
1 parent 3ccf314 commit a80bacb

File tree

7 files changed

+13
-19
lines changed

7 files changed

+13
-19
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
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",
1313
"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",
14+
"build:browser": "CLIENT_PLATFORM=Browser webpack --config webpack/browser.js",
15+
"build:rn": "CLIENT_PLATFORM=ReactNative webpack --config webpack/rn.js",
16+
"build:weapp": "CLIENT_PLATFORM=Weapp webpack --config webpack/weapp.js",
1717
"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 ..;",
1818
"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 ..;",
1919
"build": "gulp build"

src/av.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ _.extend(AVConfig, {
1818
// 服务器的 URL,默认初始化时被设置为大陆节点地址
1919
APIServerURL: AVConfig.APIServerURL || '',
2020

21-
// 当前是否为 nodejs 环境
22-
isNode: false,
23-
2421
// 禁用 currentUser,通常用于多用户环境
2522
disableCurrentUser: false,
2623

@@ -37,11 +34,6 @@ _.extend(AVConfig, {
3734
* @namespace AV
3835
*/
3936

40-
// Check whether we are running in Node.js.
41-
if (typeof(process) !== 'undefined' && process.versions && process.versions.node) {
42-
AVConfig.isNode = true;
43-
}
44-
4537
// Helpers
4638
// -------
4739

src/file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ module.exports = function(AV) {
132132
if (!data.blob.type) {
133133
data.blob.type = mimeType;
134134
}
135-
if (process.env.PLATFORM === 'ReactNative' || process.env.PLATFORM === 'Weapp') {
135+
if (process.env.CLIENT_PLATFORM === 'ReactNative' || process.env.CLIENT_PLATFORM === 'Weapp') {
136136
this._extName = extname(data.blob.uri);
137137
}
138138
this._source = Promise.resolve({ data: data.blob, type: mimeType });

src/init.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const initialize = (appId, appKey, masterKey, hookKey) => {
88
AV.applicationId = appId;
99
AV.applicationKey = appKey;
1010
AV.masterKey = masterKey;
11-
AV.hookKey = hookKey || (AV._config.isNode && process.env.LEANCLOUD_APP_HOOK_KEY);
11+
if (!process.env.CLIENT_PLATFORM) {
12+
AV.hookKey = hookKey || process.env.LEANCLOUD_APP_HOOK_KEY;
13+
}
1214
AV._useMasterKey = false;
1315
};
1416

@@ -31,7 +33,7 @@ AV.init = (...args) => {
3133
case 1:
3234
const options = args[0];
3335
if (typeof options === 'object') {
34-
if (!AV._config.isNode && options.masterKey) {
36+
if (process.env.CLIENT_PLATFORM && options.masterKey) {
3537
masterKeyWarn();
3638
}
3739
initialize(options.appId, options.appKey, options.masterKey, options.hookKey);
@@ -47,7 +49,7 @@ AV.init = (...args) => {
4749
console.warn('Please use AV.init() to replace AV.initialize(), ' +
4850
'AV.init() need an Object param, like { appId: \'YOUR_APP_ID\', appKey: \'YOUR_APP_KEY\' } . ' +
4951
'Docs: https://leancloud.cn/docs/sdk_setup-js.html');
50-
if (!AV._config.isNode && args.length === 3) {
52+
if (process.env.CLIENT_PLATFORM && args.length === 3) {
5153
masterKeyWarn();
5254
}
5355
initialize(...args);
@@ -57,7 +59,7 @@ AV.init = (...args) => {
5759
};
5860

5961
// If we're running in node.js, allow using the master key.
60-
if (AV._config.isNode) {
62+
if (!process.env.CLIENT_PLATFORM) {
6163
AV.Cloud = AV.Cloud || {};
6264
/**
6365
* Switches the LeanCloud SDK to using the Master key. The Master key grants

src/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const setHeaders = (authOptions = {}, signKey) => {
139139
if (AV._config.applicationProduction !== null) {
140140
headers['X-LC-Prod'] = String(AV._config.applicationProduction);
141141
}
142-
headers[AV._config.isNode ? 'User-Agent' : 'X-LC-UA'] = AV._config.userAgent;
142+
headers[!process.env.CLIENT_PLATFORM ? 'User-Agent' : 'X-LC-UA'] = AV._config.userAgent;
143143

144144
return Promise.resolve().then(() => {
145145
// Pass the session token

src/ua/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const version = require('../version');
2-
const comments = [process.env.PLATFORM || 'Node.js'].concat(require('./comments'));
2+
const comments = [process.env.CLIENT_PLATFORM || 'Node.js'].concat(require('./comments'));
33

44
module.exports = `LeanCloud-JS-SDK/${version} (${comments.join('; ')})`;

webpack/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = function() {
3434
},
3535
plugins: [
3636
new webpack.EnvironmentPlugin([
37-
"PLATFORM"
37+
"CLIENT_PLATFORM"
3838
])
3939
]
4040
}

0 commit comments

Comments
 (0)