UNPKG

7.42 kBMarkdownView Raw
1
2Resources
3=========
4
5- [Resource Basics](#resource-basics)
6- [Writing CSS](#writing-css)
7- [Writing JavaScript](#writing-javascript)
8- [Adding Images](#adding-images)
9- [Other Static Assets](#other-static-assets)
10- [Built-In EJS Extensions](#built-in-ejs-extensions)
11- [Writing EJS Extensions](#writing-ejs-extensions)
12- [In Production](#production-differences)
13
14
15Resource Basics
16---------------
17
18Shunter uses [Mincer](http://nodeca.github.io/mincer/) to compile and serve assets required by the front end such as CSS, JavaScript and image files. In the case of JavaScript and CSS, dependent files can be required from within a single file that will then be referenced within a layout template. Mincer makes use of [EJS templating](http://www.embeddedjs.com/) to manage requiring of resource files.
19
20Mincer takes a load path and handles every file it finds within that directory. Mincer creates a logical path for each asset, and bases its logical path on the relative folder structure to the load path. In production mode, Mincer will reference a manifest file to match these logical paths to fingerprinted file names.
21
22Note: if two assets are loaded that have the same logical path, the first one in 'wins'. This is important if you are using modules to load in extra resources and templates.
23
24For example if a module you were using were to have `resources/css/foo.css`, it would result in the logical path 'foo.css'. However, when the resources for your host app are loaded in and it also has `resources/css/foo.css`, this too will be given the logical path 'foo.css' and because the asset path for your host app is loaded first, it 'wins'. This can be useful for deliberately overriding, but if you wanted both files then your host apps file would need to be different, e.g. foo_ext.css. For more information on how inheritance works see the [Modules and Inheritance](modules.md) page.
25
26
27Writing CSS
28-----------
29
30By default CSS files should be placed in a directory named `css` within a resources directory in the root of your Shunter application. The location of the resources and CSS directory can be modified by overriding the defaults in the config object.
31
32You might want to set up a `main.css.ejs` in `resources/css` to act as your manifest file. It will require your other internal stylesheets and set some defaults. If you are including modules with CSS files in them, you could refer to them here.
33
34CSS files can be included in your layout template by using the Dust assetPath helper. This helper will determine the location of your file according to the type of file that it is and retrieve it from the location specified in your config files. By default this helper will look for CSS files in the `resources/css` directory so the following would create a path to `main.css` residing in the `resources/css` folder:
35
36```html
37<link rel="stylesheet" href="{@assetPath src="main.css"/}"/>
38```
39
40Further CSS files may be required from within a CSS file in the following way:
41
42```css
43/*
44 *= require bootstrap.css
45 */
46```
47
48If you would like to recursively include an entire directory of CSS files you may use the 'require_tree' directive. eg:
49
50```css
51/*
52 *= require_tree components
53 */
54```
55
56
57Writing Javascript
58------------------
59
60You should set up `main.js.ejs` in `resources/js` within the root of your Shunter application JavaScript files should be placed here. The location of the resources and JavaScript directory can be modified by overriding the defaults in the config object.
61
62Javascript files can be included in your layout template by using the Dust assetPath helper. This helper will determine the location of your file according to the type of file that it is and retrieve it from the location specified in your config files. By default this helper will look for JavaScript files in the `resources/js` directory so the following would create a path to `main.js` residing in the `resources/js` folder:
63
64```html
65<script src="{@assetPath src="main.js"/}"></script>
66```
67
68Further JavaScript files may be required from within a JavaScript file by doing the following:
69
70```js
71//= require jquery-1.9.1.js
72//= require components/forms.js
73```
74
75If you would like to recursively include an entire directory of JavaScript files you may use the 'require_tree' directive. eg:
76
77```js
78//= require_tree components
79```
80
81
82Adding Images
83-------------
84
85Images can also be included using the assetPath helper. Image files should be placed in a directory named img within a `resources` directory in the root of your Shunter application. The location of the resources and img directory can be modified by overriding the defaults in the config object.
86
87```css
88background-image: url(<%= asset_path('icons/png/icon-login-25x25-white.png') %>);
89```
90
91```html
92<img src="{@assetPath src="myimg.png"/}" alt="" />
93```
94
95
96Other Static Assets
97-------------------
98
99Static assets are possible, but you should only use these when you are unable to utilise Mincer (e.g. html emails, 500 error page).
100
101By default, assets in the `public` subdirectory are served on the path `public`.
102
103If it isn't convenient to have one or both of these as 'public' you can override them by setting the config option in your `local.json` file using `config.path.public` for where the assets are saved and `config.web.public` for the path that you would like to serve them on.
104
105
106Built In EJS Extensions
107-----------------------
108
109Mincer uses an [EJS](https://www.npmjs.com/package/ejs) engine for pre-processing assets within resource files. Assets to be included must therefore include the `.ejs` suffix
110
111Shunter uses an implementation of the Mincer [assetPath helper](http://nodeca.github.io/mincer/#assetPath) that is distinct from Shunter's [Dust `assetPath` helper](templates.md#the-assetpath-helper). It behaves in a similar way and this is what you would use to return paths to assets in a CSS file, for example:
112
113```css
114#logo {
115 background: url(<%= asset_path('logo.png') %>);
116}
117```
118
119
120Writing EJS Extensions
121----------------------
122
123You can write your own EJS helpers to assist with the processing of CSS and JavaScript. Custom EJS extensions should sit in a directory named `ejs` within your Shunter application.
124
125EJS helper files must export a single function which is called with the Mincer environment, manifest and Shunter config object.
126
127An example helper might look like the following, which outputs the current year:
128
129```js
130// <app>/ejs/current-year.js
131module.exports = function(environment, manifest, config) {
132 environment.registerHelper('currentYear', function() {
133 var date = new Date();
134 return date.getFullYear();
135 });
136};
137```
138
139
140Production Differences
141----------------------
142
143Shunter provides a build script that will do the following things for a production environemt:
144
145* Concatenate and minify CSS and JavaScript
146* Write static files to `public/resources` with MD5-fingerprinted file names for cache invalidation
147* Create a `manifest.json` file that maps the logical names for resources to their actual fingerprinted file names
148
149To run the build script:
150
151```
152./node_modules/.bin/shunter-build
153```
154
155After this script has run, you should see that the `public` directory in your project has a resources subfolder, with all your compiled assets present.
156
157To run Shunter in production mode (and use the built resources):
158
159```sh
160NODE_ENV=production node app.js
161```
162
163
164---
165
166Related:
167
168- [Full API Documentation](index.md)