UNPKG

2.12 kBJavaScriptView Raw
1/*jslint nomen:true, sloppy: true, stupid: true, node: true*/
2
3'use strict';
4
5var fs = require('fs'),
6 path = require('path'),
7 vm = require('vm'),
8 code = {
9 min: fs.readFileSync(path.join(__dirname, '..',
10 'node_modules', 'yui', 'yui-nodejs', 'yui-nodejs-min.js'), 'utf8'),
11 raw: fs.readFileSync(path.join(__dirname, '..',
12 'node_modules', 'yui', 'yui-nodejs', 'yui-nodejs.js'), 'utf8'),
13 debug: fs.readFileSync(path.join(__dirname, '..',
14 'node_modules', 'yui', 'yui-nodejs', 'yui-nodejs-debug.js'), 'utf8')
15 };
16
17/*
18 This is a hack to get an isolated YUI object. This is EXPERIMENTAL,
19 and eventually we want to have that capability as part of YUI.
20
21 Note: since the new YUI will run in the context of the mojito NPM
22 package, we need to replicate all YUI dependencies as part of
23 mojito package.json, this hard-dependency can be removed once
24 YUI provides a way to create a sandbox.
25
26 How to use this?
27
28 var YUI = require('yui-sandbox.js').getYUI();
29 YUI().use('foo');
30
31*/
32
33exports.getYUI = function (filter) {
34 var sandbox = {
35 console: console,
36 process: process,
37 require: require,
38 module: module,
39 setTimeout: setTimeout,
40 setInterval: setInterval,
41 clearTimeout: clearTimeout,
42 clearInterval: clearInterval,
43 JSON: JSON,
44 __filename: __filename,
45 __dirname: path.join(__dirname, '..', 'node_modules', 'yui', 'yui-nodejs'),
46 exports: {}
47 },
48 globalFunction = Function;
49
50 filter = (filter && code.hasOwnProperty(filter)) ? filter : 'raw';
51 vm.runInNewContext(code[filter], sandbox, 'build/yui-new/yui-new.js');
52 // This is a trick to solve the issue with vm, which is messing with `Function`
53 // global variable, which means Y.config.global will not be populated correctly,
54 // so we do it manually after getting YUI reference.
55 sandbox.exports.YUI.applyConfig({
56 global: globalFunction('return this')()
57 });
58 return sandbox.exports.YUI;
59};
\No newline at end of file