使用Webpack与JQuery插件

3

我正在尝试在Webpack中使用插件Galleria。如果没有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?

1
从Galleria网站的常见问题解答中可以得知:“Galleria与webpack不兼容 - 如果你正在使用webpack,你需要使用shimming,因为Galleria需要访问window.location对象:”https://galleria.io/docs/references/faq.html - Brett Gregson
是的,我通过在Webpack中添加规则来实现,但仍然无法正常工作。 - Miguel Moura
1个回答

4

我为你创建了一个使用webpack和Galleria的简单存储库
步骤如下:

  1. 按照这里的说明使用shimming
            {
                test: /galleria.js$/,
                loader: "imports-loader?this=>window"
            },
  1. jquerygalleria添加为项目依赖项:npm i -S jquery galleria
  2. 使用loadThemerun Galleria方法:
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

更新:
要复制压缩的主题文件,您可以使用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:
使用webpack imports更新存储库。 请参见此PR或此分支中的区别。 主要更改如下:

  1. 我们仍然需要使用copy-webpack-plugingalleria.classic.css文件作为<link rel="stylesheet" type="text/css" href="assets/galleria.classic.css">使用,并加载,因为在源代码中发现只能通过linkscript标签或动态地(loadTheme调用)加载css,否则将打印No theme css loadedsource
    1. 但是,我们的JavaScript代码变得非常简单。我还删除了使用imports-loader的webpack规则,并在内联样式中使用它。
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');


谢谢。我需要手动复制galleria.classic.jsgalleria.classic.css文件到项目资产文件夹,因为当我运行我的项目时,在node_modules中的路径找不到它们。有没有一种方法可以在构建时将这两个文件复制到项目资产文件夹中? - Miguel Moura
@MiguelMoura 更新了答案和代码库。我认为我的解决方案并不是最好的,因为使用webpack时最好使用import语句而不是loadTheme方法,但它可以工作。如果我有空闲时间,我会寻找一个更好的解决方案。 - nickbullock
谢谢你。如果你能够添加一个导入的示例,那会很好。但无论如何我已经接受了你的答案。 - Miguel Moura
@MiguelMoura 更新 :) - nickbullock
我感觉我已经在整个互联网上搜索了解决这个问题的方法。感谢@nick的帮助。然而,在尝试加载主题时,我仍然遇到了问题。我找到的解决方法是在HTML(或我的情况下的.ftl文件)中从CDN获取主题:Galleria.loadTheme('https://cdnjs.cloudflare.com/ajax/libs/galleria/1.6.1/themes/classic/galleria.classic.min.js'); - Consta Gorgan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接