简体   繁体   English

webpack从多个条目文件导出类

[英]webpack export classes from multiple entry files

I'm using webpack to bundle up a framework for use by 3rd parties. 我正在使用webpack捆绑一个供第三方使用的框架。 This framework should expose multiple ES6 classes. 该框架应该公开多个ES6类。 Building in a modular fashion, I have written one class per file. 以模块化方式构建,我为每个文件编写了一个类。 What I want to do is build all these files together, and have them bundled up under a given "namespace". 我想要做的是将所有这些文件一起构建,并将它们捆绑在给定的“命名空间”下。 Example: 例:

apples.js export class Apples {...} apples.js export class Apples {...}
oranges.js export class Oranges {...} oranges.js export class Oranges {...}

webpack.config.js: webpack.config.js:

module.exports = {
  entry: ['./src/apples.js', './src/oranges.js'],
  output: {
    path: './dist',
    filename: 'fruit.js',
    library: 'Fruit',
    libraryTarget: 'umd'
  }
}

However, if I load up this library in the browser and type fruit into the console, I only see the Oranges object under Fruit. 但是,如果我在浏览器中加载此库并在控制台中键入fruit ,我只会看到Fruit下的Oranges对象。 Only the last entry file is being exposed back out in the library. 只有最后一个条目文件在库中显示出来。 Surely enough, the webpack docs confirm this behavior: 当然,webpack文档证实了这种行为:

If you pass an array: All modules are loaded upon startup. 如果传递数组:启动时加载所有模块。 The last one is exported. 最后一个是导出的。 http://webpack.github.io/docs/configuration.html#entry http://webpack.github.io/docs/configuration.html#entry

My current workaround is to export all my classes from one file, but it's getting pretty unwieldy. 我目前的解决方法是从一个文件导出我的所有类,但它变得非常笨拙。

How can I go about setting up a library with multiple entry files that are all exported? 如何设置包含所有导出的多个条目文件的库? Or am I going about something the wrong way here? 或者我在这里做错了什么?

I think you'd better use a entry.js file to indicate how you organize your serveral modules. 我认为你最好使用entry.js文件来表明你如何组织你的几个模块。

import Apples from './apples';
import Oranges from './oranges';

export {
    Apples,
    Oranges
};

By the way, if you don't want to write such stupid codes by your own, use Gulp/Grunt to generate the file 'entry.autogenerated.js' by some logic then run webpack with the entry 'entry.autogenerated.js'. 顺便说一句,如果您不想自己编写这样的愚蠢代码,请使用Gulp / Grunt通过某些逻辑生成文件'entry.autogenerated.js'然后使用条目'entry.autogenerated.js'运行webpack 。

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

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