diff --git a/index.js b/index.js index 9686aad..8ec0f88 100644 --- a/index.js +++ b/index.js @@ -118,25 +118,28 @@ InstallDots.prototype.compileAll = function() { var defFolder = this.__path, sources = fs.readdirSync(defFolder), - k, l, name; + k, l, name, + defRx = /\.def(\.dot|\.jst)?$/, + dotRx = /\.dot(\.def|\.jst)?$/, + jstRx = /\.jst(\.dot|\.def)?$/; for( k = 0, l = sources.length; k < l; k++) { name = sources[k]; - if (/\.def(\.dot|\.jst)?$/.test(name)) { + if (defRx.test(name)) { if (doT.log) console.log("Loaded def " + name); - this.__includes[name.substring(0, name.indexOf('.'))] = readdata(defFolder + name); + this.__includes[name.replace(defRx, "")] = readdata(defFolder + name); } } for( k = 0, l = sources.length; k < l; k++) { name = sources[k]; - if (/\.dot(\.def|\.jst)?$/.test(name)) { + if (dotRx.test(name)) { if (doT.log) console.log("Compiling " + name + " to function"); - this.__rendermodule[name.substring(0, name.indexOf('.'))] = this.compilePath(defFolder + name); + this.__rendermodule[name.replace(dotRx, "")] = this.compilePath(defFolder + name); } - if (/\.jst(\.dot|\.def)?$/.test(name)) { + if (jstRx.test(name)) { if (doT.log) console.log("Compiling " + name + " to file"); - this.compileToFile(this.__destination + name.substring(0, name.indexOf('.')) + '.js', + this.compileToFile(this.__destination + name.replace(jstRx, "") + '.js', readdata(defFolder + name)); } } diff --git a/package.json b/package.json index b0db782..b1a1c6c 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "jshint": "*", "mkdirp": "*", "mocha": "*", + "mock-fs": "^3.12.1", "nyc": "^8.3.2", "pre-commit": "^1.1.3", "uglify-js": "*" diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..8f56623 --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,30 @@ +'use strict'; + +var mockfs = require("mock-fs"); +var assert = require("assert") +var doT = require("../index"); +doT.log = false; + +describe('index', function() { + describe('process', function() { + it('should preserve filenames', function() { + mockfs({ + './mock-fs': { + 'foo.dot': '{{= bar }}', + 'foo.bar.dot': '{{= bar }}', + 'bar.def': 'included', + 'baz.jst': 'function(){}' + } + }); + + var compiled = doT.process({ + 'path': './mock-fs' + }); + + mockfs.restore(); + + assert.equal(typeof compiled.foo, 'function'); + assert.equal(typeof compiled['foo.bar'], 'function'); + }); + }); +});