使用 webpack 将单个模块导出为库 [英] exporting single module as library with webpack

查看:35
本文介绍了使用 webpack 将单个模块导出为库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 web 应用程序,其中所有的 javascript 都捆绑在 webpack 中.

I've got a web-app with all the javascript bundled into webpack.

我想通过客户端 API 向用户公开方法的子集.

I want to expose a subset of methods to the user via a clientside api.

我以为我可以通过图书馆做到这一点

I thought I could do this via a library

 output: {
    path: BUILD_DIR,
    filename: 'bundle.js',
    publicPath: 'https://localhost:3000',
    library: 'myLibrary'
  }

这样做,我可以在控制台中获取myLibary,但它是一个空对象.我不希望我的包中的每个方法都公开,但我想要特定的公共方法可用,但我不知道如何做到这一点.

Doing this, I can get myLibary in the console, but it is an empty object. I don't want every method in my bundle exposed publicly, but I'd like to specific public methods available, but I can't figure out how to do this.

推荐答案

您需要在入口点导出您想要公开的内容.它就像常规导出一样工作,您可以只包含您想要的导出.例如这个入口点:

You need to export, what you want to expose, in your entry point. It works just like regular exports and you can include only the ones you want. For example with this entry point:

import module1 from './module';
import publicModule from './otherModule';
import privateModule from './privateModule';

function privateFun() {
  console.log('not exposed');
}

export function publicFun() {
  console.log('is exposed as myLibrary.public');
}

// Re-export, with possible rename
export { publicModule, module1 as firstModule };

只有你导出的东西被暴露,如果你想导出其他模块,你可以导入并重新导出它们.在这种情况下,您将可以访问以下属性:

Only the things you export are exposed, if you want to export other modules you import and re-export them. In this case you'll have access to the following properties:

myLibrary.firstModule
myLibrary.publicFun
myLibrary.publicModule

这篇关于使用 webpack 将单个模块导出为库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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