简体   繁体   English

如何使用webpack添加jquery-ui css / images

[英]How to add jquery-ui css/images with webpack

I am using jquery-ui together with webpack but have problems regarding how to load the bundled css and images from node_modules folder. 我正在将jquery-ui和webpack一起使用,但是在如何从node_modules文件夹中加载捆绑的CSS和图像时遇到问题。

I have my own scss files but then also need to load the jquery-ui css. 我有自己的scss文件,但是还需要加载jquery-ui css。 To acomplish this I added the following webpack config rules. 为了完成此任务,我添加了以下webpack配置规则。

{
    test: /\.scss$/,
    include: [
        path.resolve(__dirname, "client/css/scss"),
    ],
    use: [{
        loader: "style-loader" // creates style nodes from JS strings
    }, {
        loader: "css-loader" // translates CSS into CommonJS
    }, {
        loader: "sass-loader" // compiles Sass to CSS
    }]
},
{
    test: /all\.css$/,
    include: [
        path.resolve(__dirname, "node_modules/jquery-ui/themes/base"),
    ],
    use: [{
        loader: "style-loader" // creates style nodes from JS strings
    }, {
        loader: "css-loader" // translates CSS into CommonJS
    }]
}

Note versions: 注意版本:

"jquery": "3.2.1",
"jquery-ui": "1.12.1",

Next in my js entry file: 接下来在我的js条目文件中:

require("../css/scss/main.scss");
require("../../node_modules/jquery-ui/themes/base/all.css");

However on build this gives: 但是在构建时会给出:

ERROR in ./node_modules/jquery-ui/themes/base/images/ui-icons_777620_256x240.png Module parse failed: /home/anders/Code/imjustworking/node_modules/jquery-ui/themes/base/images/ui-icons_777620_256x240.png Unexpected character ' ' (1:0) ./node_modules/jquery-ui/themes/base/images/ui-icons_777620_256x240.png模块解析失败:/ home / anders / Code / imjustworking / node_modules / jquery-ui / themes / base / images / ui-icons_777620_256x240。 png意外字符' '(1:0]

So I figured I need some additional loaders for pngs: { test: /.png$/, include: [ path.resolve(__dirname, "node_modules/jquery-ui/themes/base/images"), ], use: { loader: "url-loader?limit=100000" } } But then I run into this policy I would rather not change: 因此,我想我需要为png附加一些加载器:{测试:/。png $ /,包括:[path.resolve(__ dirname,“ node_modules / jquery-ui / themes / base / images”),],使用:{加载器:“ url-loader?limit = 100000”}}但是后来我碰到了这个策略,我不想改变:

Refused to load the image 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAADwCAMAAADYSUr5AAABDlBMV…MDrH4y9V+CDsjWf1nOApn6L+F1gNV/F48BB4BR+zUwcIfzP/8S/ZOlvFfuAAAAAElFTkSuQmCC' because it violates the following Content Security Policy directive: "img-src 'self'". 拒绝加载图像'data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAADwCAMAAADYSUr5AAABDlBMV ... MDrH4y9V + CDsjWf1nOApn6L + F1gNV / F48BB4BR + zUwcIfzP / 8S / ZOlvFQAACCEl'违反了内容,因为它违反了指令':'

This is where Im at now. 我现在是这里。 I did find some other examples of how to do this but they where mostly from earlier less webpack friendly versions of jquery-ui. 我确实找到了一些其他示例,但是这些示例大多来自较早的对webpack不友好的jquery-ui版本。 Any advice appreciated. 任何建议表示赞赏。 I suspect there are better ways to do this than my attemptemts above? 我怀疑有比上述尝试更好的方法吗?

On the official jquery-ui's page explaining the usage of the npm they link to a github repository providing a minimal working example of how to set up jquery-ui with webpack. 在官方的jquery-ui的页面上,解释了npm的用法,它们链接到github存储库,提供了如何使用webpack设置jquery-ui 的最小工作示例 The way I got it working (adapting the repository's instructions a bit) was as follows: 我得到它的工作方式(稍微适应了存储库的说明)如下:

  1. installed the file-loader module ( npm i -S file-loader ) 安装了文件加载器模块( npm i -S file-loader
  2. Added the following rule: 添加了以下规则:

     { test: /\\.(jpg|png)$/, loader: 'file-loader' }, 

So basically the way I handle my css, scss and the stylesheets required by jquery ui is (webpack.config.js): 所以基本上我处理CSS,SCS和jquery ui所需的样式表的方式是(webpack.config.js):

module: {
    rules: [
    {
        test: /\.s*css$/,
        use: [
            "style-loader", // creates style nodes from JS strings
            "css-loader", // translates CSS into CommonJS
            "sass-loader" // compiles Sass to CSS, using Node Sass by default
        ]
    },
    {
        test: /\.(jpg|png)$/,
        loader: 'file-loader'
    },
    ]
},

And in my app's js file: 在我的应用程序的js文件中:

require("jquery-ui/ui/widgets/autocomplete");
require("jquery-ui/ui/widgets/draggable");
require("jquery-ui/ui/widgets/selectmenu");
require("jquery-ui/ui/widgets/slider");
require('jquery-ui/themes/base/all.css');

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

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