From feb872eb3e6894f88868bec03935997dc3bc4559 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 3 Mar 2021 17:50:17 +0000 Subject: [PATCH] Fixed clobbering the lodash template settings refs: https://github.com/lodash/lodash/issues/705 - Was seeing unexpected token = errors when using lodash templates in Ghost - This is because we're setting template settings globally in this dependency and it affects every other user of lodash - Using runInContext keeps this templateSettings change local to this lib - Test proves that after requiring limits we can require lodash and have the default values again --- ghost/limit-service/lib/limit.js | 3 ++- ghost/limit-service/test/hello.test.js | 10 ---------- ghost/limit-service/test/template.test.js | 12 ++++++++++++ 3 files changed, 14 insertions(+), 11 deletions(-) delete mode 100644 ghost/limit-service/test/hello.test.js create mode 100644 ghost/limit-service/test/template.test.js diff --git a/ghost/limit-service/lib/limit.js b/ghost/limit-service/lib/limit.js index cab8ff23ea..6edab24f1f 100644 --- a/ghost/limit-service/lib/limit.js +++ b/ghost/limit-service/lib/limit.js @@ -1,6 +1,7 @@ const errors = require('@tryghost/errors'); -const _ = require('lodash'); +// run in context allows us to change the templateSettings without causing havoc +const _ = require('lodash').runInContext(); _.templateSettings.interpolate = /{{([\s\S]+?)}}/g; class Limit { diff --git a/ghost/limit-service/test/hello.test.js b/ghost/limit-service/test/hello.test.js deleted file mode 100644 index 85d69d1e08..0000000000 --- a/ghost/limit-service/test/hello.test.js +++ /dev/null @@ -1,10 +0,0 @@ -// Switch these lines once there are useful utils -// const testUtils = require('./utils'); -require('./utils'); - -describe('Hello world', function () { - it('Runs a test', function () { - // TODO: Write me! - 'hello'.should.eql('hello'); - }); -}); diff --git a/ghost/limit-service/test/template.test.js b/ghost/limit-service/test/template.test.js new file mode 100644 index 0000000000..9af1622970 --- /dev/null +++ b/ghost/limit-service/test/template.test.js @@ -0,0 +1,12 @@ +// Switch these lines once there are useful utils +// const testUtils = require('./utils'); +require('./utils'); + +describe('Lodash Template', function () { + it('Does not get clobbered by this lib', function () { + require('../lib/limit'); + let _ = require('lodash'); + + _.templateSettings.interpolate.should.eql(/<%=([\s\S]+?)%>/g); + }); +});