From 5cd07a4a9b5695e11543d00d2249fafb526295a0 Mon Sep 17 00:00:00 2001 From: sttk Date: Wed, 4 Apr 2018 08:23:09 +0900 Subject: [PATCH] Change timing of requiring loader of config to fix the issue #90 --- index.js | 15 +---- lib/prepare_config.js | 19 ++++++ test/fixtures/respawn_and_require.js | 21 ++++++ test/index.js | 97 ++++++++++++++++++++-------- 4 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 lib/prepare_config.js create mode 100644 test/fixtures/respawn_and_require.js diff --git a/index.js b/index.js index 7415dcf..480765c 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ const silentRequire = require('./lib/silent_require'); const buildConfigName = require('./lib/build_config_name'); const registerLoader = require('./lib/register_loader'); const getNodeFlags = require('./lib/get_node_flags'); +const prepareConfig = require('./lib/prepare_config'); function Liftoff (opts) { @@ -115,19 +116,8 @@ Liftoff.prototype.buildEnvironment = function (opts) { } } - // load any modules which were requested to be required - if (preload.length) { - // unique results first - preload.filter(function (value, index, self) { - return self.indexOf(value) === index; - }).forEach(function (dep) { - this.requireLocal(dep, findCwd(opts)); - }, this); - } - var exts = this.extensions; var eventEmitter = this; - registerLoader(eventEmitter, exts, configPath, cwd); var configFiles = {}; if (isPlainObject(this.configFiles)) { @@ -200,12 +190,11 @@ Liftoff.prototype.launch = function (opts, fn) { this.emit('respawn', execArgv, child); } if (ready) { + prepareConfig.call(this, env, opts); fn.call(this, env, argv); } } }.bind(this)); }; - - module.exports = Liftoff; diff --git a/lib/prepare_config.js b/lib/prepare_config.js new file mode 100644 index 0000000..b1f6de9 --- /dev/null +++ b/lib/prepare_config.js @@ -0,0 +1,19 @@ +const registerLoader = require('./register_loader'); +const findCwd = require('./find_cwd'); + +function prepareConfig(env, opts) { + const self = this; + + const basedir = findCwd(opts); + env.require.filter(toUnique).forEach(function(module) { + self.requireLocal(module, basedir); + }); + + registerLoader(self, self.extensions, env.configPath, env.cwd); +} + +function toUnique(elem, index, array) { + return array.indexOf(elem) === index; +} + +module.exports = prepareConfig; diff --git a/test/fixtures/respawn_and_require.js b/test/fixtures/respawn_and_require.js new file mode 100644 index 0000000..ee2a722 --- /dev/null +++ b/test/fixtures/respawn_and_require.js @@ -0,0 +1,21 @@ +const Liftoff = require('../..'); + +const Test = new Liftoff({ + name: 'test', + v8flags: ['--harmony'], +}); + +Test.on('respawn', function(flags, proc) { + console.log('saw respawn', flags); +}); + +Test.on('require', function(name) { + console.log('require', name); +}); + +Test.launch({ + require: 'coffeescript/register', + forcedFlags: ['--lazy'], +}, function(env, argv) { + console.log('execute'); +}); diff --git a/test/index.js b/test/index.js index 687a700..3d974d5 100644 --- a/test/index.js +++ b/test/index.js @@ -33,33 +33,6 @@ describe('Liftoff', function () { describe('buildEnvironment', function () { - it('should attempt pre-loading local modules if they are requested', function () { - app.on('require', function (moduleName, module) { - expect(moduleName).to.equal('coffee-script/register'); - expect(module).to.equal(require('coffee-script/register')); - }); - var env = app.buildEnvironment({require:['coffee-script/register']}); - expect(env.require).to.deep.equal(['coffee-script/register']); - }); - - it('should attempt pre-loading a local module if it is requested', function () { - app.on('require', function (moduleName, module) { - expect(moduleName).to.equal('coffee-script/register'); - expect(module).to.equal(require('coffee-script/register')); - }); - var env = app.buildEnvironment({require: 'coffee-script/register'}); - expect(env.require).to.deep.equal(['coffee-script/register']); - }); - - it('should attempt pre-loading local modules based on extension option', function () { - app.on('require', function (moduleName, module) { - expect(moduleName).to.equal('coffee-script/register'); - expect(module).to.equal(require('coffee-script/register')); - }); - var env = app.buildEnvironment({ - configPath: 'test/fixtures/coffee/mochafile.coffee' - }); - }); it('should locate local module using cwd if no config is found', function () { var test = new Liftoff({name:'chai'}); @@ -310,6 +283,76 @@ describe('Liftoff', function () { describe('requireLocal', function () { + it('should attempt pre-loading local modules if they are requested', function (done) { + var app = new Liftoff({ name: 'test' }); + var logs = []; + app.on('require', function (moduleName, module) { + expect(moduleName).to.equal('coffeescript/register'); + expect(module).to.equal(require('coffeescript/register')); + logs.push('require'); + }); + app.on('requireFail', function(moduleName, err) { + fail(err); + }); + app.launch({ require: ['coffeescript/register'] }, function(env) { + expect(env.require).to.deep.equal(['coffeescript/register']); + expect(logs).to.deep.equal(['require']); + done(); + }); + }); + + it('should attempt pre-loading a local module if it is requested', function () { + var app = new Liftoff({ name: 'test' }); + var logs = []; + app.on('require', function (moduleName, module) { + expect(moduleName).to.equal('coffeescript/register'); + expect(module).to.equal(require('coffeescript/register')); + logs.push('require'); + }); + app.on('requireFail', function(moduleName, err) { + fail(err); + }); + app.launch({ require: 'coffeescript/register' }, function(env) { + expect(env.require).to.deep.equal(['coffeescript/register']); + expect(logs).to.deep.equal(['require']); + done(); + }); + }); + + it('should attempt pre-loading local modules but fail', function() { + var app = new Liftoff({ name: 'test' }); + var logs = []; + app.on('require', function (moduleName, module) { + fail(); + }); + app.on('requireFail', function(moduleName, err) { + expect(moduleName).to.equal('badmodule'); + expect(err).to.not.equal(null); + logs.push('requireFail'); + }); + app.launch({ require: 'badmodule' }, function(env) { + expect(env.require).to.deep.equal(['coffeescript/register']); + expect(logs).to.deep.equal(['requireFail']); + done(); + }); + }); + + it('should pre-load a local module only once even if be respawned', function (done) { + const fixturesDir = path.resolve(__dirname, 'fixtures'); + + exec('cd ' + fixturesDir + ' && node respawn_and_require.js', cb); + function cb(err, stdout, stderr) { + expect(err).to.equal(null); + expect(stderr).to.equal(''); + expect(stdout).to.equal( + 'saw respawn [ \'--lazy\' ]\n' + + 'require coffeescript/register\n' + + 'execute\n' + + ''); + done(); + } + }); + it('should emit `require` with the name of the module and the required module', function (done) { var requireTest = new Liftoff({name:'require'}); requireTest.on('require', function (name, module) {