diff --git a/blueprints/node-webkit/files/vendor/node-webkit/shim.js b/blueprints/node-webkit/files/vendor/node-webkit/shim.js deleted file mode 100644 index da18572..0000000 --- a/blueprints/node-webkit/files/vendor/node-webkit/shim.js +++ /dev/null @@ -1,36 +0,0 @@ -/* jshint browser: true */ -(function(window) { - if (!window.nwDispatcher) { return; } - - // Move `global.window` out of the way as it causes some third-party - // libraries to expose themselves via `global` instead of `window`. - var globalWindow = global.window; - delete global.window; - - // Rename node's `require` to avoid conflicts with AMD's `require`. - var requireNode = window.requireNode = window.require; - delete window.require; - - window.addEventListener('load', function() { - // Restore `global`. - global.window = globalWindow; - - // Once the document is loaded, make node's `require` function - // available again. This is required for certain node modules that - // can't work with the alias, and for nw.js's GUI to function properly - // (e.g. opening the dev tools from the window toolbar). - var requireAMD = window.require; - - if (requireAMD) { - window.require = function() { - try { - return requireAMD.apply(null, arguments); - } catch (e) { - return requireNode.apply(null, arguments); - } - }; - } else { - window.require = requireNode; - } - }); -})(this); diff --git a/blueprints/node-webkit/files/vendor/node-webkit/reload.js b/client/node-webkit/reload.js similarity index 100% rename from blueprints/node-webkit/files/vendor/node-webkit/reload.js rename to client/node-webkit/reload.js diff --git a/index.js b/index.js index 252872e..7ad2f97 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,22 @@ 'use strict'; +var fs = require('fs'); var path = require('path'); +function injectScript(scriptName) { + var filePath = path.join(__dirname, 'lib', 'resources', scriptName); + return ''; +} + module.exports = { name: 'ember-node-webkit', included: function(app) { this._super.included(app); - app.import('vendor/node-webkit/shim.js', { prepend: true }); - app.import({ development: 'vendor/node-webkit/reload.js' }); + if (!process.env.EMBER_CLI_NW) { return; } + app.import({ development: 'vendor/node-webkit/reload.js' }); if (process.env.NW_TESTS_DEV) { app.import({ test: 'vendor/node-webkit/browser-qunit-adapter.js' }); } else { @@ -31,11 +37,15 @@ module.exports = { }, postprocessTree: function(type, tree) { - var funnel = require('broccoli-funnel'); - var mergeTrees = require('broccoli-merge-trees'); - var replace = require('broccoli-string-replace'); + if (!process.env.EMBER_CLI_NW) { + return tree; + } + + if (type === 'all' && process.env.EMBER_ENV === 'test') { + var funnel = require('broccoli-funnel'); + var mergeTrees = require('broccoli-merge-trees'); + var replace = require('broccoli-string-replace'); - if (type === 'all' && process.env.EMBER_ENV === 'test' && process.env.NW_TEST) { // Update the base URL in `tests/index.html` var index = replace(tree, { files: ['tests/index.html'], @@ -56,16 +66,13 @@ module.exports = { if (testPageOptions) { testPkg = replace(testPkg, { files: ['tests/package.json'], - patterns: [ - { - match: /"main":\s*"(index.html\?[^\"]+)"/, - replacement: '"main": "$1&' + testPageOptions + '"' - }, - { - match: /"main":\s*"index.html"/, - replacement: '"main": "index.html?' + testPageOptions + '"' - } - ] + patterns: [{ + match: /"main":\s*"(index.html\?[^\"]+)"/, + replacement: '"main": "$1&' + testPageOptions + '"' + }, { + match: /"main":\s*"index.html"/, + replacement: '"main": "index.html?' + testPageOptions + '"' + }] }); } @@ -76,7 +83,17 @@ module.exports = { }, contentFor: function(type) { - if (type === 'test-body' && process.env.EMBER_ENV === 'test' && process.env.NW_TEST) { + if (!process.env.EMBER_CLI_NW) { return; } + + if (type === 'head') { + return injectScript('shim-head.js'); + } + + if (type === 'body-footer') { + return injectScript('shim-footer.js'); + } + + if (type === 'test-body' && process.env.EMBER_ENV === 'test') { var testemServer = process.env.NW_TESTEM_SERVER_URL; if (testemServer) { return ''; diff --git a/lib/commands/nw-package.js b/lib/commands/nw-package.js index 17d3e03..3725188 100644 --- a/lib/commands/nw-package.js +++ b/lib/commands/nw-package.js @@ -15,6 +15,10 @@ module.exports = { { name: 'config-file', type: String, default: 'config/nw-package.js', aliases: ['f'] } ], + init: function() { + process.env.EMBER_CLI_NW = true; + }, + buildApp: function(options) { var buildTask = new this.tasks.Build({ ui: this.ui, diff --git a/lib/commands/nw-test/index.js b/lib/commands/nw-test/index.js index 9ec1af1..25442ea 100644 --- a/lib/commands/nw-test/index.js +++ b/lib/commands/nw-test/index.js @@ -27,6 +27,8 @@ module.exports = { if (!this.testing) { process.env.EMBER_CLI_TEST_COMMAND = true; } + + process.env.EMBER_CLI_NW = true; }, tmp: function() { diff --git a/lib/commands/nw.js b/lib/commands/nw.js index 365d1db..f134e3e 100644 --- a/lib/commands/nw.js +++ b/lib/commands/nw.js @@ -10,6 +10,10 @@ module.exports = { name: 'nw', description: 'Builds your app and launches NW.js', + init: function() { + process.env.EMBER_CLI_NW = true; + }, + availableOptions: [ { name: 'environment', type: String, default: 'development', aliases: ['e', { 'dev': 'development' }, { 'prod': 'production' }] }, { name: 'output-path', type: String, default: 'dist/', aliases: ['o'] } diff --git a/lib/resources/shim-footer.js b/lib/resources/shim-footer.js new file mode 100644 index 0000000..9ec7ab7 --- /dev/null +++ b/lib/resources/shim-footer.js @@ -0,0 +1,25 @@ +/* jshint browser: true */ +(function(window) { + if (!window.nwDispatcher) { return; } + + // Restore NW.js variables. + global.window = window; + window.process = global.process; + + // Redefine a global `require` function that can satisfy both + // Node and AMD module systems. + var requireAMD = window.require; + var requireNode = window.requireNode; + + if (requireAMD) { + window.require = function() { + try { + return requireAMD.apply(null, arguments); + } catch (e) { + return requireNode.apply(null, arguments); + } + }; + } else { + window.require = requireNode; + } +})(this); diff --git a/lib/resources/shim-head.js b/lib/resources/shim-head.js new file mode 100644 index 0000000..c9e3fe6 --- /dev/null +++ b/lib/resources/shim-head.js @@ -0,0 +1,13 @@ +/* jshint browser: true */ +(function(window) { + if (!window.nwDispatcher) { return; } + + // Delete some variables that mislead third-party libraries into thinking + // that the running environment is Node. + delete global.window; // can be accessed via `window` + delete window.process; // can be accessed via `global.process` + + // Rename node's `require` to avoid conflicts with AMD's `require`. + window.requireNode = window.require; + delete window.require; +})(this);