UNPKG

6.5 kBMarkdownView Raw
1# resolution-map-builder [![Build Status](https://secure.travis-ci.org/glimmerjs/resolution-map-builder.svg?branch=master)](http://travis-ci.org/glimmerjs/resolution-map-builder)
2
3Utilities and a Broccoli plugin for building a resolution map compatible with
4[@glimmerjs/resolver](https://github.com/glimmerjs/glimmer-resolver)
5and the expectations of Ember's
6[module unification RFC](https://github.com/emberjs/rfcs/blob/master/text/0143-module-unification.md).
7
8This package is a low-level utility and most people should not need to use it
9directly. A new Glimmer app will be configured to generate the resolution map
10automatically via the
11[`@glimmer/application-pipeline`](http://github.com/glimmerjs/glimmer-application-pipeline)
12package.
13
14## The Resolution Map
15
16Glimmer uses a [resolver](https://github.com/glimmerjs/glimmer-resolver) to
17locate your app's modules. For example, if you use the component
18`<my-component>` in a template, the resolver is what tells Glimmer that that
19component lives in your app's `src/ui/components/my-component/component.ts`
20file.
21
22To make this process fast, Glimmer generates a _resolution map_ when you build
23your application. This resolution map allows the resolver to quickly locate the
24requested object.
25
26### Specifiers
27
28Internally, Glimmer uses _specifiers_ to identify objects in the system. A
29specifier is a specially-formatted string that encodes the _type_ and _path_ of
30an object.
31
32For example, the specifier for the `text-editor` component's template in an app might be:
33
34```js
35"template:/my-app/components/text-editor"
36```
37
38In addition to the type of object (component, template, route, etc.), a
39specifier's path includes information about the root name (an app or an addon),
40collection, and namespace.
41
42### Generated Source
43
44This package can generate the source code for a JavaScript module that imports
45the files in your app and includes them in a resolution map object. For example:
46
47```js
48import { default as __ui_components_my_app_component__ } from '../ui/components/my-app/component';
49import { default as __ui_components_my_app_page_banner_component__ } from '../ui/components/my-app/page-banner/component';
50import { default as __ui_components_my_app_page_banner_template__ } from '../ui/components/my-app/page-banner/template';
51import { default as __ui_components_my_app_page_banner_titleize__ } from '../ui/components/my-app/page-banner/titleize';
52import { default as __ui_components_my_app_template__ } from '../ui/components/my-app/template';
53export default {'component:/my-app/components/my-app': __ui_components_my_app_component__,'component:/my-app/components/my-app/page-banner': __ui_components_my_app_page_banner_component__,'template:/my-app/components/my-app/page-banner': __ui_components_my_app_page_banner_template__,'component:/my-app/components/my-app/page-banner/titleize': __ui_components_my_app_page_banner_titleize__,'template:/my-app/components/my-app': __ui_components_my_app_template__};
54```
55
56## Usage
57
58### Broccoli Plugin
59
60If using as a Broccoli plugin, instantiate the plugin with two input trees:
61
621. The `src` directory
632. The `config` directory
64
65```js
66const ResolutionMapBuilder = require('@glimmer/resolution-map-builder');
67return new ResolutionMapBuilder(srcTree, configTree, {
68 srcDir: 'src',
69 defaultModulePrefix: this.name,
70 defaultModuleConfiguration
71});
72```
73
74The Broccoli plugin will read the module prefix and module configuration from
75the `configTree`. If none are found, you can still provide fallback
76configurations with the `defaultModulePrefix` and `defaultModuleConfiguration`
77options.
78
79### Utilities
80
81If you want to generate your own resolution map without using Broccoli, this
82package includes several helper functions you can use.
83
84#### `buildResolutionMapSource()`
85
86Returns a string of generated JavaScript that imports each module and has a default export of
87an object with specifiers as keys and each module's default export as its value.
88
89```js
90const { buildResolutionMapSource } = require('@glimmer/resolution-map-builder');
91let moduleConfig = {
92 types: {
93 application: { definitiveCollection: 'main' },
94 component: { definitiveCollection: 'components' },
95 helper: { definitiveCollection: 'components' },
96 renderer: { definitiveCollection: 'main' },
97 template: { definitiveCollection: 'components' }
98 },
99 collections: {
100 main: {
101 types: ['application', 'renderer']
102 },
103 components: {
104 group: 'ui',
105 types: ['component', 'template', 'helper'],
106 defaultType: 'component',
107 privateCollections: ['utils']
108 },
109 styles: {
110 group: 'ui',
111 unresolvable: true
112 },
113 utils: {
114 unresolvable: true
115 }
116 }
117};
118
119let contents = buildResolutionMapSource({
120 projectDir: 'path/to/app',
121 srcDir: 'src',
122 modulePrefix: 'my-app',
123 moduleConfig
124});
125// returns
126// `import { default as __ui_components_my_app_component_ts__ } from '../ui/components/my-app/component.ts';
127// export default { 'component:/my-app/components/my-app': __ui_components_my_app_component_ts__ };`
128
129fs.writeFileSync('config/module-map.js', contents, { encoding: 'utf8' });
130```
131
132#### `buildResolutionMapTypeDefinitions()`
133
134Returns a string of TypeScript source code that provides type information for
135the resolution map generated by `buildResolutionMapSource()`. This source can be
136included in a `.d.ts` file with the same name as the resolution map to avoid
137TypeScript errors at compilation time.
138
139```js
140const { buildResolutionMapTypeDefinitions } = require('@glimmer/resolution-map-builder');
141
142let types = buildResolutionMapTypeDefinitions();
143fs.writeFileSync('config/module-map.d.ts', types, { encoding: 'utf8' });
144```
145
146#### `buildResolutionMap()`
147
148Similar to `buildResolutionMapSource()` (and takes the same arguments), but
149allows you to generate the JavaScript output yourself. Instead of returning
150JavaScript source, `buildResolutionMap()` returns an object with module
151specifiers as the keys and the _path_ to the module (relative to `projectDir`)
152as the value.
153
154```js
155const { buildResolutionMap } = require('@glimmer/resolution-map-builder');
156
157let map = buildResolutionMap({
158 projectDir: 'path/to/app',
159 srcDir: 'src',
160 modulePrefix: 'my-app',
161 moduleConfig
162});
163
164// returns {
165// 'component:/my-app/components/my-app': 'src/ui/components/my-app/component'
166// }
167```
168
169## Acknowledgements
170
171Thanks to [Monegraph](http://monegraph.com) and
172[Cerebris](http://www.cerebris.com) for funding the initial development of this
173library.
174
175## License
176
177MIT License.