UNPKG

7.51 kBMarkdownView Raw
1# Libraries
2
3Libraries provide abstractions that allow lumbar components to be bundled into sharable, reusable components. The are able to extend almost any aspect of the lumbar configuration, from setting global config values up to defining entire reusable modules.
4
5## Creating Libraries
6
7Libraries consist of a *lumbar.json* configuration file with at minimum a `name` field which uniquely identifies the library within the current project. This is the human readable method of referencing this library in case of conflicts. (See [Handling Conficts](#handling-conflicts) below for more information on conflicts.
8
9 {
10 "name": "my-library"
11 }
12
13With the base in we now want to add functionality to this. Generally this is going to be a combination of global settings for the project and mixins that can be loaded at specific points in the project.
14
15### Mixins
16
17A library's mixins are generally implement their primary functionality. They allow for the addition of arbitrary behaviors to a specific module. At the most basic level a mixin can apply any attribute value to a module including additional file references.
18
19For example a mixin that may be used to bootstrap a project:
20
21 "mixins": {
22 "core-lib": {
23 "scripts": [
24 {"src": "js/lib/backbone.queryparams.js", "global": true},
25 {"src": "js/lib/backbone.historytracker.js", "global": true},
26 {"src": "js/lib/fastclick.js", "global": true},
27
28 {"src": "lumbar-loader-backbone.js", "library": "lumbar-loader", "platform": "web"},
29
30 {"package-config": true},
31 {"stylus-config": true},
32
33 {"src": "js/init.js"}
34 ],
35 "styles": [
36 "styles/reset.styl",
37 "styles/normalize.styl",
38 "styles/tap-highlight.styl"
39 ]
40 }
41 }
42
43In general you would define the contents of a mixin in the same manner as the contents of a normal module, with a few alterations to the behaviors.
44
45All mixin source files are output first in the resulting module. Exception for script files which are marked as global. In that case the files will be output as follows:
46
47> [mixin globals, module globals, mixin locals, module locals]
48
49All file references in the mixin are relative to the library unless explicitly listed in the reference. In the example above, *js/init.js* refers to *libraryDir/js/init.js* at build time. Effectively mixins have their own form of a sandboxed file system. Clients may override portions of this behavior, which is discussed [below](#overriding-files).
50
51
52### Modules
53
54Libraries may also define entire modules, useful for utility modules such as the generic loader behavior:
55
56 "modules": {
57 "loader": {
58 "topLevelName": "Loader",
59 "mixins": [
60 {"name": "lumbar-loader", "env": "dev"},
61 {"name": "lumbar-localstorage-loader", "env": "production"}
62 ],
63 "scripts": [
64 "js/load.js"
65 ]
66 }
67 }
68
69Note that any modules defined in a library are automatically accessible as mixins. As a consequence of this, mixins and modules may not have the same name in a given library.
70
71### Global settings
72
73Libraries are able to provide default values for settings that otherwise would be duplicated across projects. For example the Thorax library [defines](https://github.com/walmartlabs/thorax/blob/master/lumbar.json#L73) the template handler that it expects for all projects that import the library.
74
75 "templates": {
76 "template": "Handlebars.templates['{{{without-extension name}}}'] = {{handlebarsCall}}({{{data}}});"
77 }
78
79The default behavior for global settings is to extend the current project exactly as if the `_.extend` operation occurred. Individual plugins will modify this behavior based on what makes sense. The majority of these cases will be to augment rather than replace behavior, as is the case with the root `template` setting in the example above. Please consult the documentation for each plugin to see the expected behavior for global setting changes.
80
81### Examples
82
83 - [Lumbar Loader](https://github.com/walmartlabs/lumbar-loader/blob/master/lumbar.json)
84 - [Thorax](https://github.com/walmartlabs/thorax/blob/master/lumbar.json)
85 - [Phoenix Build](https://github.com/walmartlabs/phoenix-build/blob/master/mixin/lumbar.json)
86
87## Using Libraries
88
89Once [defined](#creating-libraries) using a library is as simple as referencing it's path to load it.
90
91 "libraries": [
92 "components/lumbar-loader"
93 ]
94
95This will load the global settings defined in the config and make any mixins defined in the library available for use. Note that the default library file name is *lumbar.json*. Libraries that use another file name will need to reference that file explicitly rather than the containing directory.
96
97### Mixins
98
99Once the library is loaded, it's mixins may be used with in specific modules. This is done by creating a `mixins` array within the module and referencing the name of the library's mixin or module.
100
101 "modules": {
102 "loader": {
103 "mixins": [
104 {"name": "lumbar-loader", "env": "dev"},
105 "core-lib"
106 ]
107 }
108 }
109
110This may be done via simple strings or for overrides and conditional behavior, via a JSON object whose name value is set to the mixin name.
111
112When a mixin is included with additional properties such as the `lumbar-loader` include above, these properties will be applied to all resources defined in the mixin. For this allows for the project to override or define their own conditional behavior as necessary.
113
114#### Overriding Files
115
116If a mixin includes a file that is not desired, it's possible to replace the file by specifying the overrides key when including the mixin.
117
118 "mixins": [
119 {
120 "name": "banner-carousel",
121 "overrides": {
122 "static/images/page-dot.png": "images/page-dot.png"
123 }
124 }
125 }
126
127This will replace any file references to *static/images/page-dot.png* in the mixin with *image/page-dot.png* in the project's file structure. It's also possible to use the value `true` to specify the exact same file name, just in the local file space and `false` to prevent the file from being output.
128
129## Additional information
130
131### Handling Conflicts
132
133If two libraries define a mixin or module of the same name, lumbar will not be able to resolve them by name alone. Under situations such as this mixin's must be included using the fully qualified form:
134
135 "mixins": [
136 { "name": "banner-carousel", "library": "shared" }
137 }
138
139Outside of the additional parameter, this does not impact the include operation.
140
141### Overriding Modules
142
143It's possible to override modules that are defined in a library by creating a module in the root project lumbar config file and then importing the library module as a mixin.
144
145 "modules": {
146 "base": {
147 "mixins": [ {"name": "base", "library": "shared"} ]
148 "scripts": [ "additiona-file.js" ]
149 }
150 }
151
152Alternatively modules can be removed from output by assigning `false` to that particular module's name in the root config.
153
154### File Management
155
156Lumbar purposefully avoids tackling the hows of getting the shared library files into a location accessible to the project. There are too many different needs for this and many existing ways for doing package management.
157
158There are many ways that this can be done, each with their own pros and cons. Some investigated by the WalmartLabs team included:
159
1601. Git Submodules
1611. Git Subtrees
1621. npm
1631. bower
164