From ab984c4ad35efaad3d0a3f9bcf36ed72602b33a6 Mon Sep 17 00:00:00 2001 From: Caridy Patino Date: Wed, 30 Oct 2013 14:59:49 -0400 Subject: [PATCH 1/4] Preserving [global] as [this] when executing a yui module in nodejs. * when executing a module that has some code other than of the `YUI.add()`, that code is suppose to run in a closure where `this` is equal to nodejs' `global` variable, which is the equivalent to `window` on the browser. That way we maintain feature parity for polyfills and other vendor scripts. --- src/get/js/get-nodejs.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/get/js/get-nodejs.js b/src/get/js/get-nodejs.js index 5ab1a07a5e6..d04a878dd2e 100644 --- a/src/get/js/get-nodejs.js +++ b/src/get/js/get-nodejs.js @@ -63,7 +63,9 @@ if (typeof YUI._getLoadHook === 'function') { data = YUI._getLoadHook(data, url); } - mod._compile('module.exports = function (YUI) {' + data + '\n;return YUI;};', url); + mod._compile('module.exports = function (YUI) {' + + 'return (function () {'+ data + '\n;return YUI;}).apply(global, []);' + + '};', url); /*global YUI:true */ YUI = mod.exports(YUI); From 3d80c4753e746779c46223fed7ef465aaa1448a7 Mon Sep 17 00:00:00 2001 From: Caridy Patino Date: Tue, 12 Nov 2013 15:46:34 -0500 Subject: [PATCH 2/4] review feedback: removing unnecessary [] argument when calling apply() --- src/get/js/get-nodejs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/get/js/get-nodejs.js b/src/get/js/get-nodejs.js index d04a878dd2e..d3adf84a3d9 100644 --- a/src/get/js/get-nodejs.js +++ b/src/get/js/get-nodejs.js @@ -64,7 +64,7 @@ data = YUI._getLoadHook(data, url); } mod._compile('module.exports = function (YUI) {' + - 'return (function () {'+ data + '\n;return YUI;}).apply(global, []);' + + 'return (function () {'+ data + '\n;return YUI;}).apply(global);' + '};', url); /*global YUI:true */ From abb23066f0346c152a6d7e644be99c49d1d6d8f9 Mon Sep 17 00:00:00 2001 From: Caridy Patino Date: Tue, 12 Nov 2013 18:43:35 -0500 Subject: [PATCH 3/4] adding unit test for Y.get vendor script use-case --- src/get/tests/assets/vendor-signed.js | 10 +++++++ src/get/tests/cli/run.js | 35 ++++++++++++++++++++++++ src/get/tests/unit/assets/vendor-test.js | 15 ++++++++++ src/get/tests/unit/vendor.html | 31 +++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 src/get/tests/assets/vendor-signed.js create mode 100644 src/get/tests/cli/run.js create mode 100644 src/get/tests/unit/assets/vendor-test.js create mode 100644 src/get/tests/unit/vendor.html diff --git a/src/get/tests/assets/vendor-signed.js b/src/get/tests/assets/vendor-signed.js new file mode 100644 index 00000000000..a8dfde60f23 --- /dev/null +++ b/src/get/tests/assets/vendor-signed.js @@ -0,0 +1,10 @@ +// script trying to store a global variable called `foo` +(function (global) { + global.foo = 1; +}(this)); + +// signing the vendor script with a YUI module name +// and optionally, adding the value of `foo` to the namespace +YUI.add('vendor-script-signed', function (Y) { + Y.foo = Y.config.global.foo; +}, '', {}); \ No newline at end of file diff --git a/src/get/tests/cli/run.js b/src/get/tests/cli/run.js new file mode 100644 index 00000000000..97244169876 --- /dev/null +++ b/src/get/tests/cli/run.js @@ -0,0 +1,35 @@ +#!/usr/bin/env node + +process.chdir(__dirname); + +var YUITest = require('yuitest'), + path = require('path'), + fs = require('fs'), + dir = path.join(__dirname, '../../../../build-npm/'), + YUI = require(dir).YUI, + json; + +YUI({ useSync: true }).use('test', function(Y) { + Y.Test.Runner = YUITest.TestRunner; + Y.Test.Case = YUITest.TestCase; + Y.Test.Suite = YUITest.TestSuite; + Y.Assert = YUITest.Assert; + + Y.applyConfig({ + modules: { + 'get-vendor-test': { + fullpath: path.join(__dirname, '../unit/assets/vendor-test.js'), + requires: ['get', 'vendor-script-signed', 'test'] + }, + 'vendor-script-signed': { + fullpath: path.join(__dirname, '../assets/vendor-signed.js') + } + } + }); + + Y.use('get-vendor-test'); + + Y.Test.Runner.setName('get cli tests'); + Y.Test.Runner.add(Y.GetTests.vendor); + +}); diff --git a/src/get/tests/unit/assets/vendor-test.js b/src/get/tests/unit/assets/vendor-test.js new file mode 100644 index 00000000000..7df389f941d --- /dev/null +++ b/src/get/tests/unit/assets/vendor-test.js @@ -0,0 +1,15 @@ +YUI.add('get-vendor-test', function (Y) { + + Y.GetTests = new Y.Test.Suite("get vendor"); + + Y.GetTests.vendor = new Y.Test.Case({ + name: "Y.get vendor tests", + + 'test: global to work across runtimes' : function() { + Y.Assert.areEqual(1, Y.foo, 'Y.config.global.foo is not set correctly'); + } + }); + +}, '', { + requires: ['get', 'vendor-script-signed', 'test'] +}); diff --git a/src/get/tests/unit/vendor.html b/src/get/tests/unit/vendor.html new file mode 100644 index 00000000000..e39cec845ec --- /dev/null +++ b/src/get/tests/unit/vendor.html @@ -0,0 +1,31 @@ + + + + YUI Get Vendor Tests + + + +
+ + + + + + + + + From 7534825eb7d25ebb549893ff6edfb1f54253a33e Mon Sep 17 00:00:00 2001 From: Caridy Patino Date: Wed, 13 Nov 2013 12:34:27 -0500 Subject: [PATCH 4/4] review feedback: newline --- src/get/tests/assets/vendor-signed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/get/tests/assets/vendor-signed.js b/src/get/tests/assets/vendor-signed.js index a8dfde60f23..366eb3c7b15 100644 --- a/src/get/tests/assets/vendor-signed.js +++ b/src/get/tests/assets/vendor-signed.js @@ -7,4 +7,4 @@ // and optionally, adding the value of `foo` to the namespace YUI.add('vendor-script-signed', function (Y) { Y.foo = Y.config.global.foo; -}, '', {}); \ No newline at end of file +}, '', {});