diff --git a/README.md b/README.md index 4ea4502..5e4d751 100644 --- a/README.md +++ b/README.md @@ -594,6 +594,10 @@ All `extends` properties will be traversed and become the basis for the resultin Users can override the `configPath` via their config files by specifying a field with the same name as the primary `configName`. For example, the `hackerfile` property in a `configFile` will resolve the `configPath` and `configBase` against the path. +### `preload` + +If specified as a string or array of strings, they will be added to the list of preloads in the environment. + ## Examples Check out how [gulp][gulp-cli-index] uses Liftoff. diff --git a/index.js b/index.js index 4a0dfa6..cb5a903 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,10 @@ var buildConfigName = require('./lib/build_config_name'); var registerLoader = require('./lib/register_loader'); var getNodeFlags = require('./lib/get_node_flags'); +function isString(val) { + return typeof val === 'string'; +} + function Liftoff(opts) { EE.call(this); extend(this, parseOptions(opts)); @@ -119,7 +123,7 @@ Liftoff.prototype.buildEnvironment = function (opts) { // resolve something like `{ gulpfile: "./abc.xyz" }` to the absolute path // based on the path of the configFile if (Object.prototype.hasOwnProperty.call(configFile, configName)) { - if (typeof configFile[configName] === 'string') { + if (isString(configFile[configName])) { configFile[configName] = path.resolve(path.dirname(configFilePath), configFile[configName]); } } @@ -160,12 +164,26 @@ Liftoff.prototype.buildEnvironment = function (opts) { var configPathOverride = arrayFind(Object.keys(config), function (key) { var cfg = config[key]; if (Object.prototype.hasOwnProperty.call(cfg, configName)) { - if (typeof cfg[configName] === "string") { + if (isString(cfg[configName])) { return cfg[configName]; } } }); + var additionPreloads = arrayFind(Object.keys(config), function (key) { + var cfg = config[key]; + if (Object.prototype.hasOwnProperty.call(cfg, 'preload')) { + if (Array.isArray(cfg.preload)) { + if (cfg.preload.every(isString)) { + return cfg.preload; + } + } + if (isString(cfg.preload)) { + return cfg.preload; + } + } + }); + // if cwd was provided explicitly, only use it for searching config if (opts.cwd) { searchPaths = [cwd]; @@ -233,7 +251,7 @@ Liftoff.prototype.buildEnvironment = function (opts) { return { cwd: cwd, - preload: preload, + preload: preload.concat(additionPreloads || []), completion: opts.completion, configNameSearch: configNameSearch, configPath: configPath, diff --git a/test/fixtures/configfiles/preload-array.js b/test/fixtures/configfiles/preload-array.js new file mode 100644 index 0000000..7c87193 --- /dev/null +++ b/test/fixtures/configfiles/preload-array.js @@ -0,0 +1,3 @@ +module.exports = { + preload: ['abc', 'xyz'] +}; diff --git a/test/fixtures/configfiles/preload-invalid-array.js b/test/fixtures/configfiles/preload-invalid-array.js new file mode 100644 index 0000000..aa590cb --- /dev/null +++ b/test/fixtures/configfiles/preload-invalid-array.js @@ -0,0 +1,3 @@ +module.exports = { + preload: [{}, 123, 'no'] +}; diff --git a/test/fixtures/configfiles/preload-invalid.js b/test/fixtures/configfiles/preload-invalid.js new file mode 100644 index 0000000..b8cfc7e --- /dev/null +++ b/test/fixtures/configfiles/preload-invalid.js @@ -0,0 +1,3 @@ +module.exports = { + preload: {} +}; diff --git a/test/fixtures/configfiles/preload-string.js b/test/fixtures/configfiles/preload-string.js new file mode 100644 index 0000000..9b20bdf --- /dev/null +++ b/test/fixtures/configfiles/preload-string.js @@ -0,0 +1,3 @@ +module.exports = { + preload: 'abc' +}; diff --git a/test/index.js b/test/index.js index 07726d1..cf0ec76 100644 --- a/test/index.js +++ b/test/index.js @@ -616,6 +616,100 @@ describe('Liftoff', function () { }); }); + it('adds array of preloads specified in config', function (done) { + var app = new Liftoff({ + name: 'myapp', + configFiles: { + 'preload-array': [ + { path: 'test/fixtures/configfiles', extensions: ['.js'] } + ], + }, + }); + app.prepare({}, function (env) { + expect(env.preload).toEqual(['abc', 'xyz']); + done(); + }); + }); + + it('combines array of preloads specified in config', function (done) { + var app = new Liftoff({ + name: 'myapp', + configFiles: { + 'preload-array': [ + { path: 'test/fixtures/configfiles', extensions: ['.js'] } + ], + }, + }); + app.prepare({ + preload: ['123'] + }, function (env) { + expect(env.preload).toEqual(['123', 'abc', 'xyz']); + done(); + }); + }); + + it('adds string preload specified in config', function (done) { + var app = new Liftoff({ + name: 'myapp', + configFiles: { + 'preload-string': [ + { path: 'test/fixtures/configfiles', extensions: ['.js'] } + ], + }, + }); + app.prepare({}, function (env) { + expect(env.preload).toEqual(['abc']); + done(); + }); + }); + + it('combines string preload specified in config', function (done) { + var app = new Liftoff({ + name: 'myapp', + configFiles: { + 'preload-string': [ + { path: 'test/fixtures/configfiles', extensions: ['.js'] } + ], + }, + }); + app.prepare({ + preload: ['xyz'] + }, function (env) { + expect(env.preload).toEqual(['xyz', 'abc']); + done(); + }); + }); + + it('ignores non-string/non-array preload specified in config', function (done) { + var app = new Liftoff({ + name: 'myapp', + configFiles: { + 'preload-invalid': [ + { path: 'test/fixtures/configfiles', extensions: ['.js'] } + ], + }, + }); + app.prepare({}, function (env) { + expect(env.preload).toEqual([]); + done(); + }); + }); + + it('ignores array with any non-strings preload specified in config', function (done) { + var app = new Liftoff({ + name: 'myapp', + configFiles: { + 'preload-invalid-array': [ + { path: 'test/fixtures/configfiles', extensions: ['.js'] } + ], + }, + }); + app.prepare({}, function (env) { + expect(env.preload).toEqual([]); + done(); + }); + }); + it('should use dirname of configPath if no cwd is specified', function (done) { var app = new Liftoff({ name: 'myapp',