Haxe + Webpack导出空对象

时间:2019-03-12 13:16:53

标签: javascript webpack haxe

我正在尝试在Haxe编译器导出的JS上运行webpack-dev-server。我正在使用hxgenjs库将haxe输出拆分为单独的模块,并且尝试通过webpack合并它们(以使用热模块替换功能)。 一切似乎正常,但输出为空对象。这是我的webpack.config.js

module.exports = {
  entry: './build/Game-hxgenjs.js',
  mode: 'development',
  devtool: "inline-source-map",
  output: {
    filename: 'Game-webpack.js',
    path: path.resolve(__dirname, 'bin/js'),
    publicPath: '/bin/js/',
    libraryTarget: "umd",
    library: "MyLib"
  },
  devServer: {
    publicPath: '/bin/js/',
    compress: false,
    port: 8080,
    hot: true,
    inline: true,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': '*'
    },
    proxy: {
       // some proxy settings
    }
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ]
};

入口js文件具有以下内容:

if (module.hot) module.hot.accept();
require("./Std")
var $import = require("./import_stub").default;
function base_navigation_elements_NavigationDotsContainer() {return require("./base/navigation/elements/NavigationDotsContainer");}
function base_navigation_elements_NavigationScore() {return require("./base/navigation/elements/NavigationScore");}
function custom_game_Manager() {return require("./custom/game/Manager");}
function base_navigation_elements_NavigationDot() {return require("./base/navigation/elements/NavigationDot");}
function library_Library() {return require("./library/Library");}
function platform_topbar_TopbarProxy() {return require("./platform/topbar/TopbarProxy");}
function base_navigation_Navigation() {return require("./base/navigation/Navigation");}
function base_navigation_elements_NavigationAnimation() {return require("./base/navigation/elements/NavigationAnimation");}
function base_navigation_elements_NavigationButton() {return require("./base/navigation/elements/NavigationButton");}
function base_navigation_elements_NavigationPreloader() {return require("./base/navigation/elements/NavigationPreloader");}
function base_navigation_elements_NavigationButtonSpaceBar() {return require("./base/navigation/elements/NavigationButtonSpaceBar");}
(custom_game_Manager().default).main();
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["elements"] = exports["base"]["navigation"]["elements"] || {}
exports["base"]["navigation"]["elements"]["NavigationDotsContainer"] = (base_navigation_elements_NavigationDotsContainer().default)
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["elements"] = exports["base"]["navigation"]["elements"] || {}
exports["base"]["navigation"]["elements"]["NavigationScore"] = (base_navigation_elements_NavigationScore().default)
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["elements"] = exports["base"]["navigation"]["elements"] || {}
exports["base"]["navigation"]["elements"]["NavigationDot"] = (base_navigation_elements_NavigationDot().default)
exports["library"] = exports["library"] || {}
exports["library"]["Library"] = (library_Library().default)
exports["platform"] = exports["platform"] || {}
exports["platform"]["topbar"] = exports["platform"]["topbar"] || {}
exports["platform"]["topbar"]["TopbarProxy"] = (platform_topbar_TopbarProxy().default)
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["Navigation"] = (base_navigation_Navigation().default)
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["elements"] = exports["base"]["navigation"]["elements"] || {}
exports["base"]["navigation"]["elements"]["NavigationAnimation"] = (base_navigation_elements_NavigationAnimation().default)
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["elements"] = exports["base"]["navigation"]["elements"] || {}
exports["base"]["navigation"]["elements"]["NavigationButton"] = (base_navigation_elements_NavigationButton().default)
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["elements"] = exports["base"]["navigation"]["elements"] || {}
exports["base"]["navigation"]["elements"]["NavigationPreloader"] = (base_navigation_elements_NavigationPreloader().default)
exports["base"] = exports["base"] || {}
exports["base"]["navigation"] = exports["base"]["navigation"] || {}
exports["base"]["navigation"]["elements"] = exports["base"]["navigation"]["elements"] || {}
exports["base"]["navigation"]["elements"]["NavigationButtonSpaceBar"] = (base_navigation_elements_NavigationButtonSpaceBar().default)

这是自动生成的文件。问题是,当我运行webpack-dev-server时,它会生成它的东西..最后有下面这段代码:

/***/ 0:
/*!********************************************************************************************************************!*\
  !*** multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./build/Game-hxgenjs.js  ***!
  \********************************************************************************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

__webpack_require__(/*! C:\path\to\project\node_modules\webpack-dev-server\client\index.js?http://localhost:8080 */"./node_modules/webpack-dev-server/client/index.js?http://localhost:8080");
__webpack_require__(/*! C:\path\to\project\node_modules\webpack\hot\dev-server.js */"./node_modules/webpack/hot/dev-server.js");
__webpack_require__(/*! ./build/Game-hxgenjs.js */"./build/Game-hxgenjs.js");
module.exports = __webpack_require__(0);


/***/ })

/******/ });
});

其中

__webpack_require__(0) 

返回一个空对象,所以window.MyLib是一个空对象。当我在此处放置断点时,可以看到上一行:

__webpack_require__(/*! ./build/Game-hxgenjs.js */"./build/Game-hxgenjs.js"); 

实际上返回我需要的东西(所有从入口js文件导出的东西)。 有人可以帮我弄清楚发生了什么,模块“ 0”到底是什么?

1 个答案:

答案 0 :(得分:1)

很可能你正在使用 webpack5,它有一个已知的错误: webpack5.x.x + webpack-dev-server 3.x.x 在 devserver 模式下将模块导出为空对象,当纯 webpack 构建和监视工作正常时。

要修复安装 "webpack-dev-server": "^4.0.0-beta.0"

npm i webpack-dev-server@next -D

请不要在 webpack-dev-server 4.x.x 中将 devServer.publicPath 选项更改为 static

  devServer: {
        static: path.join(__dirname, 'build'),
        hot: true,
    },