Skip to content

Commit deaf967

Browse files
committed
wip
1 parent 083562f commit deaf967

7 files changed

Lines changed: 252 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ lerna-debug.log
66

77
coverage/
88
dist/
9+
.build/
910

1011
# user-specific ignores ought to be defined in user's `core.excludesfile`
1112
.idea/*

lib/index.browser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { Platform } from './platform_support';
2727
* null on error
2828
*/
2929

30-
3130
export const createInstance = function(config: Config): Client {
3231
const client = getOptimizelyInstance({
3332
...config,

package-lock.json

Lines changed: 31 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
}
5555
},
5656
"scripts": {
57-
"clean": "rm -rf dist",
58-
"clean:win": "(if exist dist rd /s/q dist)",
57+
"clean": "rm -rf dist .build",
58+
"clean:win": "(if exist dist rd /s/q dist) && (if exist .build rd /s/q .build)",
5959
"lint": "tsc --noEmit && eslint 'lib/**/*.js' 'lib/**/*.ts'",
6060
"validate-platform-isolation": "./scripts/platform-validator.js --validate",
6161
"fix-platform-export": "./scripts/platform-validator.js --fix-export",
@@ -75,9 +75,16 @@
7575
"test-umdbrowser": "npm run build-browser-umd && karma start karma.umd.conf.js --single-run",
7676
"test-karma-local": "karma start karma.local_chrome.bs.conf.js && npm run build-browser-umd && karma start karma.local_chrome.umd.conf.js",
7777
"prebuild": "npm run clean",
78+
<<<<<<< HEAD
7879
"build": "npm run validate-platform-isolation && tsc --noEmit && npm run genmsg && rollup -c && cp dist/index.browser.d.ts dist/index.d.ts",
7980
"build:win": "tsc --noEmit && npm run genmsg && rollup -c && type nul > dist/optimizely.lite.es.d.ts && type nul > dist/optimizely.lite.es.min.d.ts && type nul > dist/optimizely.lite.min.d.ts",
8081
"build-browser-umd": "npm run validate-platform-isolation && tsc --noEmit && npm run genmsg && rollup -c --config-umd",
82+
=======
83+
"compile": "tsc -p tsconfig.build.json",
84+
"build": "npm run validate-platform-isolation && npm run genmsg && npm run compile && rollup -c rollup.config.new.js && cp dist/index.browser.d.ts dist/index.d.ts",
85+
"build:win": "tsc --noEmit && npm run genmsg && npm run compile && rollup -c rollup.config.new.js && type nul > dist/optimizely.lite.es.d.ts && type nul > dist/optimizely.lite.es.min.d.ts && type nul > dist/optimizely.lite.min.d.ts",
86+
"build-browser-umd": "npm run compile && rollup -c rollup.config.new.js --config-umd",
87+
>>>>>>> dddfddaf (wip)
8188
"coveralls": "nyc --reporter=lcov npm test",
8289
"prepare": "npm run build",
8390
"prepublishOnly": "npm test",
@@ -108,6 +115,7 @@
108115
"devDependencies": {
109116
"@react-native-async-storage/async-storage": "^2",
110117
"@react-native-community/netinfo": "^11.3.2",
118+
"@rollup/plugin-alias": "^3.1.9",
111119
"@rollup/plugin-commonjs": "^11.0.2",
112120
"@rollup/plugin-node-resolve": "^7.1.1",
113121
"@types/chai": "^4.2.11",
@@ -149,11 +157,12 @@
149157
"ts-loader": "^9.3.1",
150158
"ts-node": "^8.10.2",
151159
"tsconfig-paths": "^4.2.0",
160+
"tslib": "^2.8.1",
152161
"typescript": "^4.7.4",
153162
"vite": "^6.4.1",
154163
"vitest": "^3.2.4",
155-
"webpack": "^5.94.0",
156-
"webdriverio": "^9.21.0"
164+
"webdriverio": "^9.21.0",
165+
"webpack": "^5.94.0"
157166
},
158167
"peerDependencies": {
159168
"@react-native-async-storage/async-storage": ">=1.0.0 <3.0.0",

rollup.config.new.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/**
2+
* Copyright 2020-2022 Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import commonjs from '@rollup/plugin-commonjs';
18+
import { terser } from 'rollup-plugin-terser';
19+
import resolve from '@rollup/plugin-node-resolve';
20+
import alias from '@rollup/plugin-alias';
21+
import { dependencies, peerDependencies } from './package.json';
22+
23+
const aliasPlugin = alias({
24+
entries: [
25+
{ find: 'error_message', replacement: './.build/message/error_message.gen.js' },
26+
{ find: 'log_message', replacement: './.build/message/log_message.gen.js' },
27+
]
28+
});
29+
30+
const cjsBundleFor = (platform, opt = {}) => {
31+
const { minify, ext } = {
32+
minify: true,
33+
ext: '.js',
34+
...opt,
35+
};
36+
37+
const min = minify ? '.min' : '';
38+
39+
return {
40+
plugins: [aliasPlugin, resolve(), commonjs()],
41+
external: ['https', 'http', 'url'].concat(Object.keys({ ...dependencies, ...peerDependencies } || {})),
42+
input: `.build/index.${platform}.js`,
43+
output: {
44+
exports: 'named',
45+
format: 'cjs',
46+
file: `dist/index.${platform}${min}${ext}`,
47+
plugins: minify ? [terser()] : undefined,
48+
sourcemap: true,
49+
},
50+
}
51+
};
52+
53+
const esmBundleFor = (platform, opt) => {
54+
const { minify, ext } = {
55+
minify: true,
56+
ext: '.js',
57+
...opt,
58+
};
59+
60+
const min = minify ? '.min' : '';
61+
62+
return {
63+
...cjsBundleFor(platform),
64+
output: [
65+
{
66+
format: 'es',
67+
file: `dist/index.${platform}.es${min}${ext}`,
68+
plugins: minify ? [terser()] : undefined,
69+
sourcemap: true,
70+
},
71+
],
72+
}
73+
};
74+
75+
const cjsBundleForUAParser = (opt = {}) => {
76+
const { minify, ext } = {
77+
minify: true,
78+
ext: '.js',
79+
...opt,
80+
};
81+
82+
const min = minify ? '.min' : '';
83+
84+
return {
85+
plugins: [aliasPlugin, resolve(), commonjs()],
86+
external: ['https', 'http', 'url'].concat(Object.keys({ ...dependencies, ...peerDependencies } || {})),
87+
input: `.build/odp/ua_parser/ua_parser.js`,
88+
output: {
89+
exports: 'named',
90+
format: 'cjs',
91+
file: `dist/ua_parser${min}${ext}`,
92+
plugins: minify ? [terser()] : undefined,
93+
sourcemap: true,
94+
},
95+
};
96+
};
97+
98+
const esmBundleForUAParser = (opt = {}) => {
99+
const { minify, ext } = {
100+
minify: true,
101+
ext: '.js',
102+
...opt,
103+
};
104+
105+
const min = minify ? '.min' : '';
106+
107+
return {
108+
...cjsBundleForUAParser(),
109+
output: [
110+
{
111+
format: 'es',
112+
file: `dist/ua_parser.es${min}${ext}`,
113+
plugins: minify ? [terser()] : undefined,
114+
sourcemap: true,
115+
},
116+
],
117+
};
118+
};
119+
120+
const umdBundle = {
121+
plugins: [
122+
aliasPlugin,
123+
resolve({ browser: true }),
124+
commonjs({
125+
namedExports: {
126+
'@optimizely/js-sdk-event-processor': ['LogTierV1EventProcessor', 'LocalStoragePendingEventsDispatcher'],
127+
'json-schema': ['validate'],
128+
},
129+
}),
130+
],
131+
input: '.build/index.browser.js',
132+
output: [
133+
{
134+
name: 'optimizelySdk',
135+
format: 'umd',
136+
file: 'dist/optimizely.browser.umd.js',
137+
exports: 'named',
138+
},
139+
{
140+
name: 'optimizelySdk',
141+
format: 'umd',
142+
file: 'dist/optimizely.browser.umd.min.js',
143+
exports: 'named',
144+
plugins: [terser()],
145+
sourcemap: true,
146+
},
147+
],
148+
};
149+
150+
// A separate bundle for json schema validator.
151+
const jsonSchemaBundle = {
152+
plugins: [aliasPlugin, resolve(), commonjs()],
153+
external: ['json-schema', 'uuid'],
154+
input: '.build/utils/json_schema_validator/index.js',
155+
output: {
156+
exports: 'named',
157+
format: 'cjs',
158+
file: 'dist/optimizely.json_schema_validator.min.js',
159+
plugins: [terser()],
160+
sourcemap: true,
161+
},
162+
};
163+
164+
const bundles = {
165+
'cjs-node-min': cjsBundleFor('node'),
166+
'cjs-browser-min': cjsBundleFor('browser'),
167+
'cjs-react-native-min': cjsBundleFor('react_native'),
168+
'cjs-universal': cjsBundleFor('universal'),
169+
'esm-browser-min': esmBundleFor('browser'),
170+
'esm-node-min': esmBundleFor('node', { ext: '.mjs' }),
171+
'esm-react-native-min': esmBundleFor('react_native'),
172+
'esm-universal': esmBundleFor('universal'),
173+
'json-schema': jsonSchemaBundle,
174+
'cjs-ua-parser-min': cjsBundleForUAParser(),
175+
'esm-ua-parser-min': esmBundleForUAParser(),
176+
umd: umdBundle,
177+
};
178+
179+
// Collect all --config-* options and return the matching bundle configs
180+
// Builds all bundles if no --config-* option given
181+
// --config-cjs will build all three cjs-* bundles
182+
// --config-umd will build only the umd bundle
183+
// --config-umd --config-json will build both umd and the json-schema bundles
184+
export default args => {
185+
const patterns = Object.keys(args)
186+
.filter(arg => arg.startsWith('config-'))
187+
.map(arg => arg.replace(/config-/, ''));
188+
189+
// default to matching all bundles
190+
if (!patterns.length) patterns.push(/.*/);
191+
192+
return Object.entries(bundles)
193+
.filter(([name, config]) => patterns.some(pattern => name.match(pattern)))
194+
.map(([name, config]) => config);
195+
};

tsconfig.build.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./.build",
5+
"declarationDir": "./dist",
6+
"declaration": true,
7+
"declarationMap": true,
8+
"sourceMap": true,
9+
"noEmit": false
10+
}
11+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"DOM",
88
],
99
"declaration": true,
10+
"importHelpers": true,
1011
"strict": true,
1112
"noImplicitThis": true,
1213
"noImplicitReturns": true,

0 commit comments

Comments
 (0)