Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Async from 'neo-async';
import fs from 'fs';
import Handlebars from './handlebars.js';
import { basename } from 'path';
import { basename, sep } from 'path';
import { SourceMapConsumer, SourceNode } from 'source-map';

export function loadTemplates(opts, callback) {
Expand Down Expand Up @@ -126,8 +126,23 @@ function loadFiles(opts, callback) {
let name = path;
if (!root) {
name = basename(name);
} else if (name.indexOf(root) === 0) {
name = name.substring(root.length + 1);
} else {
const normalizedName = name.split(sep).join('/');
const normalizedRoot = root.split(sep).join('/');
if (
normalizedName.indexOf(
normalizedRoot.endsWith('/')
? normalizedRoot
: normalizedRoot + '/'
) === 0
) {
name = normalizedName.substring(
normalizedRoot.length +
(normalizedName.charAt(normalizedRoot.length) === '/'
? 1
: 0)
);
}
}
name = name.replace(extension, '');

Expand Down
87 changes: 87 additions & 0 deletions spec/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,93 @@ describe('precompiler', function () {
expect(opts.templates[0].source).toBe('a');
});

it.skipIf(path.sep !== '\\')(
'should strip native Windows roots from explicit and enumerated paths',
async function () {
const directory = path.join(__dirname, 'artifacts');
for (const root of [directory, directory + path.sep]) {
const direct = await loadTemplatesAsync({
files: [path.join(directory, 'empty.handlebars')],
root,
});
expect(direct.templates[0].name).toBe('empty');
const enumerated = await loadTemplatesAsync({
files: [directory],
root,
});
expect(
enumerated.templates.map((template) => template.name)
).toContain('empty');
}
}
);

it.skipIf(path.sep !== '\\')(
'should normalize native Windows names but preserve root boundaries',
async function () {
const filename = path.join(__dirname, 'artifacts', 'empty.handlebars');
for (const root of [__dirname, __dirname + path.sep, __dirname + '/']) {
const opts = await loadTemplatesAsync({ files: [filename], root });
expect(opts.templates[0].name).toBe('artifacts/empty');
expect(opts.templates[0].path).toBe(filename);
}
const opts = await loadTemplatesAsync({
files: [filename],
root: path.join(__dirname, 'art'),
});
expect(opts.templates[0].name).toBe(
filename.replace(/\.handlebars$/, '')
);
}
);

it('should preserve template names with a trailing root separator', async function () {
for (const root of [
__dirname + '/artifacts',
__dirname + '/artifacts/',
]) {
var opts = await loadTemplatesAsync({
files: [__dirname + '/artifacts/empty.handlebars'],
root: root,
});
expect(opts.templates[0].name).toBe('empty');
}
});

it('should preserve nested template names with a trailing root separator', async function () {
for (const root of [__dirname, __dirname + '/']) {
var opts = await loadTemplatesAsync({
files: [__dirname + '/artifacts/empty.handlebars'],
root: root,
});
expect(opts.templates[0].name).toBe('artifacts/empty');
}
});

it('should enumerate directories with a trailing root separator', async function () {
for (const directory of [
__dirname + '/artifacts',
__dirname + '/artifacts/',
]) {
var opts = await loadTemplatesAsync({
files: [directory],
extension: 'hbs',
});
expect(opts.templates.map((template) => template.name)).toEqual([
'example_2',
'non.default.extension',
]);
}
});

it('should not strip a root that only matches part of a directory name', async function () {
var opts = await loadTemplatesAsync({
files: [__dirname + '/artifacts/empty.handlebars'],
root: __dirname + '/art',
});
expect(opts.templates[0].name).toBe(__dirname + '/artifacts/empty');
});

it('should handle different root', async function () {
var opts = await loadTemplatesAsync({
files: [__dirname + '/artifacts/empty.handlebars'],
Expand Down