【问题标题】:Webpack bundle my js but that js wont workWebpack 捆绑了我的 js,但那个 js 不起作用
【发布时间】:2019-07-30 08:15:45
【问题描述】:

我的 webpack sass-loader 工作正常没有问题(在这个例子中被删除) 我的babel-loader 编译了所有内容,但即使将 js 导入到 html 中也无法工作。

我的由 webpack 编译的 javascript 函数不起作用(有更多函数和 jquery,但例如被删除)

    // 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)

我的 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: [


    ]

};

我的 bundle.js(编译好的 javascript(那个函数))

/******/ (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?");

/***/ })

/******/ });

还有我在车把中的导入

<!-- CONTENT -->
<div class="main-content-container">
    {{{body}}}
</div>

<!-- Import Javascript -->
<script type="text/javascript" src="/dist/adminBundle.js"></script>

但基本上任何与 webpack 捆绑的 js 都无法工作。可以从网站源代码打开,导入就可以了。

即使没有 babel loader,它也不会工作。

// 更新

当我添加时

console.log("TEST123")

对于我的 javascript 文件,它可以工作,但所有其他脚本在与 webpack 捆绑时将无法工作。

【问题讨论】:

  • 我想我帮不了你,直到你stackoverflow.com/help/minimal-reproducible-example
  • @TienDuong 我试图编辑我的问题(删除了一些不必要的东西)。不知道够不够。
  • 您的入口指向entry: path.resolve(__dirname + '/public/src/js/adminMain.js'),,这只是一个文件。您是否正在导入/需要此文件中的其余脚本?如果不是,那么 webpack 无法知道要导入哪些脚本
  • @AdityaParab 是的,我在此文件中包含所有内容,甚至导入了 jquery,但现在已将其删除,以便更容易检测问题。我不知道为什么,但是 console.log 可以工作,但所有其他脚本都不会。我想我在 webpack 中有一些错误的配置。
  • ALL OTHER SCRIPTS WONT WHEN THEY ARE BUNDLED WITH WEBPACK 哪些脚本?您如何以及在哪里导入它们?

标签: javascript node.js webpack babel-loader


【解决方案1】:

解决了。我不能在客户端使用变量和函数(如果理解正确)。 所以基本上我不能使用&lt;a id="oki123"&gt; onclick="getHref(this)"&lt;/&gt;,但我需要在javascript文件中使用$('#oki123').on('click', () =&gt; getHref...;。 不确定是否是 webpack 的正常行为,但可能是的。感谢所有人的帮助和时间你失去了我。这么愚蠢的错误......

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 2017-09-17
    • 2019-01-07
    • 2019-08-23
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多