Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

feat: improve esm build #699

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 4 additions & 8 deletions .esbuild/build.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
34 changes: 32 additions & 2 deletions .esbuild/config.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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,
};
12 changes: 4 additions & 8 deletions .esbuild/watch.js
Original file line number Diff line number Diff line change
@@ -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,
}),
]);

Expand Down
Loading