Skip to content

Commit 7af4f6e

Browse files
authored
feat: rollup cloudflare sdk (#279)
Bundle cloudflare sdk using rollup and in process remove js-core and package.json imports.
1 parent 0273c6c commit 7af4f6e

File tree

11 files changed

+110
-28
lines changed

11 files changed

+110
-28
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
'import/no-extraneous-dependencies': [
1616
'error',
1717
{
18-
devDependencies: ['**/jest*.ts', '**/*.test.ts'],
18+
devDependencies: ['**/jest*.ts', '**/*.test.ts', '**/rollup.config.ts'],
1919
},
2020
],
2121
},

packages/sdk/cloudflare/example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"module": "./dist/index.mjs",
66
"packageManager": "[email protected]",
77
"dependencies": {
8-
"@launchdarkly/cloudflare-server-sdk": "^2.0.2"
8+
"@launchdarkly/cloudflare-server-sdk": "^2.1.4"
99
},
1010
"devDependencies": {
1111
"@cloudflare/workers-types": "^4.20230321.0",
@@ -23,6 +23,6 @@
2323
"build": "node build.js",
2424
"start": "wrangler dev",
2525
"deploy": "wrangler publish",
26-
"test": "yarn build && node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js"
26+
"test": "yarn build && jest"
2727
}
2828
}
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import app from './index';
22
import testData from './testData.json';
33

4-
test('variation true', async () => {
5-
// arrange
6-
const env = getMiniflareBindings();
7-
const { LD_KV } = env;
8-
await LD_KV.put('LD-Env-test-sdk-key', JSON.stringify(testData));
4+
describe('test', () => {
5+
let env: Bindings;
96

10-
// act
11-
const res = await app.fetch(new Request('http://localhost'), env);
7+
beforeEach(async () => {
8+
env = getMiniflareBindings();
9+
const { LD_KV } = env;
10+
await LD_KV.put('LD-Env-test-sdk-key', JSON.stringify(testData));
11+
});
1212

13-
// assert
14-
expect(await res.text()).toContain('testFlag1: true');
13+
test('variation true', async () => {
14+
const res = await app.fetch(new Request('http://localhost/?email=truemail'), env);
15+
expect(await res.text()).toContain('testFlag1: true');
16+
});
17+
18+
test('variation false', async () => {
19+
const res = await app.fetch(new Request('http://localhost/?email=falsemail'), env);
20+
expect(await res.text()).toContain('testFlag1: false');
21+
});
1522
});

packages/sdk/cloudflare/example/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ export default {
44
async fetch(request: Request, env: Bindings): Promise<Response> {
55
const sdkKey = 'test-sdk-key';
66
const flagKey = 'testFlag1';
7-
const context = { kind: 'user', key: 'test-user-key-1', email: '[email protected]' };
7+
const { searchParams } = new URL(request.url);
8+
9+
// falsemail will return false, other emails return true
10+
const email = searchParams.get('email') ?? '[email protected]';
11+
const context = { kind: 'user', key: 'test-user-key-1', email };
812

913
// start using ld
1014
const client = initLD(sdkKey, env.LD_KV);

packages/sdk/cloudflare/example/src/testData.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"contextKind": "user",
1515
"attribute": "/email",
1616
"op": "contains",
17-
"values": ["gmail"],
17+
"values": ["falsemail"],
1818
"negate": false
1919
}
2020
],

packages/sdk/cloudflare/package.json

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,43 @@
1717
],
1818
"type": "module",
1919
"exports": {
20-
"require": "./dist/cjs/src/index.js",
21-
"import": "./dist/esm/src/index.js"
20+
"types": "./dist/index.d.ts",
21+
"import": "./dist/esm/index.js",
22+
"require": "./dist/cjs/index.js"
2223
},
23-
"main": "./dist/cjs/src/index.js",
24-
"types": "./dist/cjs/src/index.d.ts",
24+
"main": "./dist/cjs/index.js",
25+
"types": "./dist/index.d.ts",
2526
"files": [
2627
"dist"
2728
],
2829
"scripts": {
29-
"build": "../../../scripts/build-package.sh",
3030
"clean": "rimraf dist",
31+
"rb": "rollup -c --configPlugin typescript",
32+
"rbw": "yarn rb --watch",
33+
"build": "yarn clean && yarn rb",
3134
"tsw": "yarn tsc --watch",
3235
"start": "rimraf dist && yarn tsw",
3336
"lint": "eslint . --ext .ts",
3437
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
3538
"test": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --ci --runInBand",
3639
"coverage": "yarn test --coverage",
37-
"check": "yarn prettier && yarn lint && yarn build && yarn test && yarn doc"
40+
"check": "yarn prettier && yarn lint && yarn build && yarn test"
3841
},
3942
"dependencies": {
4043
"@cloudflare/workers-types": "^4.20230321.0",
41-
"@launchdarkly/js-server-sdk-common-edge": "1.0.13",
4244
"crypto-js": "^4.1.1"
4345
},
4446
"devDependencies": {
47+
"@launchdarkly/js-server-sdk-common-edge": "1.0.13",
48+
"@rollup/plugin-commonjs": "^25.0.4",
49+
"@rollup/plugin-json": "^6.0.0",
50+
"@rollup/plugin-node-resolve": "^15.2.1",
51+
"@rollup/plugin-terser": "^0.4.3",
52+
"@rollup/plugin-typescript": "^11.1.3",
4553
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
4654
"@types/crypto-js": "^4.1.1",
4755
"@types/jest": "^29.5.0",
56+
"@types/rollup-plugin-generate-package-json": "^3.2.3",
4857
"@typescript-eslint/eslint-plugin": "^6.1.0",
4958
"@typescript-eslint/parser": "^6.1.0",
5059
"eslint": "^8.45.0",
@@ -57,9 +66,17 @@
5766
"launchdarkly-js-test-helpers": "^2.2.0",
5867
"miniflare": "^2.13.0",
5968
"prettier": "^3.0.0",
60-
"rimraf": "^5.0.0",
69+
"rimraf": "^5.0.1",
70+
"rollup": "^3.29.2",
71+
"rollup-plugin-dts": "^6.0.2",
72+
"rollup-plugin-esbuild": "^5.0.0",
73+
"rollup-plugin-filesize": "^10.0.0",
74+
"rollup-plugin-generate-package-json": "^3.2.0",
6175
"ts-jest": "^29.1.0",
6276
"typedoc": "0.25.0",
6377
"typescript": "5.1.6"
64-
}
78+
},
79+
"bundledDependencies": [
80+
"@launchdarkly/js-server-sdk-common-edge"
81+
]
6582
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import commonjs from '@rollup/plugin-commonjs';
2+
import json from '@rollup/plugin-json';
3+
import resolve from '@rollup/plugin-node-resolve';
4+
import terser from '@rollup/plugin-terser';
5+
import dts from 'rollup-plugin-dts';
6+
import esbuild from 'rollup-plugin-esbuild';
7+
import filesize from 'rollup-plugin-filesize';
8+
9+
const inputPath = 'src/index.ts';
10+
const cjsPath = 'dist/cjs/index.js';
11+
const esmPath = 'dist/esm/index.js';
12+
const typingsPath = 'dist/index.d.ts';
13+
14+
const plugins = [resolve(), commonjs(), esbuild(), json(), terser(), filesize()];
15+
16+
// the second array item is a function to include all js-core packages in the bundle so they
17+
// are not imported or required as separate npm packages
18+
const external = [/node_modules/, (id: string) => !id.includes('js-core')];
19+
20+
export default [
21+
{
22+
input: inputPath,
23+
output: [
24+
{
25+
file: cjsPath,
26+
format: 'cjs',
27+
sourcemap: true,
28+
},
29+
],
30+
plugins,
31+
external,
32+
},
33+
{
34+
input: inputPath,
35+
output: [
36+
{
37+
file: esmPath,
38+
format: 'esm',
39+
sourcemap: true,
40+
},
41+
],
42+
plugins,
43+
external,
44+
},
45+
{
46+
input: inputPath,
47+
plugins: [dts(), json()],
48+
output: {
49+
file: typingsPath,
50+
format: 'esm',
51+
},
52+
},
53+
];

packages/sdk/cloudflare/src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('init', () => {
5151
});
5252

5353
test('rule match', async () => {
54-
const contextWithEmail = { ...context, email: 'test@gmail.com' };
54+
const contextWithEmail = { ...context, email: 'test@falsemail.com' };
5555
const value = await ldClient.variation(flagKey1, contextWithEmail, false);
5656
const detail = await ldClient.variationDetail(flagKey1, contextWithEmail, false);
5757

packages/sdk/cloudflare/src/testData.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"contextKind": "user",
1515
"attribute": "/email",
1616
"op": "contains",
17-
"values": ["gmail"],
17+
"values": ["falsemail"],
1818
"negate": false
1919
}
2020
],

packages/sdk/cloudflare/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// Uses "." so it can load package.json.
44
"rootDir": ".",
55
"outDir": "dist",
6-
"target": "es2017",
6+
"target": "ES2017",
77
"lib": ["es6"],
8-
"module": "commonjs",
8+
"module": "ES6",
99
"strict": true,
1010
"noImplicitOverride": true,
1111
"allowSyntheticDefaultImports": true,

0 commit comments

Comments
 (0)