using the … spread syntax in javascript es6 named exports

后端 未结 1 1780
借酒劲吻你
借酒劲吻你 2020-12-15 04:54

I am attempting to import everything from a library as a hash, modify it, and re-export the modified hash, without knowing all of the named exports in a library. For example

相关标签:
1条回答
  • 2020-12-15 05:27

    Object rest spread is stage 3 proposal and not a part of any spec (will probably be included in ES2018).

    More importantly, export has syntax that mimics existing JS syntax but doesn't interpret { ... } as an expression. export syntax was strictly defined because ES2015 modules are supposed to be statically analyzed. This is one of their benefits, but it requires the developer to specify exports and imports explicitly.

    Since { ...wrappedReactBootstrap } introduces dynamic export (it was used here exactly for this purpose), it isn't supported by ES2015 module export and it is very unlikely that it will be.

    If it is necessary to provide dynamic behaviour for the export, it can be exported and imported as named or default object.

    import * as reactBootstrap from 'react-bootstrap';
    
    export default doFunnyThingsTo(reactBootstrap);
    

    And used like

    import wrappedReactBootstrap from '...';
    
    const { funny, thing } = wrappedReactBootstrap;
    

    Obviously, wrappedReactBootstrap object won't get the benefits of ES2015 modules this way, e.g. tree-shaking.

    0 讨论(0)
提交回复
热议问题