使用webpack创buildnpm包

我正在创build一个npm软件包,并使用webpack的加载器,如babel,eslint等。但是,我假设最终编译版本的软件包应该只包含一个模块,没有webpackBootstrap 。

我目前的软件包,webpackconfiguration和源 。 我把它剥下来,让它“工作”。

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

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,所以让我知道,如果你需要更多的信息。

将output.libraryTarget设置为umd 。 这会给你一些容易从各种模块系统(全球,AMD,CommonJS)消费的东西。

output.library是另一个有用的字段设置。 这应该匹配你想要的全局库的名称。


除此之外还有另一个问题。 为了使导入工作npm link需要使用。 此function在开发过程中非常有用。 您可以通过npm unlink链接来恢复npm unlink