【发布时间】:2016-06-17 13:35:56
【问题描述】:
我找不到如何在我自己的脚本之前加载供应商脚本。在manifest.json 我试过了:
"dependencies": {
"main.js": {
"files": [
"scripts/vendor_script.js",
"scripts/custom_script.js"
],
"main": true
},
不起作用:在我的自定义脚本之后调用供应商脚本。也试过了:
"dependencies": {
"plugins.js": {
"files": [
"scripts/vendor/owl.carousel.min.js"
]
},
"main.js": {
"files": [
"scripts/main.js"
],
"main": true
},
一样。有什么建议吗?
[编辑] 我当前的 manifest.json 文件,我在其中遵循了 https://discourse.roots.io/t/custom-javascript-in-manifest-json-and-building-out-into-a-single-file/3316 的建议:
{
"dependencies": {
"main.js": {
"vendor": [
"scripts/vendor/owl.carousel.min.js"
],
"files": [
"scripts/main.js"
],
"main": true
},
"main.css": {
"files": [
"styles/main.scss",
"styles/vendor/font-awesome.min.css",
"styles/vendor/owl.carousel.min.css"
],
"main": true
},
"customizer.js": {
"files": [
"scripts/customizer.js"
]
},
"jquery.js": {
"bower": ["jquery"]
}
},
"config": {
"devUrl": "http://127.0.0.1/pot/"
}
}
[编辑#2]
$ node
> require('asset-builder')('./assets/manifest.json').globs.js
require('asset-builder')('./assets/manifest.json').globs.js
[ { type: 'js',
name: 'main.js',
globs:
[ 'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\transition.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\alert.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\button.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\carousel.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\collapse.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\dropdown.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\modal.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\tooltip.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\popover.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\scrollspy.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\tab.js',
'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\bootstrap-sass\\assets\\javascripts\\bootstrap\\affix.js',
'scripts/vendor/owl.carousel.min.js',
'assets/scripts/main.js' ] },
{ type: 'js',
name: 'customizer.js',
globs: [ 'assets/scripts/customizer.js' ] },
{ type: 'js',
name: 'jquery.js',
globs: [ 'D:\\EasyPHP\\www\\pot\\wp-content\\themes\\pot\\bower_components\\jquery\\dist\\jquery.js' ] } ]
我尝试使用的脚本是 Owl Carousel。如果我在 head.php 中添加以下内容,它可以正常工作:
<script src="<?php bloginfo("template_url"); ?>/assets/scripts/vendor/owl.carousel.min.js" type="text/javascript" charset="utf-8"></script>
如果相反,我将 manifest.json 设置为如前所示,我在 Firebug 中得到“.owlCarousel 不是函数”,并且我的滑块不起作用。
注意:我没有使用 Bowel,这在常规 Sage 工作流程中不是强制性的,对吧?我刚刚将owl.carousel.min.js 复制到assets/scripts/vendor/。
【问题讨论】:
-
正确,Bower 不是强制性的。请参阅我的评论:stackoverflow.com/questions/37883023/…,它解决了您遇到的问题,应该使您能够解决它。您可能会遇到 JSHint 问题的脚本已经缩小,因此我建议您包含未缩小的版本。你知道 Bootstrap 已经自带了自己的轮播,对吧?这是相对基本的,但根据您的需要,它可以完成工作:getbootstrap.com/javascript/#carousel
-
globs数组显示哪些文件被连接到上面列出的文件中 (main.js)和它们被包含的顺序。所以订单对我来说看起来不错,您可以看到 gulp 正在寻找与您的主题相关的目录scripts/vendor/owl.carousel.min.js中的 Owl Carousel 脚本,因为这是您告诉它在您的manifest.json文件中查找的位置。看来您可能需要将manifest.json更改为准确反映轮播脚本在您的主题目录结构中的实际位置。
标签: wordpress gulp dependencies roots-sage