add customisable extensions to require hook #75
This commit is contained in:
parent
c2930c9650
commit
5b08924c02
20
README.md
20
README.md
@ -172,8 +172,9 @@ to5.transformFile("filename.js", options, function (err, result) {
|
||||
|
||||
#### Require hook
|
||||
|
||||
All subsequent files required by node will be transformed by 6to5. The polyfill
|
||||
specified in [Polyfill](#polyfill) is also required.
|
||||
All subsequent files required by node with the extensions `.es6` and `.js` will
|
||||
be transformed by 6to5. The polyfill specified in [Polyfill](#polyfill) is also
|
||||
required.
|
||||
|
||||
```javascript
|
||||
require("6to5/register");
|
||||
@ -183,7 +184,20 @@ require("6to5/register");
|
||||
override this by passing an ignore regex via:
|
||||
|
||||
```javascript
|
||||
require("6to5/register")(/regex/)
|
||||
require("6to5/register")(/regex/);
|
||||
```
|
||||
|
||||
You can also customise the file extensions that the require hook will use via:
|
||||
|
||||
```javascript
|
||||
require("6to5/register")({
|
||||
// optional ignore regex
|
||||
ignoreRegex: /regex/,
|
||||
|
||||
// this will remove the currently hooked extensions of .es6 and .js so you'll
|
||||
// have to add them back if you want them to be used again
|
||||
extensions: [".js", ".es6"]
|
||||
});
|
||||
```
|
||||
|
||||
### Browser
|
||||
|
||||
@ -2,6 +2,7 @@ require("./polyfill");
|
||||
|
||||
var sourceMapSupport = require("source-map-support");
|
||||
var to5 = require("./index");
|
||||
var _ = require("lodash");
|
||||
|
||||
sourceMapSupport.install({
|
||||
retrieveSourceMap: function (source) {
|
||||
@ -17,12 +18,14 @@ sourceMapSupport.install({
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
var ignoreRegex = /node_modules/;
|
||||
var exts = [];
|
||||
var maps = {};
|
||||
var old = require.extensions[".js"];
|
||||
|
||||
require.extensions[".js"] =
|
||||
require.extensions[".es6"] = function (m, filename) {
|
||||
var loader = function (m, filename) {
|
||||
if (ignoreRegex && ignoreRegex.test(filename)) {
|
||||
return old.apply(this, arguments);
|
||||
}
|
||||
@ -36,6 +39,27 @@ require.extensions[".es6"] = function (m, filename) {
|
||||
m._compile(result.code, filename);
|
||||
};
|
||||
|
||||
module.exports = function (_ignoreRegex) {
|
||||
ignoreRegex = _ignoreRegex;
|
||||
var hookExtensions = function (_exts) {
|
||||
_.each(exts, function (ext) {
|
||||
delete require.extensions[ext];
|
||||
});
|
||||
|
||||
exts = _exts;
|
||||
|
||||
_.each(exts, function (ext) {
|
||||
require.extensions[ext] = loader;
|
||||
});
|
||||
};
|
||||
|
||||
hookExtensions([".es6", ".js"]);
|
||||
|
||||
module.exports = function (opts) {
|
||||
opts = opts || {};
|
||||
if (_.isRegExp(opts)) opts = { ignoreRegex: opts };
|
||||
|
||||
if (opts.ignoreRegex != null) {
|
||||
ignoreRegex = opts.ignoreRegex;
|
||||
}
|
||||
|
||||
if (opts.extensions) hookExtensions(opts.extensions);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user