jquery-ui和webpack,如何将其管理到模块中?

问题描述 投票:58回答:6

任何想法如何处理?我的意思是jquery-ui似乎不是amd而且我不知道如何管理它,任何想法?

jquery-ui webpack
6个回答
31
投票

你很幸运我昨天这么做,这很容易。

npm install --save jquery jquery-ui

确保你有jquery别名来解决webpack.config.js中的插件

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

然后在webpack.config.js中包含两个别名

  1. node_modules文件夹
  2. jquery-ui文件夹

``````

resolve : {
    alias: {
      // bind version of jquery-ui
      "jquery-ui": "jquery-ui/jquery-ui.js",      
      // bind to modules;
      modules: path.join(__dirname, "node_modules"),

确保首先在app启动文件中加载jquery。

var $ = require("jquery"),
        require("jquery-ui");

如果需要使用主题配置css-loader和文件加载器。别忘了npm安装那些装载机。

module: {
    loaders: [
      { test: /\.css$/, loader: "style!css" },
      { test: /\.(jpe?g|png|gif)$/i, loader:"file" },

并在您的应用启动文件中使用。

require("modules/jquery-ui/themes/black-tie/jquery-ui.css");
require("modules/jquery-ui/themes/black-tie/jquery-ui.theme.css");

60
投票

出于某种原因,jquery-ui与webpack的搭配并不好。我不得不要求jquery-ui-bundle

npm i -S jquery-ui-bundle

然后在jquery之后需要它,例如

require('jquery');
require('jquery-ui-bundle');

49
投票

jquery-ui-distjquery-ui-bundle似乎没有由jquery-ui团队维护。在jquery-ui v1.12之后可以使用来自npm的官方jquery-ui包和webpack。

通过更新package.jsonnpm install将jquery-ui更新为1.12。

然后你可以像这样require每个小部件。

require("jquery-ui/ui/widgets/autocomplete");
require("jquery-ui/ui/widgets/draggable");

如果您在使用require("jquery-ui")之前需要将其替换为每个小部件的单独导入。我认为新方法更好,因为它只捆绑我们需要使用的小部件的代码。

使用1.12官方npm包查看documentation


11
投票

接受的答案对我不起作用,因为NPM上的jQuery-ui包没有预先构建,因此不包含jquery-ui.js;包将需要在使用之前构建或者包含/单独使用的所有小部件。

我获得整个软件包工作的最快方法是首先下载可分发版本:

npm install jquery-ui-dist --save

我需要为jquery-ui-dist添加一个别名以避免'找不到模块'错误(仅使用require('jquery-ui-dist')不起作用,因为.js文件名不同),通过将其添加到webpack.config.js

resolve: {
    alias: {
        'jquery-ui': 'jquery-ui-dist/jquery-ui.js'
    }
},

最后,您可以在加载模块的.js文件中使用它:

var jQuery = require('jquery');
require('jquery-ui'); 

或者,您可以通过在webpack.config.js中使用ProvidePlugin来避免在加载模块时必须使用require脚本,并且必须将jQuery声明为变量:

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

9
投票

的WebPack-jQuery的UI

webpack-jquery-ui - 使用此插件,例如如果您使用Rails 5,请运行

 yarn add webpack-jquery-ui

当jQuery UI插件启动时,它会检查是否提供了jquery,所以

将此代码添加到您的webpacker配置文件(shared.js - 如果您将其与Rails 5一起使用)

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

将这些行添加到您的应用代码中。

require('webpack-jquery-ui');
require('webpack-jquery-ui/css');

ActionController::InvalidAuthenticityToken修理jquery.ajax

你必须传递一个authenticity_token参数与你所有的PUTPOSTDELETE请求。

要做到这一点,你通常可以使用$('[name="csrf-token"]')[0].content从标题中获取它

所以你的请求看起来像:

var that = this;
$.ajax({
  url: navigator_item.url,
  data: { authenticity_token: $('[name="csrf-token"]')[0].content},
  method: 'DELETE',
  success: function(res) {
   ...
  }
});

我以另一种方式将jQuery包含到我的应用程序中

而不是使用:

plugins: [
new webpack.ProvidePlugin({...

您应该将'jquery':'jquery / src / jquery'添加到webpack配置文件中的别名:

module.exports = {
  resolve: {
    alias: {
        'jquery': 'jquery/src/jquery'
    }
  }

它将提供模块名称'jquery'。 jQuery UI在init上检查此名称。

然后在你的app.js文件中写道:

import $ from 'jquery'

import 'webpack-jquery-ui/css'
import 'webpack-jquery-ui/sortable' // if you need only sortable widget.

window.jQuery = $;
window.$ = $; // lazy fix if you want to use jQuery in your inline scripts.

0
投票

对我有用的步骤(在Rails 5.1.6项目中)与上述任何步骤不同:

安装jquery和jquery-ui:yarn add jqueryyarn add jquery-ui

将以下内容添加到webpack配置(即在/config/webpack/environment.js中)以全局设置$和jquery和jQuery:

environment.plugins.prepend(
  'Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    jquery: 'jquery'
  })
)

需要你想要的任何单独的jquery-ui包,在你的包装顶部(例如在/javascript/packs/application.js的顶部:

require("jquery-ui/ui/widgets/sortable")

然后你可以在你的背包中的任何地方打电话给$('.selector').sortable()

© www.soinside.com 2019 - 2024. All rights reserved.