[79a0317] | 1 | <p align="center">
|
---|
| 2 | <a href="http://gulpjs.com">
|
---|
| 3 | <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
|
---|
| 4 | </a>
|
---|
| 5 | </p>
|
---|
| 6 |
|
---|
| 7 | # rechoir
|
---|
| 8 |
|
---|
| 9 | [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url]
|
---|
| 10 |
|
---|
| 11 | Prepare a node environment to require files with different extensions.
|
---|
| 12 |
|
---|
| 13 | ## What is it?
|
---|
| 14 |
|
---|
| 15 | This module, in conjunction with [interpret]-like objects, can register any filetype the npm ecosystem has a module loader for. This library is a dependency of [liftoff].
|
---|
| 16 |
|
---|
| 17 | **Note:** While `rechoir` will automatically load and register transpilers like `coffee-script`, you must provide a local installation. The transpilers are **not** bundled with this module.
|
---|
| 18 |
|
---|
| 19 | ## Usage
|
---|
| 20 |
|
---|
| 21 | ```js
|
---|
| 22 | const config = require('interpret').extensions;
|
---|
| 23 | const rechoir = require('rechoir');
|
---|
| 24 | rechoir.prepare(config, './test/fixtures/test.coffee');
|
---|
| 25 | rechoir.prepare(config, './test/fixtures/test.csv');
|
---|
| 26 | rechoir.prepare(config, './test/fixtures/test.toml');
|
---|
| 27 |
|
---|
| 28 | console.log(require('./test/fixtures/test.coffee'));
|
---|
| 29 | console.log(require('./test/fixtures/test.csv'));
|
---|
| 30 | console.log(require('./test/fixtures/test.toml'));
|
---|
| 31 | ```
|
---|
| 32 |
|
---|
| 33 | ## API
|
---|
| 34 |
|
---|
| 35 | ### `prepare(config, filepath, [cwd], [noThrow])`
|
---|
| 36 |
|
---|
| 37 | Look for a module loader associated with the provided file and attempt require it. If necessary, run any setup required to inject it into [require.extensions].
|
---|
| 38 |
|
---|
| 39 | `config` An [interpret]-like configuration object.
|
---|
| 40 |
|
---|
| 41 | `filepath` A file whose type you'd like to register a module loader for.
|
---|
| 42 |
|
---|
| 43 | `cwd` An optional path to start searching for the module required to load the requested file. Defaults to the directory of `filepath`.
|
---|
| 44 |
|
---|
| 45 | `noThrow` An optional boolean indicating if the method should avoid throwing.
|
---|
| 46 |
|
---|
| 47 | If calling this method is successful (e.g. it doesn't throw), you can now require files of the type you requested natively.
|
---|
| 48 |
|
---|
| 49 | An error with a `failures` property will be thrown if the module loader(s) configured for a given extension cannot be registered.
|
---|
| 50 |
|
---|
| 51 | If a loader is already registered, this will simply return `true`.
|
---|
| 52 |
|
---|
| 53 | ## License
|
---|
| 54 |
|
---|
| 55 | MIT
|
---|
| 56 |
|
---|
| 57 | <!-- prettier-ignore-start -->
|
---|
| 58 | [downloads-image]: https://img.shields.io/npm/dm/rechoir.svg?style=flat-square
|
---|
| 59 | [npm-url]: https://www.npmjs.com/package/rechoir
|
---|
| 60 | [npm-image]: https://img.shields.io/npm/v/rechoir.svg?style=flat-square
|
---|
| 61 |
|
---|
| 62 | [ci-url]: https://github.com/gulpjs/rechoir/actions?query=workflow:dev
|
---|
| 63 | [ci-image]: https://img.shields.io/github/workflow/status/gulpjs/rechoir/dev?style=flat-square
|
---|
| 64 |
|
---|
| 65 | [coveralls-url]: https://coveralls.io/r/gulpjs/rechoir
|
---|
| 66 | [coveralls-image]: https://img.shields.io/coveralls/gulpjs/rechoir/master.svg
|
---|
| 67 | <!-- prettier-ignore-end -->
|
---|
| 68 |
|
---|
| 69 | <!-- prettier-ignore-start -->
|
---|
| 70 | [interpret]: https://github.com/gulpjs/interpret
|
---|
| 71 | [require.extensions]: https://nodejs.org/api/modules.html#modules_require_extensions
|
---|
| 72 | [liftoff]: https://github.com/js-cli/js-liftoff
|
---|
| 73 | <!-- prettier-ignore-end -->
|
---|