我正在尝试将插件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吗?
答案 0 :(得分:4)
为您创建了一个带有Webpack和Galleria的简单repo。
步骤是:
shimming
: {
test: /galleria.js$/,
loader: "imports-loader?this=>window"
},
jquery
和galleria
添加为项目的依赖项:
npm i -S jquery galleria
loadTheme
和run
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
。
UPD:
要复制缩小的主题文件,可以使用CopyWebpackPlugin
:
npm i -D copy-webpack-plugin
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' },
])
]
loadTheme
通话中的路径:Galleria.loadTheme('assets/galleria.classic.js');
UPD2:
使用webpack imports
更新了仓库。
请查看此PR或此branch的不同之处。
主要变化是:
galleria.classic.css
文件与copy-webpack-plugin
一起使用并作为<link rel="stylesheet" type="text/css" href="assets/galleria.classic.css">
加载,因为我在源代码中发现css
只能通过{{1}加载}或link
标签或动态(script
调用),否则将打印loadTheme
。
No theme css loaded
删除了webpack规则,并以内联样式使用了它。imports-loader