my webpack sass-loader
works allright without problems (removed for this example)
my babel-loader
compiles everything but that js wont work even when is imported into html.
My javascript function that is compiled by webpack that is not working ( there was more functions and jquery but is removed for example)
// Get href for pagination
function getHref(obj) {
event.preventDefault()
var url = window.location.href;
paginationQueries = url.split("?")[1];
paginationHref = obj.getAttribute("href");
if (paginationQueries) {
pageRefUrl = paginationHref + "?" + paginationQueries
} else {
pageRefUrl = paginationHref
}
window.location.href = pageRefUrl;
}
console.log("TEST123") // THIS WORKS BUT ALL OTHER SCRIPTS WONT WHEN THEY ARE BUNDLED WITH WEBPACK)
My webpack.config.js
const path = require('path');
module.exports = {
entry: path.resolve(__dirname + '/public/src/js/adminMain.js'),
output: {
path: path.resolve(__dirname + '/public/dist/'),
filename: 'adminBundle.js'
},
module: {
rules: [{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
]
},
plugins: [
]
};
My bundle.js (compiled javascript (that one function)
/******/ (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, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // 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 = "./public/src/js/adminMain.js");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./public/src/js/adminMain.js":
/*!************************************!*\
!*** ./public/src/js/adminMain.js ***!
\************************************/
/*! no static exports found */
/***/ (function(module, exports) {
eval("// Get href for pagination\nfunction getHref(obj) {\n event.preventDefault();\n var url = window.location.href;\n paginationQueries = url.split(\"?\")[1];\n paginationHref = obj.getAttribute(\"href\");\n\n if (paginationQueries) {\n pageRefUrl = paginationHref + \"?\" + paginationQueries;\n } else {\n pageRefUrl = paginationHref;\n }\n\n window.location.href = pageRefUrl;\n}\n\n//# sourceURL=webpack:///./public/src/js/adminMain.js?");
/***/ })
/******/ });
And my import in handlebars
<!-- CONTENT -->
<div class="main-content-container">
{{{body}}}
</div>
<!-- Import Javascript -->
<script type="text/javascript" src="/dist/adminBundle.js"></script>
But basically any js bundled with webpack wont work. I can open it from website source code and is imported allright.
Even without babel loader it wont work.
// UPDATE
When i add
console.log("TEST123")
To my javascript file it works but all other scripts wont work when they are bundled with webpack.
Its solved. I cant use variables and functions on client side (if understand correctly).
So basically i cant use <a id="oki123"> onclick="getHref(this)"</>
but i need to use $('#oki123').on('click', () => getHref...;
in javascript file.
Not sure if is normal behaviou of webpack but probably yes. Thanks to all people for help and time u lost on me. So stupid mistake...