我只是刮伤与的WebPack,的WebPack-dev的服务器,和热模块重装打造我的开发环境的表面。我希望最终能组件添加反应主要为静态网站(让我得到具有抓取HTML的搜索引擎优化的好处。我已经决定,我不会用咕嘟咕嘟或咕噜,而不是我将只使用NPM脚本运行开发,测试,建设和出版shell命令。
回到这个问题的标题/话题。我一直没能获得浏览器读取的WebPack生成我bundle.js文件。我煮我的图书馆下来,你可以看到下面的简单index.html
和index.js
。
错误输出到控制台:
Uncaught ReferenceError: handleClick is not defined at HTMLButtonElement.onclick ((index):7)
发射bundle.js
文件必须是错误所在:
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
const handleClick = () => {
document.getElementById("demo").innerHTML = "Hello World!";
};
/***/ })
/******/ ]);
我index.html
文件:
<html>
<body>
<p id="demo">Simple JS demo</p>
<script src="bundle.js"></script>
<button type="button" onclick='handleClick()'>Click Me!</button>
</body>
</html>
我index.js
文件:
const handleClick = () => {
document.getElementById("demo").innerHTML = "Hello World!"
}
我webpack.config.js
文件:
const config = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: '/home/andrew/code/my-site/public',
},
module: {
rules: [
{
test: /\.(js)$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
}
}
]
}
};
module.exports = config;
我npm start
脚本:
"scripts": {
"start": "webpack-dev-server --content-base public/"
}
我曾经的WebPack全球的装机量,所以我可以生成与命令bundle.js
的public/
文件夹中的文件webpack
,虽然npm start
发出bundle.js
文件反正。
一定有什么是我正在做一些简单的错误。
通过@Marek Takac解决方案:这里的错误是handleClick()
功能的范围是不是全球性的。这可以通过从index.js
文件中导出的模块进行补救
module.exports = {
handleClick: handleClick
}
并且使用的WebPack的output.library and output.libraryTarget选项来定义一个全局变量。
另见的WebPack的exports-loader
你的WebPack束似乎是确定。问题是在你的代码。因为你是从全球环境调用它handleClick
功能是不确定的。基本上,你尝试调用window.hanldeClick
但你完全不同的范围定义您的handleClick
功能。的WebPack回放功能为单独关闭以防止污染的全球环境。我建议你去通过一些的WebPack /反应教程,指南和文档。但是,如果你想只是为了测试你的安装工作正常登录您index.js
文件东西到控制台。另外我认为,如果你改变const handleClick
到window.handleClick
(虽然我从来没有尝试过这样的事情),你的代码应该工作。