Skip to content

Commit

Permalink
Setup jsconfig.json to check TypeScript against js files.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Sep 16, 2019
1 parent 8eacf1e commit 1bf3213
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
11 changes: 3 additions & 8 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion examples/docs/index.js
@@ -1,9 +1,11 @@
import "elm-oembed";
import "./lib/code-editor.js";
import "./style.css";
// @ts-ignore
const { Elm } = require("./src/Main.elm");
// const pagesInit = require("elm-pages");
const pagesInit = require("../../index.js");
// @ts-ignore
const pagesInit = require("../../index.js").default;

pagesInit({
mainElmModule: Elm.Main
Expand Down
46 changes: 34 additions & 12 deletions index.js
@@ -1,14 +1,19 @@
// @ts-ignore
const elmPagesVersion = require("./package.json").version;

module.exports = function pagesInit({ mainElmModule }) {
module.exports = function pagesInit(
/** @type { { mainElmModule: { init: any } } } */ { mainElmModule }
) {
let prefetchedPages = [window.location.pathname];

document.addEventListener("DOMContentLoaded", function() {
let app = mainElmModule.init({
flags: {}
});

app.ports.toJsPort.subscribe(payload => {
app.ports.toJsPort.subscribe((
/** @type { HeadTag[] } headTags */ headTags
) => {
appendTag({
name: "meta",
attributes: [
Expand All @@ -21,6 +26,11 @@ module.exports = function pagesInit({ mainElmModule }) {
appendTag(headTag);
});
} else {
console.log("headTags", headTags);

headTags.forEach(headTag => {
appendTag(headTag);
});
setupLinkPrefetching();
}

Expand All @@ -36,7 +46,10 @@ module.exports = function pagesInit({ mainElmModule }) {
});
}

function observeFirstRender(mutationList, firstRenderObserver) {
function observeFirstRender(
/** @type {MutationRecord[]} */ mutationList,
/** @type {MutationObserver} */ firstRenderObserver
) {
for (let mutation of mutationList) {
if (mutation.type === "childList") {
console.log(
Expand All @@ -53,7 +66,10 @@ module.exports = function pagesInit({ mainElmModule }) {
subtree: false
});
}
function observeUrlChanges(mutationList, theObserver) {
function observeUrlChanges(
/** @type {MutationRecord[]} */ mutationList,
/** @type {MutationObserver} */ _theObserver
) {
for (let mutation of mutationList) {
if (mutation.type === "attributes") {
console.log(
Expand All @@ -65,14 +81,19 @@ module.exports = function pagesInit({ mainElmModule }) {
}
}

function setupLinkPrefetchingHelp(mutationList, theObserver) {
function setupLinkPrefetchingHelp(
/** @type {MutationObserver} */ _mutationList,
/** @type {MutationObserver} */ _theObserver
) {
const links = document.querySelectorAll("a");
window.links = links;
console.log("LINKS");
links.forEach(link => {
console.log(link.pathname);
// console.log(link.pathname);
link.addEventListener("mouseenter", function(event) {
if (event && event.target) {
if (
event &&
event.target &&
event.target instanceof HTMLAnchorElement
) {
prefetchIfNeeded(event.target);
} else {
console.log("Couldn't prefetch with event", event);
Expand All @@ -81,10 +102,10 @@ module.exports = function pagesInit({ mainElmModule }) {
});
}

function prefetchIfNeeded(target) {
function prefetchIfNeeded(/** @type {HTMLAnchorElement} */ target) {
if (target.host === window.location.host) {
if (prefetchedPages.includes(target.pathname)) {
console.log("Already preloaded", event.target.href);
console.log("Already preloaded", target.href);
} else {
prefetchedPages.push(target.pathname);
console.log("Preloading...", target.pathname);
Expand All @@ -96,7 +117,8 @@ module.exports = function pagesInit({ mainElmModule }) {
}
}

function appendTag(tagDetails) {
/** @typedef {{ name: string; attributes: string[][]; }} HeadTag */
function appendTag(/** @type {HeadTag} */ tagDetails) {
const meta = document.createElement(tagDetails.name);
tagDetails.attributes.forEach(([name, value]) => {
meta.setAttribute(name, value);
Expand Down
12 changes: 12 additions & 0 deletions jsconfig.json
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"strictNullChecks": true,
"sourceMap": false,
"resolveJsonModule": true,
"lib": ["es5", "es2015.promise", "dom", "es2015"],
"checkJs": true
}
}

0 comments on commit 1bf3213

Please sign in to comment.