forked from lazd/gulp-declare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (52 loc) · 1.85 KB
/
index.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
var map = require('vinyl-map');
var path = require('path');
var extend = require('xtend');
var declare = require('nsdeclare');
// Default name processing function should give the filename without extension
var defaultProcessName = function(name) { return path.basename(name, path.extname(name)); };
var processNameByPath = function(filePath, separatorFormat) {
// By default the separator format for join is a dot but it can be something else
separatorFormat = typeof separatorFormat !== 'undefined' ? separatorFormat : '.';
// Make the directory relative
filePath = path.relative(process.cwd(), filePath);
// Split the path into its components based on the separator
var parts = filePath.split(path.sep);
// Remove and process template name
var templateName = path.basename(parts.pop(), '.js');
// Add template name back
parts.push(templateName);
// Turn the path into dot notation
return parts.join(separatorFormat);
};
module.exports = function(options) {
options = extend({
processName: defaultProcessName,
namespace: 'this',
separator: '\n',
root: 'this',
noRedeclare: false
}, options);
// Support removal of duplicate declarations
var alreadyDeclared = null;
if (options.noRedeclare) {
alreadyDeclared = {};
}
var declareNamespace = function(contents, filename) {
contents = contents.toString();
// Get the name of the template
var name = options.processName(filename);
// Prepend namespace to name
if (options.namespace !== 'this') {
name = options.namespace+'.'+name;
}
// Get namespace information for the final template name
return declare(name, {
declared: alreadyDeclared,
value: contents,
separator: options.separator,
root: options.root
});
};
return map(declareNamespace);
};
module.exports.processNameByPath = processNameByPath;