nodejs.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2013 The Closure Library Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A nodejs script for dynamically requiring Closure within
  16. * nodejs.
  17. *
  18. * Example of usage:
  19. * <code>
  20. * require('./bootstrap/nodejs')
  21. * goog.require('goog.ui.Component')
  22. * </code>
  23. *
  24. * This loads goog.ui.Component in the global scope.
  25. *
  26. * If you want to load custom libraries, you can require the custom deps file
  27. * directly. If your custom libraries introduce new globals, you may
  28. * need to run goog.nodeGlobalRequire to get them to load correctly.
  29. *
  30. * <code>
  31. * require('./path/to/my/deps.js')
  32. * goog.bootstrap.nodeJs.nodeGlobalRequire('./path/to/my/base.js')
  33. * goog.require('my.Class')
  34. * </code>
  35. *
  36. * @author nick@medium.com (Nick Santos)
  37. *
  38. * @nocompile
  39. */
  40. var fs = require('fs');
  41. var path = require('path');
  42. var vm = require('vm');
  43. /**
  44. * The goog namespace in the global scope.
  45. */
  46. global.goog = {};
  47. /**
  48. * Imports a script using Node's require() API.
  49. *
  50. * @param {string} src The script source.
  51. * @param {string=} opt_sourceText The optional source text to evaluate.
  52. * @return {boolean} True if the script was imported, false otherwise.
  53. */
  54. global.CLOSURE_IMPORT_SCRIPT = function(src, opt_sourceText) {
  55. // Sources are always expressed relative to closure's base.js, but
  56. // require() is always relative to the current source.
  57. if (opt_sourceText === undefined) {
  58. require('./../' + src);
  59. } else {
  60. eval(opt_sourceText);
  61. }
  62. return true;
  63. };
  64. /**
  65. * Loads a file when using Closure's goog.require() API with goog.modules.
  66. *
  67. * @param {string} src The file source.
  68. * @return {string} The file contents.
  69. */
  70. global.CLOSURE_LOAD_FILE_SYNC = function(src) {
  71. return fs.readFileSync(
  72. path.resolve(__dirname, '..', src), {encoding: 'utf-8'});
  73. };
  74. // Declared here so it can be used to require base.js
  75. function nodeGlobalRequire(file) {
  76. vm.runInThisContext.call(global, fs.readFileSync(file), file);
  77. }
  78. // Load Closure's base.js into memory. It is assumed base.js is in the
  79. // directory above this directory given this script's location in
  80. // bootstrap/nodejs.js.
  81. nodeGlobalRequire(path.resolve(__dirname, '..', 'base.js'));
  82. /**
  83. * Bootstraps a file into the global scope.
  84. *
  85. * This is strictly for cases where normal require() won't work,
  86. * because the file declares global symbols with 'var' that need to
  87. * be added to the global scope.
  88. * @suppress {missingProvide}
  89. *
  90. * @param {string} file The path to the file.
  91. */
  92. goog.nodeGlobalRequire = nodeGlobalRequire;