diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-05-01 19:07:57 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-05-01 19:07:57 +0200 |
commit | 084925de8b0fb820796b5ce60bffdd708c7dad67 (patch) | |
tree | 6a9278228ff751e952d6f79628fb3b91f84146fb /lib/plugins | |
parent | 5bac2fe695b6f56088c03c8144a05a8e0418ea50 (diff) | |
download | gitbook-084925de8b0fb820796b5ce60bffdd708c7dad67.zip gitbook-084925de8b0fb820796b5ce60bffdd708c7dad67.tar.gz gitbook-084925de8b0fb820796b5ce60bffdd708c7dad67.tar.bz2 |
Improve plugin loading to not fake module errors
Diffstat (limited to 'lib/plugins')
-rw-r--r-- | lib/plugins/loadPlugin.js | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/lib/plugins/loadPlugin.js b/lib/plugins/loadPlugin.js index 400146e..276d201 100644 --- a/lib/plugins/loadPlugin.js +++ b/lib/plugins/loadPlugin.js @@ -32,6 +32,7 @@ function loadPlugin(book, plugin) { var p = Promise() .then(function() { var packageContent; + var packageMain; var content; // Locate plugin and load package.json @@ -49,15 +50,20 @@ function loadPlugin(book, plugin) { return; } - // Load plugin JS content + // Locate the main package try { - content = require(pkgPath); - } catch(err) { - // It's no big deal if the plugin doesn't have an "index.js" - // (For example: themes) - if (isModuleNotFound(err)) { - content = {}; - } else { + var indexJs = path.normalize(packageContent.main || 'index.js'); + packageMain = resolve.sync('./' + indexJs, { basedir: pkgPath }); + } catch (err) { + if (!isModuleNotFound(err)) throw err; + packageMain = undefined; + } + + // Load plugin JS content + if (packageMain) { + try { + content = require(packageMain); + } catch(err) { throw new error.PluginError(err, { plugin: name }); |