webpack从多个条目文件导出类 [英] webpack export classes from multiple entry files

查看:224
本文介绍了webpack从多个条目文件导出类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用webpack捆绑一个供第三方使用的框架。该框架应该公开多个ES6类。以模块化方式构建,我为每个文件编写了一个类。我想要做的是将所有这些文件一起构建,并将它们捆绑在给定的命名空间下。示例:

I'm using webpack to bundle up a framework for use by 3rd parties. This framework should expose multiple ES6 classes. 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 {...}

oranges。 js 导出类Oranges {...}

apples.js export class Apples {...}
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'
  }
}

但是,如果我在浏览器中加载此库并在控制台中输入 fruit ,我只看到Fruit下的Oranges对象。只有最后一个条目文件在库中显示出来。当然,webpack文档确认了这种行为:

However, if I load up this library in the browser and type fruit into the console, I only see the Oranges object under Fruit. Only the last entry file is being exposed back out in the library. Surely enough, the webpack docs confirm this behavior:


如果传递数组:所有模块在启动时加载。最后一个是导出的。
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?

推荐答案

我认为你最好使用entry.js文件来表示如何组织你的几个模块。

I think you'd better use a entry.js file to indicate how you organize your serveral modules.

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

export {
    Apples,
    Oranges
};

顺便说一句,如果您不想自己编写这样的愚蠢代码,请使用Gulp / Grunt通过某些逻辑生成文件'entry.autogenerated.js'然后使用条目'entry.autogenerated.js'运行webpack。

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'.

这篇关于webpack从多个条目文件导出类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆