Skip to content

Commit 21ea133

Browse files
committed
fix: Fix typescript exports for both cjs and es modules
1 parent 87e5873 commit 21ea133

File tree

11 files changed

+129
-151
lines changed

11 files changed

+129
-151
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ yarn.lock
33
dist/
44
node_modules/
55
**/*.tsbuildinfo
6+
**/*.rollup.cache
67
coverage/
78
**/tmp_test/
89
docs/

packages/sdk/akamai-base/example/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { logger } from 'log';
22
import { LDMultiKindContext } from '@launchdarkly/akamai-server-base-sdk';
3-
import { evaluateFlagFromCustomFeatureStore } from './ldClient';
3+
import { evaluateFlagFromCustomFeatureStore } from './ldClient.js';
44

55
const createLDContext = (r: EW.IngressClientRequest): LDMultiKindContext => ({
66
kind: 'multi',
77
location: {
88
key: 'context-key',
9-
country: r.userLocation.country,
9+
country: r.userLocation?.country,
1010
},
1111
});
1212

@@ -29,7 +29,7 @@ export async function onClientRequest(request: EW.IngressClientRequest) {
2929

3030
request.respondWith(200, {}, response);
3131
} catch (err) {
32-
request.respondWith(500, {}, `Something went wrong: ${err.toString()}`);
32+
request.respondWith(500, {}, `Something went wrong: ${err?.toString()}`);
3333
}
3434
}
3535

packages/sdk/akamai-base/example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"config": {
77
"ewid": "74943"
88
},
9+
"type": "module",
910
"scripts": {
1011
"clean": "rimraf ./dist/ew",
1112
"build": "npm run clean && npm run build-ts && npm run build-ew-tgz",
12-
"build-ts": "rollup -c --bundleConfigAsCjs",
13+
"build-ts": "rollup -c rollup.config.js",
1314
"build-ew-tgz": "cd dist/ew && tar czvf ../'ew_'$npm_package_version'.tgz' *",
1415
"validate": "akamai edgeworkers validate ./dist/'ew_'$npm_package_version'.tgz'",
1516
"dev": "npm run build && npm run validate && akamai sandbox add-edgeworker $npm_package_config_ewid ./dist/'ew_'$npm_package_version'.tgz' && akamai sandbox start"
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
22
"compilerOptions": {
3-
"module": "es2015",
4-
"target": "ES2015",
5-
"noImplicitAny": false,
3+
"incremental": true,
4+
"strict": true,
5+
"module": "NodeNext",
66
"outDir": "dist/ew",
7-
"rootDir": ".",
8-
"sourceMap": false,
9-
"moduleResolution": "node"
7+
"target": "ESNext",
8+
"lib": ["es2023", "DOM"],
9+
"moduleResolution": "nodenext",
10+
"esModuleInterop": true,
11+
"allowSyntheticDefaultImports": true
1012
},
11-
"exclude": ["node_modules"]
13+
"include": ["**/*.ts"],
14+
"exclude": ["dist", "node_modules"]
1215
}

packages/sdk/akamai-base/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"edge",
1616
"worker"
1717
],
18-
"type": "module",
1918
"exports": {
2019
".": {
2120
"import": {
@@ -28,6 +27,8 @@
2827
}
2928
}
3029
},
30+
"type": "module",
31+
"module": "./dist/esm/bundle.es.js",
3132
"main": "./dist/cjs/bundle.cjs.js",
3233
"types": "./dist/cjs/src/index.d.ts",
3334
"files": [

packages/sdk/akamai-base/rollup.config.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,61 @@ import common from '@rollup/plugin-commonjs';
44
import terser from '@rollup/plugin-terser';
55
import generatePackageJson from 'rollup-plugin-generate-package-json';
66

7-
export default {
8-
/* Specify main file for EdgeWorker */
7+
const getSharedConfig = (format, file) => ({
98
input: 'src/index.ts',
10-
11-
/* Define output format as an esm module and cjs module and specify the output directory */
129
output: [
1310
{
14-
format: 'es',
11+
format: format,
1512
sourcemap: true,
16-
file: 'dist/esm/bundle.es.js',
13+
file: file,
1714
intro: 'var setInterval = () => {}; var setTimeout = () => (callback) => { callback(); };',
1815
},
19-
{
20-
format: 'cjs',
21-
sourcemap: true,
22-
file: 'dist/cjs/bundle.cjs.js',
23-
intro: 'var setInterval = () => {}; var setTimeout = () => (callback) => { callback(); };',
24-
}
2516
],
26-
27-
/* Bundle all modules into a single output module */
2817
preserveModules: false,
29-
external: ['text-encode-transform', 'streams', 'http-request', 'edgekv_tokens.js', 'crypto'],
30-
31-
plugins: [
32-
/* Each build output folder cjs and esm needs a package.json */
33-
generatePackageJson({
34-
baseContents: (pkg) => ({ ...pkg }),
35-
}),
36-
37-
typescript(),
38-
39-
common({
40-
transformMixedEsModules: true,
41-
esmExternals: true,
42-
}),
43-
resolve(),
44-
terser(),
45-
],
18+
external: ['text-encode-transform', 'streams', 'http-request', 'crypto'],
4619
onwarn: (warning) => {
4720
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
4821
console.error(`(!) ${warning.message}`);
4922
}
5023
},
51-
};
24+
});
25+
26+
export default [
27+
{
28+
...getSharedConfig('es', 'dist/esm/bundle.es.js'),
29+
plugins: [
30+
generatePackageJson({
31+
baseContents: (pkg) => ({
32+
name: pkg.name,
33+
version: pkg.version,
34+
type: 'module',
35+
}),
36+
}),
37+
typescript({
38+
module: 'esnext',
39+
}),
40+
common({
41+
transformMixedEsModules: true,
42+
esmExternals: true,
43+
}),
44+
resolve(),
45+
terser(),
46+
],
47+
},
48+
{
49+
...getSharedConfig('cjs', 'dist/cjs/bundle.cjs.js'),
50+
plugins: [
51+
generatePackageJson({
52+
baseContents: (pkg) => ({
53+
name: pkg.name,
54+
version: pkg.version,
55+
type: 'commonjs',
56+
}),
57+
}),
58+
typescript(),
59+
common(),
60+
resolve(),
61+
terser(),
62+
],
63+
},
64+
];

packages/sdk/akamai-base/tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"rootDir": ".",
55
"outDir": "dist",
66
"target": "es6",
7-
"lib": ["es6"],
7+
"lib": ["ESNext"],
88
"module": "es6",
99
"strict": true,
1010
"noImplicitOverride": true,
@@ -15,9 +15,9 @@
1515
"declarationMap": true, // enables importers to jump to source
1616
"resolveJsonModule": true,
1717
"stripInternal": true,
18-
"moduleResolution": "node",
18+
"moduleResolution": "nodenext",
1919
"types": ["jest", "node"],
2020
"skipLibCheck": true
2121
},
2222
"exclude": ["**/*.test.ts", "dist", "node_modules", "__tests__", "example"]
23-
}
23+
}

packages/sdk/akamai-edgekv/rollup.config.js

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,61 @@ import common from '@rollup/plugin-commonjs';
44
import terser from '@rollup/plugin-terser';
55
import generatePackageJson from 'rollup-plugin-generate-package-json';
66

7-
export default {
8-
/* Specify main file for EdgeWorker */
7+
const getSharedConfig = (format, file) => ({
98
input: 'src/index.ts',
10-
11-
/* Define output format as an esm module and cjs module and specify the output directory */
12-
output: [{
13-
format: 'es',
14-
sourcemap: true,
15-
file: 'dist/esm/bundle.es.js',
16-
intro: 'var setInterval = () => {}; var setTimeout = () => (callback) => { callback(); };',
17-
},
18-
{
19-
format: 'cjs',
20-
sourcemap: true,
21-
file: 'dist/cjs/bundle.cjs.js',
22-
intro: 'var setInterval = () => {}; var setTimeout = () => (callback) => { callback(); };',
23-
}
24-
],
25-
26-
/* Bundle all modules into a single output module */
27-
preserveModules: false,
28-
external: ['text-encode-transform', 'streams', 'http-request', 'edgekv_tokens.js', 'crypto'],
29-
30-
plugins: [
31-
/* Each build output folder cjs and esm needs a package.json */
32-
generatePackageJson({
33-
baseContents: (pkg) => ({ ...pkg }),
34-
}),
35-
36-
typescript(),
37-
38-
common({
39-
transformMixedEsModules: true,
40-
esmExternals: true,
41-
}),
42-
resolve(),
43-
terser(),
9+
output: [
10+
{
11+
format: format,
12+
sourcemap: true,
13+
file: file,
14+
intro: 'var setInterval = () => {}; var setTimeout = () => (callback) => { callback(); };',
15+
},
4416
],
17+
preserveModules: false,
18+
external: ['text-encode-transform', 'streams', 'http-request', 'crypto', 'edgekv_tokens.js'],
4519
onwarn: (warning) => {
4620
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
4721
console.error(`(!) ${warning.message}`);
4822
}
4923
},
50-
};
24+
});
25+
26+
export default [
27+
{
28+
...getSharedConfig('es', 'dist/esm/bundle.es.js'),
29+
plugins: [
30+
generatePackageJson({
31+
baseContents: (pkg) => ({
32+
name: pkg.name,
33+
version: pkg.version,
34+
type: 'module',
35+
}),
36+
}),
37+
typescript({
38+
module: 'esnext',
39+
}),
40+
common({
41+
transformMixedEsModules: true,
42+
esmExternals: true,
43+
}),
44+
resolve(),
45+
terser(),
46+
],
47+
},
48+
{
49+
...getSharedConfig('cjs', 'dist/cjs/bundle.cjs.js'),
50+
plugins: [
51+
generatePackageJson({
52+
baseContents: (pkg) => ({
53+
name: pkg.name,
54+
version: pkg.version,
55+
type: 'commonjs',
56+
}),
57+
}),
58+
typescript(),
59+
common(),
60+
resolve(),
61+
terser(),
62+
],
63+
},
64+
];

packages/shared/akamai-edgeworker-sdk/package.json

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@
1414
"edge",
1515
"worker"
1616
],
17-
"type": "module",
18-
"exports": {
19-
"import": "./dist/esm/index.es.js"
20-
},
21-
"main": "./dist/esm/index.es.js",
22-
"types": "./dist/esm/src/index.d.ts",
17+
"type": "commonjs",
18+
"main": "./dist/index.js",
19+
"types": "./dist/index.d.ts",
2320
"files": [
2421
"dist"
2522
],
2623
"scripts": {
2724
"doc": "../../../scripts/build-doc.sh .",
28-
"build": "rollup -c rollup.config.js",
29-
"clean": "rimraf dist",
30-
"tsw": "yarn tsc --watch",
31-
"start": "rimraf dist && yarn tsw",
32-
"lint": "eslint . --ext .ts",
33-
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
34-
"test": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --ci --runInBand",
35-
"coverage": "yarn test --coverage",
36-
"check": "yarn prettier && yarn lint && yarn build && yarn test && yarn doc"
25+
"test": "npx jest --ci",
26+
"build": "npx tsc",
27+
"clean": "npx tsc --build --clean",
28+
"lint": "npx eslint . --ext .ts",
29+
"lint:fix": "yarn run lint -- --fix"
3730
},
3831
"devDependencies": {
3932
"@rollup/plugin-commonjs": "^25.0.0",

packages/shared/akamai-edgeworker-sdk/rollup.config.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)