Skip to content

Commit 977e2b4

Browse files
committed
Single File Components and Tests
1 parent e917c0f commit 977e2b4

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

sfc/loader.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,24 @@ class UComponent extends HTMLElement {
9696

9797

9898
window.loadComponent = (function() {
99-
console.debug('LOADER', `loadComponent...`);
99+
// plain script includes can read document.currentScript.src
100+
// modules can use meta
101+
const loaderURL = new URL(document.currentScript.src);
100102

101-
async function fetchSFC(tagName, folder = '') {
102-
let def;
103+
console.debug('SFC', `loadComponent...`);
103104

104-
if (folder === undefined) folder = '';
105-
if (folder.length > 0 && (!folder.endsWith('/'))) folder += '/';
106-
const url = folder + tagName + '.vue';
107-
console.debug('SFC', `registerComponent(${tagName}, ${url})`);
105+
async function fetchSFC(tagName, folder = undefined) {
106+
let def;
107+
108+
const sfcImpl = tagName + '.vue';
109+
let baseUrl = loaderURL;
110+
111+
if (folder) {
112+
baseUrl = new URL(folder, document.location.href);
113+
}
114+
115+
const url = new URL(sfcImpl, baseUrl);
116+
console.debug('SFC', `loading module ${tagName} from ${url.href}...`);
108117

109118
// get DOM from sfc-file
110119
const dom = await fetch(url)

sfc/u-markdown.vue

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ the markdown-it.js is required to be loaded. Ir can be found in https://markdown
2323

2424
Open Topics:
2525

26-
The foldername /sfc/ is still hardcoded. It should be replaced by a variable / placeholder.
26+
The foldername /sfc/ is hardcoded. It should be replaced by a variable / placeholder.
2727

2828
References:
2929
* https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements
3030
* https://zerodevx.github.io/zero-md/
3131
* requestIdleCallback, SetTimeout VS RequestAnimationFrame
3232

33-
34-
3533
-->
3634

3735

test/test-md.htm

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ <h2>MD</h2>
6060
<u-markdown src="./test-md1.md" style=" background-color: azure; "></u-markdown>
6161

6262
<script>
63-
loadComponent('u-markdown', '/sfc/').then(() => {
63+
loadComponent('u-markdown').then(() => {
6464
console.log('Page', "all components loaded.");
6565
});
6666

0 commit comments

Comments
 (0)