1 | # interpret
|
2 | > A dictionary of file extensions and associated module loaders.
|
3 |
|
4 | [![NPM](https://nodei.co/npm/interpret.png)](https://nodei.co/npm/interpret/)
|
5 |
|
6 | ## What is it
|
7 | This is used by [Liftoff](http://github.com/tkellen/node-liftoff) to automatically require dependencies for configuration files, and by [rechoir](http://github.com/tkellen/node-rechoir) for registering module loaders.
|
8 |
|
9 | ## API
|
10 |
|
11 | ### extensions
|
12 | Map file types to modules which provide a [require.extensions] loader.
|
13 |
|
14 | ```js
|
15 | {
|
16 | '.babel.js': {
|
17 | module: 'babel/register',
|
18 | register: function (module) {
|
19 | module({
|
20 | // register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353
|
21 | // which only captures the final extension (.babel.js -> .js)
|
22 | extensions: '.js'
|
23 | })
|
24 | }
|
25 | },
|
26 | '.cirru': 'cirru-script/lib/register',
|
27 | '.cjsx': 'node-cjsx/register',
|
28 | '.co': 'coco',
|
29 | '.coffee': ['coffee-script/register', 'coffee-script'],
|
30 | '.coffee.md': ['coffee-script/register', 'coffee-script'],
|
31 | '.csv': 'require-csv',
|
32 | '.iced': ['iced-coffee-script/register', 'iced-coffee-script'],
|
33 | '.iced.md': 'iced-coffee-script/register',
|
34 | '.ini': 'require-ini',
|
35 | '.js': null,
|
36 | '.json': null,
|
37 | '.json5': 'json5/lib/require',
|
38 | '.jsx': [
|
39 | {
|
40 | module: 'babel/register',
|
41 | register: function (module) {
|
42 | module({
|
43 | extensions: '.jsx'
|
44 | });
|
45 | },
|
46 | },
|
47 | {
|
48 | module: 'node-jsx',
|
49 | register: function (module) {
|
50 | module.install({
|
51 | extension: '.jsx',
|
52 | harmony: true
|
53 | });
|
54 | }
|
55 | }
|
56 | ],
|
57 | '.litcoffee': ['coffee-script/register', 'coffee-script'],
|
58 | '.liticed': 'iced-coffee-script/register',
|
59 | '.ls': ['livescript', 'LiveScript'],
|
60 | '.node': null,
|
61 | '.toml': {
|
62 | module: 'toml-require',
|
63 | register: function (module) {
|
64 | module.install();
|
65 | }
|
66 | },
|
67 | '.ts': ['typescript-register', 'typescript-require'],
|
68 | '.wisp': 'wisp/engine/node',
|
69 | '.xml': 'require-xml',
|
70 | '.yaml': 'require-yaml',
|
71 | '.yml': 'require-yaml'
|
72 | };
|
73 | ```
|
74 |
|
75 | ### jsVariants
|
76 | Same as above, but only include the extensions which are javascript variants.
|
77 |
|
78 | ## How to use it
|
79 |
|
80 | Consumers should use the exported `extensions` or `jsVariants` object to determine which module should be loaded for a given extension. If a matching extension is found, consumers should do the following:
|
81 |
|
82 | 1. If the value is null, do nothing.
|
83 |
|
84 | 2. If the value is a string, try to require it.
|
85 |
|
86 | 3. If the value is an object, try to require the `module` property. If successful, the `register` property (a function) should be called with the module passed as the first argument.
|
87 |
|
88 | 4. If the value is an array, iterate over it, attempting step #2 or #3 until one of the attempts does not throw.
|
89 |
|
90 | [require.extensions]: http://nodejs.org/api/globals.html#globals_require_extensions
|