From 0f3de255606d498c3debad6afceadd36f185bfbf Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 5 Jan 2017 17:10:12 -0700 Subject: [PATCH] Fixes #506 - Fixed how compiler options are handled by the node-require hook --- node-require.js | 11 ++-- .../api/require-hook-compiler-options/a.marko | 0 .../api/require-hook-compiler-options/b.marko | 0 .../api/require-hook-compiler-options/c.marko | 0 .../api/require-hook-compiler-options/d.marko | 0 .../api/require-hook-compiler-options/test.js | 51 +++++++++++++++++++ .../api/write-to-disk-require/test.js | 4 ++ 7 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 test/autotests/api/require-hook-compiler-options/a.marko create mode 100644 test/autotests/api/require-hook-compiler-options/b.marko create mode 100644 test/autotests/api/require-hook-compiler-options/c.marko create mode 100644 test/autotests/api/require-hook-compiler-options/d.marko diff --git a/node-require.js b/node-require.js index 88aed17e4..c4923977a 100644 --- a/node-require.js +++ b/node-require.js @@ -13,12 +13,12 @@ function normalizeExtension(extension) { } function compile(templatePath, markoCompiler, compilerOptions) { - if (compilerOptions) { - compilerOptions = markoCompiler.defaultOptions; - } else { compilerOptions = Object.assign({}, markoCompiler.defaultOptions, compilerOptions); + } else { + compilerOptions = markoCompiler.defaultOptions; } + var writeToDisk = compilerOptions.writeToDisk; var templateSrc; @@ -124,10 +124,7 @@ function install(options) { extensions.forEach((extension) => { extension = normalizeExtension(extension); - - if (!requireExtensions[extension]) { - requireExtensions[extension] = markoRequireExtension; - } + requireExtensions[extension] = markoRequireExtension; }); } diff --git a/test/autotests/api/require-hook-compiler-options/a.marko b/test/autotests/api/require-hook-compiler-options/a.marko new file mode 100644 index 000000000..e69de29bb diff --git a/test/autotests/api/require-hook-compiler-options/b.marko b/test/autotests/api/require-hook-compiler-options/b.marko new file mode 100644 index 000000000..e69de29bb diff --git a/test/autotests/api/require-hook-compiler-options/c.marko b/test/autotests/api/require-hook-compiler-options/c.marko new file mode 100644 index 000000000..e69de29bb diff --git a/test/autotests/api/require-hook-compiler-options/d.marko b/test/autotests/api/require-hook-compiler-options/d.marko new file mode 100644 index 000000000..e69de29bb diff --git a/test/autotests/api/require-hook-compiler-options/test.js b/test/autotests/api/require-hook-compiler-options/test.js index de1950658..3e30342ed 100644 --- a/test/autotests/api/require-hook-compiler-options/test.js +++ b/test/autotests/api/require-hook-compiler-options/test.js @@ -1,3 +1,19 @@ +var expect = require('chai').expect; +var fs = require('fs'); + +function compileAndCheck(path, shouldWriteToDisk) { + var resolved = require.resolve(path); + var compiledFile = resolved + '.js'; + + try { + fs.unlinkSync(compiledFile); + } catch(e) {} + + require(resolved); + + expect(fs.existsSync(compiledFile)).to.equal(shouldWriteToDisk); +} + exports.check = function(marko, markoCompiler, expect, done) { markoCompiler.configure({ writeToDisk: true, @@ -7,6 +23,8 @@ exports.check = function(marko, markoCompiler, expect, done) { expect(markoCompiler.config.writeToDisk).to.equal(true); expect(markoCompiler.config.preserveWhitespace).to.equal(true); + compileAndCheck('./a.marko', true /* should write to disk */); + require('marko/node-require').install({ compilerOptions: { writeToDisk: false, @@ -17,8 +35,41 @@ exports.check = function(marko, markoCompiler, expect, done) { expect(markoCompiler.config.writeToDisk).to.equal(false); expect(markoCompiler.config.preserveWhitespace).to.equal(false); + markoCompiler.configure({ + writeToDisk: true, + preserveWhitespace: true + }); + + expect(markoCompiler.config.writeToDisk).to.equal(true); + expect(markoCompiler.config.preserveWhitespace).to.equal(true); + + compileAndCheck('./b.marko', false /* should write to disk */); + markoCompiler.configure(); // Reset to defaults expect(markoCompiler.config.writeToDisk).to.equal(true); expect(markoCompiler.config.preserveWhitespace).to.equal(false); + + require('marko/node-require').install({ + compilerOptions: { + writeToDisk: true, + preserveWhitespace: false + } + }); + + compileAndCheck('./c.marko', true /* should write to disk */); + + require('marko/node-require').install({ + compilerOptions: { + preserveWhitespace: false + } + }); + + markoCompiler.configure({ + writeToDisk: false, + preserveWhitespace: true + }); + + compileAndCheck('./d.marko', false /* should write to disk */); + done(); }; \ No newline at end of file diff --git a/test/autotests/api/write-to-disk-require/test.js b/test/autotests/api/write-to-disk-require/test.js index 737925fdc..f77e94870 100644 --- a/test/autotests/api/write-to-disk-require/test.js +++ b/test/autotests/api/write-to-disk-require/test.js @@ -4,6 +4,10 @@ var fs = require('fs'); exports.check = function(marko, markoCompiler, expect, done) { var compiledPath; + require('marko/compiler').configure({ + writeToDisk: true + }); + try { var templatePath = nodePath.join(__dirname, 'template.marko'); compiledPath = nodePath.join(__dirname, 'template.marko.js');