Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/bootstrap/nodejs.js
2868 views
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
/**
16
* @fileoverview A nodejs script for dynamically requiring Closure within
17
* nodejs.
18
*
19
* Example of usage:
20
* <code>
21
* require('./bootstrap/nodejs')
22
* goog.require('goog.ui.Component')
23
* </code>
24
*
25
* This loads goog.ui.Component in the global scope.
26
*
27
* If you want to load custom libraries, you can require the custom deps file
28
* directly. If your custom libraries introduce new globals, you may
29
* need to run goog.nodeGlobalRequire to get them to load correctly.
30
*
31
* <code>
32
* require('./path/to/my/deps.js')
33
* goog.bootstrap.nodeJs.nodeGlobalRequire('./path/to/my/base.js')
34
* goog.require('my.Class')
35
* </code>
36
*
37
* @author [email protected] (Nick Santos)
38
*
39
* @nocompile
40
*/
41
42
43
var fs = require('fs');
44
var path = require('path');
45
var vm = require('vm');
46
47
48
/**
49
* The goog namespace in the global scope.
50
*/
51
global.goog = {};
52
53
54
/**
55
* Imports a script using Node's require() API.
56
*
57
* @param {string} src The script source.
58
* @param {string=} opt_sourceText The optional source text to evaluate.
59
* @return {boolean} True if the script was imported, false otherwise.
60
*/
61
global.CLOSURE_IMPORT_SCRIPT = function(src, opt_sourceText) {
62
// Sources are always expressed relative to closure's base.js, but
63
// require() is always relative to the current source.
64
if (opt_sourceText === undefined) {
65
require('./../' + src);
66
} else {
67
eval(opt_sourceText);
68
}
69
return true;
70
};
71
72
73
/**
74
* Loads a file when using Closure's goog.require() API with goog.modules.
75
*
76
* @param {string} src The file source.
77
* @return {string} The file contents.
78
*/
79
global.CLOSURE_LOAD_FILE_SYNC = function(src) {
80
return fs.readFileSync(
81
path.resolve(__dirname, '..', src), {encoding: 'utf-8'});
82
};
83
84
85
// Declared here so it can be used to require base.js
86
function nodeGlobalRequire(file) {
87
vm.runInThisContext.call(global, fs.readFileSync(file), file);
88
}
89
90
91
// Load Closure's base.js into memory. It is assumed base.js is in the
92
// directory above this directory given this script's location in
93
// bootstrap/nodejs.js.
94
nodeGlobalRequire(path.resolve(__dirname, '..', 'base.js'));
95
96
97
/**
98
* Bootstraps a file into the global scope.
99
*
100
* This is strictly for cases where normal require() won't work,
101
* because the file declares global symbols with 'var' that need to
102
* be added to the global scope.
103
* @suppress {missingProvide}
104
*
105
* @param {string} file The path to the file.
106
*/
107
goog.nodeGlobalRequire = nodeGlobalRequire;
108
109