init.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict'
  2. const path = require('path')
  3. const Module = require('module')
  4. // We modified the original process.argv to let node.js load the
  5. // init.js, we need to restore it here.
  6. process.argv.splice(1, 1)
  7. // Clear search paths.
  8. require('../common/reset-search-paths')
  9. // Import common settings.
  10. require('../common/init')
  11. // Expose public APIs.
  12. Module.globalPaths.push(path.join(__dirname, 'api', 'exports'))
  13. // Export node bindings to global.
  14. global.require = require
  15. global.module = module
  16. // Set the __filename to the path of html file if it is file: protocol.
  17. if (self.location.protocol === 'file:') {
  18. let pathname = process.platform === 'win32' && self.location.pathname[0] === '/' ? self.location.pathname.substr(1) : self.location.pathname
  19. global.__filename = path.normalize(decodeURIComponent(pathname))
  20. global.__dirname = path.dirname(global.__filename)
  21. // Set module's filename so relative require can work as expected.
  22. module.filename = global.__filename
  23. // Also search for module under the html file.
  24. module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname))
  25. } else {
  26. global.__filename = __filename
  27. global.__dirname = __dirname
  28. }