Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Dead Code Elimination with dynamic import() statements. #747

Closed
osdevisnot opened this issue Feb 3, 2021 · 2 comments
Closed

Comments

@osdevisnot
Copy link
Contributor

hi @evanw, I know this has been brought up before in #16 and various other threads (#661, #453, #322, #56), but I wanted to ensure the dead code elimination is covered when using dynamic imports.

More precisely, I want to ensure below test case from tooling.report is covered.

This test bundles two modules - an entry module, and a utils.js module it dynamically imports to create a split point. The dynamically imported module has two exports, but only the logCaps export is used.

index.js

(async function() {
  const { logCaps } = await import('./utils.js');
  logCaps('This is index');
})();

utils.js

export function logCaps(msg) {
  console.log(msg.toUpperCase());
}

export function thisIsNeverCalled(msg) {
  return msg + '!';
}

Expected Output:

Once built for production, the thisIsNeverCalled function from utils.js should not be present in the resulting bundle(s).

Output as of v0.8.39 of esbuild:

// ...cjs interop code removed to reduce noise.

// src/utils.js
var require_utils = __commonJS((exports) => {
  __export(exports, {
    logCaps: () => logCaps,
    thisIsNeverCalledEither: () => thisIsNeverCalledEither
  });
  function logCaps(msg) {
    console.log(msg.toUpperCase());
  }
  function thisIsNeverCalledEither(msg) {
    return msg + "!";
  }
});

// src/index.js
(function() {
  return __async(this, null, function* () {
    const {logCaps} = yield Promise.resolve().then(() => __toModule(require_utils()));
    logCaps("This is index");
  });
})();
@evanw
Copy link
Owner

evanw commented Feb 3, 2021

Sure, sounds good. I can keep this issue open to track this feature.

@osdevisnot
Copy link
Contributor Author

The work in tooling.report has paused and we can always re-evaluate this effort later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants