Skip to content

commonjs: simplify the output for a single default import - #3585

Closed
hzoo wants to merge 5 commits into
masterfrom
simplify-single-default-import
Closed

hzoo wants to merge 5 commits into
masterfrom
simplify-single-default-import

Conversation

@hzoo

@hzoo hzoo commented Jul 18, 2016 •

Copy link
Copy Markdown
Member

Ref https://twitter.com/dan_abramov/status/755104686471049218 (thanks to @gaearon for the question/suggestion).

screen shot 2016-07-18 at 7 00 52 pm

src change

  • might be flawed, but the approach was to check (for each individual import) if there was only a single specifier and if that single specifier was a default.

new actual test

import a from "a";
import b from "b";
a;
b;

expected test of old code 😢

var _a = require("a");
var _a2 = babelHelpers.interopRequireDefault(_a);
var _b = require("b");
var _b2 = babelHelpers.interopRequireDefault(_b);
_a2.default;
_b2.default;

new expected test 😸

var _a = babelHelpers.interopRequireDefault(require("a"));
var _b = babelHelpers.interopRequireDefault(require("b"));
_a.default;
_b.default;

Old test changes

FYI: I remembered that babylon had a feature where if you don't create an expected.js file, it will be generated for you automatically. I remembered there might of been something like that for babel and so I found this. I just uncommented this for the tests to change to the new expected behavior (saved a lot of time, and yes it should be probably be fixed/added under some flag and added to contributing in https://github.com/babel/babel/blob/master/CONTRIBUTING.md#writing-tests).


Will need to think about where else this could be applied (just have to look at some existing tests). I think we can do it for wildcard too)

  • wildcard change

@hzoo hzoo added the PR: Polish 💅 A type of pull request used for our changelog categories label Jul 18, 2016
@hzoo hzoo added this to the Next Patch milestone Jul 18, 2016
@codecov-io

codecov-io commented Jul 18, 2016 •

Copy link
Copy Markdown

Current coverage is 87.56%

Merging #3585 into master will increase coverage by 0.08%

@@             master      #3585   diff @@
==========================================
  Files           194        194          
  Lines          8949       9306   +357   
  Methods        1007       1052    +45   
  Messages          0          0          
  Branches       2004       2107   +103   
==========================================
+ Hits           7829       8149   +320   
- Misses         1120       1157    +37   
  Partials          0          0          

Powered by Codecov. Last updated by 40ec299...bb6b9bb

@gaearon

gaearon commented Jul 18, 2016

Copy link
Copy Markdown
Member

Thanks! 👍

});
} else {
uid = addRequire(source, maxBlockHoist);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you validate this with a file like

import Foo from 'foo';
import {named} from 'foo';

console.log(Foo, named);

I think at the moment we merge the require calls for multiple imports with the same source, so this might be an issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'm getting

import a from "bar";
import {b} from "bar";
console.log(a, b);
var _bar = require("bar");
var _bar2 = babelHelpers.interopRequireDefault(_bar);
console.log(_bar2.default, _bar.b);

@hzoo

hzoo commented Jul 19, 2016 •

Copy link
Copy Markdown
Member Author

Ok, slight complication is that amd/umd depend on commonjs so this breaks those (this is probably the reason why it was split up), so need to add some logic to amd as well to account for this..

@hzoo
hzoo force-pushed the simplify-single-default-import branch from e0cf517 to 52dee1c Compare July 19, 2016 15:39
@hzoo
hzoo force-pushed the simplify-single-default-import branch from 52dee1c to 3665f3f Compare July 19, 2016 15:52
@hzoo

hzoo commented Jul 19, 2016 •

Copy link
Copy Markdown
Member Author

So @loganfsmyth brought up the issue of users who might be using the es2015 preset but with a fixed version of the umd/amd plugin which would fail in the current setup

"babel-preset-es2015": "6.0.0",
"babel-plugin-es2015-modules-umd": "6.1.0"

in this case the commonjs plugin which is part of es2015 would be updated and the umd/amd plugin would still be pinned so it would break because it depends on both working?

@jdalton

jdalton commented Jul 19, 2016 •

Copy link
Copy Markdown
Member

@hzoo

Old test changes

I did a similar mod when cleaning up my plugin tests 😀

@jmm

jmm commented Jul 26, 2016

Copy link
Copy Markdown
Member

@hzoo can that be solved without being able to version the packages independently and publish a new major? (And is the benefit worth the effort?)

@hzoo

hzoo commented Jul 27, 2016 •

Copy link
Copy Markdown
Member Author

I'm not sure it can? Both plugins inherit from cjs transform so I was trying to figure out how to only run something for the cjs transform.. but if they both run everything in the cjs plugin and you can't change the other 2 plugins (because of the pinned dependency) it would break since it changes the default behavior of the cjs transform. Basically did bb6b9bb (the last commit)

@hzoo

hzoo commented Aug 2, 2017 •

Copy link
Copy Markdown
Member Author

Ok looks like we can't really do this to support live bindings according to @loganfsmyth, @benjamn. I'l keep up by rebased version of this PR at #6039 if people really want this as another option, but it can go in later after 7.x unless someone else wants to review/finish it up.

new PR: #6039

@hzoo hzoo closed this Aug 2, 2017
@xtuc
xtuc deleted the simplify-single-default-import branch August 3, 2017 07:08
@hzoo

hzoo commented Sep 13, 2017

Copy link
Copy Markdown
Member Author

This should land in beta.1

@lock lock Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Polish 💅 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants