webpack、webpack-dev-server、およびホットモジュールの再ロードを使用して、自分の開発環境を構築しています。最終的に静的なサイトに反応コンポーネントを追加することができます(私はクロール可能なHTMLを持っているというSEOの利点が得られるようにしたいと思います.GulpやGruntを使わないことに決めました。webpack-dev-serverを使用して簡単なWebpackバンドルを稼働させる方法
この質問のタイトル/トピックに戻る私はwebpackによって生成されたbundle.jsファイルを読むためのブラウザを取得できませんでした。 。
最も簡単なindex.htmlに、あなたは以下を参照することができますindex.jsまでの私のライブラリを煮コンソールにエラー出力がある:Uncaught ReferenceError: handleClick is not defined
at HTMLButtonElement.onclick ((index):7)
/******/ (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の誤差がある場合ファイルbundle.js放出されなければなりません。 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
スクリプト:私はコマンドwebpack
とパブリック/フォルダ内のbundle.jsファイルを生成することができるようにnpm start
がbundle.jsがとにかくファイルを発しても、私はWebPACKの、グローバル
"scripts": {
"start": "webpack-dev-server --content-base public/"
}
インストールされています。
私が作っている簡単なエラーがあるはずです。 @Marek Takac経由
ソリューション: ここでエラーがhandleClick()
関数のスコープがグローバルではないということです。これはindex.jsファイル
module.exports = {
handleClick: handleClick
}
、グローバル変数を定義するためのWebPACKのoutput.library and output.libraryTargetオプションを使用して内からモジュールをエクスポートすることによって改善することができます。
も参照のWebPACKのexports-loader
返信いただきありがとうございますが、私はそのスコープがここでも問題であるとは考えていません。パブリックフォルダ内のbundle.jsをindexに置き換えます。js(そしてhtmlファイルの 'src ='というスクリプト)を実行してから、webpack-dev-serverで提供するのではなく、ブラウザでindex.htmlファイルを開くだけです。私はまだ反応していません。ウェブパックのドキュメントをかなり徹底的に梳きました。 – Drew2
'index.js'ファイルの' console.log'の内容はどうでしょうか?それは実際に何かをコンソールに記録しますか? –
@ Drew2これは範囲の問題です。 bundle.jsを確認すると、ファイル全体がグローバルにアクセスできない新しいスコープになっていることがわかります。あなたが実際に私が私の答えに示唆しているものを試してみると、それは動作すると思うでしょう:) –