From 44ced673e23f610fe63b0245d7d18a96b3bd3c85 Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 25 Jun 2024 18:57:17 -0300 Subject: [PATCH] fix: add suport to ESM entrypoint and build it in separated chunks --- .esbuild/build.js | 33 +++++++++++++++++++++------------ .esbuild/config.js | 17 +++++++++++++---- .esbuild/watch.js | 32 +++++++++++++++++++------------- package.json | 4 ++++ 4 files changed, 57 insertions(+), 29 deletions(-) diff --git a/.esbuild/build.js b/.esbuild/build.js index aa0a0fd1..d6ca62c5 100644 --- a/.esbuild/build.js +++ b/.esbuild/build.js @@ -1,16 +1,25 @@ -const baseConfig = require('./config'); +const config = require('./config'); +const esbuild = require('esbuild'); -const config = Object.assign({}, baseConfig, { - minify: true, - drop: ['debugger', 'console'], -}); +(async () => { + try { + await Promise.all([ + esbuild.build({ + ...config, + format: 'cjs', + outdir: 'lib', + outExtension: { '.js': '.cjs' }, + }), -require('esbuild') - .build({ - ...config, - outfile: 'lib/index.js', - }) - .catch((error) => { + esbuild.build({ + ...config, + format: 'esm', + outdir: 'lib', + splitting: true, + }), + ]); + } catch (error) { console.error(error); process.exit(1); - }); + } +})(); diff --git a/.esbuild/config.js b/.esbuild/config.js index 55375571..720cc593 100644 --- a/.esbuild/config.js +++ b/.esbuild/config.js @@ -1,11 +1,15 @@ require('dotenv').config(); const { style } = require('./plugins/style-loader'); +const glob = require('glob'); const entries = Object.entries(process.env).filter((key) => key[0].startsWith('SDK_')); const env = Object.fromEntries(entries); +const entryPoints = glob + .sync('./src/**/*.ts') + .filter((file) => !file.endsWith('.d.ts') && !file.endsWith('.test.ts')); -module.exports = { - entryPoints: ['./src/index.ts'], +const config = { + entryPoints, loader: { '.png': 'file', '.svg': 'file', @@ -16,9 +20,14 @@ module.exports = { }, plugins: [style()], bundle: true, - target: 'es6', - format: 'esm', + color: true, + minify: false, + logLevel: 'info', + sourcemap: true, + chunkNames: 'chunks/[name]-[hash]', define: { 'process.env': JSON.stringify(env), }, }; + +module.exports = config; diff --git a/.esbuild/watch.js b/.esbuild/watch.js index 017885e5..7b6b8a0e 100644 --- a/.esbuild/watch.js +++ b/.esbuild/watch.js @@ -1,22 +1,28 @@ -const baseConfig = require('./config'); +const config = require('./config'); const esbuild = require('esbuild'); -const config = Object.assign({}, baseConfig, { - outfile: 'dist/index.js', -}); - (async () => { try { - const context = await esbuild.context(config); - context.watch(); + const [cjsContext, esmContext] = await Promise.all([ + esbuild.context({ + ...config, + format: 'cjs', + outdir: 'dist', + outExtension: { '.js': '.cjs' }, + }), + + esbuild.context({ + ...config, + format: 'esm', + outdir: 'dist', + splitting: true, + }), + ]); + + cjsContext.watch(); + esmContext.watch(); } catch (error) { console.error(error); process.exit(1); } })(); - -// .build(config) -// .catch((error) => { -// console.error(error); -// process.exit(1); -// }); diff --git a/package.json b/package.json index 81d92fc9..cde7a13f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,10 @@ "description": "SuperViz SDK", "main": "./lib/index.js", "types": "./lib/index.d.ts", + "exports": { + "import": "./lib/index.js", + "require": "./lib/index.cjs.js" + }, "files": [ "lib" ],