-
Notifications
You must be signed in to change notification settings - Fork 15
/
exportRequiredTemplates.js
119 lines (101 loc) · 3.45 KB
/
exportRequiredTemplates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Angular Builder middleware module.
*
* @module middleware/exportRequiredTemplates
*
* @license
* Copyright 2013 Cláudio Manuel Brás da Silva
* http://github.com/claudio-silva
* Licensed under the MIT license.
*/
'use strict';
var MATCH_DIRECTIVE = /\/\/#\s*templates?\s*\((.*?)\)/g;
//----------------------------------------------------------------------------------------------------------------------
// OPTIONS
//----------------------------------------------------------------------------------------------------------------------
/**
* Options specific to the Template References Handler middleware.
* @constructor
*/
function ExportRequiredTemplatesOptions ()
{}
ExportRequiredTemplatesOptions.prototype = {
/**
* The name of the Gruntfile config property to where the list of required template paths will be exported.
* These HTML templates are those required by javascript files included in the build via build-directives.
* @type {string}
*/
exportToConfigProperty: 'requiredTemplates'
};
/**
* @mixin
*/
var ExportRequiredTemplatesOptionsMixin = {
/**
* Options specific to the Template References Handler middleware.
* @type {ExportRequiredTemplatesOptions}
*/
requiredTemplates: new ExportRequiredTemplatesOptions ()
};
exports.options = ExportRequiredTemplatesOptionsMixin;
//----------------------------------------------------------------------------------------------------------------------
exports.middleware = ExportRequiredTemplatesMiddleware;
/**
* Exports to Grunt's global configuration the paths of all templates required by the application,
* in the order defined by the modules' dependency graph.
* @constructor
* @implements {MiddlewareInterface}
* @param {Context} context The execution context for the middleware stack.
*/
function ExportRequiredTemplatesMiddleware (context)
{
var options = context.options.requiredTemplates;
var path = require ('path');
/**
* Paths of the required templates.
* @type {string[]}
*/
var paths = [];
//--------------------------------------------------------------------------------------------------------------------
// PUBLIC API
//--------------------------------------------------------------------------------------------------------------------
this.analyze = function (filesArray)
{
/* jshint unused: vars */
// Do nothing
};
this.trace = function (module)
{
scan (module.head, module.headPath);
module.bodies.forEach (function (path, i)
{
scan (path, module.bodyPaths[i]);
});
};
this.build = function (targetScript)
{
/* jshint unused: vars */
// Export file paths.
context.grunt.config (options.exportToConfigProperty, paths);
};
//--------------------------------------------------------------------------------------------------------------------
// PRIVATE
//--------------------------------------------------------------------------------------------------------------------
/**
* Extracts file paths from embedded comment references to templates and appends them to `paths`.
* @param {string} sourceCode
* @param {string} filePath
*/
function scan (sourceCode, filePath)
{
/* jshint -W083 */
var match;
while ((match = MATCH_DIRECTIVE.exec (sourceCode))) {
match[1].split (',').forEach (function (s)
{
var url = s.match (/(["'])(.*?)\1/)[2];
paths.push (path.normalize (path.dirname (filePath) + '/' + url));
});
}
}
}