Skip to content
Draft
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
24 changes: 22 additions & 2 deletions modules/dev-tools/src/configuration/get-esbuild-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {join} from 'path';
import util from 'util';
import {getOcularConfig} from '../helpers/get-ocular-config.js';
import ext from 'esbuild-plugin-external-global';
import type {BuildOptions} from 'esbuild';
import type {BuildOptions, Plugin} from 'esbuild';

/**
* Get list of dependencies to exclude using esbuild-plugin-external-global
Expand Down Expand Up @@ -67,6 +67,25 @@ function umdWrapper(libName: string | undefined) {
};
}

/**
* ESBuild plugin to inline important ESM-only dependencies, for CJS compatibility.
* Reference: https://github.com/evanw/esbuild/issues/3442
*/
const inlineESMOnly = (): Plugin => {
const packageRoot = process.cwd();

return {
name: 'inline-esm-only',
setup(build) {
// TODO: Detect ESM-only from package.json, instead of hard-coding package names?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is difficult to do, can we make it a ocularrc config instead of hard-coding it here?

build.onResolve({filter: /^@mapbox\/tiny\-sdf$/}, () => {
const path = join(packageRoot, 'node_modules/@mapbox/tiny-sdf/index.js');
return {path, external: false};
});
}
};
};

/** Returns esbuild config for building .cjs bundles */
export async function getCJSExportConfig(opts: {
input: string;
Expand All @@ -81,7 +100,8 @@ export async function getCJSExportConfig(opts: {
target: 'node16',
packages: 'external',
sourcemap: true,
logLevel: 'info'
logLevel: 'info',
plugins: [inlineESMOnly()]
};
}

Expand Down