• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..16-Feb-2021-

LICENSEH A D16-Feb-20211.9 KiB4634

README.mdH A D16-Feb-20217.3 KiB207160

bower.jsonH A D16-Feb-202184 66

package.jsonH A D16-Feb-20212.4 KiB8685

README.md

1# text
2
3A [RequireJS](http://requirejs.org)/AMD loader plugin for loading text
4resources.
5
6Known to work in RequireJS, but should work in other AMD loaders that support
7the same loader plugin API.
8
9## Docs
10
11See the [RequireJS API text section](http://requirejs.org/docs/api.html#text).
12
13## Latest release
14
15The latest release is always available from [the "latest" tag](https://raw.github.com/requirejs/text/latest/text.js).
16
17It can also be installed using [volo](https://github.com/volojs/volo):
18
19    volo add requirejs/text
20
21If using npm:
22
23    npm install requirejs/text
24
25## Usage
26
27It is nice to build HTML using regular HTML tags, instead of building up DOM
28structures in script. However, there is no good way to embed HTML in a
29JavaScript file. The best that can be done is using a string of HTML, but that
30can be hard to manage, particularly for multi-line HTML.
31
32The text.js AMD loader plugin can help with this issue. It will automatically be
33loaded if the text! prefix is used for a dependency. Download the plugin and put
34it in the app's [baseUrl](http://requirejs.org/docs/api.html#config-baseUrl)
35directory (or use the [paths config](http://requirejs.org/docs/api.html#config-paths) to place it in other areas).
36
37You can specify a text file resource as a dependency like so:
38
39```javascript
40require(["some/module", "text!some/module.html", "text!some/module.css"],
41    function(module, html, css) {
42        //the html variable will be the text
43        //of the some/module.html file
44        //the css variable will be the text
45        //of the some/module.css file.
46    }
47);
48```
49
50Notice the .html and .css suffixes to specify the extension of the file. The
51"some/module" part of the path will be resolved according to normal module name
52resolution: it will use the **baseUrl** and **paths** [configuration
53options](http://requirejs.org/docs/api.html#config) to map that name to a path.
54
55For HTML/XML/SVG files, there is another option. You can pass !strip, which
56strips XML declarations so that external SVG and XML documents can be added to a
57document without worry. Also, if the string is an HTML document, only the part
58inside the body tag is returned. Example:
59
60```javascript
61require(["text!some/module.html!strip"],
62    function(html) {
63        //the html variable will be the text of the
64        //some/module.html file, but only the part
65        //inside the body tag.
66    }
67);
68```
69
70The text files are loaded via asynchronous XMLHttpRequest (XHR) calls, so you
71can only fetch files from the same domain as the web page (see **XHR
72restrictions** below).
73
74However, [the RequireJS optimizer](http://requirejs.org/docs/optimization.html)
75will inline any text! references with the actual text file contents into the
76modules, so after a build, the modules that have text! dependencies can be used
77from other domains.
78
79## Configuration
80
81### XHR restrictions
82
83The text plugin works by using XMLHttpRequest (XHR) to fetch the text for the
84resources it handles.
85
86However, XHR calls have some restrictions, due to browser/web security policies:
87
881) Many browsers do not allow file:// access to just any file. You are better
89off serving the application from a local web server than using local file://
90URLs. You will likely run into trouble otherwise.
91
922) There are restrictions for using XHR to access files on another web domain.
93While CORS can help enable the server for cross-domain access, doing so must
94be done with care (in particular if you also host an API from that domain),
95and not all browsers support CORS.
96
97So if the text plugin determines that the request for the resource is on another
98domain, it will try to access a ".js" version of the resource by using a
99script tag. Script tag GET requests are allowed across domains. The .js version
100of the resource should just be a script with a define() call in it that returns
101a string for the module value.
102
103Example: if the resource is 'text!example.html' and that resolves to a path
104on another web domain, the text plugin will do a script tag load for
105'example.html.js'.
106
107The [requirejs optimizer](http://requirejs.org/docs/optimization.html) will
108generate these '.js' versions of the text resources if you set this in the
109build profile:
110
111    optimizeAllPluginResources: true
112
113In some cases, you may want the text plugin to not try the .js resource, maybe
114because you have configured CORS on the other server, and you know that only
115browsers that support CORS will be used. In that case you can use the
116[module config](http://requirejs.org/docs/api.html#config-moduleconfig)
117(requires RequireJS 2+) to override some of the basic logic the plugin uses to
118determine if the .js file should be requested:
119
120```javascript
121requirejs.config({
122    config: {
123        text: {
124            useXhr: function (url, protocol, hostname, port) {
125                //Override function for determining if XHR should be used.
126                //url: the URL being requested
127                //protocol: protocol of page text.js is running on
128                //hostname: hostname of page text.js is running on
129                //port: port of page text.js is running on
130                //Use protocol, hostname, and port to compare against the url
131                //being requested.
132                //Return true or false. true means "use xhr", false means
133                //"fetch the .js version of this resource".
134            }
135        }
136    }
137});
138```
139
140### Custom XHR hooks
141
142There may be cases where you might want to provide the XHR object to use
143in the request, or you may just want to add some custom headers to the
144XHR object used to make the request. You can use the following hooks:
145
146```javascript
147requirejs.config({
148    config: {
149        text: {
150            onXhr: function (xhr, url) {
151                //Called after the XHR has been created and after the
152                //xhr.open() call, but before the xhr.send() call.
153                //Useful time to set headers.
154                //xhr: the xhr object
155                //url: the url that is being used with the xhr object.
156            },
157            createXhr: function () {
158                //Overrides the creation of the XHR object. Return an XHR
159                //object from this function.
160                //Available in text.js 2.0.1 or later.
161            },
162            onXhrComplete: function (xhr, url) {
163                //Called whenever an XHR has completed its work. Useful
164                //if browser-specific xhr cleanup needs to be done.
165            }
166        }
167    }
168});
169```
170
171### Forcing the environment implementation
172
173The text plugin tries to detect what environment it is available for loading
174text resources, Node, XMLHttpRequest (XHR) or Rhino, but sometimes the
175Node or Rhino environment may have loaded a library that introduces an XHR
176implementation. You can force the environment implementation to use by passing
177an "env" module config to the plugin:
178
179```javascript
180requirejs.config({
181    config: {
182        text: {
183            //Valid values are 'node', 'xhr', or 'rhino'
184            env: 'rhino'
185        }
186    }
187});
188```
189
190## License
191
192MIT
193
194## Code of Conduct
195
196[jQuery Foundation Code of Conduct](https://jquery.org/conduct/).
197
198## Where are the tests?
199
200They are in the [requirejs](https://github.com/requirejs/requirejs) and
201[r.js](https://github.com/requirejs/r.js) repos.
202
203## History
204
205This plugin was in the [requirejs repo](https://github.com/requirejs/requirejs)
206up until the requirejs 2.0 release.
207