deedubs / require-jade

Add jade to requireJS
MIT License
55 stars 17 forks source link

support java javascript engine #32

Open rmannibucau opened 8 years ago

rmannibucau commented 8 years ago

r.js is often used with nashorn but jade plugin doesn't support it yet. This mainly affect fetchText and can be solved adding this detection and impl:

if (typeof Packages !== 'undefined' && typeof java !== 'undefined') {

fetchText = function (url, callback) {
  var stringBuffer, line,
      encoding = "utf-8",
      file = new java.io.File(url),
      lineSeparator = java.lang.System.getProperty("line.separator"),
      input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
      content = '';
  try {
    stringBuffer = new java.lang.StringBuffer();
    line = input.readLine();

    // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
    // http://www.unicode.org/faq/utf_bom.html

    // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
    if (line && line.length() && line.charAt(0) === 0xfeff) {
      // Eat the BOM, since we've already found the encoding on this file,
      // and we plan to concatenating this buffer with others; the BOM should
      // only appear at the top of a file.
      line = line.substring(1);
    }

    if (line !== null) {
      stringBuffer.append(line);
    }

    while ((line = input.readLine()) !== null) {
      stringBuffer.append(lineSeparator);
      stringBuffer.append(line);
    }
    //Make sure we return a JavaScript string and not a Java string.
    content = String(stringBuffer.toString()); //String
  } finally {
    input.close();
  }
  callback(content);
};

}