简体   繁体   English

从文件夹webpack重新导出所有文件

[英]reexport all files from a folder webpack

We're using a folder structure like this 我们正在使用这样的文件夹结构

components
  | Button.js
  | Nav.js
  | ...etc
  | index.js

somefolder
  |somefile.js

in the inderx file we're importing every component and reexporting it like this 在inderx文件中,我们导入每个组件并像这样重新导出它

 // index.js
  import Button from './Button'
  import Nav from './Nav'

  export {Button, Nav}

this way we can import many components into a file like this 这样我们就可以将许多组件导入到这样的文件中

 // somefile.js
  import {Button, Nav} from '../components'

Maintaining that index file is a bit of a pain though and discourages flexible use of components. 维护索引文件虽然有点痛苦,但不鼓励灵活使用组件。 I know that Webpack can import many files with a syntax like this 我知道Webpack可以使用这样的语法导入许多文件

function requireAll(r) { r.keys().forEach(r); }
requireAll(require.context('./components/', true, /\.js$/));

however, I didn't yet find a way to reexport all of these components to use them like above. 但是,我还没有找到一种方法来重新导出所有这些组件,以便像上面那样使用它们。

The desired outcome is to replace the index.js file with something that automates the process of bundling all the files from a folder without having to add every file manually. 理想的结果是将index.js文件替换为自动捆绑文件夹中所有文件的过程,而无需手动添加每个文件。

I think getting rid of the file index will not be the best solution and may cause questions from other developers. 我认为摆脱文件索引不是最好的解决方案,可能会引起其他开发人员的质疑。 But I can offer a slightly more simplified way: 但我可以提供一种稍微简化的方式:

index.js: index.js:

export * from './some-component1.js';
export * from './some-component2.js';

some-component1.js: 一些-component1.js:

export {SomeComponent1};

some-component2.js: 一些-component2.js:

export {SomeComponent2};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM