index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var path = require('path');
  2. var endsInBabelJs = /\.babel\.[jt]s(x)$/;
  3. var mjsStub = path.join(__dirname, 'mjs-stub');
  4. function ignoreNonBabelAndNodeModules(file) {
  5. return !endsInBabelJs.test(file) &&
  6. path.relative(process.cwd(), file).split(path.sep).indexOf('node_modules') >= 0;
  7. }
  8. var extensions = {
  9. '.babel.js': [
  10. {
  11. module: '@babel/register',
  12. register: function(hook) {
  13. hook({
  14. extensions: '.js',
  15. rootMode: 'upward-optional',
  16. ignore: [ignoreNonBabelAndNodeModules],
  17. });
  18. },
  19. },
  20. {
  21. module: 'babel-register',
  22. register: function(hook) {
  23. hook({
  24. extensions: '.js',
  25. ignore: ignoreNonBabelAndNodeModules,
  26. });
  27. },
  28. },
  29. {
  30. module: 'babel-core/register',
  31. register: function(hook) {
  32. hook({
  33. extensions: '.js',
  34. ignore: ignoreNonBabelAndNodeModules,
  35. });
  36. },
  37. },
  38. {
  39. module: 'babel/register',
  40. register: function(hook) {
  41. hook({
  42. extensions: '.js',
  43. ignore: ignoreNonBabelAndNodeModules,
  44. });
  45. },
  46. },
  47. ],
  48. '.babel.ts': [
  49. {
  50. module: '@babel/register',
  51. register: function(hook) {
  52. hook({
  53. extensions: '.ts',
  54. rootMode: 'upward-optional',
  55. ignore: [ignoreNonBabelAndNodeModules],
  56. });
  57. },
  58. },
  59. ],
  60. '.buble.js': 'buble/register',
  61. '.cirru': 'cirru-script/lib/register',
  62. '.cjsx': 'node-cjsx/register',
  63. '.co': 'coco',
  64. '.coffee': ['coffeescript/register', 'coffee-script/register', 'coffeescript', 'coffee-script'],
  65. '.coffee.md': ['coffeescript/register', 'coffee-script/register', 'coffeescript', 'coffee-script'],
  66. '.csv': 'require-csv',
  67. '.eg': 'earlgrey/register',
  68. '.esm.js': {
  69. module: 'esm',
  70. register: function(hook) {
  71. // register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353
  72. // which only captures the final extension (.babel.js -> .js)
  73. var esmLoader = hook(module);
  74. require.extensions['.js'] = esmLoader('module')._extensions['.js'];
  75. },
  76. },
  77. '.iced': ['iced-coffee-script/register', 'iced-coffee-script'],
  78. '.iced.md': 'iced-coffee-script/register',
  79. '.ini': 'require-ini',
  80. '.js': null,
  81. '.json': null,
  82. '.json5': ['json5/lib/register', 'json5/lib/require'],
  83. '.jsx': [
  84. {
  85. module: '@babel/register',
  86. register: function(hook) {
  87. hook({
  88. extensions: '.jsx',
  89. rootMode: 'upward-optional',
  90. ignore: [ignoreNonBabelAndNodeModules],
  91. });
  92. },
  93. },
  94. {
  95. module: 'babel-register',
  96. register: function(hook) {
  97. hook({
  98. extensions: '.jsx',
  99. ignore: ignoreNonBabelAndNodeModules,
  100. });
  101. },
  102. },
  103. {
  104. module: 'babel-core/register',
  105. register: function(hook) {
  106. hook({
  107. extensions: '.jsx',
  108. ignore: ignoreNonBabelAndNodeModules,
  109. });
  110. },
  111. },
  112. {
  113. module: 'babel/register',
  114. register: function(hook) {
  115. hook({
  116. extensions: '.jsx',
  117. ignore: ignoreNonBabelAndNodeModules,
  118. });
  119. },
  120. },
  121. {
  122. module: 'node-jsx',
  123. register: function(hook) {
  124. hook.install({ extension: '.jsx', harmony: true });
  125. },
  126. },
  127. ],
  128. '.litcoffee': ['coffeescript/register', 'coffee-script/register', 'coffeescript', 'coffee-script'],
  129. '.liticed': 'iced-coffee-script/register',
  130. '.ls': ['livescript', 'LiveScript'],
  131. '.mjs': mjsStub,
  132. '.node': null,
  133. '.toml': {
  134. module: 'toml-require',
  135. register: function(hook) {
  136. hook.install();
  137. },
  138. },
  139. '.ts': [
  140. 'ts-node/register',
  141. 'typescript-node/register',
  142. 'typescript-register',
  143. 'typescript-require',
  144. 'sucrase/register/ts',
  145. {
  146. module: '@babel/register',
  147. register: function(hook) {
  148. hook({
  149. extensions: '.ts',
  150. rootMode: 'upward-optional',
  151. ignore: [ignoreNonBabelAndNodeModules],
  152. });
  153. },
  154. },
  155. ],
  156. '.tsx': [
  157. 'ts-node/register',
  158. 'typescript-node/register',
  159. 'sucrase/register',
  160. {
  161. module: '@babel/register',
  162. register: function(hook) {
  163. hook({
  164. extensions: '.tsx',
  165. rootMode: 'upward-optional',
  166. ignore: [ignoreNonBabelAndNodeModules],
  167. });
  168. },
  169. },
  170. ],
  171. '.wisp': 'wisp/engine/node',
  172. '.xml': 'require-xml',
  173. '.yaml': 'require-yaml',
  174. '.yml': 'require-yaml',
  175. };
  176. var jsVariantExtensions = [
  177. '.js',
  178. '.babel.js',
  179. '.babel.ts',
  180. '.buble.js',
  181. '.cirru',
  182. '.cjsx',
  183. '.co',
  184. '.coffee',
  185. '.coffee.md',
  186. '.eg',
  187. '.esm.js',
  188. '.iced',
  189. '.iced.md',
  190. '.jsx',
  191. '.litcoffee',
  192. '.liticed',
  193. '.ls',
  194. '.mjs',
  195. '.ts',
  196. '.tsx',
  197. '.wisp',
  198. ];
  199. module.exports = {
  200. extensions: extensions,
  201. jsVariants: jsVariantExtensions.reduce(function(result, ext) {
  202. result[ext] = extensions[ext];
  203. return result;
  204. }, {}),
  205. };