javascriptrequirejsrequirejs-textrequirejs-optimizer

Prevent optimization of text! and json! plugins on requirejs optimization tool


I'm using the following architecture for my multipage requirejs based application.: https://github.com/requirejs/example-multipage-shim

The repository explains how to optimize the application by running r.js, the command line tool used for this kind of task.

Everything should work fine but my project as some modules that have dependencies that perform HTTP GET request to fetch data from server (wich can be text or json)

This is because some of my jsons and templates used by some pages need to be server-side processed for localization.

Here is a basic example to show what i'm talking about:

 define( function( require ){

    var appLang = require('json!/pagelang/login'),           //(a)
        loginTemplate = require('text!/template/login'),     //(b)
        module = require('app/model/user'),                  //(c)
        ....

An HTTP get request is made to my server localhost/pagelang/login which returns a json

{
   "hash": "translated_value",
    ...
}     

The same applied for template/template_namewhere an html with it's UI translated into the user language is returned from the server.

By running r.js it attempts to load those locations for an existant directory directory on the server, which obviously, don't exist.

Tracing dependencies for: app/main/login
Error: Error: Loader plugin did not call the load callback in the build:
json:
  json!/pagelang/login: Error: ENOENT, no such file or directory '/pagelang/login'
Module loading did not complete for: app/main/login

So, i would like to prevent the command line tool from optimizing text! and json! modules. Is it possible?

I checked requirejs build settings but i didn't find the solution for my problem. Any help? https://github.com/jrburke/r.js/blob/master/build/example.build.js


Solution

  • The json plugin uses if (( config.isBuild && (config.inlineJSON === false || name.indexOf(CACHE_BUST_QUERY_PARAM +'=') !== -1)) || (url.indexOf('empty:') === 0)) { when the optimiser runs so you have a couple of options.

    The text plugin uses if (config.isBuild && !config.inlineText) { and if (url.indexOf('empty:') === 0) {

    =================================================================

    Update: if you can't get the option inlineJSON to work, try using inlineText, which seems to cover JSON as well. Reference: https://github.com/requirejs/r.js/blob/master/build/example.build.js