Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rollup cloudflare sdk #279

Merged
merged 14 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/sdk/cloudflare/example/link-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

rm -rf node_modules/@launchdarkly
mkdir -p node_modules/@launchdarkly
rsync -aq ../dist node_modules/@launchdarkly/cloudflare-server-sdk
rsync -aq ../package.json node_modules/@launchdarkly/cloudflare-server-sdk
7 changes: 4 additions & 3 deletions packages/sdk/cloudflare/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "./dist/index.mjs",
"packageManager": "[email protected]",
"dependencies": {
"@launchdarkly/cloudflare-server-sdk": "^2.0.2"
"@launchdarkly/cloudflare-server-sdk": "^2.1.4"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230321.0",
Expand All @@ -20,9 +20,10 @@
"wrangler": "2.20.1"
},
"scripts": {
"build": "node build.js",
"build": "yarn link-dev && node build.js",
"start": "wrangler dev",
"deploy": "wrangler publish",
"test": "yarn build && node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js"
"test": "yarn build && jest",
"link-dev": "./link-dev.sh"
}
}
3 changes: 2 additions & 1 deletion packages/sdk/cloudflare/example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default {
async fetch(request: Request, env: Bindings): Promise<Response> {
const sdkKey = 'test-sdk-key';
const flagKey = 'testFlag1';
const context = { kind: 'user', key: 'test-user-key-1', email: '[email protected]' };
// gmail will return false, other emails return true
const context = { kind: 'user', key: 'test-user-key-1', email: '[email protected]' };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use email addresses that did not involve trademarks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// start using ld
const client = initLD(sdkKey, env.LD_KV);
Expand Down
31 changes: 23 additions & 8 deletions packages/sdk/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,42 @@
],
"type": "module",
"exports": {
"require": "./dist/cjs/src/index.js",
"import": "./dist/esm/src/index.js"
"types": "./dist/index.d.ts",
"import": "./dist/esm/src/index.js",
"require": "./dist/cjs/src/index.js"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types was missing and this caused an issue in the react sdk with typescript so this is a pre-emptive fix.

},
"main": "./dist/cjs/src/index.js",
"types": "./dist/cjs/src/index.d.ts",
"types": "./dist/index.d.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved typings one level up so we have 1 copy to share now between cjs and esm.

"files": [
"dist"
],
"scripts": {
"build": "../../../scripts/build-package.sh",
Copy link
Contributor Author

@yusinto yusinto Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need to create sub package.jsons because it is now bundled with the sdk.

"clean": "rimraf dist",
"rb": "rollup -c --configPlugin typescript",
"rbw": "yarn rb --watch",
"build": "yarn clean && yarn rb",
"tsw": "yarn tsc --watch",
"start": "rimraf dist && yarn tsw",
"lint": "eslint . --ext .ts",
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
"test": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --ci --runInBand",
"coverage": "yarn test --coverage",
"check": "yarn prettier && yarn lint && yarn build && yarn test && yarn doc"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yarn doc never existed so this command was failing before.

"check": "yarn prettier && yarn lint && yarn build && yarn test"
},
"dependencies": {
"@cloudflare/workers-types": "^4.20230321.0",
"@launchdarkly/js-server-sdk-common-edge": "1.0.13",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer a runtime dependency because all js-core deps are included in the bundle.

"crypto-js": "^4.1.1"
},
"devDependencies": {
"@launchdarkly/js-server-sdk-common-edge": "1.0.13",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-terser": "^0.4.3",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@types/crypto-js": "^4.1.1",
"@types/jest": "^29.5.0",
"@types/rollup-plugin-generate-package-json": "^3.2.3",
"@typescript-eslint/eslint-plugin": "^6.1.0",
"@typescript-eslint/parser": "^6.1.0",
"eslint": "^8.45.0",
Expand All @@ -55,11 +63,18 @@
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"launchdarkly-js-test-helpers": "^2.2.0",
"load-json-file": "^7.0.1",
"miniflare": "^2.13.0",
"prettier": "^3.0.0",
"rimraf": "^5.0.0",
"rimraf": "^5.0.1",
"rollup": "^3.29.2",
"rollup-plugin-dts": "^6.0.2",
"rollup-plugin-esbuild": "^5.0.0",
"rollup-plugin-filesize": "^10.0.0",
"rollup-plugin-generate-package-json": "^3.2.0",
"ts-jest": "^29.1.0",
"typedoc": "0.25.0",
"typescript": "5.1.6"
"typescript": "5.1.6",
"write-json-file": "^5.0.0"
}
}
84 changes: 84 additions & 0 deletions packages/sdk/cloudflare/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint-disable import/no-extraneous-dependencies */
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import { loadJsonFileSync } from 'load-json-file';
import { OutputOptions } from 'rollup';
import dts from 'rollup-plugin-dts';
import esbuild from 'rollup-plugin-esbuild';
import filesize from 'rollup-plugin-filesize';
import { writeJsonFileSync } from 'write-json-file';

const cjsIndex = 'dist/cjs/src/index.js';
const cjsPackageJson = 'dist/cjs/package.json';
const esmIndex = 'dist/esm/src/index.js';
const esmPackageJson = 'dist/esm/package.json';

function injectPackageJson() {
yusinto marked this conversation as resolved.
Show resolved Hide resolved
return {
name: 'inject-package-json',
generateBundle({ format }: OutputOptions) {
const { name, version } = loadJsonFileSync('package.json') as any;
const minimalPackageJson = {
name,
version,
type: format === 'cjs' ? 'commonjs' : 'module',
};

const packageJsonPath = format === 'cjs' ? cjsPackageJson : esmPackageJson;
writeJsonFileSync(packageJsonPath, minimalPackageJson, {
indent: 2,
});
},
};
}

const plugins = [
resolve(),
commonjs(),
esbuild(),
json(),
terser(),
filesize(),
injectPackageJson(),
];

// the second array item is a function to include all js-core packages in the bundle so they
// are not imported or required as separate npm packages
const external = [/node_modules/, (id: string) => !id.includes('js-core')];

export default [
{
input: 'src/index.ts',
output: [
{
file: cjsIndex,
format: 'cjs',
sourcemap: true,
},
],
plugins,
external,
},
{
input: 'src/index.ts',
output: [
{
file: esmIndex,
format: 'esm',
sourcemap: true,
},
],
plugins,
external,
},
{
input: 'src/index.ts',
plugins: [dts(), json()],
output: {
file: 'dist/index.d.ts',
format: 'es',
},
},
];
2 changes: 2 additions & 0 deletions packages/sdk/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
import type { KVNamespace } from '@cloudflare/workers-types';

// eslint-disable-next-line import/no-extraneous-dependencies
yusinto marked this conversation as resolved.
Show resolved Hide resolved
import {
BasicLogger,
EdgeFeatureStore,
Expand All @@ -20,6 +21,7 @@ import {

import createPlatformInfo from './createPlatformInfo';

// eslint-disable-next-line import/no-extraneous-dependencies
export * from '@launchdarkly/js-server-sdk-common-edge';

export type { LDClient };
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/cloudflare/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// Uses "." so it can load package.json.
"rootDir": ".",
"outDir": "dist",
"target": "es2017",
"target": "ES2017",
"lib": ["es6"],
"module": "commonjs",
"module": "ES6",
"strict": true,
"noImplicitOverride": true,
"allowSyntheticDefaultImports": true,
Expand Down
3 changes: 2 additions & 1 deletion scripts/build-package.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
# Run this script like:
# ./scripts/build-package.sh

Expand All @@ -23,7 +24,7 @@ ESM_PACKAGE_JSON=$( jq -n \
--arg type "module" \
'{ name: $name, version: $version, type: $type }' )

tsc --module commonjs --outDir dist/cjs/
tsc --module commonjs --outDir dist/cjs/
echo "$CJS_PACKAGE_JSON" > dist/cjs/package.json

tsc --module es2022 --outDir dist/esm/
Expand Down
Loading