简体   繁体   English

如何在webpack中包含jquery-ui文件?

[英]How to include jquery-ui file in webpack?

Desired Behaviour 期望的行为

Include jQuery UI in webpack build. 在webpack构建中包括jQuery UI。

Actual Behaviour 实际行为

Error when running webpack: 运行webpack时出错:

ERROR in ./src/js/jquery-ui.js ./src/js/jquery-ui.js中的错误
Module not found: Error: Can't resolve 找不到模块:错误:无法解决
'jquery' in 'C:\\Me\\Documents\\my_repo\\src\\js' 'C:\\ Me \\ Documents \\ my_repo \\ src \\ js'中的'jquery'

What I've Tried 我尝试过的

I tried adding this to webpack.config.js but it didn't change anything: 我尝试将其添加到webpack.config.js但没有任何改变:

plugins: [
   new UglifyJsPlugin(),
   new webpack.ProvidePlugin({
        $: "./jquery-3.3.1",
        jQuery: "./jquery-3.3.1",
        jquery: "./jquery-3.3.1"
    })
]

I also tried adding this to webpack.config.js (so that the path is relative to location of webpack.config.js ) and that caused additional errors Module not found: Error: Can't resolve '/src/js/jquery-3.3.1' : 我还尝试将其添加到webpack.config.js (这样路径是相对于webpack.config.js位置的),并且导致了其他错误。 Module not found: Error: Can't resolve '/src/js/jquery-3.3.1'

plugins: [
   new UglifyJsPlugin(),
   new webpack.ProvidePlugin({
        $: "/src/js/jquery-3.3.1",
        jQuery: "/src/js/jquery-3.3.1",
        jquery: "/src/js/jquery-3.3.1"
    })
],

Steps To Replicate 复制步骤

1) Go to jQuery UI download builder 1)转到jQuery UI 下载生成器
2) Select the following only and download the file: 2) 选择以下内容并下载文件:

-- Version 1.12.1 -版本1.12.1
-- Effects > "Effects Core" -效果>“效果核心”
-- Effects > "Slide Effect" -效果>“幻灯片效果”
-- Theme > "No Theme" -主题>“无主题”

In the unzipped folder, get the jquery-ui.js file. 在解压缩的文件夹中,获取jquery-ui.js文件。

Note: I only need slide effect, not all of jQuery UI, hence the custom download. 注意:我只需要幻灯片效果,而不是全部jQuery UI,因此需要自定义下载。

my_entry_file.js my_entry_file.js

import './jquery-ui';

webpack.config.js webpack.config.js

const path = require('path');
const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); 

var webpack = require("webpack");

module.exports = {
    entry: "./src/js/my_entry_file.js",
    output: {
        filename: "bundle.js",
        path: path.resolve(__dirname, "dist/js")
    },
    module: {
        rules: [{
                test: /\.js$/,
                exclude: /(node_modules)/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["env", "stage-0"]
                    }
                }
            },
            {
                test: /\.css$/,
                use: [
                    { loader: "style-loader" },
                    { loader: "css-loader" }
                ]
            },
            {
                test: /\.less$/,
                use: [
                    { loader: "style-loader" },
                    { loader: "css-loader" },
                    { loader: "less-loader" }
                ]
            },
            {
                test: /\.jpg$/,
                use: [
                    { loader: "url-loader" }
                ]
            },
            {
            test: path.resolve(__dirname, 'src/js/jquery-3.3.1'),
            use: [{
            loader: 'expose-loader',
            options: '$'
            }]
            }
        ]
    },
    plugins: [
       new UglifyJsPlugin(),
       new webpack.ProvidePlugin({
            $: "./jquery-3.3.1",
            jQuery: "./jquery-3.3.1"
        })
    ],
    resolve: {
    alias: {
        'uikit-util': path.resolve(__dirname, 'node_modules/uikit/src/js/util')
    }
    }
}

I tried installing jquery with npm install jquery and changing webpack.config.js to: 我尝试使用npm install jquery并将webpack.config.js更改为:

plugins: [
   new UglifyJsPlugin(),
   new webpack.ProvidePlugin({
        $: "jquery",  
        jQuery: "jquery"
    })
],

and build completes and front end functionality is working. 并完成构建,并且前端功能正在运行。

So it seems it might have been related to user MatheusSilva's comment regarding path to jquery in webpack.ProvidePlugin . 因此,似乎它可能与用户MatheusSilva的有关webpack.ProvidePlugin jquery路径的注释有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM