我试图弄清楚这个webpack库是如何工作的。我使用的示例代码来自此处
https://github.com/webpack/webpack/tree/master/examples/multi-part-library
当我跑步时#34; webpack"它产生" dist / MyLibrary.alpha.js"和" dist / MyLibrary.beta.js"在同一个文件夹中。然后我创建一个test.js来使用库并运行它。除了运行webpack和创建test.js之外,样本文件中没有任何内容被更改。消费alpha.js工作正常,但MyLibrary.alpha没有。
//inside test.js
1 const c = require('./alpha') //ran with no error, and correct output
2 const c = require('./dist/MyLibrary.alpha') //ran with error below
3 console.log(c)
//ran test.js with either line 1 or 2 commented out
node test
//error produced when line 1 is commented out
C:\Users\USER\Desktop\Repos\Test1\webpack-master\examples\multi-part-library\dist\MyLibrary.alpha.js:1
(function (exports, require, module, __filename, __dirname) { !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.alpha=t():(e.MyLibrary=e.MyLibrary||{},e.MyLibrary.alpha=t())}(window,function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=1)}([,function(e,t){e.exports="alpha"}])});
ReferenceError: window is not defined
at Object.<anonymous> (C:\Users\USER\Desktop\Repos\Test1\webpack-master\examples\multi-part-library\dist\MyLibrary.alpha.js:1:294)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\USER\Desktop\Repos\Test1\webpack-master\examples\multi-part-library\test.js:2:12)
at Module._compile (module.js:643:30)