使用webpack创建npm包 [英] Create npm package using webpack

查看:188
本文介绍了使用webpack创建npm包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个npm软件包,并使用webpack加载诸如babel,eslint等的加载程序.但是,我假设该软件包的最终编译版本应仅包含一个模块,而没有

I'm creating an npm package and using webpack for loader like babel, eslint etc.. However I'm under assumption that final compiled version of the package should only contain that one module, without webpackBootstrap.

我当前的软件包,webpack配置和源.我将其剥离以使其正常工作".

My current package, webpack config and source. I stripped it down to just make it "work".

我采取的步骤来检查它是否正常工作:

Steps I took to check if it's working:

npm install
npm run build
npm install -g .
node
var test = require('test-package');

导致此错误:

Error: Cannot find module 'test-package'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at repl:1:12
    at REPLServer.defaultEval (repl.js:248:27)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:412:12)
    at emitOne (events.js:82:20)

我是webpack和npm的新手,所以如果您需要更多信息,请告诉我.

I'm new to webpack and npm so let me know if you need any more information.

推荐答案

设置输出.libraryTarget umd.这将为您提供易于从各种模块系统(全局,AMD,CommonJS)中使用的东西.

Set output.libraryTarget to umd. That will give you something that's easy to consume from various module systems (global, AMD, CommonJS).

output.library 是另一个有用的字段.那应该与您想要的全局库名称相匹配.

output.library is another useful field to set. That should match the name of your library global you want.

除此之外,还有另一个问题.要进行导入工作,需要使用npm link.此功能在开发过程中非常有用.您可以通过npm unlink还原链接.

There was another problem beyond this. To make the import work npm link needed be used. This feature is highly useful during development. You can revert a link through npm unlink.

这篇关于使用webpack创建npm包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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