diff --git a/filenames.gypi b/filenames.gypi index 31d4fddd65..3b557fcb88 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -76,6 +76,7 @@ 'lib/renderer/extensions/i18n.js', 'lib/renderer/extensions/storage.js', 'lib/renderer/extensions/web-navigation.js', + 'lib/worker/init.js', ], 'js2c_sources': [ 'lib/common/asar.js', diff --git a/lib/renderer/init.js b/lib/renderer/init.js index 892be116d9..38441c9ec1 100644 --- a/lib/renderer/init.js +++ b/lib/renderer/init.js @@ -6,7 +6,7 @@ const Module = require('module') const resolvePromise = Promise.resolve.bind(Promise) // We modified the original process.argv to let node.js load the -// atom-renderer.js, we need to restore it here. +// init.js, we need to restore it here. process.argv.splice(1, 1) // Clear search paths. diff --git a/lib/worker/init.js b/lib/worker/init.js new file mode 100644 index 0000000000..6a38ce85dc --- /dev/null +++ b/lib/worker/init.js @@ -0,0 +1,37 @@ +'use strict' + +const path = require('path') +const Module = require('module') + +// We modified the original process.argv to let node.js load the +// init.js, we need to restore it here. +process.argv.splice(1, 1) + +// Clear search paths. +require('../common/reset-search-paths') + +// Import common settings. +require('../common/init') + +// Expose public APIs. +Module.globalPaths.push(path.join(__dirname, 'api', 'exports')) + +// Export node bindings to global. +global.require = require +global.module = module + +// Set the __filename to the path of html file if it is file: protocol. +if (self.location.protocol === 'file:') { + let pathname = process.platform === 'win32' && self.location.pathname[0] === '/' ? self.location.pathname.substr(1) : self.location.pathname + global.__filename = path.normalize(decodeURIComponent(pathname)) + global.__dirname = path.dirname(global.__filename) + + // Set module's filename so relative require can work as expected. + module.filename = global.__filename + + // Also search for module under the html file. + module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname)) +} else { + global.__filename = __filename + global.__dirname = __dirname +}