A RequireJS/AMD loader plugin for loading text resources.
Known to work in RequireJS, but should work in other AMD loaders that support the same loader plugin API.
See the RequireJS API text section.
The latest release is always available from the "latest" tag.
It can also be installed using volo:
volo add requirejs/text
The text.js AMD loader plugin can help with this issue. It will automatically be loaded if the text! prefix is used for a dependency. Download the plugin and put it in the app's baseUrl directory (or use the paths config to place it in other areas).
You can specify a text file resource as a dependency like so:
Notice the .html and .css suffixes to specify the extension of the file. The "some/module" part of the path will be resolved according to normal module name resolution: it will use the baseUrl and paths configuration options to map that name to a path.
For HTML/XML/SVG files, there is another option. You can pass !strip, which strips XML declarations so that external SVG and XML documents can be added to a document without worry. Also, if the string is an HTML document, only the part inside the body tag is returned. Example:
The text files are loaded via asynchronous XMLHttpRequest (XHR) calls, so you can only fetch files from the same domain as the web page (see XHR restrictions below).
However, the RequireJS optimizer will inline any text! references with the actual text file contents into the modules, so after a build, the modules that have text! dependencies can be used from other domains.
The text plugin works by using XMLHttpRequest (XHR) to fetch the text for the resources it handles.
However, XHR calls have some restrictions, due to browser/web security policies:
Many browsers do not allow file:// access to just any file. You are better off serving the application from a local web server than using local file:// URLs. You will likely run into trouble otherwise.
There are restrictions for using XHR to access files on another web domain. While CORS can help enable the server for cross-domain access, doing so must be done with care (in particular if you also host an API from that domain), and not all browsers support CORS.
So if the text plugin determines that the request for the resource is on another domain, it will try to access a ".js" version of the resource by using a script tag. Script tag GET requests are allowed across domains. The .js version of the resource should just be a script with a define() call in it that returns a string for the module value.
Example: if the resource is 'text!example.html' and that resolves to a path on another web domain, the text plugin will do a script tag load for 'example.html.js'.
The requirejs optimizer will generate these '.js' versions of the text resources if you set this in the build profile:
In some cases, you may want the text plugin to not try the .js resource, maybe because you have configured CORS on the other server, and you know that only browsers that support CORS will be used. In that case you can use the module config (requires RequireJS 2+) to override some of the basic logic the plugin uses to determine if the .js file should be requested:
Custom XHR hooks
There may be cases where you might want to provide the XHR object to use in the request, or you may just want to add some custom headers to the XHR object used to make the request. You can use the following hooks:
Forcing the environment implemention
The text plugin tries to detect what environment it is available for loading text resources, Node, XMLHttpRequest (XHR) or Rhino, but sometimes the Node or Rhino environment may have loaded a library that introduces an XHR implementation. You can foce the environment implementation to use by passing an "env" module config to the plugin:
Dual-licensed -- new BSD or MIT.
Where are the tests?
They are in the requirejs and r.js repos.
This plugin was in the requirejs repo up until the requirejs 2.0 release.