TypeStrong / grunt-ts

A grunt task to manage your complete typescript development to production workflow
https://www.npmjs.com/package/grunt-ts
MIT License
330 stars 121 forks source link

Add support for file names with multiple dashes on them. #324

Closed xenit-raven closed 8 years ago

xenit-raven commented 8 years ago

When compiling html files with names containing dashes, the compile function creates invalid ts output.

I've started using dashes in my file names as suggested by JohnPapa's style guide. This pull request is inspired by https://github.com/TypeStrong/grunt-ts/pull/237

Options used:

options: {
    htmlModuleTemplate: '<%= filename %>_<%= ext %>_module',    // Template for module name for generated ts from html files [(default) '<%= filename %>']
    htmlVarTemplate:    '<%= filename %>_<%= ext %>_variable',  // Template for variable name used in generated ts from html files [(default) '<%= ext %>]
},

Example: example-page.tpl.html -> example-page.tpl.html.ts

Outputs

/* tslint:disable:max-line-length */
module example-page.tpl_html_module {
    export var example-page.tpl_html_variable = '<div>Hello World</div>';
}

With compile errors:

example-page.tpl.html.ts(2,15): error TS1005: '{' expected.
example-page.tpl.html.ts(2,37): error TS1005: ';' expected.
example-page.tpl.html.ts(3,21): error TS1005: '=' expected.
example-page.tpl.html.ts(3,45): error TS1005: ',' expected.
example-page.tpl.html.ts(3,47): error TS1134: Variable declaration expected.

Dashes are not accepted as variable (and module) names, so I added a fix that would convert a dash to camelCase. I've also added the relevant tests. I hope my contribution is correct and useful!

nycdotnet commented 8 years ago

Neat. Will check it out soon.

xenit-raven commented 8 years ago

I'd be an interesting addition if you could transform the variable in the template. For example by adding a callback on the resolved variable:

options {
    htmlModuleTemplate: '<%= filename %>'
}

This option would now convert example-page.tpl.html to the module examplePage.tpl, which currently clashes with my directive called examplePage.

Foolishly, I tried performing regex on the template variable

options {
    htmlModuleTemplate: '<%= filename %>'.replace(/\.tpl/,'')
}

which of course only acts on the string '<%= filename %>'.

didlich commented 8 years ago

would welcome this fix, because I am using also john papa's guide and many of my templates have dashes, for now I am not able to use this html template compile feature

nycdotnet commented 8 years ago

Sorry for sitting on this. I'm finally done with my Pluralsight course so I should have some time to review this soon.

nycdotnet commented 8 years ago

Thanks very much for contributing!!