Yeoman中的动态路径创建注释标签 [英] Dynamic path in Yeoman build comment tag

查看:101
本文介绍了Yeoman中的动态路径创建注释标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让动态路径进入 Yeoman 构建标记,例如

<! - build:js scripts / modernizr.js - >



用例可以在布局中的不同路径中嵌入不同的嵌套页面, / 到webroot或 ../../ 更高一级,例如类似于



<! - build:js<%= config.path.script%> /modernizr.js - > ;



相关主题已在 Yeoman / Grunt usemin子文件夹,但更优雅/灵活的解决方案呢?如果你编译成静态的HTML,你可以看看我的 github.com/ben-eb/grunt-magic-pathsrel =nofollow> grunt-magic-paths 任务。它可以使用用户定义的针(默认值是 path:),并根据其名称自动解析文件的路径。所以你可以这样做:

 < script src =path:modernizr.js>< / script> 

并根据输出目录自动解析路径名称。有一点需要注意的是,一条连接的路径必须是唯一的;即你不能将Modernizr的两个不同副本(版本)链接到相同的文件名。



你可能会发现,对于像这样构建步骤的大型应用程序来说,特别是随着应用程序的增长和路径数量的增加。我强烈建议将绝对路径用于大于个人投资组合的任何事物;然后你可以将域路径保存在一个配置文件中(在JS中,如下所示):

  module.exports ='http ://mydomain.com; 

然后用于制作:

  var domain = require('config.js'); 
console.log(domain);
// => http://mydomain.com

更改 config.js 文件,具体取决于您的环境;你可能想要 .gitignore 它,并且提供一个带有虚拟数据的示例配置。因为Grunt在Node上运行,所以在构建步骤和应用程序中都有这个配置(我假设你正在编写一个Node应用程序,对于其他项目你可能更喜欢JSON或PHP配置文件)。 p>

Is there a way to get dynamic path into the Yeoman build tag, e.g. for

<!-- build:js scripts/modernizr.js -->

The use-case would be to push in a different path in the layout for differently nested pages, either / to webroot or ../../ to an upper level, e.g. something like

<!-- build:js <%= config.path.script %>/modernizr.js -->

Related topic has already been discussed in a question of Yeoman/Grunt usemin subfolders, but what about a more graceful/flexible solution? Ideas?

解决方案

If you compile to static HTML, you can have a look at my grunt-magic-paths task. It can take a user defined needle (default is path:) and automatically resolve the path to a file based on its name alone. So you could do this:

<script src="path:modernizr.js"></script>

And based on the output directory it will resolve the path names automatically. The one caveat is that a linked path must be a unique one; i.e. you can't link two different copies (versions) of Modernizr with the same filename.

You may find though that for larger applications having a build step like this is brittle, especially as your application grows and the number of paths increase. I'd strongly recommend the use of absolute paths for anything larger than a personal portfolio; you can then keep the domain path in a config file (in JS, something like this):

module.exports = 'http://mydomain.com';

Then for use in production:

var domain = require('config.js');
console.log(domain);
// => http://mydomain.com

Change the config.js file depending on your environment; you may want to .gitignore it, and provide a sample config with dummy data instead. Because Grunt runs on top of Node you then have this config in your build step as well as your application (again I'm assuming you're writing a Node application, for other projects you may prefer JSON or PHP config files).

这篇关于Yeoman中的动态路径创建注释标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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