Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pug file extensions in html-resource-plugin #385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 22 additions & 17 deletions src/html-resource-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import {ViewEngine} from 'aurelia-templating';
import {_createDynamicElement} from './dynamic-element';

const VIEW_EXTENSIONS = ['html', 'jade', 'pug'];
const VIEW_REGEXP = new RegExp('([^\\/^\\?]+)\\.(' + VIEW_EXTENSIONS.join('|') + ')', 'i');

export function getElementName(address) {
return /([^\/^\?]+)\.html/i.exec(address)[1].toLowerCase();
return VIEW_REGEXP.exec(address)[1].toLowerCase();
}

export function configure(config) {
const viewEngine = config.container.get(ViewEngine);
const loader = config.aurelia.loader;

viewEngine.addResourcePlugin('.html', {
'fetch': function(viewUrl) {
return loader.loadTemplate(viewUrl).then(registryEntry => {
let bindableNames = registryEntry.template.getAttribute('bindable');
const useShadowDOMmode: null | '' | 'open' | 'closed' = registryEntry.template.getAttribute('use-shadow-dom');
const name = getElementName(viewUrl);
const fetch = function(viewUrl) {
return loader.loadTemplate(viewUrl).then(registryEntry => {
let bindableNames = registryEntry.template.getAttribute('bindable');
const useShadowDOMmode: null | '' | 'open' | 'closed' = registryEntry.template.getAttribute('use-shadow-dom');
const name = getElementName(viewUrl);

if (bindableNames) {
bindableNames = bindableNames.split(',').map(x => x.trim());
registryEntry.template.removeAttribute('bindable');
} else {
bindableNames = [];
}

if (bindableNames) {
bindableNames = bindableNames.split(',').map(x => x.trim());
registryEntry.template.removeAttribute('bindable');
} else {
bindableNames = [];
}
return { [name]: _createDynamicElement({name, viewUrl, bindableNames, useShadowDOMmode}) };
});
};

return { [name]: _createDynamicElement({name, viewUrl, bindableNames, useShadowDOMmode}) };
});
}
});
for (const ext of VIEW_EXTENSIONS) {
viewEngine.addResourcePlugin('.' + ext, {fetch});
}
}