From 0bdf132bd9f0223d4858c59d25555ae66bae83d0 Mon Sep 17 00:00:00 2001 From: Carlos Date: Thu, 27 Jun 2024 10:17:55 -0300 Subject: [PATCH 1/2] feat: improve esm build --- .esbuild/build.js | 12 ++++-------- .esbuild/config.js | 34 ++++++++++++++++++++++++++++++++-- .esbuild/watch.js | 12 ++++-------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/.esbuild/build.js b/.esbuild/build.js index d6ca62c5..7a7e82d5 100644 --- a/.esbuild/build.js +++ b/.esbuild/build.js @@ -1,21 +1,17 @@ -const config = require('./config'); +const { cjsConfig, esmConfig } = require('./config'); const esbuild = require('esbuild'); (async () => { try { await Promise.all([ esbuild.build({ - ...config, - format: 'cjs', - outdir: 'lib', - outExtension: { '.js': '.cjs' }, + ...cjsConfig, + outfile: 'lib/index.cjs.js', }), esbuild.build({ - ...config, - format: 'esm', + ...esmConfig, outdir: 'lib', - splitting: true, }), ]); } catch (error) { diff --git a/.esbuild/config.js b/.esbuild/config.js index 2b442bfe..eedae2ee 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 entryPoints = glob + .sync('./src/**/*.ts') + .filter((file) => !file.endsWith('.d.ts') && !file.endsWith('.test.ts')); const entries = Object.entries(process.env).filter((key) => key[0].startsWith('SDK_')); const env = Object.fromEntries(entries); const config = { - entryPoints: ['./src/index.ts'], loader: { '.png': 'file', '.svg': 'file', @@ -26,4 +30,30 @@ const config = { }, }; -module.exports = config; +const esmConfig = { + ...config, + entryPoints, + bundle: true, + sourcemap: true, + minify: true, + splitting: true, + format: 'esm', + define: { global: 'window' }, + target: ['esnext'], + chunkNames: 'chunks/[name]-[hash]', +}; + +const cjsConfig = { + ...config, + entryPoints: ['src/index.ts'], + bundle: true, + sourcemap: true, + minify: true, + platform: 'node', + target: ['node16'], +}; + +module.exports = { + esmConfig, + cjsConfig, +}; diff --git a/.esbuild/watch.js b/.esbuild/watch.js index 7b6b8a0e..ce04b380 100644 --- a/.esbuild/watch.js +++ b/.esbuild/watch.js @@ -1,21 +1,17 @@ -const config = require('./config'); +const { cjsConfig, esmConfig } = require('./config'); const esbuild = require('esbuild'); (async () => { try { const [cjsContext, esmContext] = await Promise.all([ esbuild.context({ - ...config, - format: 'cjs', - outdir: 'dist', - outExtension: { '.js': '.cjs' }, + cjsConfig, + outfile: 'dist/index.cjs.js', }), esbuild.context({ - ...config, - format: 'esm', + esmConfig, outdir: 'dist', - splitting: true, }), ]); From 10ad452196047cacb2cb7e43807e5dd247e31c8f Mon Sep 17 00:00:00 2001 From: Carlos Date: Thu, 27 Jun 2024 10:20:56 -0300 Subject: [PATCH 2/2] fix: watch options --- .esbuild/watch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.esbuild/watch.js b/.esbuild/watch.js index ce04b380..c3baf868 100644 --- a/.esbuild/watch.js +++ b/.esbuild/watch.js @@ -5,12 +5,12 @@ const esbuild = require('esbuild'); try { const [cjsContext, esmContext] = await Promise.all([ esbuild.context({ - cjsConfig, + ...cjsConfig, outfile: 'dist/index.cjs.js', }), esbuild.context({ - esmConfig, + ...esmConfig, outdir: 'dist', }), ]);