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

Mixing named exports and re-exports in Babel #36

Closed
just-boris opened this issue Feb 14, 2021 · 7 comments · Fixed by #39
Closed

Mixing named exports and re-exports in Babel #36

just-boris opened this issue Feb 14, 2021 · 7 comments · Fixed by #39

Comments

@just-boris
Copy link

This code transpiled by Babel does not detect the re-export:

export {named} from './named';
export * from './re-export';

Looking at the test-cases I see that this re-export alone is supported:

exports.__esModule = true;

var _reExport = require("./re-export");

Object.keys(_reExport).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (key in exports && exports[key] === _reExport[key]) return;
  exports[key] = _reExport[key];
});

The code above correctly detects ./re-export. However, adding an extra named export changes the output slightly

exports.__esModule = true;
var _exportNames = {
  named: true
};
exports.named = void 0;

var _named = require("./named");

exports.named = _named.named;

var _reExport = require("./re-export");

Object.keys(_reExport).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _reExport[key]) return;
  exports[key] = _reExport[key];
});

Link to the Babel playground.

Runing this code through the lexer returns { exports: [ '__esModule', 'named' ], reexports: [] }, ./re-export was not detected

@guybedford
Copy link
Collaborator

Thanks @just-boris for posting, yes we should definitely support these cases, and ideally aim to get this upstreamed before the coming April releases if possible.

@nicolo-ribaudo seems I missed this one - wondering if there might be other scenarios I might have missed as well with the Babel output, now would be a good time to check if you're able to assist.

@nicolo-ribaudo
Copy link
Contributor

I'm not able to check until Thursday, but I could try setting up an automated script that checks that Babel's fixtures work with this package.

@guybedford
Copy link
Collaborator

@nicolo-ribaudo that sounds very useful to me. As I say we have a milestone coming up with April so would be a good time to ship any fixes this month already if possible. It might be too late for Node.js 12 but we can at least catch Node.js 14+.

@guybedford
Copy link
Collaborator

(I'll make sure to allocate some time to this myself in the coming week or so too)

@nicolo-ribaudo
Copy link
Contributor

nicolo-ribaudo commented Feb 21, 2021

@guybedford I created https://github.com/nicolo-ribaudo/validate-cjs-module-lexer, which compares the output of cjs-module-lexer with the ESM AST for Babel's fixtures (https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-modules-commonjs/test/fixtures).

This is the current output
OK auxiliary comment / overview
OK class properties / live rewrite
OK class properties / private
OK class properties / private method
OK class properties / public
OK disable strict mode / strictMode false
FAIL interop / export all
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	    exports: [
	      'default'
	    ],
	+   reexports: []
	-   reexports: [
	-     'react'
	-   ]
	  }
OK interop / export default
OK interop / export default 10
OK interop / export default 11
OK interop / export default 2
OK interop / export default 3
OK interop / export default 4
OK interop / export default 5
OK interop / export default 6
OK interop / export default 7
OK interop / export default 8
OK interop / export default 9
OK interop / export destructured
OK interop / export from
OK interop / export from 2
OK interop / export from 3
OK interop / export from 4
OK interop / export from 5
OK interop / export from 6
OK interop / export from 7
OK interop / export from 8
OK interop / export named
OK interop / export named 2
OK interop / export named 3
OK interop / export named 4
OK interop / export named 5
OK interop / exports variable
OK interop / hoist function exports
OK interop / illegal export esmodule
OK interop / illegal export esmodule 2
OK interop / imports
OK interop / imports default
OK interop / imports glob
OK interop / imports hoisting
OK interop / imports mixing
OK interop / imports named
OK interop / imports ordering
OK interop / module shadow
OK interop / multi load
OK interop / overview
OK interop / remap
OK interop loose / export default
OK interop loose / export default 10
OK interop loose / export default 11
OK interop loose / export default 2
OK interop loose / export default 3
OK interop loose / export default 4
OK interop loose / export default 5
OK interop loose / export default 6
OK interop loose / export default 7
OK interop loose / export default 8
OK interop loose / export default 9
OK interop loose / export destructured
OK interop loose / export from
OK interop loose / export from 2
OK interop loose / export from 3
OK interop loose / export from 4
OK interop loose / export from 5
OK interop loose / export from 6
OK interop loose / export from 7
OK interop loose / export from 8
OK interop loose / export named
OK interop loose / export named 2
OK interop loose / export named 3
OK interop loose / export named 4
OK interop loose / export named 5
OK interop loose / exports variable
OK interop loose / hoist function exports
OK interop loose / illegal export esmodule
OK interop loose / illegal export esmodule 2
OK interop loose / imports
OK interop loose / imports default
OK interop loose / imports glob
OK interop loose / imports hoisting
OK interop loose / imports mixing
OK interop loose / imports named
OK interop loose / imports ordering
OK interop loose / module shadow
OK interop loose / multi load
OK interop loose / overview
OK interop loose / remap
FAIL interop module string names / export from
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'some exports'
	-   ],
	    reexports: []
	  }
FAIL interop module string names / export from string
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'some exports'
	-   ],
	    reexports: []
	  }
FAIL interop module string names / export from string as string
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'some exports'
	-   ],
	    reexports: []
	  }
FAIL interop module string names / export named
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	    exports: [
	      'bar',
	-     'default exports'
	    ],
	    reexports: []
	  }
OK interop module string names / export named string can be identifier
OK interop module string names / import named
OK interop module string names / import named string can be identifier
FAIL interop module string names loose / export from
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'some exports'
	-   ],
	    reexports: []
	  }
FAIL interop module string names loose / export from string
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'some exports'
	-   ],
	    reexports: []
	  }
FAIL interop module string names loose / export from string as string
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'some exports'
	-   ],
	    reexports: []
	  }
FAIL interop module string names loose / export named
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	    exports: [
	      'bar',
	-     'default exports'
	    ],
	    reexports: []
	  }
OK interop module string names loose / export named string can be identifier
OK interop module string names loose / import named
OK interop module string names loose / import named string can be identifier
OK lazy dep / import default
OK lazy dep / import named
OK lazy dep / import namespace
OK lazy dep / reexport all
FAIL lazy dep / reexport default
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'default'
	-   ],
	    reexports: []
	  }
FAIL lazy dep / reexport named
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'named'
	-   ],
	    reexports: []
	  }
FAIL lazy dep / reexport namespace
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'namespace'
	-   ],
	    reexports: []
	  }
OK lazy dep / sideeffect
OK lazy local / import default
OK lazy local / import named
OK lazy local / import namespace
OK lazy local / reexport all
OK lazy local / reexport default
OK lazy local / reexport named
OK lazy local / reexport namespace
OK lazy local / sideeffect
OK lazy whitelist / import default
OK lazy whitelist / import named
OK lazy whitelist / import namespace
OK lazy whitelist / reexport all
FAIL lazy whitelist / reexport default
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	+   exports: [],
	-   exports: [
	-     'default'
	-   ],
	    reexports: []
	  }
FAIL lazy whitelist / reexport named
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	    exports: [
	-     'named1',
	      'named2'
	    ],
	    reexports: []
	  }
FAIL lazy whitelist / reexport namespace
	Expected values to be strictly deep-equal:
	+ actual - expected

	  {
	    exports: [
	-     'namespace1',
	      'namespace2'
	    ],
	    reexports: []
	  }
OK lazy whitelist / sideeffect
OK misc / class static block
OK misc / copy getters setters
OK misc / copy getters setters star
OK misc / for of in export
OK misc / import const throw
OK misc / import shadowed assign
OK misc / local exports decl
OK misc / module exports
OK misc / reference source map
OK misc / undefined this arrow function
OK misc / undefined this computed class method 1
OK misc / undefined this computed class method 2
OK misc / undefined this computed class method 3
OK misc / undefined this computed class property name
OK misc / undefined this root call
OK misc / undefined this root declaration
OK misc / undefined this root reference
OK noInterop / export from
OK noInterop / import default only
OK noInterop / import wildcard
OK regression / 4462 T7565
OK regression / T7160
OK regression / T7165
OK regression / T7199
OK regression / T7272
OK regression / es3 compatibility
OK regression / es3 compatibility class
OK regression / es3 compatibility function
OK regression / es3 compatibility named class
OK regression / es3 compatibility named function
OK regression / issue 9155
OK regression / issue 9611
OK regression / lazy 7176
OK source map / exec
OK strict / export
OK strict / export 1
OK strict / export 2
OK strict / export 3
FAIL strict / export all
	Expected values to be strictly deep-equal:
	+ actual - expected ... Lines skipped

	  {
	    exports: [
	...
	      'z'
	    ],
	+   reexports: []
	-   reexports: [
	-     'mod'
	-   ]
	  }
OK strict / export const destructuring array
OK strict / export const destructuring array default params
OK strict / export const destructuring array rest
OK strict / export const destructuring deep
OK strict / export const destructuring object
OK strict / export const destructuring object default params
OK strict / export const destructuring object rest
OK strict / import
OK strict / import wildcard
OK update expression / negative suffix
OK update expression / positive suffix

@guybedford
Copy link
Collaborator

@nicolo-ribaudo thanks for checking these thoroughly, putting these on my list and hopefully will be able to tackle some next month.

@nicolo-ribaudo
Copy link
Contributor

@guybedford I can work on this.

Note that all the other failures are not actually a problem because:

  1. Node.js doesn't support string import names
  2. The "lazy" tests are not Babel's default output, but an advanced option

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

Successfully merging a pull request may close this issue.

3 participants