Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

feat: add decorator option to preprocessor function #29

Closed
wants to merge 3 commits 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
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = (on) => {

## Options

Pass in options as the second argument to `browserify`:
Pass in options as the first argument to `browserify`:

```javascript
module.exports = (on) => {
Expand Down Expand Up @@ -164,6 +164,28 @@ module.exports = (on) => {
}
```

## Decoratoring the Browserify instance

Pass in a decorator function as the second argument to `browserify`:

```javascript
module.exports = (on) => {
const options = {
// options here
}

const decorator = (b) => {
// b is the instance of Browserify.
// You can call any of the [methods](https://github.com/browserify/browserify#methods)
// on b that you need in order to further customize Browserify
b.ignoreFile('module-to-ignore')
};

on('file:preprocessor', browserify(options, decorator))
}
```


## Contributing

Run all tests once:
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ const getBrowserifyOptions = (entry, userBrowserifyOptions = {}) => {
//
// on('file:preprocessor', browserify(options))
//
const preprocessor = (options = {}) => {
// the decorator function will be called with the Browserify instance
//
const preprocessor = (options = {}, decorator = undefined) => {
debug('received user options: %o', options)

// we return function that accepts the arguments provided by
Expand Down Expand Up @@ -132,6 +134,10 @@ const preprocessor = (options = {}) => {

const bundler = browserify(browserifyOptions)

if (decorator) {
decorator(bundler)
}

if (file.shouldWatch) {
debug('watching')
bundler.plugin(watchify, watchifyOptions)
Expand Down
14 changes: 12 additions & 2 deletions test/unit/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe('browserify preprocessor', function () {
emit: sandbox.spy(),
}

this.run = () => {
return preprocessor(this.options)(this.file)
this.run = (decorator) => {
return preprocessor(this.options, decorator)(this.file)
}
})

Expand Down Expand Up @@ -90,6 +90,16 @@ describe('browserify preprocessor', function () {
})
})

it('browserify can be decorated', function () {
const decorator = sandbox.spy();

return this.run(decorator).then(() => {
expect(browserify).to.be.called
expect(decorator).to.be.called
})

});

it('returns existing bundle if called again with same filePath', function () {
browserify.reset()
browserify.returns(this.bundlerApi)
Expand Down