将JQuery插件与Webpack一起使用

米格尔·莫拉(Miguel Moura)

我正在尝试将插件Galleria与Webpack一起使用如果没有Webpack,则Galleria可以用作:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script src="galleria/galleria-1.4.min.js"></script>

<script>
  (function() {
    Galleria.loadTheme('https://cdnjs.cloudflare.com/ajax/libs/galleria/1.5.7/themes/classic/galleria.classic.min.js');
    Galleria.run('.galleria');
  }());
</script>

也可以手动加载主题,而不是使用方法loadTheme

<link rel=”stylesheet” type=”text/css” href=”https://cdnjs.cloudflare.com/ajax/libs/galleria/1.5.7/themes/fullscreen/galleria.classic.min.css” />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>   
<script src=”https://cdnjs.cloudflare.com/ajax/libs/galleria/1.5.7/galleria.min.js”></script>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/galleria/1.5.7/themes/classic/galleria.classic.min.js”></script>

<script>
  (function() {
    Galleria.run('.galleria');
  }());
</script>

使用WebPack,我将以下代码添加到Index.js:

import galleria from 'galleria';

import '../../node_modules/galleria/dist/themes/classic/galleria.classic.css';
import '../../node_modules/galleria/dist/themes/classic/galleria.classic.js';

window.Galleria = galleria; 

(function() {
  Galleria.run('.galleria');
}());

当我运行它时,我得到以下错误:

No theme CSS loaded.

Init failed: Galleria could not find the element "undefined".

知道如何在Webpack中使用Galleria吗?

尼克布洛克

创建了一个简单的回购与的WebPack和拱廊你。
步骤如下:

  1. shimming此处记录使用
            {
                test: /galleria.js$/,
                loader: "imports-loader?this=>window"
            },
  1. 在项目中添加jquerygalleria作为依赖项:npm i -S jquery galleria
  2. 使用loadThemerunGalleria方法:
import * as $ from 'jquery';
import galleria from 'galleria';

window.Galleria = galleria;
window.jQuery = $;

Galleria.loadTheme('node_modules/galleria/dist/themes/classic/galleria.classic.js');
Galleria.run('.galleria');

要查看项目的运行情况,请运行npm run start

UPD:
要复制缩小的主题文件,可以使用CopyWebpackPlugin

  1. npm i -D copy-webpack-plugin
  2. 将其添加到webpack插件中:
plugins: [
        new CopyWebpackPlugin([
            { from: 'node_modules/galleria/dist/themes/classic/galleria.classic.min.js', to: 'assets/galleria.classic.js' },
            { from: 'node_modules/galleria/dist/themes/classic/galleria.classic.min.css', to: 'assets/galleria.classic.css' },
        ])
]
  1. 更改loadTheme通话路径Galleria.loadTheme('assets/galleria.classic.js');

UPD2:
使用Webpackimports使用情况更新了回购请查看此PR或此分支的区别主要变化是:

  1. 我们仍然需要galleria.classic.css使用copy-webpack-plugin和加载文件,<link rel="stylesheet" type="text/css" href="assets/galleria.classic.css">因为我在源代码中发现css只能通过linkscript标签或动态(loadTheme调用)加载,否则No theme css loaded将被打印。资源
    1. 但是随后我们的javascript代码变得非常简单。我也删除了webpack规则,imports-loader以内联样式使用它。
import * as $ from 'jquery';
import * as Galleria from 'galleria'
import 'imports-loader?define=>false!./node_modules/galleria/src/themes/classic/galleria.classic';

Galleria.run('.galleria');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将Webpack与Express一起使用?

将Hibernate与Dynamic Eclipse插件一起使用

.val()无法与jQuery Multiselect插件一起使用

我如何将JQuery与getElementsById一起使用?

如何使用node-webkit中的webpack将节点模块与本机插件捆绑在一起?

将Velocity.js与Webpack一起使用

与需要填充的Webpack一起使用的jquery.mockjax

将jQuery Panzoom与jQuery Draggable一起使用

我可以将HTML Webpack插件与django基本模板一起使用吗?

如何将Webpack与Typescript一起使用?

将Pug与NodeJS和Webpack一起使用

将bulma与webpack一起使用

无法将tslint与webpack一起使用

将Gradle Witness插件与Java库一起使用

将Vue.js与Webpack一起使用

将EclEmma(eclipse插件)与JMockit一起使用时出现错误(仅与Delegate()一起使用)

angularJS + jQuery可编辑插件可一起使用

将searchkick与elasticsearch插件一起使用

如何使用Python鼻子测试将内置插件与自定义插件一起调用

将jQuery插件与$ .on()一起使用

当将Image map与ImageMapster jQuery插件一起使用时,坐标会更改

将IBM Watson API与jquery的$ .ajax一起使用

让columnFilter插件与DataTables一起使用

将jQuery Validation插件与动态表单元素一起使用

包含readyselector插件时,将$符号与jQuery一起使用会产生错误

将jQuery与Shadow dom一起使用

将jquery-ui与具有特定文件结构的webpack一起使用

将jquery-mobile与Webpack一起使用

将 ++ 与 jquery $.each() 一起使用