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

Have Brocfile.js export a function #386

Merged
merged 2 commits into from
Feb 16, 2019
Merged
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm install --global broccoli-cli
## Brocfile.js

A `Brocfile.js` file in the project root contains the build specification. It
should export a tree.
should export a function that returns a tree.

A tree can be any string representing a directory path, like `'app'` or
`'src'`. Or a tree can be an object conforming to the [Plugin API
Expand All @@ -35,7 +35,7 @@ The following simple `Brocfile.js` would export the `app/` subdirectory as a
tree:

```js
module.exports = 'app'
module.exports = () => 'app';
```

With that Brocfile, the build result would equal the contents of the `app`
Expand All @@ -62,11 +62,13 @@ the following folder within your project folder:
The following `Brocfile.js` exports the `app/` subdirectory as `appkit/`:

```js
var Funnel = require('broccoli-funnel')
const Funnel = require('broccoli-funnel')

module.exports = new Funnel('app', {
module.exports = () => {
return new Funnel('app', {
destDir: 'appkit'
})
})
}
```

That example uses the plugin
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = function broccoliCLI(args) {

function getBuilder(options) {
const brocfile = broccoli.loadBrocfile(options);
return new broccoli.Builder(brocfile);
return new broccoli.Builder(brocfile());
}

function getWatcher(options) {
Expand Down
20 changes: 14 additions & 6 deletions lib/load_brocfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ module.exports = function loadBrocfile(options) {
options = {};
}

let brocfile;
let brocfilePath;

if (options.brocfilePath) {
brocfile = path.resolve(options.brocfilePath);
brocfilePath = path.resolve(options.brocfilePath);
} else {
brocfile = findup('Brocfile.js', {
brocfilePath = findup('Brocfile.js', {
nocase: true,
});
}

if (!brocfile) {
if (!brocfilePath) {
throw new Error('Brocfile.js not found');
}

const baseDir = options.cwd || path.dirname(brocfile);
const baseDir = options.cwd || path.dirname(brocfilePath);

// The chdir should perhaps live somewhere else and not be a side effect of
// this function, or go away entirely
process.chdir(baseDir);

return require(brocfile);
// Load brocfile
const brocfile = require(brocfilePath);

if (typeof brocfile === 'function') {
return brocfile;
}

// Wrap brocfile result in a function for backwards compatibility
return () => brocfile;
};
1 change: 1 addition & 0 deletions test/fixtures/project/Brocfile-Function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'subdir';
17 changes: 14 additions & 3 deletions test/load_brocfile_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai');

const projectPath = 'test/fixtures/project';
const brocfileFixture = require('../' + projectPath + '/Brocfile.js');
const brocfileFunctionFixture = require('../' + projectPath + '/Brocfile-Function.js');

describe('loadBrocfile', function() {
let oldCwd = null;
Expand Down Expand Up @@ -33,7 +34,9 @@ describe('loadBrocfile', function() {
});

it('return tree definition', function() {
chai.expect(loadBrocfile()).to.equal(brocfileFixture);
const brocfile = loadBrocfile();
chai.expect(brocfile).to.be.a('function');
chai.expect(brocfile()).to.equal(brocfileFixture);
});
});

Expand All @@ -43,14 +46,14 @@ describe('loadBrocfile', function() {
});

it('return tree definition', function() {
chai.expect(loadBrocfile()).to.equal(brocfileFixture);
chai.expect(loadBrocfile()()).to.equal(brocfileFixture);
});
});

context('with path', function() {
it('return tree definition', function() {
chai
.expect(loadBrocfile({ brocfilePath: projectPath + '/Brocfile.js' }))
.expect(loadBrocfile({ brocfilePath: projectPath + '/Brocfile.js' })())
.to.equal(brocfileFixture);
});

Expand All @@ -59,4 +62,12 @@ describe('loadBrocfile', function() {
chai.expect(() => loadBrocfile({ brocfilePath })).to.throw(Error, /missing-brocfile.js/);
});
});

context('with Brocfile that returns a function', function() {
it('does not wrap the function', function() {
const brocfile = loadBrocfile({ brocfilePath: projectPath + '/Brocfile-Function.js' });
chai.expect(brocfile).to.equal(brocfileFunctionFixture);
chai.expect(brocfile()).to.equal(brocfileFixture);
});
});
});