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 |
|
3 | Utilities and a Broccoli plugin for building a resolution map compatible with
|
4 | [@glimmerjs/resolver](https://github.com/glimmerjs/glimmer-resolver)
|
5 | and the expectations of Ember's
|
6 | [module unification RFC](https://github.com/emberjs/rfcs/blob/master/text/0143-module-unification.md).
|
7 |
|
8 | This package is a low-level utility and most people should not need to use it
|
9 | directly. A new Glimmer app will be configured to generate the resolution map
|
10 | automatically via the
|
11 | [`@glimmer/application-pipeline`](http://github.com/glimmerjs/glimmer-application-pipeline)
|
12 | package.
|
13 |
|
14 | ## The Resolution Map
|
15 |
|
16 | Glimmer uses a [resolver](https://github.com/glimmerjs/glimmer-resolver) to
|
17 | locate 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
|
19 | component lives in your app's `src/ui/components/my-component/component.ts`
|
20 | file.
|
21 |
|
22 | To make this process fast, Glimmer generates a _resolution map_ when you build
|
23 | your application. This resolution map allows the resolver to quickly locate the
|
24 | requested object.
|
25 |
|
26 | ### Specifiers
|
27 |
|
28 | Internally, Glimmer uses _specifiers_ to identify objects in the system. A
|
29 | specifier is a specially-formatted string that encodes the _type_ and _path_ of
|
30 | an object.
|
31 |
|
32 | For 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 |
|
38 | In addition to the type of object (component, template, route, etc.), a
|
39 | specifier's path includes information about the root name (an app or an addon),
|
40 | collection, and namespace.
|
41 |
|
42 | ### Generated Source
|
43 |
|
44 | This package can generate the source code for a JavaScript module that imports
|
45 | the files in your app and includes them in a resolution map object. For example:
|
46 |
|
47 | ```js
|
48 | import { default as __ui_components_my_app_component__ } from '../ui/components/my-app/component';
|
49 | import { default as __ui_components_my_app_page_banner_component__ } from '../ui/components/my-app/page-banner/component';
|
50 | import { default as __ui_components_my_app_page_banner_template__ } from '../ui/components/my-app/page-banner/template';
|
51 | import { default as __ui_components_my_app_page_banner_titleize__ } from '../ui/components/my-app/page-banner/titleize';
|
52 | import { default as __ui_components_my_app_template__ } from '../ui/components/my-app/template';
|
53 | export 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 |
|
60 | If using as a Broccoli plugin, instantiate the plugin with two input trees:
|
61 |
|
62 | 1. The `src` directory
|
63 | 2. The `config` directory
|
64 |
|
65 | ```js
|
66 | const ResolutionMapBuilder = require('@glimmer/resolution-map-builder');
|
67 | return new ResolutionMapBuilder(srcTree, configTree, {
|
68 | srcDir: 'src',
|
69 | defaultModulePrefix: this.name,
|
70 | defaultModuleConfiguration
|
71 | });
|
72 | ```
|
73 |
|
74 | The Broccoli plugin will read the module prefix and module configuration from
|
75 | the `configTree`. If none are found, you can still provide fallback
|
76 | configurations with the `defaultModulePrefix` and `defaultModuleConfiguration`
|
77 | options.
|
78 |
|
79 | ### Utilities
|
80 |
|
81 | If you want to generate your own resolution map without using Broccoli, this
|
82 | package includes several helper functions you can use.
|
83 |
|
84 | #### `buildResolutionMapSource()`
|
85 |
|
86 | Returns a string of generated JavaScript that imports each module and has a default export of
|
87 | an object with specifiers as keys and each module's default export as its value.
|
88 |
|
89 | ```js
|
90 | const { buildResolutionMapSource } = require('@glimmer/resolution-map-builder');
|
91 | let 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 |
|
119 | let 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 |
|
129 | fs.writeFileSync('config/module-map.js', contents, { encoding: 'utf8' });
|
130 | ```
|
131 |
|
132 | #### `buildResolutionMapTypeDefinitions()`
|
133 |
|
134 | Returns a string of TypeScript source code that provides type information for
|
135 | the resolution map generated by `buildResolutionMapSource()`. This source can be
|
136 | included in a `.d.ts` file with the same name as the resolution map to avoid
|
137 | TypeScript errors at compilation time.
|
138 |
|
139 | ```js
|
140 | const { buildResolutionMapTypeDefinitions } = require('@glimmer/resolution-map-builder');
|
141 |
|
142 | let types = buildResolutionMapTypeDefinitions();
|
143 | fs.writeFileSync('config/module-map.d.ts', types, { encoding: 'utf8' });
|
144 | ```
|
145 |
|
146 | #### `buildResolutionMap()`
|
147 |
|
148 | Similar to `buildResolutionMapSource()` (and takes the same arguments), but
|
149 | allows you to generate the JavaScript output yourself. Instead of returning
|
150 | JavaScript source, `buildResolutionMap()` returns an object with module
|
151 | specifiers as the keys and the _path_ to the module (relative to `projectDir`)
|
152 | as the value.
|
153 |
|
154 | ```js
|
155 | const { buildResolutionMap } = require('@glimmer/resolution-map-builder');
|
156 |
|
157 | let 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 |
|
171 | Thanks to [Monegraph](http://monegraph.com) and
|
172 | [Cerebris](http://www.cerebris.com) for funding the initial development of this
|
173 | library.
|
174 |
|
175 | ## License
|
176 |
|
177 | MIT License.
|