Wildcard or asterisk (*) vs named or selective import es6 javascript

前端 未结 4 925
一个人的身影
一个人的身影 2020-12-08 09:39

Just wondering which one is the best way to use import:

import * as Foo from \'./foo\';

VS:

import { bar, bar2, bar3 } from \'./fo

4条回答
  •  無奈伤痛
    2020-12-08 10:03

    If you use webpack with the dead code elimination provided by the new uglify, or rollupjs with tree-shaking, then the unused imports will be stripped.

    I partially agree with the airbnb styleguide to not to use wildcard imports, although javascripts wildcard imports do not suffer from the same diseases as for example pythons or javas wildcard imports, namely it does not pollute the scope with variable names defined in other modules (you can only access them by moduleB.foo, not foo when using import * as moduleB from ...).

    About the article on testing: I kindof understand the concerns, but I see nothing that cannot be solved there. You can mock the imports themselves with some custom module loader (a custom amd module loader is literally 15 lines of code), so you dont have to mess with the local scope of the tested module.

提交回复
热议问题