window is not defined when consuming webpack multi

2019-08-08 17:22发布

问题:

I'm trying to figure out how this webpack library works. The sample code I use is from here

https://github.com/webpack/webpack/tree/master/examples/multi-part-library

When I ran "webpack" it produce "dist/MyLibrary.alpha.js" and "dist/MyLibrary.beta.js" in the same folder. Then I create a test.js to consume the library and ran it. Other than running webpack and creating the test.js, nothing from the sample files were changed. Consuming alpha.js works fine, but MyLibrary.alpha did not.

//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)