具有第三方库的Grails Asset-Pipeline系统 [英] Grails Asset-Pipeline system with 3rd party libs

查看:98
本文介绍了具有第三方库的Grails Asset-Pipeline系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails 2.4现在使用资产管道来管理和处理Grails应用程序中的静态资产,而不是Resources系统。这是非常新的,并没有太多关于它在互联网上的文档。



我的问题是,如何正确处理第三方库?



例如, select2 http:/ /ivaynberg.github.io/select2/ )库的所有.css,.js,图像文件都放在一个文件夹中。



我应该将不同的文件类型复制到相应的 assets / javascripts / assets / stylesheets / 等子文件夹中?如何处理文件,这些文件实际上没有像.json,text或doc文件这样明显的位置?



我应该创建一个 vendors / select2 / 文件夹?在 assets / web-app / 中?然后,我应该如何从我的GSP中加载所有必需的文件?



这个库只会在窗体视图中使用,因此不应该加载,除非这是必要的。

解决方案

Baxxabit的回答让我想到了我的解决方案。我一直在努力做同样的事情,但想要充分利用管道,以便我可以使用less-asset-pipeline插件。



使用引导程序作为示例,我创建了一个供应商文件夹作为标准资产文件夹的同行,然后将引导文件放在那里。



application.js:

  //这是一个javascript文件,其顶级require指令
// =需要jquery
// =需要引导程序-3.1.1 / javascript / bootstrap
// = require_tree视图
// = require_self
...

和application.css:

  / * 
* = require_self
* =需要main
* =需要移动
* =需要bootstrap-3.1.1 / stylesheets / bootstrap
* =编码UTF-8
* /

要覆盖boostrap自定义,我可以简单地将自定义的variables.less文件放入./grails-app/assets/供应商/自举-3.1.1 /样式表/ variabl es.less。资产下的第一个文件夹在覆盖文件时会被忽略,此后路径需要与您在卖方下放的任何内容匹配。



这给了我这种层次结构:

  grails-app / assets / 
├──images
...
├─ ─javascripts
│└──application.js
├──样式表
│├──application.css
│├──bootstrap-3.1.1
│ │└──stylesheets
││└──variables.less
│├──errors.css
│├──main.css
│└─mobile.css
└──vendor
└──bootstrap-3.1.1
├──javascript
│├──bootstrap.js
│└─bootstrap.min .js
└──stylesheets
├──alerts.less
...
├──variables.less
└──wells.less

我喜欢这个结果。我已经清楚地描述了分布式软件包并且相对容易升级,并且我对该软件包进行了更改,并将其分离为明确可识别的定制文件。



https://github.com/ggascoigne/grails-pipeline-scss上使用sass-asset-pipeline插件举了一个例子


Grails 2.4 is now using the Asset Pipeline for managing and processing static assets in Grails applications instead of the Resources system. This is pretty new and there isn't much doc about it on the internet yet.

My question is, how to correctly handle 3rd party libraries?

For instance, the select2 (http://ivaynberg.github.io/select2/) library comes with all its .css, .js, image files in a single flat folder.

Should I copy the different file types in to their corresponding assets/javascripts/, assets/stylesheets/, etc. subfolders? What to do with files that don't really have an obvious location like the .json, text, or doc files?

Should I create a vendors/select2/ folder? Where, in assets/ or in web-app/? And then, how should I load all the necessary files from my GSPs?

This lib will also be needed only in a form view, and therefore shouldn't be loaded unless it's needed.

解决方案

Baxxabit's answer led me to my solution. I've been struggling with much the same thing, but wanting to make the most of the pipeline so that I can use the less-asset-pipeline plugin.

Using bootstrap as an example, I created a vendor folder as a peer of the standard assets folders and then dropped the bootstrap files under there.

application.js:

//This is a javascript file with its top level require directives
//= require jquery
//= require bootstrap-3.1.1/javascript/bootstrap
//= require_tree views
//= require_self
...

and application.css:

/*
*= require_self
*= require main
*= require mobile
*= require bootstrap-3.1.1/stylesheets/bootstrap
*= encoding UTF-8
*/

To override the boostrap customization I can simply drop a customized variables.less file into ./grails-app/assets/vendor/bootstrap-3.1.1/stylesheets/variables.less. The first folder under assets is ignored when it comes to overriding files, thereafter the path needs to match whatever you put under vendor.

This gives me this sort of hierarchy:

grails-app/assets/
├── images
...
├── javascripts
│   └── application.js
├── stylesheets
│   ├── application.css
│   ├── bootstrap-3.1.1
│   │   └── stylesheets
│   │       └── variables.less
│   ├── errors.css
│   ├── main.css
│   └── mobile.css
└── vendor
    └── bootstrap-3.1.1
        ├── javascript
        │   ├── bootstrap.js
        │   └── bootstrap.min.js
        └── stylesheets
            ├── alerts.less
...
            ├── variables.less
            └── wells.less

I like the result. I've got the distributed package cleanly delineated and relatively easily upgradable, and I've got my changes to that package separated off into a clearly identifiable customization file.

I've put an example using the sass-asset-pipeline plugin at https://github.com/ggascoigne/grails-pipeline-scss.

这篇关于具有第三方库的Grails Asset-Pipeline系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆