From cefb1cf2fe9623a15b17f0dd56bf61c5ef537874 Mon Sep 17 00:00:00 2001 From: Elco Klingen Date: Tue, 15 Oct 2019 23:31:27 +0200 Subject: [PATCH] Prevent optional dependency Chokidar from loading when not watching --- nunjucks/src/node-loaders.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/nunjucks/src/node-loaders.js b/nunjucks/src/node-loaders.js index 16147caa..96309aa6 100644 --- a/nunjucks/src/node-loaders.js +++ b/nunjucks/src/node-loaders.js @@ -7,9 +7,6 @@ const path = require('path'); const Loader = require('./loader'); const {PrecompiledLoader} = require('./precompiled-loader.js'); let chokidar; -try { - chokidar = require('chokidar'); // eslint-disable-line global-require -} catch (e) {} // eslint-disable-line no-empty class FileSystemLoader extends Loader { constructor(searchPaths, opts) { @@ -37,7 +34,9 @@ class FileSystemLoader extends Loader { if (opts.watch) { // Watch all the templates in the paths and fire an event when // they change - if (!chokidar) { + try { + chokidar = require('chokidar'); // eslint-disable-line global-require + } catch (e) { throw new Error('watch requires chokidar to be installed'); } const paths = this.searchPaths.filter(fs.existsSync); @@ -94,7 +93,9 @@ class NodeResolveLoader extends Loader { this.noCache = !!opts.noCache; if (opts.watch) { - if (!chokidar) { + try { + chokidar = require('chokidar'); // eslint-disable-line global-require + } catch (e) { throw new Error('watch requires chokidar to be installed'); } this.watcher = chokidar.watch();