Skip to content

Commit f4961c6

Browse files
igorDykhtaIhor Dykhta
andauthored
chore: add dual ESM/CJS build for all workspace packages (#3607)
* chore: add esm build for modules Signed-off-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local> * follow up Signed-off-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local> * follow up Signed-off-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local> * more follow up Signed-off-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local> * follow up Signed-off-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local> * remove mapbox blocking alert - we can use maplibre Signed-off-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local> --------- Signed-off-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local> Co-authored-by: Ihor Dykhta <ihordykhta@Ihors-MacBook-Pro.local>
1 parent c99fc43 commit f4961c6

42 files changed

Lines changed: 790 additions & 503 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/actions/babel.config.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33

44
const KeplerPackage = require('./package');
55

6-
const PRESETS = ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'];
7-
const PLUGINS = [
6+
// Plugins shared by both CJS and ESM builds.
7+
const SHARED_PLUGINS = [
88
['@babel/plugin-transform-typescript', {isTSX: true, allowDeclareFields: true}],
9-
'@babel/plugin-transform-modules-commonjs',
109
'@babel/plugin-transform-class-properties',
1110
'@babel/plugin-transform-optional-chaining',
1211
'@babel/plugin-transform-logical-assignment-operators',
1312
'@babel/plugin-transform-nullish-coalescing-operator',
1413
'@babel/plugin-transform-export-namespace-from',
15-
[
16-
'@babel/transform-runtime',
17-
{
18-
regenerator: true
19-
}
20-
],
2114
[
2215
'search-and-replace',
2316
{
@@ -30,22 +23,35 @@ const PLUGINS = [
3023
}
3124
]
3225
];
33-
const ENV = {
34-
test: {
35-
plugins: ['istanbul']
36-
},
37-
debug: {
38-
sourceMaps: 'inline',
39-
retainLines: true
40-
}
41-
};
4226

4327
module.exports = function babel(api) {
44-
api.cache(true);
28+
// Cache per BABEL_ENV so that CJS and ESM builds get separate cached results.
29+
api.cache.using(() => process.env.BABEL_ENV || process.env.NODE_ENV || 'development');
30+
31+
const isEsm = api.env('esm');
32+
const isTest = api.env('test');
33+
const isDebug = api.env('debug');
34+
35+
// ESM build: modules:false preserves import/export so Vite/esbuild can tree-shake.
36+
// CJS build (default / test): transforms import/export to require/module.exports.
37+
const presets = [
38+
['@babel/preset-env', isEsm ? {modules: false} : {}],
39+
'@babel/preset-react',
40+
'@babel/preset-typescript'
41+
];
42+
43+
const plugins = [
44+
...SHARED_PLUGINS,
45+
// CJS and test builds need the CommonJS transform; ESM must omit it.
46+
...(isEsm ? [] : ['@babel/plugin-transform-modules-commonjs']),
47+
// Runtime helpers: ESM variant uses ESM imports, no require() calls.
48+
['@babel/transform-runtime', {regenerator: true, ...(isEsm ? {useESModules: true} : {})}],
49+
...(isTest ? ['istanbul'] : [])
50+
];
4551

4652
return {
47-
presets: PRESETS,
48-
plugins: PLUGINS,
49-
env: ENV
53+
presets,
54+
plugins,
55+
...(isDebug ? {sourceMaps: 'inline', retainLines: true} : {})
5056
};
5157
};

src/actions/package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "3.3.0-alpha.5",
55
"description": "kepler.gl constants used by kepler.gl components, actions and reducers",
66
"license": "MIT",
7-
"main": "dist/index.js",
7+
"main": "dist/cjs/index.js",
88
"types": "dist/index.d.ts",
99
"keywords": [
1010
"babel",
@@ -19,10 +19,10 @@
1919
"url": "https://github.com/keplergl/kepler.gl.git"
2020
},
2121
"scripts": {
22-
"build": "rm -fr dist && babel src --out-dir dist --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
22+
"build": "rm -fr dist && babel src --out-dir dist/cjs --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts' && BABEL_ENV=esm babel src --out-dir dist/esm --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
2323
"build:types": "tsc --project ./tsconfig.production.json",
2424
"prepublishOnly": "babel-node ../../scripts/license-header/bin --license ../../FILE-HEADER && yarn build && yarn build:types",
25-
"stab": "mkdir -p dist && touch dist/index.js"
25+
"stab": "mkdir -p dist/cjs dist/esm && touch dist/cjs/index.js dist/esm/index.js"
2626
},
2727
"files": [
2828
"dist"
@@ -63,5 +63,14 @@
6363
"node": "20.19.3",
6464
"yarn": "4.4.0"
6565
},
66-
"packageManager": "yarn@4.4.0"
66+
"packageManager": "yarn@4.4.0",
67+
"module": "dist/esm/index.js",
68+
"exports": {
69+
".": {
70+
"types": "./dist/index.d.ts",
71+
"import": "./dist/esm/index.js",
72+
"require": "./dist/cjs/index.js",
73+
"default": "./dist/cjs/index.js"
74+
}
75+
}
6776
}

src/ai-assistant/babel.config.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33

44
const KeplerPackage = require('./package');
55

6-
const PRESETS = ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'];
7-
const PLUGINS = [
6+
// Plugins shared by both CJS and ESM builds.
7+
const SHARED_PLUGINS = [
88
['@babel/plugin-transform-typescript', {isTSX: true, allowDeclareFields: true}],
9-
'@babel/plugin-transform-modules-commonjs',
109
'@babel/plugin-transform-class-properties',
1110
'@babel/plugin-transform-optional-chaining',
1211
'@babel/plugin-transform-logical-assignment-operators',
1312
'@babel/plugin-transform-nullish-coalescing-operator',
1413
'@babel/plugin-transform-export-namespace-from',
15-
[
16-
'@babel/transform-runtime',
17-
{
18-
regenerator: true
19-
}
20-
],
2114
[
2215
'search-and-replace',
2316
{
@@ -30,22 +23,35 @@ const PLUGINS = [
3023
}
3124
]
3225
];
33-
const ENV = {
34-
test: {
35-
plugins: ['istanbul']
36-
},
37-
debug: {
38-
sourceMaps: 'inline',
39-
retainLines: true
40-
}
41-
};
4226

4327
module.exports = function babel(api) {
44-
api.cache(true);
28+
// Cache per BABEL_ENV so that CJS and ESM builds get separate cached results.
29+
api.cache.using(() => process.env.BABEL_ENV || process.env.NODE_ENV || 'development');
30+
31+
const isEsm = api.env('esm');
32+
const isTest = api.env('test');
33+
const isDebug = api.env('debug');
34+
35+
// ESM build: modules:false preserves import/export so Vite/esbuild can tree-shake.
36+
// CJS build (default / test): transforms import/export to require/module.exports.
37+
const presets = [
38+
['@babel/preset-env', isEsm ? {modules: false} : {}],
39+
'@babel/preset-react',
40+
'@babel/preset-typescript'
41+
];
42+
43+
const plugins = [
44+
...SHARED_PLUGINS,
45+
// CJS and test builds need the CommonJS transform; ESM must omit it.
46+
...(isEsm ? [] : ['@babel/plugin-transform-modules-commonjs']),
47+
// Runtime helpers: ESM variant uses ESM imports, no require() calls.
48+
['@babel/transform-runtime', {regenerator: true, ...(isEsm ? {useESModules: true} : {})}],
49+
...(isTest ? ['istanbul'] : [])
50+
];
4551

4652
return {
47-
presets: PRESETS,
48-
plugins: PLUGINS,
49-
env: ENV
53+
presets,
54+
plugins,
55+
...(isDebug ? {sourceMaps: 'inline', retainLines: true} : {})
5056
};
5157
};

src/ai-assistant/package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "3.3.0-alpha.5",
55
"description": "kepler.gl AI assistant",
66
"license": "MIT",
7-
"main": "dist/index.js",
7+
"main": "dist/cjs/index.js",
88
"types": "dist/index.d.ts",
99
"keywords": [
1010
"babel",
@@ -19,10 +19,10 @@
1919
"url": "https://github.com/keplergl/kepler.gl.git"
2020
},
2121
"scripts": {
22-
"build": "rm -fr dist && babel src --out-dir dist --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
22+
"build": "rm -fr dist && babel src --out-dir dist/cjs --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts' && BABEL_ENV=esm babel src --out-dir dist/esm --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
2323
"build:types": "tsc --project ./tsconfig.production.json",
2424
"prepublishOnly": "babel-node ../../scripts/license-header/bin --license ../../FILE-HEADER && yarn build && yarn build:types",
25-
"stab": "mkdir -p dist && touch dist/index.js"
25+
"stab": "mkdir -p dist/cjs dist/esm && touch dist/cjs/index.js dist/esm/index.js"
2626
},
2727
"files": [
2828
"dist"
@@ -68,5 +68,14 @@
6868
"node": "20.19.3",
6969
"yarn": "4.4.0"
7070
},
71-
"packageManager": "yarn@4.4.0"
71+
"packageManager": "yarn@4.4.0",
72+
"module": "dist/esm/index.js",
73+
"exports": {
74+
".": {
75+
"types": "./dist/index.d.ts",
76+
"import": "./dist/esm/index.js",
77+
"require": "./dist/cjs/index.js",
78+
"default": "./dist/cjs/index.js"
79+
}
80+
}
7281
}

src/cloud-providers/babel.config.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33

44
const KeplerPackage = require('./package');
55

6-
const PRESETS = ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'];
7-
const PLUGINS = [
6+
// Plugins shared by both CJS and ESM builds.
7+
const SHARED_PLUGINS = [
88
['@babel/plugin-transform-typescript', {isTSX: true, allowDeclareFields: true}],
9-
'@babel/plugin-transform-modules-commonjs',
109
'@babel/plugin-transform-class-properties',
1110
'@babel/plugin-transform-optional-chaining',
1211
'@babel/plugin-transform-logical-assignment-operators',
1312
'@babel/plugin-transform-nullish-coalescing-operator',
1413
'@babel/plugin-transform-export-namespace-from',
15-
[
16-
'@babel/transform-runtime',
17-
{
18-
regenerator: true
19-
}
20-
],
2114
[
2215
'search-and-replace',
2316
{
@@ -30,22 +23,35 @@ const PLUGINS = [
3023
}
3124
]
3225
];
33-
const ENV = {
34-
test: {
35-
plugins: ['istanbul']
36-
},
37-
debug: {
38-
sourceMaps: 'inline',
39-
retainLines: true
40-
}
41-
};
4226

4327
module.exports = function babel(api) {
44-
api.cache(true);
28+
// Cache per BABEL_ENV so that CJS and ESM builds get separate cached results.
29+
api.cache.using(() => process.env.BABEL_ENV || process.env.NODE_ENV || 'development');
30+
31+
const isEsm = api.env('esm');
32+
const isTest = api.env('test');
33+
const isDebug = api.env('debug');
34+
35+
// ESM build: modules:false preserves import/export so Vite/esbuild can tree-shake.
36+
// CJS build (default / test): transforms import/export to require/module.exports.
37+
const presets = [
38+
['@babel/preset-env', isEsm ? {modules: false} : {}],
39+
'@babel/preset-react',
40+
'@babel/preset-typescript'
41+
];
42+
43+
const plugins = [
44+
...SHARED_PLUGINS,
45+
// CJS and test builds need the CommonJS transform; ESM must omit it.
46+
...(isEsm ? [] : ['@babel/plugin-transform-modules-commonjs']),
47+
// Runtime helpers: ESM variant uses ESM imports, no require() calls.
48+
['@babel/transform-runtime', {regenerator: true, ...(isEsm ? {useESModules: true} : {})}],
49+
...(isTest ? ['istanbul'] : [])
50+
];
4551

4652
return {
47-
presets: PRESETS,
48-
plugins: PLUGINS,
49-
env: ENV
53+
presets,
54+
plugins,
55+
...(isDebug ? {sourceMaps: 'inline', retainLines: true} : {})
5056
};
5157
};

src/cloud-providers/package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "3.3.0-alpha.5",
55
"description": "kepler.gl constants used by kepler.gl components, actions and reducers",
66
"license": "MIT",
7-
"main": "dist/index.js",
7+
"main": "dist/cjs/index.js",
88
"types": "dist/index.d.ts",
99
"keywords": [
1010
"babel",
@@ -19,10 +19,10 @@
1919
"url": "https://github.com/keplergl/kepler.gl.git"
2020
},
2121
"scripts": {
22-
"build": "rm -fr dist && babel src --out-dir dist --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
22+
"build": "rm -fr dist && babel src --out-dir dist/cjs --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts' && BABEL_ENV=esm babel src --out-dir dist/esm --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
2323
"build:types": "tsc --project ./tsconfig.production.json",
2424
"prepublishOnly": "babel-node ../../scripts/license-header/bin --license ../../FILE-HEADER && yarn build && yarn build:types",
25-
"stab": "mkdir -p dist && touch dist/index.js"
25+
"stab": "mkdir -p dist/cjs dist/esm && touch dist/cjs/index.js dist/esm/index.js"
2626
},
2727
"files": [
2828
"dist"
@@ -48,5 +48,14 @@
4848
"node": "20.19.3",
4949
"yarn": "4.4.0"
5050
},
51-
"packageManager": "yarn@4.4.0"
51+
"packageManager": "yarn@4.4.0",
52+
"module": "dist/esm/index.js",
53+
"exports": {
54+
".": {
55+
"types": "./dist/index.d.ts",
56+
"import": "./dist/esm/index.js",
57+
"require": "./dist/cjs/index.js",
58+
"default": "./dist/cjs/index.js"
59+
}
60+
}
5261
}

0 commit comments

Comments
 (0)