gulp-declare icon indicating copy to clipboard operation
gulp-declare copied to clipboard

How to include dots in filenames?

Open giladgray opened this issue 10 years ago • 19 comments

Our template files are named according to this scheme: unique.resource.identifier-FileName where the URI is a dot-separated string a la Java packages. We expose them at the namespace Handlebars.templates. gulp-declare converts the URI parts into namespaces but I want them to stay as one big word. Is this possible?

What I want:

this["Handlebars"] = this["Handlebars"] || {};
this["Handlebars"]["templates"] = this["Handlebars"]["templates"] || {};
this["Handlebars"]["templates"]["unique.resource.identifier-FileName"] = Handlebars.template ...

What I get:

this["Handlebars"] = this["Handlebars"] || {};
this["Handlebars"]["templates"] = this["Handlebars"]["templates"] || {};
this["Handlebars"]["templates"]["unique"] = this["Handlebars"]["templates"]["unique"] || {};
this["Handlebars"]["templates"]["unique"]["resource"] = this["Handlebars"]["templates"]["unique"]["resource"] || {};
this["Handlebars"]["templates"]["unique"]["resource"]["identifier"] = this["Handlebars"]["templates"]["unique"]["resource"]["identifier"] || {};
this["Handlebars"]["templates"]["unique"]["resource"]["identifier"]["FileName"] = Handlebars.template(...)

Current usage:

.pipe declare({
  namespace: 'Handlebars.templates'
  processName: (path) ->
    # path example: path/to/<uri>/scripts/path/to/<filename>.js
    match = /([^\/]+)\/scripts.*\/([^\.]+)/.exec path
    return "#{match[1]}-#{match[2]}"
})

giladgray avatar Jan 22 '15 18:01 giladgray

I tried escaping the periods in the template name but it didn't have any effect.

return "#{match[1]}-#{match[2]}".replace /\./g, "\."

giladgray avatar Jan 22 '15 18:01 giladgray

Currently, there is no option to do this. We can certainly add one, something like subNamespaceWithFilenames: true, which you can set to false for your case... Thoughts?

lazd avatar Jan 22 '15 18:01 lazd

I'd be into that. Seems like a reasonable use case to support. I'm happy to put that together if you can point me in the right direction.

giladgray avatar Jan 22 '15 18:01 giladgray

Hmm looks a lot like this open PR: https://github.com/lazd/nsdeclare/pull/4

giladgray avatar Jan 22 '15 18:01 giladgray

Yes, but I'm not a fan of the way it was implemented or the "bracket wrapping" approach of enabling the behavior.

On Jan 22, 2015, at 10:33 AM, Gilad Gray [email protected] wrote:

Hmm looks a lot like this open PR: lazd/nsdeclare#4

— Reply to this email directly or view it on GitHub.

lazd avatar Jan 22 '15 18:01 lazd

So you'd prefer a simple options flag that changes how filenames are interpreted? I liked how that PR made it opt-in with special syntax but a flag makes things even easier.

giladgray avatar Jan 22 '15 19:01 giladgray

Hmm since everything goes through declare() it seems to me a string-based approach is the simplest to accomplish this new feature. Like, can we encode in the namespace string that these dots are part of the name? This is similar to what is done in that PR linked above, but I don't like all the extra quotes so I'd propose one of the following:

bracket grouping: "Handlebars.templates.[unique.resource.identifier-FileName]". contents of bracket are treated as once word, quotes not allowed.

escape dots: "Handlebars.templates.unique\.resource\.identifier-FileName". simply escape the dots that you want to preserve.

Thoughts?

giladgray avatar Jan 22 '15 19:01 giladgray

In light of #6, I've decided to implement this solely using gulp-wrap by including the namespace and generating the template name in the wrapper function.

Would still like to see this solved eventually...

giladgray avatar Jan 22 '15 22:01 giladgray

@lazd introducing a flag such as subNamespaceWithFilenames means that all the dots will be ignored and included in the keys, where as a string-based pattern limits it to where ever its used, which can be a combination of both. i.e. Handlebars.templates.unique\.resource\.identifier-FileName

escaping the dots seems like a good idea, but the reason I went with the brackets in https://github.com/lazd/nsdeclare/pull/4, is because it follows the normal behavior of any JS interpreter, if you ever want to use dots in keys.

ns.a.b.c // ns = { a: { b: { c : "foo" } } }
// vs
ns.a["b.c"] // ns = { a: { "b.c": "bar" } }

akhoury avatar Apr 21 '15 17:04 akhoury

@akhoury I see what you mean about escaping dots being more flexible. I suppose that's something we could support, but I wonder if we'll need \\ to get a literal backslash in there...

lazd avatar Apr 21 '15 19:04 lazd

I wonder if we'll need \ to get a literal backslash in there..

yes, of course, just like any JS string

akhoury avatar Apr 21 '15 19:04 akhoury

var a = {};
a["\\"] = 1;
console.log(a); // prints {\: 1}
console.log(a["\\"] );  // prints 1

You can virtually use anything as a key in JS, under the hood the compiler will call toString() on whatever you use.

akhoury avatar Apr 21 '15 19:04 akhoury

Hi,

I am also having the same issue but with the extension. so my extension .hbss is getting considered as ['filePath']["hbss"] please let me know if you had found a way to pass DOT with the filename.

imran5387 avatar Dec 08 '15 11:12 imran5387

@imran5387 that's actually a different issue, it shouldn't be putting hbss in the namespace. Can you open another issue and paste your gulpfile and your input directory structure/file naming?

lazd avatar Dec 08 '15 17:12 lazd

Is this still in discussion? Regarding whether this is a common use case...it seems for handlebars precompilation in node the templates are assigned to handlebars.templates["filenameincludingdots"]. When using with Typescript a single string key is preferable over using namespaces because the namespaces do not exist without referencing the compiled output.

mykohsu avatar Jan 12 '16 22:01 mykohsu

Another bump to see if this is still being considered. I see that this plugin hasn't been updated in a couple of years - is anyone still supporting this? I'd be keen to see the ability to use filenames including dots, as others above have indicated.

Thanks!

EDIT: As a workaround, I'm using the processName to find the dot in the filename and replace it with a token (@SEPERATOR@), and then using gulp-replace to post-process the file. Not the nicest solution, but avoids me needing to refactor a load of code as I move from Grunt to Gulp. Hopefully this is of use to others.

james-braund-gw avatar Sep 13 '16 14:09 james-braund-gw

@james-braund-gw I'm still open to it, and PRs are welcome.

lazd avatar Sep 13 '16 15:09 lazd

@lazd - Good to know, cheers!

There was a PR previously mentioned which I see is still open:

https://github.com/lazd/nsdeclare/pull/4

Was there a particular reason you weren't keen on this approach? I see from the comments that there was some debate over syntax vs flag to indicate whether or not to use the dot as a separator or as part of the name, but it didn't really get resolved.

james-braund-gw avatar Sep 13 '16 15:09 james-braund-gw

@james-braund-gw let's take the discussion over to https://github.com/lazd/nsdeclare/issues/5.

lazd avatar Sep 13 '16 18:09 lazd