UNPKG

2.81 kBJavaScriptView Raw
1(function () {
2 // Set up local filename, dirname, and `module.filename`
3 // DEV: `__filename` would be a path to Electron since we are loading an HTTP URL
4 // /home/todd/github/karma-electron/node_modules/electron/dist/resources/electron.asar/renderer/init.js
5 // This overrides to user's expectations of Karma's `file://` filepaths
6 // DEV: Ignore JSHint so we can override `__filename` in our closure
7 // DEV: We use `window.require` sniffing to prevent emulating a Node.js environment in a non-Node.js one
8 var __filename, __dirname;
9 if (window.require) {
10 // If we have a `__filename` override, then use it
11 // DEV: To work around JSCS errors, we are using `if_`, `else_`, and `end_`
12 // jscs:disable
13 // jshint ignore:start
14 {{if___filenameOverride}}
15 __filename = '{{!__filenameOverride}}';
16 {{end___filenameOverride}}
17 // Otherwise, use `context.html` as our `__filename`
18 {{notif___filenameOverride}}
19 __filename = window.location.pathname !== '/debug.html' ? '{{!karmaContextFile}}' : '{{!karmaDebugFile}}';
20 {{end___filenameOverride}}
21 // jshint ignore:end
22 // jscs:enable
23 __dirname = window.require('path').dirname(__filename);
24 if (window.module) {
25 module.filename = __filename;
26 }
27 }
28
29 // Save original require/require.resolve
30 if (window.require) {
31 var __require = window.require;
32 var __requireResolve = __require.resolve;
33
34 // Add our base directory as the path to find node modules from
35 if (!window.__karmaBasePathAddedToModule) {
36 module.paths = module.paths.concat(__require('module')._nodeModulePaths('{{!karmaBasePath}}'));
37 window.__karmaBasePathAddedToModule = true;
38 }
39
40 // Define our patched require/require.resolve
41 var __requireFilepath = function (filepath) {
42 // If the filepath is relative, prepend the filename
43 // e.g. `./submodule` -> `/home/todd/.../integration-test/./submodule`
44 if (filepath && filepath[0] === '.') {
45 filepath = __dirname + '{{!sep}}' + filepath;
46 }
47
48 // Return our filepath
49 return filepath;
50 };
51 // Define our require/resolve.resolve
52 window.require = function (filepath) {
53 return __require(__requireFilepath(filepath));
54 };
55 window.require.resolve = function (filepath) {
56 return __requireResolve(__requireFilepath(filepath));
57 };
58 }
59
60 // If we want to require our content, then load it via `require`
61 // DEV: To work around JSCS errors, we are using `if_`, `else_`, and `end_`
62 // jscs:disable
63 // jshint ignore:start
64 {{if_loadScriptsViaRequire}}
65 require('{{!filename}}');
66 {{end_loadScriptsViaRequire}}
67 // Otherwise, inject our content
68 {{notif_loadScriptsViaRequire}}
69 __CONTENT__
70 {{end_loadScriptsViaRequire}}
71 // jshint ignore:end
72 // jscs:enable
73}());