Path: blob/trunk/third_party/closure/goog/bootstrap/nodejs.js
2868 views
// Copyright 2013 The Closure Library Authors.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A nodejs script for dynamically requiring Closure within16* 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 file27* directly. If your custom libraries introduce new globals, you may28* 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 [email protected] (Nick Santos)37*38* @nocompile39*/404142var fs = require('fs');43var path = require('path');44var vm = require('vm');454647/**48* The goog namespace in the global scope.49*/50global.goog = {};515253/**54* Imports a script using Node's require() API.55*56* @param {string} src The script source.57* @param {string=} opt_sourceText The optional source text to evaluate.58* @return {boolean} True if the script was imported, false otherwise.59*/60global.CLOSURE_IMPORT_SCRIPT = function(src, opt_sourceText) {61// Sources are always expressed relative to closure's base.js, but62// require() is always relative to the current source.63if (opt_sourceText === undefined) {64require('./../' + src);65} else {66eval(opt_sourceText);67}68return true;69};707172/**73* Loads a file when using Closure's goog.require() API with goog.modules.74*75* @param {string} src The file source.76* @return {string} The file contents.77*/78global.CLOSURE_LOAD_FILE_SYNC = function(src) {79return fs.readFileSync(80path.resolve(__dirname, '..', src), {encoding: 'utf-8'});81};828384// Declared here so it can be used to require base.js85function nodeGlobalRequire(file) {86vm.runInThisContext.call(global, fs.readFileSync(file), file);87}888990// Load Closure's base.js into memory. It is assumed base.js is in the91// directory above this directory given this script's location in92// bootstrap/nodejs.js.93nodeGlobalRequire(path.resolve(__dirname, '..', 'base.js'));949596/**97* Bootstraps a file into the global scope.98*99* This is strictly for cases where normal require() won't work,100* because the file declares global symbols with 'var' that need to101* be added to the global scope.102* @suppress {missingProvide}103*104* @param {string} file The path to the file.105*/106goog.nodeGlobalRequire = nodeGlobalRequire;107108109