Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change timing of requiring a loader for config to fix the issue #90 #93

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
19 changes: 19 additions & 0 deletions lib/prepare_config.js
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 21 additions & 0 deletions test/fixtures/respawn_and_require.js
Original file line number Diff line number Diff line change
@@ -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');
});
97 changes: 70 additions & 27 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand Down Expand Up @@ -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) {
Expand Down