Skip to content

Commit

Permalink
tree shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Feb 19, 2025
1 parent 8dfd213 commit 9d0806b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
25 changes: 20 additions & 5 deletions packages/bundler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Handlebars.registerHelper('json', function (context) {
});

export async function bundler(config: BundlerConfig): Promise<string> {
// Get current file's directory path in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

Expand All @@ -37,11 +36,27 @@ export async function bundler(config: BundlerConfig): Promise<string> {
const compiledTemplate = Handlebars.compile(template);
const compiledCode = compiledTemplate(config);

// Bundle with esbuild
const result = await esbuild.transform(compiledCode, {
minify: true,
// Bundle with esbuild using stdin
const result = await esbuild.build({
stdin: {
contents: compiledCode,
loader: 'js',
resolveDir: path.resolve(__dirname, '..'),
},
bundle: true,
write: false,
format: 'iife',
minify: true,
treeShaking: true,
platform: 'node', // TODO: Add browser support
target: 'es2015',
splitting: false,
metafile: true, // Helps with tree-shaking analysis
mainFields: ['module'], // Prefer ESM versions for better tree-shaking
});

return result.code;
// Optionally log bundle analysis
console.log(await esbuild.analyzeMetafile(result.metafile));

return result.outputFiles[0].text;
}
4 changes: 2 additions & 2 deletions packages/bundler/src/templates/basic.js.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { elb } from '@elbwalker/utils/web';
import { debounce } from '@elbwalker/utils';
// Import specific utilities from their exact paths
import { elb, debounce } from '@elbwalker/utils';

console.log('Project configuration:', {
name: '{{name}}',
Expand Down

0 comments on commit 9d0806b

Please sign in to comment.