package-transpilation-registry-spec.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /** @babel */
  2. import path from 'path';
  3. import PackageTranspilationRegistry from '../src/package-transpilation-registry';
  4. const originalCompiler = {
  5. getCachePath: (sourceCode, filePath) => {
  6. return 'orig-cache-path';
  7. },
  8. compile: (sourceCode, filePath) => {
  9. return sourceCode + '-original-compiler';
  10. },
  11. shouldCompile: (sourceCode, filePath) => {
  12. return path.extname(filePath) === '.js';
  13. }
  14. };
  15. describe('PackageTranspilationRegistry', () => {
  16. let registry;
  17. let wrappedCompiler;
  18. beforeEach(() => {
  19. registry = new PackageTranspilationRegistry();
  20. wrappedCompiler = registry.wrapTranspiler(originalCompiler);
  21. });
  22. it('falls through to the original compiler by default', () => {
  23. spyOn(originalCompiler, 'getCachePath');
  24. spyOn(originalCompiler, 'compile');
  25. spyOn(originalCompiler, 'shouldCompile');
  26. wrappedCompiler.getCachePath('source', '/path/to/file.js');
  27. wrappedCompiler.compile('source', '/path/to/filejs');
  28. wrappedCompiler.shouldCompile('source', '/path/to/file.js');
  29. expect(originalCompiler.getCachePath).toHaveBeenCalled();
  30. expect(originalCompiler.compile).toHaveBeenCalled();
  31. expect(originalCompiler.shouldCompile).toHaveBeenCalled();
  32. });
  33. describe('when a file is contained in a path that has custom transpilation', () => {
  34. const hitPath = path.join('/path/to/lib/file.js');
  35. const hitPathCoffee = path.join('/path/to/file2.coffee');
  36. const missPath = path.join('/path/other/file3.js');
  37. const hitPathMissSubdir = path.join('/path/to/file4.js');
  38. const hitPathMissExt = path.join('/path/to/file5.ts');
  39. const nodeModulesFolder = path.join('/path/to/lib/node_modules/file6.js');
  40. const hitNonStandardExt = path.join('/path/to/file7.omgwhatisthis');
  41. const jsSpec = {
  42. glob: 'lib/**/*.js',
  43. transpiler: './transpiler-js',
  44. options: { type: 'js' }
  45. };
  46. const coffeeSpec = {
  47. glob: '*.coffee',
  48. transpiler: './transpiler-coffee',
  49. options: { type: 'coffee' }
  50. };
  51. const omgSpec = {
  52. glob: '*.omgwhatisthis',
  53. transpiler: './transpiler-omg',
  54. options: { type: 'omg' }
  55. };
  56. const expectedMeta = {
  57. name: 'my-package',
  58. path: path.join('/path/to'),
  59. meta: { some: 'metadata' }
  60. };
  61. const jsTranspiler = {
  62. transpile: (sourceCode, filePath, options) => {
  63. return { code: sourceCode + '-transpiler-js' };
  64. },
  65. getCacheKeyData: (sourceCode, filePath, options) => {
  66. return 'js-transpiler-cache-data';
  67. }
  68. };
  69. const coffeeTranspiler = {
  70. transpile: (sourceCode, filePath, options) => {
  71. return { code: sourceCode + '-transpiler-coffee' };
  72. },
  73. getCacheKeyData: (sourceCode, filePath, options) => {
  74. return 'coffee-transpiler-cache-data';
  75. }
  76. };
  77. const omgTranspiler = {
  78. transpile: (sourceCode, filePath, options) => {
  79. return { code: sourceCode + '-transpiler-omg' };
  80. },
  81. getCacheKeyData: (sourceCode, filePath, options) => {
  82. return 'omg-transpiler-cache-data';
  83. }
  84. };
  85. beforeEach(() => {
  86. jsSpec._transpilerSource = 'js-transpiler-source';
  87. coffeeSpec._transpilerSource = 'coffee-transpiler-source';
  88. omgTranspiler._transpilerSource = 'omg-transpiler-source';
  89. spyOn(registry, 'getTranspiler').andCallFake(spec => {
  90. if (spec.transpiler === './transpiler-js') return jsTranspiler;
  91. if (spec.transpiler === './transpiler-coffee') return coffeeTranspiler;
  92. if (spec.transpiler === './transpiler-omg') return omgTranspiler;
  93. throw new Error('bad transpiler path ' + spec.transpiler);
  94. });
  95. registry.addTranspilerConfigForPath(
  96. path.join('/path/to'),
  97. 'my-package',
  98. { some: 'metadata' },
  99. [jsSpec, coffeeSpec, omgSpec]
  100. );
  101. });
  102. it('always returns true from shouldCompile for a file in that dir that match a glob', () => {
  103. spyOn(originalCompiler, 'shouldCompile').andReturn(false);
  104. expect(wrappedCompiler.shouldCompile('source', hitPath)).toBe(true);
  105. expect(wrappedCompiler.shouldCompile('source', hitPathCoffee)).toBe(true);
  106. expect(wrappedCompiler.shouldCompile('source', hitNonStandardExt)).toBe(
  107. true
  108. );
  109. expect(wrappedCompiler.shouldCompile('source', hitPathMissExt)).toBe(
  110. false
  111. );
  112. expect(wrappedCompiler.shouldCompile('source', hitPathMissSubdir)).toBe(
  113. false
  114. );
  115. expect(wrappedCompiler.shouldCompile('source', missPath)).toBe(false);
  116. expect(wrappedCompiler.shouldCompile('source', nodeModulesFolder)).toBe(
  117. false
  118. );
  119. });
  120. it('calls getCacheKeyData on the transpiler to get additional cache key data', () => {
  121. spyOn(registry, 'getTranspilerPath').andReturn('./transpiler-js');
  122. spyOn(jsTranspiler, 'getCacheKeyData').andCallThrough();
  123. wrappedCompiler.getCachePath('source', missPath, jsSpec);
  124. expect(jsTranspiler.getCacheKeyData).not.toHaveBeenCalledWith(
  125. 'source',
  126. missPath,
  127. jsSpec.options,
  128. expectedMeta
  129. );
  130. wrappedCompiler.getCachePath('source', hitPath, jsSpec);
  131. expect(jsTranspiler.getCacheKeyData).toHaveBeenCalledWith(
  132. 'source',
  133. hitPath,
  134. jsSpec.options,
  135. expectedMeta
  136. );
  137. });
  138. it('compiles files matching a glob with the associated transpiler, and the old one otherwise', () => {
  139. spyOn(jsTranspiler, 'transpile').andCallThrough();
  140. spyOn(coffeeTranspiler, 'transpile').andCallThrough();
  141. spyOn(omgTranspiler, 'transpile').andCallThrough();
  142. expect(wrappedCompiler.compile('source', hitPath)).toEqual(
  143. 'source-transpiler-js'
  144. );
  145. expect(jsTranspiler.transpile).toHaveBeenCalledWith(
  146. 'source',
  147. hitPath,
  148. jsSpec.options,
  149. expectedMeta
  150. );
  151. expect(wrappedCompiler.compile('source', hitPathCoffee)).toEqual(
  152. 'source-transpiler-coffee'
  153. );
  154. expect(coffeeTranspiler.transpile).toHaveBeenCalledWith(
  155. 'source',
  156. hitPathCoffee,
  157. coffeeSpec.options,
  158. expectedMeta
  159. );
  160. expect(wrappedCompiler.compile('source', hitNonStandardExt)).toEqual(
  161. 'source-transpiler-omg'
  162. );
  163. expect(omgTranspiler.transpile).toHaveBeenCalledWith(
  164. 'source',
  165. hitNonStandardExt,
  166. omgSpec.options,
  167. expectedMeta
  168. );
  169. expect(wrappedCompiler.compile('source', missPath)).toEqual(
  170. 'source-original-compiler'
  171. );
  172. expect(wrappedCompiler.compile('source', hitPathMissExt)).toEqual(
  173. 'source-original-compiler'
  174. );
  175. expect(wrappedCompiler.compile('source', hitPathMissSubdir)).toEqual(
  176. 'source-original-compiler'
  177. );
  178. expect(wrappedCompiler.compile('source', nodeModulesFolder)).toEqual(
  179. 'source-original-compiler'
  180. );
  181. });
  182. describe('when the packages root path contains node_modules', () => {
  183. beforeEach(() => {
  184. registry.addTranspilerConfigForPath(
  185. path.join('/path/with/node_modules/in/root'),
  186. 'my-other-package',
  187. { some: 'metadata' },
  188. [jsSpec]
  189. );
  190. });
  191. it('returns appropriate values from shouldCompile', () => {
  192. spyOn(originalCompiler, 'shouldCompile').andReturn(false);
  193. expect(
  194. wrappedCompiler.shouldCompile(
  195. 'source',
  196. '/path/with/node_modules/in/root/lib/test.js'
  197. )
  198. ).toBe(true);
  199. expect(
  200. wrappedCompiler.shouldCompile(
  201. 'source',
  202. '/path/with/node_modules/in/root/lib/node_modules/test.js'
  203. )
  204. ).toBe(false);
  205. });
  206. });
  207. });
  208. });