-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standardized templating #30
Comments
I think standardizing a templating language is important and I can add that to the plugin guidelines. I'm not sure which lightweight templating language I would like to use for this. As for gulp.define - is there any reason it needs to be a stream? Why not just call gulp.define at the top of your file? Adding gulp.define as a getter/setter for config would be easy and would add only a few lines of code. |
It is completely possible to use gulp.define at the top. Now that I think about it, yes, that is a better idea. There isn't any use case otherwise(that I can think of right now) for making it an inline plugin. Once one arises, then an inline plugin would be useful. On standardizing templating languages -- yes, that is a really good idea. But I would also really recommend defining some function like gulp.template that compiles whatever templating language you choose. This would give plugin authors no incentive to use any other templating, because the templating function is right there. It'd probably be okay to just copy the grunt.template api. It'd be also a good idea to attach all properties defined by gulp(e.g. file base, file path, etc.) to templates when they are compiled, so you can do something like |
lodash.template is probably the most minimalist template module. If you
|
this looks like a bad idea. it will increase tight coupling between plugin and task runner. It doesn't really matter if plugin uses template X or Y, all that matters is that they share the same API style. In fact I'm against plugins since they lock cool features into a single runner, which is a bad thing unless you have a LOT of advantages by reusing the original lib. npm solves all the dependencies issues, no need to enforce a single style. - see #31 for more feedback related to plugins. |
@millermedeiros the gulp-util library is just another dependency, it
|
Gulp has to have opinions otherwise usability suffers. As nice as it sounds to let everyone run wild with whatever they want most users don't want to learn a different templating style for every gulp plugin stream they use in their build process. Gulp won't enforce anything on your "plugin" (really just a stream that takes in our file format and puts out our file format) but the convention should exist |
I agree with @contra a lot here. Grunt is popular, even being so painful,
|
Might also be a good idea to just include all of lodash in gulp-util, like how grunt does. I'm still 50/50 on that specific idea, though. |
Don't turn gulp into grunt, we already have grunt. Take the clean slate opportunity to solve the problems better. |
@geddski Absolutely not but templating in plugins is common and defining a standard for doing so is going to make using gulp even easier than before. I don't think anything needs to be added to core but I will just note that they should use X templating language in the plugin guidelines. Does anyone have good arguments for a templating language other than lodash? |
All else being equal, handlebars/mustache syntax seems the most universal at the moment. Angular uses the same syntax. Underscore's (and by extension lodash's) syntax seems overly verbose. |
Handlebars and mustache are templating languages for html and make it a
|
I agree with @phated. Lo-dash is a definite yes, because underscore templates are already used for situations like this. And the ES6 style is really cool, I didn't know they supported that. Just taking the example above, we could do this: gulp.files('./**/**').pipe(exec('git checkout ${gulp.file.path}')); In comparison, handlebars and mustache are for html. If you as a plugin developer really want to use handlebars/mustache, Lo-Dash lets you do that too: _.templateSettings = {
'interpolate': /{{([\s\S]+?)}}/g
}; Although that I wouldn't recommend. |
I'm good with the group's choice here. Should plugins break when migrating to this new format or should they support both ways for a time? |
Break and bump, as per semver.
|
Okay so I thought a bit more about .define and I'm leaning towards no. Anything that needs to be passed to a plugin can be done using function arguments. Templating should be reserved for file attributes or fields from the object passed into the plugin. Having a global object being mutated by .define() statements and then letting plugins just pick and choose stuff off of it seems really against what gulp is about. There are also cases where you want to pass different stuff to different plugins, mutating state while asynchronous tasks happen, etc. that can be avoided by just keeping it simple. The same example: gulp.files('./**/**').pipe(exec('git checkout <%= file.path %>'));
var headerText = '' +
'/*! <%= file.path %> - '+
'Copyright <%= year %> MyCompany - '+
'MIT License - '+
'generated <%= now %> - <%= foo %> */'+
'';
gulp.src('./lib/*.js')
.pipe(concat('merged.js'))
.pipe(header(headerText, {
foo: 'bar',
year: '2013',
now: function () {
return new Date().toISOString();
}
})
.pipe(gulp.dest('./dist/') In a template |
@contra fair enough. I thought there may be some global things the user may want to define, but they can do that on their own with just an object literal. |
@contra +1, the exec plugin could simply define it's own templating system for the command. |
+1 for not adding
Sounds great to me :-) |
Solved by gulp-util 1.1. New gulp release coming out shortly |
2.6.1 is out |
How about coffeescript, I know there's a plugin for running the compiler as a task, but is it possible to use CS as the gulp language? If so, string formatting isn't much of a problem. |
@backspaces It is possible to run gulp as a coffeescript file(as it says on the readme), however, coffeescript only allows for templating on the compiler level. E.g. if I do this: exec "git checkout #{file}" It gets compiled to this: exec("git checkout " + file); Which is not what we want because the plugin provides the file object, but coffeescript only offers templating for the running file. This would result in an exception. Even if coffeescript magically had a way to compile strings to some invisible template functions or what have you, forcing every user to use coffeescript is incredibly opinionated and should be avoided. |
@gratimax: Thanks, very clear .. and no, I'd not ever require CS, but knowing its available for my own use is nice. |
@backspaces My gulpfiles are usually in CS too |
I believe that the lack of many things is good for gulp, however, if we don't have a standard string templating system quick, then there's going to be a problem.
Let's take gulp-exec vs gulp-header:
gulp-exec uses
$file
(hint hint) as a sort of templating string for receiving files.Notice that gulp-header uses templates like
{{filename}}
(hint hint) as templates for receiving files.I think this is going to become a problem very soon. Lots of gulp plugins need some form of templating, at least. Grunt got this solved just by using underscore templates, and I think if we also had something like that, we'd be way more happy.
What I propose: use underscore/lodash templates, along with adding a new function, gulp.define. Underscore/Lodash templates can be compiled when they are passed to a plugin, and define takes an object(or two values) and passes them on to the immediate next task(that is not a .define() call).
Then, The top two examples become:
This is probably a very hacky and quick solution to this problem. I'm thinking of other ways to solve it right now.
The text was updated successfully, but these errors were encountered: