forked from welldone-software/gulp-durandal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (115 loc) · 4.88 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var fs = require('fs'),
path = require('path'),
requirejs = require('requirejs'),
es = require('event-stream'),
glob = require('glob'),
_ = require('lodash')._,
gutil = require('gulp-util');
var PLUGIN_NAME = 'gulp-durandaljs',
durandalDynamicTransitions = ['transitions/entrance'],
durandalDynamicPlugins = ['plugins/dialog', 'plugins/history','plugins/http',
'plugins/observable', 'plugins/router', 'plugins/serializer', 'plugins/widget'],
defOptions = {
baseDir: 'app',
main: 'main.js',
extraModules: [],
durandalDynamicModules: true,
verbose : false,
output: undefined,
minify: false,
require : undefined,
almond: false,
moduleFilter: function(m){return true;},
textModuleExtensions : ['.json', '.html', '.txt']
};
module.exports = function gulpDurandaljs(userOptions){
var _s = es.pause(),
options = _.defaults(userOptions || {}, defOptions),
baseDir = options.baseDir,
mainFile = path.join(baseDir, options.main),
almondWrapper = (function(){
var almond = options.almond,
almondPath = typeof(almond) === 'string' ? almond : path.join(__dirname, 'res/custom-almond.js');
if(!almond){
return undefined;
}
return {
start: '(function() {\n' + fs.readFileSync(almondPath, {encoding: 'utf-8'}),
end: '}());'
};
})(),
dynamicModules = (function(){
var mainFileContent = fs.readFileSync(mainFile, {encoding: 'utf-8'}),
plugins = mainFileContent.match(/['"]?plugins['"]?\s*:/) ? durandalDynamicPlugins : [],
transitions = mainFileContent.match(/['"]?transitions['"]?\s*:/) ? durandalDynamicTransitions : [];
return [].concat(plugins, transitions);
})(),
scannedModules = (function(){
var stripExtension = function(p){ return p.substr(0, p.length - path.extname(p).length); },
expand = function(p){ return glob.sync(path.normalize(path.join(baseDir, p))); },
relativeToBaseDir = path.relative.bind(path, baseDir),
jsFiles = _.unique( _.flatten([ mainFile, expand('/**/*.js') ])),
jsModules = jsFiles.map(relativeToBaseDir).map(stripExtension),
textFiles = _.flatten(_.map(options.textModuleExtensions, function(ext){return expand('/**/*'+ext);})),
textModules = textFiles.map(relativeToBaseDir).map(function(m){ return 'text!' + m; }),
scannedModules = {js: jsModules, text: textModules};
return scannedModules;
})(),
allModules = (function(){
var fixSlashes = function(p){ return p.replace(new RegExp('\\\\','g'),'/');},
modules =
_.flatten([scannedModules.js, options.extraModules || [], dynamicModules, scannedModules.text])
.map(fixSlashes)
.filter(options.moduleFilter);
return _.unique(modules);
})(),
insertRequireModules = (function(){
if(typeof(options.require) === 'string' || _.isArray(options.require)){
return _.flatten([options.require]);
}
else if(options.require === true || (options.almond && options.require !== false)){
return [allModules[0]];
}
return undefined;
})(),
rjsCb = function(text, sourceMapText){
var output = options.output || path.basename(mainFile),
mapOutput = output + '.map';
_s.resume();
text += '//# sourceMappingURL=' + path.basename(mapOutput);
_s.write(new gutil.File({
path: output,
contents: new Buffer(text)
}));
if(sourceMapText){
_s.write(new gutil.File({
path: mapOutput,
contents: new Buffer(sourceMapText)
}));
}
_s.end();
},
errCb = function(err){
_s.resume();
_s.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
_s.end();
};
var rjsConfig = {
logLevel: options.verbose ? 0 : 4,
baseUrl : baseDir,
mainConfigFile: mainFile,
include : allModules,
out: rjsCb,
optimize: options.minify ? 'uglify2' : 'none',
preserveLicenseComments: !options.minify,
generateSourceMaps : true,
insertRequire : insertRequireModules,
wrap: almondWrapper
};
rjsConfig = _.defaults(options.requirejs, rjsConfig);
requirejs.optimize(rjsConfig, null, errCb);
_s.on('error', function(e){
gutil.log('Durandal ' + gutil.colors.red(e.message));
});
return _s;
};