https://github.com/devongovett/browserify-optional
A browserify transform that allows optional dependencies in try..catch blocks
https://github.com/devongovett/browserify-optional
Last synced: 8 months ago
JSON representation
A browserify transform that allows optional dependencies in try..catch blocks
- Host: GitHub
- URL: https://github.com/devongovett/browserify-optional
- Owner: devongovett
- Created: 2015-03-23T04:50:44.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-08-28T23:09:50.000Z (almost 9 years ago)
- Last Synced: 2025-02-27T05:51:12.720Z (over 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 9
- Watchers: 4
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# browserify-optional
It is a common pattern in Node to support optional dependencies via requires in try..catch blocks.
Browserify doesn't support this by default and throws a compile time error when it cannot find a
module. You can solve the problem by using browserify's exclude option, but this works globally
instead of at a per-module level. This transform fixes the problem by moving the compile time
error to a runtime error for requires of missing modules inside try..catch blocks.
## Example
The transform would transform the following code such that requiring `missing-module` would throw
a runtime error instead of a compile time error, making the code work as expected.
```javascript
try {
var x = require('missing-module');
} catch (e) {
var x = require('replacement-module');
}
```
To set it up in browserify, add this to your package.json:
```json
"browserify": {
"transform": ["browserify-optional"]
}
```
## License
MIT