Skip to content

Commit

Permalink
feat!: Populate additional preload modules with configFiles (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Mar 16, 2024
1 parent 6bcd381 commit fad21a9
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 21 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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]);
}
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/configfiles/preload-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
preload: ['abc', 'xyz']
};
3 changes: 3 additions & 0 deletions test/fixtures/configfiles/preload-invalid-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
preload: [{}, 123, 'no']
};
3 changes: 3 additions & 0 deletions test/fixtures/configfiles/preload-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
preload: {}
};
3 changes: 3 additions & 0 deletions test/fixtures/configfiles/preload-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
preload: 'abc'
};
94 changes: 94 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit fad21a9

Please sign in to comment.