我只是在使用 webpack、webpack-dev-server 和热模块重新加载来构建我的开发环境的皮毛。我希望最终能够将 React 组件添加到一个主要是静态的站点(这样我就可以获得具有可抓取 html 的 SEO 优势。我决定不使用 Gulp 或 Grunt,而是只使用 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 和 output.libraryTarget选项来定义一个全局变量。
参见 webpack 的exports-loader