Skip to content

Commit 3c0c606

Browse files
author
Jeff May
committed
Merge pull request #104 from lee-elenbaas/master
Templates for html2ts module and var
2 parents e80bb28 + 33e58df commit 3c0c606

File tree

9 files changed

+167
-78
lines changed

9 files changed

+167
-78
lines changed

Gruntfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ module.exports = function (grunt) {
277277
grunt.loadNpmTasks('grunt-contrib-nodeunit');
278278
grunt.loadNpmTasks('grunt-contrib-watch');
279279

280+
grunt.loadNpmTasks('grunt-debug-task');
281+
280282
// Build
281283
grunt.registerTask('prep', ['clean', 'jshint:support']);
282284
grunt.registerTask('build', ['prep', 'ts-internal', 'tslint:source']);

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ It is possible to specify this string to the template on a view: http://emberjs.
161161

162162
Specifically: http://stackoverflow.com/a/9867375/390330
163163

164+
#### Control generated TypeScript module and variable names
165+
166+
In the task options htmlModuleTemplate and htmlVarTemplate can specify an Underscore templates to be used in order to generate the module and variable names for the generated TypeScript.
167+
168+
Those Underscore template recieve the following parameters:
169+
170+
* filename - The html file name without the extension ("test" if the file was named test.html)
171+
* ext - The html extension without the dot ("html" if the file was named test.html)
172+
173+
The default templates are:
174+
175+
* "<%= filename %>" - for the module name. (This maintain existing behavior of older versions, and allow controlling the module name by simply renaming the file.)
176+
* "<%= ext %>" - for the variable name. (This maintain existing behavior of older versions, again allowing to control variable name by renaming the file.)
177+
178+
Usage example is setting the module template to "MyModule.Templates" and the variable template to "<%= filename %>" this will result for the test.html file above with the generated TypeScript
179+
```typescript
180+
module MyModule.Templates { export var test = '<div Some content </div>' }
181+
```
182+
164183
### Live file watching and building
165184

166185
Grunt-ts can watch a directory and recompile TypeScript files when any TypeScript file changes, gets added, gets removed. Internallythe `chokidar` module is used to makes sure the project is always build ready :)

package.json

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
11
{
2-
"author": "basarat",
3-
"name": "grunt-ts",
4-
"description": "Compile and manage your TypeScript project",
5-
"version": "1.11.2",
6-
"homepage": "https://github.com/grunt-ts/grunt-ts",
7-
"repository": {
8-
"type": "git",
9-
"url": "[email protected]:grunt-ts/grunt-ts.git"
2+
"author": "basarat",
3+
"name": "grunt-ts",
4+
"description": "Compile and manage your TypeScript project",
5+
"version": "1.11.2",
6+
"homepage": "https://github.com/grunt-ts/grunt-ts",
7+
"repository": {
8+
"type": "git",
9+
"url": "[email protected]:grunt-ts/grunt-ts.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/grunt-ts/grunt-ts/issues"
13+
},
14+
"licenses": [
15+
{
16+
"type": "MIT",
17+
"url": "https://github.com/grunt-ts/grunt-ts/blob/master/LICENSE"
18+
}
19+
],
20+
"main": "grunt.js",
21+
"maintainers": [
22+
{
23+
"name": "basarat",
24+
"email": "[email protected]"
1025
},
11-
"bugs": {
12-
"url": "https://github.com/grunt-ts/grunt-ts/issues"
26+
{
27+
"name": "jeffmay",
28+
"email": "[email protected]"
1329
},
14-
"licenses": [
15-
{
16-
"type": "MIT",
17-
"url": "https://github.com/grunt-ts/grunt-ts/blob/master/LICENSE"
18-
}
19-
],
20-
"main": "grunt.js",
21-
"maintainers": [
22-
{
23-
"name": "basarat",
24-
"email": "[email protected]"
25-
},
26-
{
27-
"name": "jeffmay",
28-
"email": "[email protected]"
29-
},
30-
{
31-
"name": "bartvds",
32-
"email": "[email protected]"
33-
}
34-
],
35-
"engines": {
36-
"node": ">= 0.8.0"
37-
},
38-
"scripts": {
39-
"test": "grunt test"
40-
},
41-
"dependencies": {
42-
"typescript": "1.0.0",
43-
"chokidar": "0.8.2",
44-
"underscore": "1.5.1",
45-
"underscore.string": "2.3.3",
46-
"es6-promise": "~0.1.1",
47-
"rimraf": "2.2.6",
48-
"ncp": "0.5.1"
49-
},
50-
"peerDependencies": {
51-
"grunt": "~0.4.0"
52-
},
53-
"devDependencies": {
54-
"grunt": "~0.4.0",
55-
"grunt-tslint": "~0.4.0",
56-
"grunt-contrib-watch": "~0.6.1",
57-
"grunt-contrib-clean": "~0.4.0",
58-
"grunt-contrib-jshint": "~0.8.0",
59-
"grunt-contrib-nodeunit": "~0.2.2",
60-
"tslint-path-formatter": "~0.1.1",
61-
"source-map-support": "~0.2.5",
62-
"jshint-path-reporter": "~0.1.3",
63-
"grunt-continue": "0.0.1"
64-
},
65-
"optionalDependencies": {},
66-
"keywords": [
67-
"gruntplugin",
68-
"typescript",
69-
"compiler"
70-
]
30+
{
31+
"name": "bartvds",
32+
"email": "[email protected]"
33+
}
34+
],
35+
"engines": {
36+
"node": ">= 0.8.0"
37+
},
38+
"scripts": {
39+
"test": "grunt test"
40+
},
41+
"dependencies": {
42+
"typescript": "1.0.0",
43+
"chokidar": "0.8.2",
44+
"underscore": "1.5.1",
45+
"underscore.string": "2.3.3",
46+
"es6-promise": "~0.1.1",
47+
"rimraf": "2.2.6",
48+
"ncp": "0.5.1"
49+
},
50+
"peerDependencies": {
51+
"grunt": "~0.4.0"
52+
},
53+
"devDependencies": {
54+
"grunt": "~0.4.0",
55+
"grunt-continue": "0.0.1",
56+
"grunt-contrib-clean": "~0.4.0",
57+
"grunt-contrib-jshint": "~0.8.0",
58+
"grunt-contrib-nodeunit": "~0.2.2",
59+
"grunt-contrib-watch": "~0.6.1",
60+
"grunt-debug-task": "^0.1.4",
61+
"grunt-tslint": "~0.4.0",
62+
"jshint-path-reporter": "~0.1.3",
63+
"source-map-support": "~0.2.5",
64+
"tslint-path-formatter": "~0.1.1"
65+
},
66+
"optionalDependencies": {},
67+
"keywords": [
68+
"gruntplugin",
69+
"typescript",
70+
"compiler"
71+
]
7172
}

sample/Gruntfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module.exports = function (grunt) {
1515
sourceRoot: '', // where to locate TypeScript files. [(default) '' == source ts location]
1616
mapRoot: '', // where to locate .map.js files. [(default) '' == generated js location.]
1717
declaration: false, // generate a declaration .d.ts file for every output js file. [true | false (default)]
18+
htmlModuleTemplate: 'My.Module.<%= filename %>', // Template for module name for generated ts from html files [(default) '<%= filename %>']
19+
htmlVarTemplate: '<%= ext %>' // Template for variable name used in generated ts from html files [(default) '<%= ext %>]
20+
// Both html templates accept the ext and filename parameters.
1821
},
1922
// a particular target
2023
dev: {

tasks/modules/html2ts.js

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/modules/html2ts.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,25 @@ function stripBOM(str) {
2828

2929
var htmlTemplate = _.template('module <%= modulename %> { export var <%= varname %> = \'<%= content %>\' } ');
3030

31+
export interface IHtml2TSOptions {
32+
moduleFunction: Function;
33+
varFunction: Function
34+
}
35+
3136
// Compile an HTML file to a TS file
3237
// Return the filename. This filename will be required by reference.ts
33-
export function compileHTML(filename: string): string {
38+
export function compileHTML(filename: string, options: IHtml2TSOptions): string {
3439
var htmlContent = escapeContent(fs.readFileSync(filename).toString());
3540
htmlContent = stripBOM(htmlContent);
3641
// TODO: place a minification pipeline here if you want.
3742

38-
var ext = path.extname(filename);
39-
var extFreename = path.basename(filename, ext);
40-
var fileContent = htmlTemplate({ modulename: extFreename, varname: ext.replace('.', ''), content: htmlContent });
43+
var ext = path.extname(filename).replace('.', '');
44+
var extFreename = path.basename(filename, '.' + ext);
45+
46+
var moduleName = options.moduleFunction({ ext: ext, filename: extFreename });
47+
var varName = options.varFunction({ ext: ext, filename: extFreename }).replace('.', '_');
48+
49+
var fileContent = htmlTemplate({ modulename: moduleName, varname: varName, content: htmlContent });
4150

4251
// Write the content to a file
4352
var outputfile = filename + '.ts';

tasks/modules/interfaces.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ interface ITaskOptions {
4949
verbose: boolean;
5050
compile: boolean;
5151
fast: string; // never | always | watch (default)
52+
53+
htmlModuleTemplate: string;
54+
htmlVarTemplate: string;
5255
}

tasks/ts.js

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/ts.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,18 @@ function pluginFn(grunt: IGrunt) {
105105
target: 'es5', // es3 , es5
106106
verbose: false,
107107
fast: 'watch',
108+
htmlModuleTemplate: '<%= filename %>',
109+
htmlVarTemplate: '<%= ext %>',
108110
failOnTypeErrors: true
109111
});
110112

113+
// get unprocessed templates from configuration
114+
var rawTaskOptions = <ITaskOptions>(grunt.config.getRaw(currenttask.name + '.options') || {});
115+
var rawTargetOptions = <ITaskOptions>(grunt.config.getRaw(currenttask.name + '.' + currenttask.target + '.options') || {});
116+
117+
options.htmlModuleTemplate = rawTargetOptions.htmlModuleTemplate || rawTaskOptions.htmlModuleTemplate;
118+
options.htmlVarTemplate = rawTargetOptions.htmlVarTemplate || rawTaskOptions.htmlVarTemplate;
119+
111120
// fix the properly cased options to their appropriate values
112121
options.allowBool = 'allowbool' in options ? options['allowbool'] : options.allowBool;
113122
options.allowImportModule = 'allowimportmodule' in options ? options['allowimportmodule'] : options.allowImportModule;
@@ -119,6 +128,16 @@ function pluginFn(grunt: IGrunt) {
119128
options.fast = 'watch';
120129
}
121130

131+
if (!options.htmlModuleTemplate) {
132+
console.warn(('htmlModuleTemplate must be provided, reverting to default template: "<%= filename %>"').magenta);
133+
options.htmlModuleTemplate = '<%= filename %>';
134+
}
135+
136+
if (!options.htmlVarTemplate) {
137+
console.warn(('htmlVarTemplate must be provided, reverting to default template: "<%= ext %>"').magenta);
138+
options.htmlVarTemplate = '<%= ext %>';
139+
}
140+
122141
// Remove comments based on the removeComments flag first then based on the comments flag, otherwise true
123142
if (options.removeComments === null) {
124143
options.removeComments = !options.comments;
@@ -198,7 +217,7 @@ function pluginFn(grunt: IGrunt) {
198217
function runCompilation(files: string[], target: ITargetOptions, options: ITaskOptions): Promise<boolean> {
199218
// Don't run it yet
200219
grunt.log.writeln('Compiling...'.yellow);
201-
220+
202221
// The files to compile
203222
var filesToCompile = files;
204223

@@ -293,8 +312,13 @@ function pluginFn(grunt: IGrunt) {
293312
// compile html files must be before reference file creation
294313
var generatedFiles = [];
295314
if (currenttask.data.html) {
315+
var html2tsOptions = {
316+
moduleFunction: _.template(options.htmlModuleTemplate),
317+
varFunction: _.template(options.htmlVarTemplate)
318+
};
319+
296320
var htmlFiles = grunt.file.expand(currenttask.data.html);
297-
generatedFiles = _.map(htmlFiles, (filename) => html2tsModule.compileHTML(filename));
321+
generatedFiles = _.map(htmlFiles, (filename) => html2tsModule.compileHTML(filename, html2tsOptions));
298322
}
299323

300324
///// Template cache

0 commit comments

Comments
 (0)