Webpack и JQuery - PullRequest
       5

Webpack и JQuery

0 голосов
/ 13 сентября 2018

У меня есть две темы WordPress. Родитель и детская тема. Родительская тема использует NPM для добавления всех библиотек bootstrap, popper.js и jQuery. В дочерней теме я выбираю привносить в свой проект любые фрагменты этих пакетов. Когда я записываю функцию jQuery в другой файл и помещаю ее в мой файл app.js и компилирую webpack, она продолжает выдавать jQuery не определенные ошибки. Я использовал эту же настройку несколько раз и никогда не было никаких проблем. Вот мой код ниже.

:: Родительская тема ::

package.json

  {
    "name": "ouw_parent",
    "version": "0.1.3",
    "scripts": {
      "dev": "webpack --mode development --watch",
      "prod": "webpack --mode production"
    },
    "license": "GPLv3",
    "devDependencies": {
      "babel-core": "^6.26.3",
      "babel-loader": "^7.1.4",
      "babel-preset-env": "^1.7.0",
      "bootstrap": "^4.1.1",
      "bump-webpack-plugin": "^0.1.0",
      "css-loader": "^0.28.11",
      "jquery": "^3.3.1",
      "mini-css-extract-plugin": "^0.4.0",
      "node-sass": "^4.9.0",
      "optimize-css-assets-webpack-plugin": "^4.0.2",
      "popper.js": "^1.14.3",
      "postcss-loader": "^2.1.5",
      "sass-loader": "^7.0.1",
      "style-loader": "^0.21.0",
      "uglifyjs-webpack-plugin": "^1.2.5",
      "webpack": "^4.8.0",
      "webpack-cli": "^2.1.3",
      "webpack-dev-server": "^3.1.4"
    },
    "dependencies": {}
  }

postcss.config.js

module.exports = {
        plugins: [
            require('autoprefixer')
        ]
    };

.babelrc

{
    "presets": [
        "env"
    ]
}

webpack.config.js

const path                      = require('path');
const Bump                      = require("bump-webpack-plugin");
const UglifyJsPlugin            = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin      = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin   = require("optimize-css-assets-webpack-plugin");


const config = {
    entry: {
        app: './assets/js/app.js',
    },
    output: {
        filename: './dist/js/[name].js',
        path: path.resolve(__dirname, 'assets'),
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "/dist/css/[name].css",
            chunkFilename: "/dist/css/[id].css" 
        }),
        new Bump([
            'package.json',
        ]),
    ],
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                        'css-loader',
                        {loader: 'postcss-loader', options: {grid: false}}, 
                        'sass-loader'
                ],
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                  loader: "babel-loader"
                }
              }
        ]
    },
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
            cache: true,
            parallel: true,
            sourceMap: true // set to true if you want JS source maps
            }),
            new OptimizeCSSAssetsPlugin({})
        ]
    }
}

module.exports = config;

:: Детская тема ::

package.json

 {
  "name": "ouw_child",
  "version": "0.1.193",
  "scripts": {
    "dev": "webpack --mode development --watch",
    "prod": "webpack --mode production"
  },
  "license": "GPLv3",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "bump-webpack-plugin": "^0.1.0",
    "css-loader": "^0.28.11",
    "mini-css-extract-plugin": "^0.4.0",
    "node-sass": "^4.9.0",
    "optimize-css-assets-webpack-plugin": "^4.0.2",
    "postcss-loader": "^2.1.5",
    "sass-loader": "^7.0.1",
    "style-loader": "^0.21.0",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "webpack": "^4.8.0",
    "webpack-cli": "^2.1.3",
    "webpack-dev-server": "^3.1.4"
  },
  "dependencies": {}
}

postcss.config.js

module.exports = {
        plugins: [
            require('autoprefixer')
        ]
    };

.babelrc

{
    "presets": [
        "env"
    ]
}

webpack.config.js

const path                      = require('path');
const Bump                      = require("bump-webpack-plugin");
const UglifyJsPlugin            = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin      = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin   = require("optimize-css-assets-webpack-plugin");


const config = {
    entry: {
        app: './assets/js/app.js',
    },
    output: {
        filename: './dist/js/[name].js',
        path: path.resolve(__dirname, 'assets'),
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "/dist/css/[name].css",
            chunkFilename: "/dist/css/[id].css" 
        }),
        new Bump([
            'package.json',
        ]),
    ],
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                        'css-loader',
                        {loader: 'postcss-loader', options: {grid: false}}, 
                        'sass-loader'
                ],
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                  loader: "babel-loader"
                }
              }
        ]
    },
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
            cache: true,
            parallel: true,
            sourceMap: true // set to true if you want JS source maps
            }),
            new OptimizeCSSAssetsPlugin({})
        ]
    }
}

module.exports = config;

:: Теперь здесь происходит ошибка ::

Вот моя дочерняя структура темы:

-Активы

---- DIST

-------- * JS 1041 *

-------- app.js

-------- tooltip.js

---- * 1047 СКС *

Наконец ... Вот файл дочерней темы app.js, в котором содержится мой файл tooltip.js

// bootstrap scss
import '../../assets/scss/core.scss';

// bootstrap js
import '../../../ouw-parent-theme/node_modules/jquery';
import '../../../ouw-parent-theme/node_modules/popper.js';
import '../../../ouw-parent-theme/node_modules/bootstrap';

// or you could pull JS by utilities
// import '../../../ouw-parent-theme/node_modules/bootstrap/js/dist/util';
// import '../../../ouw-parent-theme/node_modules/bootstrap/js/dist/dropdown';
// import '../../../ouw-parent-theme/node_modules/bootstrap/js/dist/tooltip';

// other
import '../js/tooltip.js';

А вот мой файл tooltip.js

( function($){
    $('[data-toggle="tooltip"]').tooltip();
})(jQuery)

В файле tooltip.js по-прежнему говорится, что jQuery не определен. Несмотря на то, что мое меню Bootstrap прекрасно переключается, я знаю, что jQuery есть. Также ... Вот окончательный файл сборки, который получает выходные данные. Вы можете видеть, что все зависимости есть.

Пожалуйста, помогите! Я не могу разобраться. У меня есть такая же настройка на другом сайте, и она работает.

Я удалил большие куски кода, чтобы его можно было посмотреть здесь

/******/ (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 = "./assets/js/app.js");
/******/ })
/************************************************************************/
/******/ ({

/***/ "../ouw-parent-theme/node_modules/bootstrap/dist/js/bootstrap.js":
/*!***********************************************************************!*\
  !*** ../ouw-parent-theme/node_modules/bootstrap/dist/js/bootstrap.js ***!
  \***********************************************************************/


/***/ "../ouw-parent-theme/node_modules/jquery/dist/jquery.js":
/*!**************************************************************!*\
  !*** ../ouw-parent-theme/node_modules/jquery/dist/jquery.js ***!
  \**************************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {



/***/ "../ouw-parent-theme/node_modules/popper.js/dist/esm/popper.js":
/*!*********************************************************************!*\
  !*** ../ouw-parent-theme/node_modules/popper.js/dist/esm/popper.js ***!
  \*********************************************************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {


/***/ "./assets/js/app.js":
/*!**************************!*\
  !*** ./assets/js/app.js ***!
  \**************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\n\n__webpack_require__(/*! ../../assets/scss/core.scss */ \"./assets/scss/core.scss\");\n\n__webpack_require__(/*! ../../../ouw-parent-theme/node_modules/jquery */ \"../ouw-parent-theme/node_modules/jquery/dist/jquery.js\");\n\n__webpack_require__(/*! ../../../ouw-parent-theme/node_modules/popper.js */ \"../ouw-parent-theme/node_modules/popper.js/dist/esm/popper.js\");\n\n__webpack_require__(/*! ../../../ouw-parent-theme/node_modules/bootstrap */ \"../ouw-parent-theme/node_modules/bootstrap/dist/js/bootstrap.js\");\n\n__webpack_require__(/*! ../js/tooltip.js */ \"./assets/js/tooltip.js\");\n\n//# sourceURL=webpack:///./assets/js/app.js?");

/***/ }),

/***/ "./assets/js/tooltip.js":
/*!******************************!*\
  !*** ./assets/js/tooltip.js ***!
  \******************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\n\n// ( function($){\n//     $('[data-toggle=\"tooltip\"]').tooltip();\n// })(jQuery)\n\n\n$(document).ready(function () {\n  console.log($);\n});\n\n//# sourceURL=webpack:///./assets/js/tooltip.js?");

/***/ }),

/***/ "./assets/scss/core.scss":
/*!*******************************!*\
  !*** ./assets/scss/core.scss ***!
  \*******************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

eval("// extracted by mini-css-extract-plugin\n\n//# sourceURL=webpack:///./assets/scss/core.scss?");

/***/ }),

/***/ "./node_modules/webpack/buildin/global.js":
/*!***********************************!*\
  !*** (webpack)/buildin/global.js ***!
  \***********************************/
/*! no static exports found */
/***/ (function(module, exports) {

eval("var g;\n\n// This works in non-strict mode\ng = (function() {\n\treturn this;\n})();\n\ntry {\n\t// This works if eval is allowed (see CSP)\n\tg = g || Function(\"return this\")() || (1, eval)(\"this\");\n} catch (e) {\n\t// This works if the window reference is available\n\tif (typeof window === \"object\") g = window;\n}\n\n// g can still be undefined, but nothing to do about it...\n// We return undefined, instead of nothing here, so it's\n// easier to handle this case. if(!global) { ...}\n\nmodule.exports = g;\n\n\n//# sourceURL=webpack:///(webpack)/buildin/global.js?");

/***/ })

/******/ });

1 Ответ

0 голосов
/ 13 сентября 2018

Чтобы определить jQuery глобально через Webpack, используйте плагин ProvidePlugin в вашем файле webpack.config.js:

const webpack = require('webpack');    

module.exports = {
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    });
  ]
}

См. документацию плагина для дополнительных примеров.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...