Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 8fe8c99

Browse files
refactor: Convert entry points to es module and create minified bundles (optimizely#445)
Summary: This is the first step in an effort to convert javascript SDK from Common JS to ES Module. The changes include 1. Convert all the entry point files to ES Module. 2. Minified Common JS bundles are being created for each entry point using Rollup and Terser main: dist/optimizely.node.min.js browser: dist/optimizely.browser.min.js react-native: dist/optimizely.react_native.min.js 3. All the bundles including umd bundle are generated using Rollup and terser. Webpack is only being used to keep the Karma tests running. Why Webpack for Karma? We tried using Rollup with Karma for the same purpose but couldnt run it despite trying many times. To not block progress on the ES module conversion, we recommend moving forward with using webpack to keep running karma tests when all the bundles are being created by Rollup and terser. We can revisit and modify Karma to use Rollup later. Note about Testapp: This PR uses a specific branch of javascript-testapp. This change is required because now testapp has to run an additional command to build the bundles which was not the case earlier. This testapp PR needs to be merged before merging this SDK branch. https://github.com/optimizely/javascript-testapp/pull/83 Test plan: All unit tests and Full stack compatibility suite tests pass. Co-authored-by: Zeeshan Ashraf <zashraf@folio3.com> Co-authored-by: zashraf1985 <35262377+zashraf1985@users.noreply.github.com>
1 parent fd18bfe commit 8fe8c99

13 files changed

+997
-1322
lines changed

packages/optimizely-sdk/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = {
1111
Promise: 'readonly',
1212
},
1313
parserOptions: {
14-
ecmaVersion: 5,
14+
ecmaVersion: 6,
15+
sourceType: 'module',
1516
},
1617
rules: {
1718
'no-prototype-builtins': 'off',

packages/optimizely-sdk/karma.base.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2018, Optimizely
2+
* Copyright 2018, 2020 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 141 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, 2019, Optimizely
2+
* Copyright 2016-2017, 2019-2020 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,146 +13,166 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var logging = require('@optimizely/js-sdk-logging');
17-
var fns = require('./utils/fns');
18-
var configValidator = require('./utils/config_validator');
19-
var defaultErrorHandler = require('./plugins/error_handler');
20-
var defaultEventDispatcher = require('./plugins/event_dispatcher/index.browser');
21-
var enums = require('./utils/enums');
22-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
23-
var loggerPlugin = require('./plugins/logger');
24-
var Optimizely = require('./optimizely');
25-
var eventProcessorConfigValidator = require('./utils/event_processor_config_validator');
26-
27-
var logger = logging.getLogger();
28-
logging.setLogHandler(loggerPlugin.createLogger());
29-
logging.setLogLevel(logging.LogLevel.INFO);
16+
import {
17+
getLogger,
18+
setLogHandler,
19+
setLogLevel,
20+
setErrorHandler,
21+
getErrorHandler,
22+
LogLevel,
23+
} from '@optimizely/js-sdk-logging';
24+
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor';
25+
26+
import fns from './utils/fns';
27+
import configValidator from './utils/config_validator';
28+
import defaultErrorHandler from './plugins/error_handler';
29+
import defaultEventDispatcher from './plugins/event_dispatcher/index.browser';
30+
import enums from './utils/enums';
31+
import loggerPlugin from './plugins/logger';
32+
import Optimizely from './optimizely';
33+
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
34+
35+
var logger = getLogger();
36+
setLogHandler(loggerPlugin.createLogger());
37+
setLogLevel(LogLevel.INFO);
3038

3139
var MODULE_NAME = 'INDEX_BROWSER';
32-
3340
var DEFAULT_EVENT_BATCH_SIZE = 10;
3441
var DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
3542

3643
var hasRetriedEvents = false;
44+
3745
/**
38-
* Entry point into the Optimizely Browser SDK
46+
* Creates an instance of the Optimizely class
47+
* @param {Object} config
48+
* @param {Object} config.datafile
49+
* @param {Object} config.errorHandler
50+
* @param {Object} config.eventDispatcher
51+
* @param {Object} config.logger
52+
* @param {Object} config.logLevel
53+
* @param {Object} config.userProfileService
54+
* @param {Object} config.eventBatchSize
55+
* @param {Object} config.eventFlushInterval
56+
* @return {Object} the Optimizely object
3957
*/
40-
module.exports = {
41-
logging: loggerPlugin,
42-
errorHandler: defaultErrorHandler,
43-
eventDispatcher: defaultEventDispatcher,
44-
enums: enums,
58+
var createInstance = function(config) {
59+
try {
60+
config = config || {};
4561

46-
setLogger: logging.setLogHandler,
47-
setLogLevel: logging.setLogLevel,
48-
49-
/**
50-
* Creates an instance of the Optimizely class
51-
* @param {Object} config
52-
* @param {Object} config.datafile
53-
* @param {Object} config.errorHandler
54-
* @param {Object} config.eventDispatcher
55-
* @param {Object} config.logger
56-
* @param {Object} config.logLevel
57-
* @param {Object} config.userProfileService
58-
* @param {Object} config.eventBatchSize
59-
* @param {Object} config.eventFlushInterval
60-
* @return {Object} the Optimizely object
61-
*/
62-
createInstance: function(config) {
63-
try {
64-
config = config || {};
62+
// TODO warn about setting per instance errorHandler / logger / logLevel
63+
if (config.errorHandler) {
64+
setErrorHandler(config.errorHandler);
65+
}
66+
if (config.logger) {
67+
setLogHandler(config.logger);
68+
// respect the logger's shouldLog functionality
69+
setLogLevel(LogLevel.NOTSET);
70+
}
71+
if (config.logLevel !== undefined) {
72+
setLogLevel(config.logLevel);
73+
}
6574

66-
// TODO warn about setting per instance errorHandler / logger / logLevel
67-
if (config.errorHandler) {
68-
logging.setErrorHandler(config.errorHandler);
69-
}
70-
if (config.logger) {
71-
logging.setLogHandler(config.logger);
72-
// respect the logger's shouldLog functionality
73-
logging.setLogLevel(logging.LogLevel.NOTSET);
74-
}
75-
if (config.logLevel !== undefined) {
76-
logging.setLogLevel(config.logLevel);
77-
}
75+
try {
76+
configValidator.validate(config);
77+
config.isValidInstance = true;
78+
} catch (ex) {
79+
logger.error(ex);
80+
config.isValidInstance = false;
81+
}
7882

79-
try {
80-
configValidator.validate(config);
81-
config.isValidInstance = true;
82-
} catch (ex) {
83-
logger.error(ex);
84-
config.isValidInstance = false;
83+
var eventDispatcher;
84+
// prettier-ignore
85+
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
86+
// only wrap the event dispatcher with pending events retry if the user didnt override
87+
eventDispatcher = new LocalStoragePendingEventsDispatcher({
88+
eventDispatcher: defaultEventDispatcher,
89+
});
90+
91+
if (!hasRetriedEvents) {
92+
eventDispatcher.sendPendingEvents();
93+
hasRetriedEvents = true;
8594
}
95+
} else {
96+
eventDispatcher = config.eventDispatcher;
97+
}
8698

87-
var eventDispatcher;
88-
// prettier-ignore
89-
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
90-
// only wrap the event dispatcher with pending events retry if the user didnt override
91-
eventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({
92-
eventDispatcher: defaultEventDispatcher,
93-
});
94-
95-
if (!hasRetriedEvents) {
96-
eventDispatcher.sendPendingEvents();
97-
hasRetriedEvents = true;
98-
}
99-
} else {
100-
eventDispatcher = config.eventDispatcher;
99+
config = fns.assign(
100+
{
101+
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
102+
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
103+
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
104+
},
105+
config,
106+
{
107+
eventDispatcher: eventDispatcher,
108+
// always get the OptimizelyLogger facade from logging
109+
logger: logger,
110+
errorHandler: getErrorHandler(),
101111
}
112+
);
102113

103-
config = fns.assign(
104-
{
105-
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
106-
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
107-
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
108-
},
109-
config,
110-
{
111-
eventDispatcher: eventDispatcher,
112-
// always get the OptimizelyLogger facade from logging
113-
logger: logger,
114-
errorHandler: logging.getErrorHandler(),
115-
}
114+
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
115+
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
116+
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
117+
}
118+
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
119+
logger.warn(
120+
'Invalid eventFlushInterval %s, defaulting to %s',
121+
config.eventFlushInterval,
122+
DEFAULT_EVENT_FLUSH_INTERVAL
116123
);
124+
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
125+
}
117126

118-
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
119-
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
120-
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
121-
}
122-
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
123-
logger.warn(
124-
'Invalid eventFlushInterval %s, defaulting to %s',
125-
config.eventFlushInterval,
126-
DEFAULT_EVENT_FLUSH_INTERVAL
127-
);
128-
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
129-
}
127+
var optimizely = new Optimizely(config);
130128

131-
var optimizely = new Optimizely(config);
132-
133-
try {
134-
if (typeof window.addEventListener === 'function') {
135-
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
136-
window.addEventListener(
137-
unloadEvent,
138-
function() {
139-
optimizely.close();
140-
},
141-
false
142-
);
143-
}
144-
} catch (e) {
145-
logger.error(enums.LOG_MESSAGES.UNABLE_TO_ATTACH_UNLOAD, MODULE_NAME, e.message);
129+
try {
130+
if (typeof window.addEventListener === 'function') {
131+
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
132+
window.addEventListener(
133+
unloadEvent,
134+
function() {
135+
optimizely.close();
136+
},
137+
false
138+
);
146139
}
147-
148-
return optimizely;
149140
} catch (e) {
150-
logger.error(e);
151-
return null;
141+
logger.error(enums.LOG_MESSAGES.UNABLE_TO_ATTACH_UNLOAD, MODULE_NAME, e.message);
152142
}
153-
},
154143

155-
__internalResetRetryState: function() {
156-
hasRetriedEvents = false;
157-
},
144+
return optimizely;
145+
} catch (e) {
146+
logger.error(e);
147+
return null;
148+
}
149+
};
150+
151+
var __internalResetRetryState = function() {
152+
hasRetriedEvents = false;
153+
};
154+
155+
/**
156+
* Entry point into the Optimizely Browser SDK
157+
*/
158+
export {
159+
loggerPlugin as logging,
160+
defaultErrorHandler as errorHandler,
161+
defaultEventDispatcher as eventDispatcher,
162+
enums,
163+
setLogHandler as setLogger,
164+
setLogLevel,
165+
createInstance,
166+
__internalResetRetryState,
167+
}
168+
169+
export default {
170+
logging: loggerPlugin,
171+
errorHandler: defaultErrorHandler,
172+
eventDispatcher: defaultEventDispatcher,
173+
enums: enums,
174+
setLogger: setLogHandler,
175+
setLogLevel: setLogLevel,
176+
createInstance: createInstance,
177+
__internalResetRetryState: __internalResetRetryState,
158178
};

packages/optimizely-sdk/lib/index.browser.tests.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019, Optimizely
2+
* Copyright 2016-2020 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,19 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var logging = require('@optimizely/js-sdk-logging');
17-
var configValidator = require('./utils/config_validator');
18-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
19-
var Optimizely = require('./optimizely');
20-
var optimizelyFactory = require('./index.browser');
21-
var packageJSON = require('../package.json');
22-
var testData = require('./tests/test_data');
23-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
24-
var eventProcessorConfigValidator = require('./utils/event_processor_config_validator');
25-
26-
var chai = require('chai');
27-
var assert = chai.assert;
28-
var sinon = require('sinon');
16+
import { assert } from 'chai';
17+
import sinon from 'sinon';
18+
import * as logging from '@optimizely/js-sdk-logging';
19+
import * as eventProcessor from '@optimizely/js-sdk-event-processor';
20+
21+
import Optimizely from './optimizely';
22+
import testData from './tests/test_data';
23+
import packageJSON from '../package.json';
24+
import optimizelyFactory from './index.browser';
25+
import configValidator from './utils/config_validator';
26+
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
2927

3028
var LocalStoragePendingEventsDispatcher = eventProcessor.LocalStoragePendingEventsDispatcher;
3129

@@ -57,7 +55,6 @@ describe('javascript-sdk', function() {
5755
sinon.stub(configValidator, 'validate');
5856

5957
xhr = sinon.useFakeXMLHttpRequest();
60-
global.XMLHttpRequest = xhr;
6158
requests = [];
6259
xhr.onCreate = function(req) {
6360
requests.push(req);

0 commit comments

Comments
 (0)