From 862d17c588058dc43fdb2dfb3537957700e7e20f Mon Sep 17 00:00:00 2001 From: Greg Franko Date: Wed, 18 Jun 2014 08:18:05 -0700 Subject: [PATCH] Add support for a new onReadFileError optimizer hook --- build/jslib/build.js | 4 ++-- build/jslib/node/file.js | 18 ++++++++++++++---- build/jslib/requirePatch.js | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/build/jslib/build.js b/build/jslib/build.js index 6c6993e1..4360b990 100644 --- a/build/jslib/build.js +++ b/build/jslib/build.js @@ -43,7 +43,7 @@ define(function (require) { //Caching function for performance. Attached to //require so it can be reused in requirePatch.js. _cachedRawText //set up by requirePatch.js - require._cacheReadAsync = function (path, encoding) { + require._cacheReadAsync = function (path, encoding, moduleName, context) { var d; if (lang.hasProp(require._cachedRawText, path)) { @@ -51,7 +51,7 @@ define(function (require) { d.resolve(require._cachedRawText[path]); return d.promise; } else { - return file.readFileAsync(path, encoding).then(function (text) { + return file.readFileAsync(path, encoding, moduleName, context).then(function (text) { require._cachedRawText[path] = text; return text; }); diff --git a/build/jslib/node/file.js b/build/jslib/node/file.js index faf38dbd..73195c54 100644 --- a/build/jslib/node/file.js +++ b/build/jslib/node/file.js @@ -204,7 +204,7 @@ define(['fs', 'path', 'prim'], function (fs, path, prim) { /** * Reads a *text* file. */ - readFile: function (/*String*/path, /*String?*/encoding) { + readFile: function (/*String*/path, /*String?*/encoding, /*String?*/moduleName, /*String?*/context) { if (encoding === 'utf-8') { encoding = 'utf8'; } @@ -212,7 +212,17 @@ define(['fs', 'path', 'prim'], function (fs, path, prim) { encoding = 'utf8'; } - var text = fs.readFileSync(path, encoding); + var text; + + try { + text = fs.readFileSync(path, encoding); + } catch(e) { + if(context && context.config && context.config.onReadFileError) { + text = context.config.onReadFileError(moduleName, path); + } else { + throw e; + } + } //Hmm, would not expect to get A BOM, but it seems to happen, //remove it just in case. @@ -223,10 +233,10 @@ define(['fs', 'path', 'prim'], function (fs, path, prim) { return text; }, - readFileAsync: function (path, encoding) { + readFileAsync: function (path, encoding, moduleName, context) { var d = prim(); try { - d.resolve(file.readFile(path, encoding)); + d.resolve(file.readFile(path, encoding, moduleName, context)); } catch (e) { d.reject(e); } diff --git a/build/jslib/requirePatch.js b/build/jslib/requirePatch.js index bf35c53c..1681a9ad 100644 --- a/build/jslib/requirePatch.js +++ b/build/jslib/requirePatch.js @@ -239,7 +239,7 @@ define([ 'env!env/file', 'pragma', 'parse', 'lang', 'logger', 'commonJs', 'prim' } else { //Load the file contents, process for conditionals, then //evaluate it. - return require._cacheReadAsync(url).then(function (text) { + return require._cacheReadAsync(url, null, moduleName, context).then(function (text) { contents = text; if (context.config.cjsTranslate &&