babel-plugin-transform-commonjs icon indicating copy to clipboard operation
babel-plugin-transform-commonjs copied to clipboard

Issue declaring "keyword" at export

Open reggi opened this issue 3 years ago • 3 comments

I have exports.super and it's converting to export let super which doesn't compile. Example here.

Any chance that rather then declaring each variable there can be one export?

export {
    super: 'something'
}

Also I would love the ability to ignore throwing this error and keeping in those requires I can do something with them with another plugin after. This isn't a big deal as I can patch-package to stop it from throwing.

throw new Error(`Invalid require signature: ${path.toString()}`);

reggi avatar Jul 08 '20 06:07 reggi

Thanks for reporting, I was able to reproduce the issue.

input:

exports.super = () => {};

output:

var module = {
  exports: {}
};
var exports = module.exports;

exports.super = () => {};

export let super = exports.super;
export default module.exports;

The fix here, if I'm understanding correctly, is to filter out named exports when it's a reserved word. We already do this for invalid identifiers, we just need to expand this for reserved words.

tbranyen avatar Jul 14 '20 04:07 tbranyen

@tbranyen if you filter it out then the module won't line up with the source.

Is it possible to do an exported object, and never define the variable ?

Like this:

export {
    super: 'something'
}

reggi avatar Jul 14 '20 14:07 reggi

@reggi the export name would still available on the default export, which is exactly what you're asking for here:

Is it possible to do an exported object, and never define the variable

The code you've shown:

export {
    super: 'something'
}

is not valid JavaScript and will not work.

var module = {
  exports: {}
};
var exports = module.exports;

exports.super = () => {};
export default module.exports;

Is how the output will look after I apply the patch.

tbranyen avatar Jul 14 '20 16:07 tbranyen