1

我只是在使用 webpack、webpack-dev-server 和热模块重新加载来构建我的开发环境的皮毛。我希望最终能够将 React 组件添加到一个主要是静态的站点(这样我就可以获得具有可抓取 html 的 SEO 优势。我决定不使用 Gulp 或 Grunt,而是只使用 npm 脚本来运行用于开发、测试、构建和发布的 shell 命令。

回到这个问题的标题/主题。我无法让浏览器读取 webpack 生成的 bundle.js 文件。我已经将我的库简化为最简单的index.htmlindex.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/webpacknpm startbundle.js

我犯了一些简单的错误。

通过@Marek Takac 解决: 这里的错误是handleClick()函数的范围不是全局的。这可以通过从index.js文件中导出模块来解决

module.exports = {
  handleClick: handleClick
}

并使用 webpack 的output.library 和 output.libraryTarget选项来定义一个全局变量。

参见 webpack 的exports-loader

4

1 回答 1

0

你的 webpack 包似乎没问题。问题出在您的代码中。handleClick函数未定义,因为您是从全局环境中调用它。您基本上尝试调用window.hanldeClick,但您handleClick在完全不同的范围内定义了您的函数。Webpack 将函数放入单独的闭包中,以防止污染您的全局环境。我建议您阅读一些 webpack / react 教程、指南和文档。但是,如果您只想测试您的设置是否正常工作,请从您的index.js文件中将某些内容记录到控制台。或者,我认为如果您更改const handleClick为,您的代码应该可以工作window.handleClick(尽管我从未尝试过这样的事情)。

于 2017-08-14T18:57:05.657 回答