Doh leverages esbuild, a fast JavaScript bundler, to provide optimized bundle sizes and improved loading times. The integration includes:
esbuild_doh_plugin.js
)esbuild_processor.js
)The ESBuild processor is configured through the esbuild_processor
Pod:
Doh.Pod('esbuild_processor', {
esbuild_options: {
bundle: true,
outdir: DohPath('/dist/esm-bundles'),
format: 'esm',
splitting: true,
chunkNames: 'chunks/[name]-[hash]',
minify: true,
treeShaking: false,
keepNames: true,
preserveSymlinks: false,
target: 'esnext',
platform: 'browser',
mainFields: [
'browser',
'module',
'main'
],
logLevel: 'silent'
}
})
The primary options include:
Option | Description |
---|---|
bundle |
Combines all dependencies into a single file |
outdir |
Output directory for bundled files |
format |
Output format (esm, cjs, iife) |
splitting |
Enables code splitting for optimized chunks |
minify |
Reduces file size through minification |
treeShaking |
Removes unused code (disabled by default) |
target |
JavaScript target version |
platform |
Target platform (browser or node) |
mainFields |
Package entry point resolution order |
The custom Doh ESBuild plugin is configured via the esbuild_doh_plugin
Pod:
Doh.Pod('esbuild_doh_plugin', {
esbuild_doh_plugin: {
deduplicate_packages: true,
fix_files: true,
replace_native_modules: true,
allow_external_resolution: true,
exclude_from_deduplication: [],
include_as_node_builtins: [
'http', 'https','worker_threads', 'term.js', 'pty.js'
],
empty_modules: []
}
})
The key options include:
Option | Description |
---|---|
deduplicate_packages |
Enables package deduplication |
fix_files |
Applies fixes for problematic files |
replace_native_modules |
Replaces Node.js builtin modules with browser-compatible versions |
allow_external_resolution |
Allows external module resolution |
exclude_from_deduplication |
Packages to exclude from deduplication |
include_as_node_builtins |
Additional modules to treat as Node.js builtins |
empty_modules |
Modules to replace with empty implementations |
One of the key features of Doh's esbuild integration is intelligent package deduplication. This feature helps reduce bundle size by identifying and resolving duplicate package instances.
node_modules
directoriesYou can control deduplication through the following settings:
deduplicate_packages
: Enable/disable deduplication (default: true
)exclude_from_deduplication
: Array of package names to exclude from deduplication// Without deduplication
// - node_modules/package-a/node_modules/lodash (v4.17.20)
// - node_modules/package-b/node_modules/lodash (v4.17.21)
// - node_modules/lodash (v4.17.21)
// With deduplication
// All imports of lodash are redirected to the root instance (v4.17.21)
Doh integrates with the banal
tool to provide visualization of bundle sizes and dependencies.
dist/esm-bundles/build-meta.json
banal
CLI command processes this file to create an interactive visualization:node doh banal
node doh banal open
The banal
module is configured through its Doh module configuration:
Doh.Pod('banal', {
always_banal: false,
always_open_after_banal: false
});
Set always_banal
to true
to automatically generate visualizations after each build.
Doh's esbuild integration includes robust support for ECMAScript Modules (ESM):
// Generated at doh_js/manifests/browser_esm_manifest.json
{
"imports": {
"package-name": "/dist/esm-bundles/package-name.js",
// other mappings...
}
}
For browser compatibility, Doh uses the esbuild-plugins-node-modules-polyfill
plugin to provide browser-compatible implementations of Node.js built-in modules.
The following Node.js builtins are polyfilled by default:
http
and https
for network requestsworker_threads
for multithreadinginclude_as_node_builtins
To use esbuild in your Doh project:
Doh.Install('esbuild', [
'npm:esbuild',
'npm:esbuild-plugins-node-modules-polyfill'
]);
Doh.Pod('my_project', {
esbuild_processor: {
// Custom esbuild options...
},
esbuild_doh_plugin: {
// Custom plugin options...
}
});
node doh esbuild
You can configure certain modules to be treated as external (not bundled):
Doh.Pod('esbuild_processor', {
esbuild_options: {
external: [
'large-package',
'problematic-package'
]
}
});
The esbuild_doh_plugin
includes hooks for custom file transformations:
toForwardSlash
The plugin includes sophisticated import path resolution:
mainFields
Duplicate packages in bundle:
exclude_from_deduplication
Missing Node.js API in browser:
include_as_node_builtins
Large bundle sizes:
treeShaking: true
in esbuild optionsbanal
to identify large dependenciesThe esbuild plugin includes detailed logging:
logLevel: 'debug'
in esbuild options for verbose outputFor additional help with esbuild integration: