browser - How to get simple Webpack bundle up and running using webpack-dev-server -


i scratching surface of building development environment webpack, webpack-dev-server, , hot module reloading. want able add react components static site (so seo benefit of having crawlable html. i've decided not use gulp or grunt, instead i'll solely use npm scripts run shell commands development, testing, building, , publishing.

returning title/topic of question. have not been able browsers read bundle.js file generated webpack. i've boiled library down simplest index.html , index.js can see below.

the error output console is:

uncaught referenceerror: handleclick not defined @ htmlbuttonelement.onclick ((index):7) 

the emitted bundle.js file must error is:

/******/ (function(modules) { // webpackbootstrap /******/    // module cache /******/    var installedmodules = {}; /******/ /******/    // require function /******/    function __webpack_require__(moduleid) { /******/ /******/        // check if module in cache /******/        if(installedmodules[moduleid]) { /******/            return installedmodules[moduleid].exports; /******/        } /******/        // create new module (and put cache) /******/        var module = installedmodules[moduleid] = { /******/            i: moduleid, /******/            l: false, /******/            exports: {} /******/        }; /******/ /******/        // execute module function /******/        modules[moduleid].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/        // flag module loaded /******/        module.l = true; /******/ /******/        // return exports of module /******/        return module.exports; /******/    } /******/ /******/ /******/    // expose modules object (__webpack_modules__) /******/    __webpack_require__.m = modules; /******/ /******/    // expose module cache /******/    __webpack_require__.c = installedmodules; /******/ /******/    // define getter function 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 compatibility 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 , return exports /******/    return __webpack_require__(__webpack_require__.s = 0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) {  const handleclick = () => {   document.getelementbyid("demo").innerhtml = "hello world!"; };  /***/ }) /******/ ]); 

my index.html file:

<html> <body>  <p id="demo">simple js demo</p>  <script src="bundle.js"></script> <button type="button" onclick='handleclick()'>click me!</button>  </body> </html> 

my index.js file:

const handleclick = () => {   document.getelementbyid("demo").innerhtml = "hello world!" } 

my webpack.config.js file:

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; 

my npm start script:

  "scripts": {     "start": "webpack-dev-server --content-base public/"   } 

i have webpack installed globally can generate bundle.js file in public/ folder command webpack, though npm start emits bundle.js file anyways.

there's got simple error i'm making.

solution via @marek takac: error here scope of handleclick() function not global. can remedied exporting module within index.js file

module.exports = {   handleclick: handleclick } 

and using webpack's output.library , output.librarytarget options define global variable.

see webpack's exports-loader

you're webpack bundle seems ok. problem in code. handleclick function undefined because calling global environment. try call window.hanldeclick defined handleclick function in totally different scope. webpack puts function separate closure prevent polluting global environment. suggest go through webpack / react tutorials, guides , documentation. if want test setup works correctly logging out console index.js file. alternatively think code should work if change const handleclick window.handleclick (although never tried this).


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -