简体   繁体   English

使用webpack创建npm包

[英]Create npm package using webpack

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 . 我正在创建一个npm包并使用webpack作为加载程序,如babel,eslint等。但是我假设程序包的最终编译版本应该只包含那个模块,没有webpackBootstrap

My current package, webpack config and source . 我当前的包,webpack配置和源代码 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');

Resulting in this error: 导致此错误:

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)

I'm new to webpack and npm so let me know if you need any more information. 我是webpack和npm的新手,所以如果您需要更多信息请告诉我。

Set output.libraryTarget to umd . output.libraryTarget设置为umd That will give you something that's easy to consume from various module systems (global, AMD, CommonJS). 这将为您提供易于从各种模块系统(全局,AMD,CommonJS)使用的东西。

output.library is another useful field to set. output.library是另一个有用的字段。 That should match the name of your library global you want. 这应该与您想要的库全局名称相匹配。


There was another problem beyond this. 除此之外还有另一个问题。 To make the import work npm link needed be used. 要使用导入工作,需要使用npm link This feature is highly useful during development. 此功能在开发过程中非常有用。 You can revert a link through npm unlink . 您可以通过npm unlink恢复npm unlink

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

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