B2G/gaia/build/webapp-optimize.js
/**
 * Optimizes the JS and l10n resources for an HTML document in a webapp.
 *
 * @param {Object} webapp details of current web app.
 * @param {NSFile} file filename/path of the HTML document.
 * @param {Function} callback function to trigger when all optimizations are
 *                            done for this HTML document.
 */
Optimizes the JS and l10n resources for an HTML document in a webapp.
@param {Object} webapp details of current web app.
@param {NSFile} file filename/path of the HTML document.
@param {Function} callback function to trigger when all optimizations are
done for this HTML document.
Optimizes the JS and l10n resources for an HTML document in a webapp . @param { Object } webapp details of current web app . @param { NSFile } file filename / path of the HTML document . @param { Function } callback function to trigger when all optimizations are done for this HTML document .
function optimize_compile(webapp, file, callback) {
  let mozL10n = win.navigator.mozL10n;
  let processedLocales = 0;
  let subDict = newDictionary();
  let fullDict = newDictionary();
  function optimize_dump(str) {
    dump(file.path.replace(GAIA_DIR, '') + ': ' + str + '\n');
  }
  win.console = {
    log: optimize_dump,
    warn: optimize_dump,
    info: optimize_dump
  };
  win.XMLHttpRequest = function() {
    debug('loadResource');
    function open(type, url, async) {
      this.readyState = 4;
      this.status = 200;
      this.responseText = optimize_getFileContent(webapp, file, url);
    }
    function send() {
      this.onreadystatechange();
    }
    return {
      open: open,
      send: send,
      onreadystatechange: null
    };
  };
  win.dispatchEvent = function() {
    processedLocales++;
    debug('fireL10nReadyEvent - ' +
        processedLocales + '/' + l10nLocales.length);
    let docElt = win.document.documentElement;
    subDict[mozL10n.language.code] = mozL10n.getDictionary(docElt);
    fullDict[mozL10n.language.code] = mozL10n.getDictionary();
    if (processedLocales < l10nLocales.length) {
      mozL10n.language.code = l10nLocales[processedLocales];
    } else {
      docElt.dir = mozL10n.language.direction;
      docElt.lang = mozL10n.language.code;
      let newFile = new FileUtils.File(file.path + '.' + GAIA_DEFAULT_LOCALE);
      optimize_embedL10nResources(win.document, subDict);
      optimize_concatL10nResources(win.document, webapp, fullDict);
      optimize_aggregateJsResources(win.document, webapp, newFile);
      optimize_serializeHTMLDocument(win.document, newFile);
      callback();
    }
  };
  let DOMParser = CC('@mozilla.org/xmlextras/domparser;1', 'nsIDOMParser');
  win.document = (new DOMParser()).
      parseFromString(getFileContent(file), 'text/html');
  if ((win.document.querySelector('script[src$="l10n.js"]') ||
      win.document.querySelector('link[type$="application/l10n"]')) &&
      L10N_OPTIMIZATION_BLACKLIST.indexOf(webapp.sourceDirectoryName) < 0) {
    debug('localizing: ' + file.path);
    mozL10n.language.code = l10nLocales[processedLocales];
  } else {
    callback();
  }
}
function optimize_compile ( webapp , file , callback ) { let mozL10n = win . navigator . mozL10n ; let processedLocales = 0 ; let subDict = newDictionary ( ) ; let fullDict = newDictionary ( ) ; function optimize_dump ( str ) { dump ( file . path . replace ( GAIA_DIR , '' ) + ': ' + str + '\n' ) ; } win . console = { log : optimize_dump , warn : optimize_dump , info : optimize_dump } ; win . XMLHttpRequest = function ( ) { debug ( 'loadResource' ) ; function open ( type , url , async ) { this . readyState = 4 ; this . status = 200 ; this . responseText = optimize_getFileContent ( webapp , file , url ) ; } function send ( ) { this . onreadystatechange ( ) ; } return { open : open , send : send , onreadystatechange : null } ; } ; win . dispatchEvent = function ( ) { processedLocales ++ ; debug ( 'fireL10nReadyEvent - ' + processedLocales + '/' + l10nLocales . length ) ; let docElt = win . document . documentElement ; subDict [ mozL10n . language . code ] = mozL10n . getDictionary ( docElt ) ; fullDict [ mozL10n . language . code ] = mozL10n . getDictionary ( ) ; if ( processedLocales < l10nLocales . length ) { mozL10n . language . code = l10nLocales [ processedLocales ] ; } else { docElt . dir = mozL10n . language . direction ; docElt . lang = mozL10n . language . code ; let newFile = new FileUtils . File ( file . path + '.' + GAIA_DEFAULT_LOCALE ) ; optimize_embedL10nResources ( win . document , subDict ) ; optimize_concatL10nResources ( win . document , webapp , fullDict ) ; optimize_aggregateJsResources ( win . document , webapp , newFile ) ; optimize_serializeHTMLDocument ( win . document , newFile ) ; callback ( ) ; } } ; let DOMParser = CC ( '@mozilla.org/xmlextras/domparser;1' , 'nsIDOMParser' ) ; win . document = ( new DOMParser ( ) ) . parseFromString ( getFileContent ( file ) , 'text/html' ) ; if ( ( win . document . querySelector ( 'script[src$="l10n.js"]' ) || win . document . querySelector ( 'link[type$="application/l10n"]' ) ) && L10N_OPTIMIZATION_BLACKLIST . indexOf ( webapp . sourceDirectoryName ) < 0 ) { debug ( 'localizing: ' + file . path ) ; mozL10n . language . code = l10nLocales [ processedLocales ] ; } else { callback ( ) ; } }
Optimizes the JS and l10n resources for an HTML document in a webapp.
Optimizes the JS and l10n resources for an HTML document in a webapp .
/**
   * For each HTML file, we retrieve two multi-locale dictionaries:
   *
   *  - subDict (used with GAIA_INLINE_LOCALES)
   *    = minimal set of strings required to translate all HTML elements that
   *    use data-l10n-id attributes; it gets embedded in the HTML document.
   *
   *  - fullDict (used with GAIA_CONCAT_LOCALES)
   *    = full set of all l10n strings that are loaded by the HTML document,
   *    including subDict and all strings that are used dynamically from JS;
   *    it gets merged into webapp.dictionary.
   */ // catch console.[log|warn|info] calls and redirect them to `dump()' // XXX for some reason, this won't work if gDEBUG >= 2 in l10n.js // catch the XHR in `loadResource' and use a local file reader instead // catch the `localized' event dispatched by `fireL10nReadyEvent()' // load next locale // we expect the last locale to be the default one: // set the lang/dir attributes of the current document // save localized / optimized document // notify the world that this HTML document has been optimized // load and parse the HTML document // If this HTML document uses l10n.js, pre-localize it -- //   note: a document can use l10n.js by including either l10n.js or //   application/l10n resource link elements (see /shared/js/lazy_l10n.js). // selecting a language triggers `XMLHttpRequest' and `dispatchEvent' above