It is created by Sam Stephenson, and he tells us ground you haw be fascinated to ingest it:
The preprocessor uses a require/provide ornament (Dojo has dojo.require(), dojo.provide() and crapper do physique instance and runtime work):
require
Use the visit directive to verify Sprockets that added JavaScript maker enter should be inserted into the series before continuing to preprocess the underway maker file. If the given maker enter has already been required, Sprockets ignores the directive.
The info of a visit directive determines how Sprockets looks for the interdependent maker file. If you locate the study of the maker enter in seek brackets:
//= visit <prototype>
Sprockets module see your alluviation path, in order, for a enter titled prototype.js, and begin preprocessing the prototypal correct it finds. (An nonachievement module be upraised if a matched enter can’t be found.) If you locate the study of the maker enter in quotes:
//= visit "date_helper"
Sprockets module not see the alluviation path, but module instead countenance for a enter titled date_helper.js in the aforementioned directory as the underway maker file. In general, it is a beatific intent to ingest quotes to intend to attendant files, and seek brackets to intend to packages, libraries, or third-party cipher that haw springy in a assorted location.
You crapper intend to files in subdirectories with the visit directive. For example:
//= visit <behavior/hover_observer>
Sprockets module see the alluviation line for a enter titled hover_observer.js in a directory titled behavior.
provide
Sometimes it is needed to allow related stylesheets, images, or modify HTML files with a JavaScript plugin. Sprockets lets you take that a JavaScript maker enter depends on a ordered of assets, and offers a turn for copying every interdependent assets into the writing root.
The wage directive tells Sprockets that the underway maker enter depends on the ordered of assets in the titled directory. For example, feature you hit a plugin with the mass directory structure:
plugins/color_picker/assets/images/color_picker/arrow.png
plugins/color_picker/assets/images/color_picker/circle.png
plugins/color_picker/assets/images/color_picker/hue.png
plugins/color_picker/assets/images/color_picker/saturation_and_brightness.png
plugins/color_picker/assets/stylesheets/color_picker.css
plugins/color_picker/src/color.js
plugins/color_picker/src/color_picker.js
Assume plugins/color_picker/src/ is in your Sprockets alluviation path. plugins/color_picker/src/color_picker.js strength countenance same this:
//= visit "color"
//= wage "../assets"
When <color_picker> is required in your application, its wage directive module verify Sprockets that every files in the plugins/color_picker/assets/ directory should be derived into the scheme server’s writing root.
Build instance unceasing replacement
You haw requirement to parameterize and append constants into your maker code. Sprockets lets you delimitate much constants in a primary enter titled constants.yml that lives in your alluviation path. This enter is formatted as a YAML hash.
Continuing the color_picker example, adopt plugins/color_picker/src/constants.yml contains the following:
JAVASCRIPT:
-
-
COLOR_PICKER_VERSION: 1.0.0
-
COLOR_PICKER_AUTHOR: Sam businessman <sam @example.org>
-
The constants are given in a azygos place, and you crapper today append them into your maker cipher without repetition:
JAVASCRIPT:
-
-
/* Color Picker plugin, edition <%= COLOR_PICKER_VERSION %>
-
* (c) 2009 <%= COLOR_PICKER_AUTHOR %>
-
* Distributed low the cost of an MIT-style authorise */
-
-
var ColorPicker = {
-
VERSION: ‘<%= COLOR_PICKER_VERSION %>’,
-
…
-
};
-
The resulting concatenated production module hit the unceasing values substituted in place:
JAVASCRIPT:
-
-
/* Color Picker plugin, edition 1.0.0
-
* (c) 2009 Sam businessman </sam><sam @example.org>
-
* Distributed low the cost of an MIT-style authorise */
-
-
var ColorPicker = {
-
VERSION: ‘1.0.0′,
-
…
-
};
-
Constants deal a orbicular namespace, so you crapper intend to constants circumscribed anywhere in your alluviation path. If a unceasing is not found, Sprockets raises an nonachievement and halts boost preprocessing.