-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathresolver.js
More file actions
62 lines (54 loc) · 2.24 KB
/
resolver.js
File metadata and controls
62 lines (54 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { classify } from '@ember/string';
import { get } from '@ember/object';
import Resolver from 'ember-resolver';
import ReactComponent from 'ember-cli-react/components/react-component';
export default Resolver.extend({
// `resolveComponent` is triggered when rendering a component in template.
// For example, having `{{foo-bar}}` in a template will trigger `resolveComponent`
// with the name full name of `component:foo-bar`.
resolveComponent(parsedName) {
// First try to resolve with React-styled file name (e.g. SayHi).
// If nothing is found, try again with original convention via `resolveOther`.
let result =
this._resolveReactStyleFile(parsedName) || this.resolveOther(parsedName);
// If there is no result found after all, return nothing
if (!result) {
return;
}
// If there is an Ember component found, return it.
// This includes the `react-component` Ember component.
if (get(result, 'isComponentFactory')) {
return result;
} else {
// This enables using React Components directly in template
return ReactComponent.extend({
reactComponent: result,
});
}
},
// This resolver method is defined when we try to lookup from `react-component`.
// We create a new namespace `react-component:the-component` for them.
resolveReactComponent(parsedName) {
parsedName.type = 'component';
const result =
this._resolveReactStyleFile(parsedName) || this.resolveOther(parsedName);
parsedName.type = 'react-component';
return result;
},
// This resolver method attempt to find a file with React-style file name.
// A React-style file name is in PascalCase.
// This is made a private method to prevent creation of "react-style-file:*"
// factory.
_resolveReactStyleFile(parsedName) {
const originalName = parsedName.fullNameWithoutType;
// Convert the compnent name while preserving namespaces
const parts = originalName.split('/');
parts[parts.length - 1] = classify(parts[parts.length - 1]);
const newName = parts.join('/');
const parsedNameWithPascalCase = Object.assign({}, parsedName, {
fullNameWithoutType: newName,
});
const result = this.resolveOther(parsedNameWithPascalCase);
return result;
},
});