Skip to content

Commit 9d0806b

Browse files
tree shaking
1 parent 8dfd213 commit 9d0806b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

packages/bundler/src/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Handlebars.registerHelper('json', function (context) {
1515
});
1616

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

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

40-
// Bundle with esbuild
41-
const result = await esbuild.transform(compiledCode, {
42-
minify: true,
39+
// Bundle with esbuild using stdin
40+
const result = await esbuild.build({
41+
stdin: {
42+
contents: compiledCode,
43+
loader: 'js',
44+
resolveDir: path.resolve(__dirname, '..'),
45+
},
46+
bundle: true,
47+
write: false,
4348
format: 'iife',
49+
minify: true,
50+
treeShaking: true,
51+
platform: 'node', // TODO: Add browser support
52+
target: 'es2015',
53+
splitting: false,
54+
metafile: true, // Helps with tree-shaking analysis
55+
mainFields: ['module'], // Prefer ESM versions for better tree-shaking
4456
});
4557

46-
return result.code;
58+
// Optionally log bundle analysis
59+
console.log(await esbuild.analyzeMetafile(result.metafile));
60+
61+
return result.outputFiles[0].text;
4762
}

packages/bundler/src/templates/basic.js.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { elb } from '@elbwalker/utils/web';
2-
import { debounce } from '@elbwalker/utils';
1+
// Import specific utilities from their exact paths
2+
import { elb, debounce } from '@elbwalker/utils';
33

44
console.log('Project configuration:', {
55
name: '{{name}}',

0 commit comments

Comments
 (0)