Set up a build environment for typescript

This commit is contained in:
Savanni D'Gerinel 2023-04-24 19:39:54 -04:00
parent 83e8fc9be9
commit 50af58c993
8 changed files with 1640 additions and 53 deletions

View File

@ -1,51 +0,0 @@
import init, { CoreApp } from "./kifu_wasm.js";
const inputField = document.getElementById('input-temp');
const fromUnitField = document.getElementById('input-unit');
const toUnitField = document.getElementById('output-unit');
const outputField = document.getElementById('output-temp');
const form = document.getElementById('converter');
function convertTemp(value, fromUnit, toUnit) {
if (fromUnit === 'c') {
if (toUnit === 'f') {
return value * 9 / 5 + 32;
} else if (toUnit === 'k') {
return value + 273.15;
}
return value
}
if (fromUnit === 'f') {
if (toUnit === 'c') {
return (value - 32) * 5 / 9;
} else if (toUnit === 'k') {
return (value + 459.67) * 5 / 9;
}
return value;
}
if (fromUnit === 'k') {
if (toUnit === 'c') {
return value - 273.15;
} else if (toUnit === 'f') {
return value * 9 / 5 - 459.67;
}
return value;
}
throw new Error('Invalid unit');
}
form.addEventListener('input', () => {
const inputTemp = parseFloat(inputField.value);
const fromUnit = fromUnitField.value;
const toUnit = toUnitField.value;
const outputTemp = convertTemp(inputTemp, fromUnit, toUnit);
outputField.value = (Math.round(outputTemp * 100) / 100) + ' ' + toUnit.toUpperCase();
});
init().then(async () => {
let app = new CoreApp();
console.log("app: ", app, CoreApp);
await app.dispatch({ type: "PlayingField" });
console.log("kifu_wasm successfully initted");
});

View File

@ -5,7 +5,7 @@
<link rel="manifest" href="/manifest.json">
<link rel="stylesheet" href="converter.css">
<title>Temperature converter</title>
<script type="module" src="converter.js"></script>
<script type="module" src="/dist/app.js"></script>
</head>
<body>
<h1> Temperature Converter </h1>
@ -28,7 +28,7 @@
</form>
<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js', { scope: '/' });
navigator.serviceWorker.register('/dist/sw.js', { scope: '/' });
}
</script>
</body>

1549
kifu/kifu-pwa/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
{
"devDependencies": {
"@types/lodash": "^4.14.194",
"ts-loader": "^9.4.2",
"typescript": "^5.0.4",
"webpack": "^5.80.0",
"webpack-cli": "^5.0.2"
},
"dependencies": {
"kifu-wasm": "file:../ffi/kifu-wasm/pkg",
"lodash": "^4.17.21"
}
}

View File

@ -0,0 +1,52 @@
import init, { CoreApp } from "kifu-wasm";
const inputField = document.getElementById("input-temp") as HTMLInputElement;
const fromUnitField = document.getElementById("input-unit") as HTMLInputElement;
const toUnitField = document.getElementById("output-unit") as HTMLInputElement;
const outputField = document.getElementById("output-temp") as HTMLInputElement;
const form = document.getElementById("converter") as HTMLFormElement;
function convertTemp(value: number, fromUnit: string, toUnit: string) {
if (fromUnit === "c") {
if (toUnit === "f") {
return (value * 9) / 5 + 32;
} else if (toUnit === "k") {
return value + 273.15;
}
return value;
}
if (fromUnit === "f") {
if (toUnit === "c") {
return ((value - 32) * 5) / 9;
} else if (toUnit === "k") {
return ((value + 459.67) * 5) / 9;
}
return value;
}
if (fromUnit === "k") {
if (toUnit === "c") {
return value - 273.15;
} else if (toUnit === "f") {
return (value * 9) / 5 - 459.67;
}
return value;
}
throw new Error("Invalid unit");
}
form?.addEventListener("input", () => {
const inputTemp = parseFloat(inputField.value);
const fromUnit = fromUnitField.value;
const toUnit = toUnitField.value;
const outputTemp = convertTemp(inputTemp, fromUnit, toUnit);
outputField.value =
Math.round(outputTemp * 100) / 100 + " " + toUnit.toUpperCase();
});
init().then(async () => {
let app = new CoreApp();
console.log("app: ", app, CoreApp);
await app.dispatch({ type: "PlayingField" });
console.log("kifu_wasm successfully initted");
});

View File

@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"skipLibCheck": true
}
}

View File

@ -0,0 +1,13 @@
module.exports = {
mode: 'development',
entry: {
app: './src/converter.ts',
sw: './src/sw.js',
},
module: {
rules: [
{ test: /.ts$/, use: 'ts-loader', exclude: /node_modules/ },
{ test: /\.wasm$/, type: 'asset/inline' }
],
},
};