Skip to content

Commit

Permalink
Create issue evanw/esbuild#1551
Browse files Browse the repository at this point in the history
  • Loading branch information
marc136 committed Aug 25, 2021
1 parent 14ab54d commit 56276e3
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions esbuild-bug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// I created https://github.com/evanw/esbuild/issues/1551 for this problem

const esbuild = require('esbuild');
const fs = require('fs');
const os = require('os');

function print(caption, code) {
console.log(caption, '\n```js\n', code.toString().trim(), '\n```\n');
}

const esm = `// as ESM
function unusedFn(a) {
return a + 1;
}
const a = 5;
let b = 7;
export const c = 2;
`;

const iife = `// as IIFE
function unusedFn(a) {
return a + 1;
}
const a = 5;
let b = 7;
window.c = 2;
`;

const options = {
target: "es2020",
format: "esm",
};

let result = esbuild.transformSync(iife, Object.assign({}, options, { format: "iife" }));
print('Unused code is removed with format:iife', result.code);

result = esbuild.transformSync(esm, options);
print('Unused code is kept with format:esm', result.code);

const tmpFile = `${os.tmpdir()}/esbuild-test-${Date.now()}.js`
fs.writeFileSync(tmpFile, esm, { encoding: 'utf8' });
esbuild.buildSync(Object.assign(options, {
entryPoints: [tmpFile],
outfile: tmpFile,
allowOverwrite: true,
bundle: true,
}));
result = fs.readFileSync(tmpFile, { encoding: 'utf8' });
fs.rmSync(tmpFile);
print('Unused code is removed with format:esm and bundle:true', result);

0 comments on commit 56276e3

Please sign in to comment.