add build setup
This commit is contained in:
parent
8d137c5e15
commit
eb5e78346e
5 changed files with 96 additions and 3 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
dist/
|
1
domutil.ts
Normal file
1
domutil.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from "./makeDOM";
|
|
@ -16,7 +16,7 @@ type WithClassOrId<T extends keyof HTMLElementTagNameMap> =
|
|||
| `${T}#${string}`
|
||||
| `${T}.${string}`;
|
||||
|
||||
export default function $el<T extends keyof HTMLElementTagNameMap>(
|
||||
export function $el<T extends keyof HTMLElementTagNameMap>(
|
||||
name: T | WithClassOrId<T>,
|
||||
...desc: [Partial<ElementProperties<T>>, ...DOMElem[]] | DOMElem[]
|
||||
): HTMLElementTagNameMap[T] {
|
||||
|
@ -45,7 +45,7 @@ export default function $el<T extends keyof HTMLElementTagNameMap>(
|
|||
const attributes = desc[0];
|
||||
if (typeof attributes === "object" && !(attributes instanceof Node)) {
|
||||
for (const attr in attributes) {
|
||||
const value = attributes[attr];
|
||||
const value = (attributes as Record<string, any>)[attr];
|
||||
if (attr.startsWith("@")) {
|
||||
el.addEventListener(attr.substring(1), value);
|
||||
} else if (attr === "dataset") {
|
||||
|
@ -53,7 +53,7 @@ export default function $el<T extends keyof HTMLElementTagNameMap>(
|
|||
el.dataset[key] = value[key];
|
||||
}
|
||||
} else {
|
||||
el[attr] = value;
|
||||
el[attr as keyof HTMLElementTagNameMap[T]] = value;
|
||||
}
|
||||
}
|
||||
desc.shift();
|
||||
|
@ -65,3 +65,5 @@ export default function $el<T extends keyof HTMLElementTagNameMap>(
|
|||
|
||||
return el;
|
||||
}
|
||||
|
||||
export default $el;
|
||||
|
|
68
scripts/build.ts
Normal file
68
scripts/build.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
import { build, stop } from "https://deno.land/x/esbuild@v0.19.2/mod.js";
|
||||
import ts from "npm:typescript";
|
||||
|
||||
// from https://github.com/Microsoft/TypeScript/issues/6387#issuecomment-169739615
|
||||
function error(diagnostics: ts.Diagnostic[]): void {
|
||||
diagnostics.forEach((diagnostic) => {
|
||||
let message = "Error";
|
||||
if (diagnostic.file) {
|
||||
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
|
||||
diagnostic.start || 0
|
||||
);
|
||||
message += ` ${diagnostic.file.fileName} (${line + 1},${character + 1})`;
|
||||
}
|
||||
message +=
|
||||
": " + ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
||||
console.log(message);
|
||||
});
|
||||
}
|
||||
|
||||
async function readConfigFile(configFileName: string) {
|
||||
const configFileText = await Deno.readTextFile(configFileName);
|
||||
const result = ts.parseConfigFileTextToJson(configFileName, configFileText);
|
||||
if (result.error) {
|
||||
error([result.error]);
|
||||
Deno.exit(1);
|
||||
}
|
||||
|
||||
const config = ts.parseJsonConfigFileContent(
|
||||
result.config,
|
||||
ts.sys,
|
||||
await Deno.realPath(".")
|
||||
);
|
||||
if (config.errors.length > 0) {
|
||||
error(config.errors);
|
||||
Deno.exit(1);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
async function buildDTS() {
|
||||
const config = await readConfigFile("./tsconfig.json");
|
||||
const emitted = ts
|
||||
.createProgram(config.fileNames, {
|
||||
...config.options,
|
||||
emitDeclarationOnly: true,
|
||||
})
|
||||
.emit();
|
||||
if (emitted.diagnostics.length > 0) {
|
||||
error(emitted.diagnostics.slice());
|
||||
Deno.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async function buildESM() {
|
||||
await build({
|
||||
bundle: true,
|
||||
minify: true,
|
||||
keepNames: true,
|
||||
target: "es2017", // Required because OBS ships with a really old version of Chrome
|
||||
entryPoints: ["./domutil.ts"],
|
||||
outfile: "./dist/domutil.js",
|
||||
format: "esm",
|
||||
sourcemap: true,
|
||||
});
|
||||
await stop();
|
||||
}
|
||||
|
||||
await Promise.all([buildDTS(), buildESM()]);
|
21
tsconfig.json
Normal file
21
tsconfig.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"module": "ES2020",
|
||||
"moduleResolution": "node",
|
||||
"declaration": true,
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictBindCallApply": true,
|
||||
"strictPropertyInitialization": true,
|
||||
"noImplicitThis": true,
|
||||
"alwaysStrict": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"outDir": "./dist",
|
||||
"lib": ["ES2017", "dom"]
|
||||
},
|
||||
"exclude": ["dist", "scripts"],
|
||||
}
|
Loading…
Reference in a new issue