|
1 |
| -/* eslint-disable */ |
2 |
| -exports.registerCustomLoaders = function (loader, loaders, callback) { |
| 1 | +function registerCustomLoaders(loader, loaders, callback) { |
3 | 2 | if (!loaders.length) {
|
4 |
| - return callback(); |
| 3 | + callback(); |
| 4 | + return; |
5 | 5 | }
|
6 |
| - var customLoader = loaders.shift(); |
7 |
| - loader.registerLoader(customLoader, function (err) { |
| 6 | + const customLoader = loaders.shift(); |
| 7 | + loader.registerLoader(customLoader, (err) => { |
8 | 8 | if (err) {
|
9 |
| - return callback(err); |
| 9 | + callback(err); |
| 10 | + return; |
10 | 11 | }
|
11 | 12 | exports.registerCustomLoaders(loader, loaders, callback);
|
12 | 13 | });
|
13 |
| -}; |
14 |
| - |
| 14 | +} |
15 | 15 |
|
16 |
| -exports.setSource = function (sources, loader, packageId, name, source, language, callback) { |
17 |
| - var implementation; |
18 |
| - var originalSource = source; |
| 16 | +function setSource(sources, loader, packageId, name, originalSource, language, callback) { |
| 17 | + let implementation; |
| 18 | + let source = originalSource; |
19 | 19 | // Transpiling
|
20 | 20 | if (language === 'coffeescript') {
|
21 | 21 | if (typeof window !== 'undefined' && !window.CoffeeScript) {
|
22 |
| - return callback(new Error('CoffeeScript compiler needed for ' + packageId + '/' + name + ' not available')); |
| 22 | + callback(new Error(`CoffeeScript compiler needed for ${packageId}/${name} not available`)); |
| 23 | + return; |
23 | 24 | }
|
24 | 25 | try {
|
25 | 26 | source = window.CoffeeScript.compile(source, {
|
26 |
| - bare: true |
| 27 | + bare: true, |
27 | 28 | });
|
28 | 29 | } catch (e) {
|
29 |
| - return callback(e); |
| 30 | + callback(e); |
| 31 | + return; |
30 | 32 | }
|
31 | 33 | }
|
32 | 34 | if (language === 'es6' || language === 'es2015') {
|
33 | 35 | if (typeof window !== 'undefined' && window.babel) {
|
34 | 36 | try {
|
35 | 37 | source = window.babel.transform(source).code;
|
36 | 38 | } catch (e) {
|
37 |
| - return callback(e); |
| 39 | + callback(e); |
| 40 | + return; |
38 | 41 | }
|
39 | 42 | }
|
40 | 43 | }
|
41 | 44 | // Eval the contents to get a runnable component
|
42 | 45 | try {
|
43 |
| - var withExports = '(function () { var exports = {}; ' + source + '; return exports; })();'; |
| 46 | + const withExports = `(function () { var exports = {}; ${source}; return exports; })();`; |
| 47 | + // eslint-disable-next-line no-eval |
44 | 48 | implementation = eval(withExports);
|
45 | 49 | } catch (e) {
|
46 |
| - return callback(e); |
| 50 | + callback(e); |
| 51 | + return; |
47 | 52 | }
|
48 | 53 |
|
49 | 54 | if (typeof implementation !== 'function' && (!implementation.getComponent || typeof implementation.getComponent !== 'function')) {
|
50 |
| - return callback(new Error('Provided source for ' + packageId + '/' + name + ' failed to create a runnable component')); |
| 55 | + callback(new Error(`Provided source for ${packageId}/${name} failed to create a runnable component`)); |
| 56 | + return; |
51 | 57 | }
|
52 | 58 |
|
53 |
| - var fullName = packageId + '/' + name; |
| 59 | + const fullName = `${packageId}/${name}`; |
| 60 | + // eslint-disable-next-line no-param-reassign |
54 | 61 | sources[fullName] = {
|
55 |
| - language: language, |
56 |
| - source: originalSource |
| 62 | + language, |
| 63 | + source: originalSource, |
57 | 64 | };
|
58 | 65 |
|
59 | 66 | loader.registerComponent(packageId, name, implementation, callback);
|
60 |
| -}; |
| 67 | +} |
61 | 68 |
|
62 |
| -exports.getSource = function (sources, loader, name, callback) { |
| 69 | +function getSource(sources, loader, name, callback) { |
63 | 70 | if (!loader.components[name]) {
|
64 |
| - return callback(new Error('Component ' + name + ' not available')); |
| 71 | + callback(new Error(`Component ${name} not available`)); |
| 72 | + return; |
65 | 73 | }
|
66 |
| - var component = loader.components[name]; |
| 74 | + const component = loader.components[name]; |
| 75 | + let componentData; |
67 | 76 | if (name.indexOf('/') !== -1) {
|
68 |
| - var nameParts = name.split('/'); |
69 |
| - var componentData = { |
| 77 | + const nameParts = name.split('/'); |
| 78 | + componentData = { |
70 | 79 | name: nameParts[1],
|
71 |
| - library: nameParts[0] |
| 80 | + library: nameParts[0], |
72 | 81 | };
|
73 | 82 | } else {
|
74 |
| - var componentData = { |
75 |
| - name: name, |
76 |
| - library: '' |
| 83 | + componentData = { |
| 84 | + name, |
| 85 | + library: '', |
77 | 86 | };
|
78 | 87 | }
|
79 | 88 | if (loader.isGraph(component)) {
|
80 | 89 | componentData.code = JSON.stringify(component, null, 2);
|
81 | 90 | componentData.language = 'json';
|
82 |
| - return callback(null, componentData); |
83 |
| - } else if (sources[name]) { |
| 91 | + callback(null, componentData); |
| 92 | + return; |
| 93 | + } if (sources[name]) { |
84 | 94 | componentData.code = sources[name].source;
|
85 | 95 | componentData.language = sources[name].language;
|
86 | 96 | componentData.tests = sources[name].tests;
|
87 |
| - return callback(null, componentData); |
88 |
| - } else if (typeof component === 'function') { |
| 97 | + callback(null, componentData); |
| 98 | + return; |
| 99 | + } if (typeof component === 'function') { |
89 | 100 | componentData.code = component.toString();
|
90 | 101 | componentData.language = 'javascript';
|
91 |
| - return callback(null, componentData); |
92 |
| - } else if (typeof component.getComponent === 'function') { |
| 102 | + callback(null, componentData); |
| 103 | + return; |
| 104 | + } if (typeof component.getComponent === 'function') { |
93 | 105 | componentData.code = component.getComponent.toString();
|
94 | 106 | componentData.language = 'javascript';
|
95 |
| - return callback(null, componentData); |
| 107 | + callback(null, componentData); |
| 108 | + return; |
96 | 109 | }
|
97 |
| - return callback(new Error('Unable to get sources for ' + name)); |
98 |
| -}; |
| 110 | + callback(new Error(`Unable to get sources for ${name}`)); |
| 111 | +} |
99 | 112 |
|
100 |
| -exports.getLanguages = function () { |
101 |
| - var languages = ['javascript', 'es2015']; |
| 113 | +function getLanguages() { |
| 114 | + const languages = ['javascript', 'es2015']; |
102 | 115 | if (typeof window !== 'undefined' && window.CoffeeScript) {
|
103 | 116 | languages.push('coffeescript');
|
104 | 117 | }
|
105 | 118 | return languages;
|
| 119 | +} |
| 120 | + |
| 121 | +module.exports = { |
| 122 | + registerCustomLoaders, |
| 123 | + setSource, |
| 124 | + getSource, |
| 125 | + getLanguages, |
106 | 126 | };
|
0 commit comments