forked from karma-runner/karma-commonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
53 lines (42 loc) · 1.63 KB
/
plugin.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
var path = require('path');
var os = require('os');
var BRIDGE_FILE_PATH = path.normalize(__dirname + '/../client/commonjs_bridge.js');
var initCommonJS = function(/* config.files */ files) {
// Include the file that resolves all the dependencies on the client.
files.push({
pattern: BRIDGE_FILE_PATH,
included: true,
served: true,
watched: false
});
};
var createPreprocesor = function(logger, config, basePath) {
var log = logger.create('preprocessor.commonjs');
var modulesRootPath = path.resolve(config && config.modulesRoot ? config.modulesRoot : path.join(basePath, 'node_modules'));
//normalize root path on Windows
if (process.platform === 'win32') {
modulesRootPath = modulesRootPath.replace(/\\/g, '/');
}
log.debug('Configured root path for modules "%s".', modulesRootPath);
return function(content, file, done) {
if (file.originalPath === BRIDGE_FILE_PATH) {
return done(content);
}
log.debug('Processing "%s".', file.originalPath);
var indentStr = function(str) {
return str.replace(/(\r\n|\n|\r)/g, '\n ');
};
var output =
'window.__cjs_modules_root__ = "' + modulesRootPath + '"' + "\n" +
'window.__cjs_module__ = window.__cjs_module__ || {}' + "\n" +
'window.__cjs_module__["' + file.path + '"] = (require, module, exports) ->' + "\n" +
indentStr(content) + "\n";
done(output);
};
};
createPreprocesor.$inject = ['logger', 'config.commonjsPreprocessor', 'config.basePath'];
// PUBLISH DI MODULE
module.exports = {
'framework:commonjs': ['factory', initCommonJS],
'preprocessor:commonjs': ['factory', createPreprocesor]
};