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

Transform require/import syntax of "externals" to match output format? #572

Closed
popeindustries opened this issue Dec 2, 2020 · 2 comments

Comments

@popeindustries
Copy link

Presently (0.8.17), when marking a dependency as external (via externals argument or custom onResolve plugin), the original import syntax is preserved instead of being transformed to match that of the output format.

For example, when bundling a cjs file to esm:

const ms = require('ms');
module.exports = ms('10m');

...with externals: ['ms'] and format: 'esm' results in:

var __commonJS = (callback, module) => () => {
  if (!module) {
    module = {exports: {}};
    callback(module.exports, module);
  }
  return module.exports;
};

// input.js
var require_input = __commonJS((exports) => {
  var ms = require("ms");
  exports.dep = ms("10m");
});
export default require_input();

...which preserves the original var ms = require("ms") instead of transforming to import ms from "ms" to match the output format.

Perhaps it's out of scope, but fixing something like this in user space, without access to the AST, would be quite error prone.

@evanw
Copy link
Owner

evanw commented Dec 2, 2020

This is a duplicate of #566.

fixing something like this in user space, without access to the AST, would be quite error prone

It's actually easy to do this with a plugin: #566 (comment).

@popeindustries
Copy link
Author

Sorry for the duplicate! I'll take a look at the plugin solution.

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