1 |
|
2 | var assert = require('assert');
|
3 | var fs = require('fs');
|
4 | var path = require('path');
|
5 | var minstache = require('minstache');
|
6 | var jsStringEscape = require('js-string-escape');
|
7 | var combineSourceMap = require('combine-source-map');
|
8 | var convertSourceMap = require('convert-source-map');
|
9 |
|
10 |
|
11 | var CONTENT_PLACEHOLDER_STR = '__CONTENT__';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var karmaWebServerFilepath = require.resolve('karma/lib/web-server');
|
18 | var karmaStaticDirectoryFilepath = path.normalize(path.join(path.dirname(karmaWebServerFilepath), '/../static'));
|
19 |
|
20 | var karmaDefaultContextFile = karmaStaticDirectoryFilepath + '/context.html';
|
21 | var karmaDefaultDebugFile = karmaStaticDirectoryFilepath + '/debug.html';
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var templateStr = fs.readFileSync(__dirname + '/node-integration-iframe.mustache.js', 'utf8');
|
29 | var minifiedTemplateStr = templateStr.replace(/\/\/[^\n]+/g, '\n').replace(/\n/g, '');
|
30 |
|
31 | minifiedTemplateStr = minifiedTemplateStr.replace(/{{if_/g, '{{#')
|
32 | .replace(/{{notif_/g, '{{^').replace(/{{end_/g, '{{/');
|
33 |
|
34 | minifiedTemplateStr = minifiedTemplateStr.replace(CONTENT_PLACEHOLDER_STR, CONTENT_PLACEHOLDER_STR + '\n');
|
35 | var template = minstache.compile(minifiedTemplateStr);
|
36 |
|
37 |
|
38 | var $inject = ['config.basePath', 'config.client', 'config.customContextFile', 'config.customDebugFile'];
|
39 | function createElectronPreprocessor(karmaBasePath, karmaClientConfig, karmaCustomContextFile, karmaCustomDebugFile) {
|
40 |
|
41 | function electronPreprocessor(content, file, done) {
|
42 |
|
43 | var output = template({
|
44 | __filenameOverride: karmaClientConfig.__filenameOverride,
|
45 | dirname: jsStringEscape(path.dirname(file.originalPath)),
|
46 | filename: jsStringEscape(file.originalPath),
|
47 | karmaBasePath: jsStringEscape(karmaBasePath),
|
48 | karmaContextFile: jsStringEscape(karmaCustomContextFile || karmaDefaultContextFile),
|
49 | karmaDebugFile: jsStringEscape(karmaCustomDebugFile || karmaDefaultDebugFile),
|
50 | loadScriptsViaRequire: !!karmaClientConfig.loadScriptsViaRequire,
|
51 | sep: jsStringEscape(path.sep)
|
52 | });
|
53 |
|
54 |
|
55 | if (karmaClientConfig.loadScriptsViaRequire) {
|
56 |
|
57 | var contentSourceMap = convertSourceMap.fromSource(content);
|
58 | if (contentSourceMap) {
|
59 | console.warn('Detected sourcemap in "' + file.originalPath + '" while `loadScriptsViaRequire: true` was set. ' +
|
60 | '`karma-electron` cannot sourcemap this content as it\'s no longer present in the file ' +
|
61 | 'as it\'s loaded via `require`');
|
62 | }
|
63 |
|
64 | } else {
|
65 |
|
66 |
|
67 | var splitOutput = output.split(CONTENT_PLACEHOLDER_STR);
|
68 | assert.strictEqual(splitOutput.length, 2);
|
69 | var sourcemapCombiner = combineSourceMap.create();
|
70 | sourcemapCombiner.addFile({
|
71 |
|
72 | sourceFile: __dirname + '/_node-integration-iframe.mustache.js' + file.originalPath,
|
73 | source: splitOutput[0]
|
74 | }, {line: 0});
|
75 | sourcemapCombiner.addFile({
|
76 | sourceFile: file.originalPath,
|
77 | source: content
|
78 | }, {line: 1});
|
79 |
|
80 |
|
81 | output = [
|
82 | splitOutput[0],
|
83 | content,
|
84 | splitOutput[1],
|
85 | sourcemapCombiner.comment()
|
86 | ].join('\n');
|
87 | }
|
88 |
|
89 |
|
90 | done(null, output);
|
91 | }
|
92 |
|
93 |
|
94 | return electronPreprocessor;
|
95 | }
|
96 |
|
97 |
|
98 | createElectronPreprocessor.$inject = $inject;
|
99 |
|
100 |
|
101 | module.exports = {
|
102 | 'preprocessor:electron': ['factory', createElectronPreprocessor]
|
103 | };
|