Skip to content

Commit

Permalink
Convert to TypeScript/ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Nov 19, 2023
1 parent 6a660a7 commit d5e1910
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 84 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@

.makefiles/%:
@curl -sfL https://makefiles.dev/v1 | bash /dev/stdin "$@"

################################################################################

artifacts/dist: tsconfig.build.json artifacts/link-dependencies.touch $(JS_SOURCE_FILES)
@rm -rf "$@"
$(JS_EXEC) tsc -p "$<"
@touch "$@"
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@
"publishConfig": {
"access": "public"
},
"types": "src/main.d.ts",
"main": "src/main.js",
"type": "module",
"main": "artifacts/dist/index.js",
"types": "artifacts/dist/index.d.ts",
"exports": {
".": {
"types": "./src/main.d.ts",
"default": "./src/main.js"
},
"./sandbox": {
"types": "./src/sandbox.d.ts",
"default": "./src/sandbox.js"
"types": "./artifacts/dist/index.d.ts",
"import": "./artifacts/dist/index.js",
"default": "./artifacts/dist/index.js"
}
},
"sideEffects": false,
"files": [
"/src/"
"/artifacts/dist/"
],
"engines": {
"node": ">=14"
},
"scripts": {
"prepublishOnly": "make artifacts/dist"
},
"dependencies": {
"@docusaurus/core": "^3.0.0",
"@docusaurus/module-type-aliases": "^3.0.0",
Expand Down
37 changes: 22 additions & 15 deletions src/main.js → src/create-config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
const { readFileSync } = require("fs");
const { resolve } = require("path");
import { Config } from "@docusaurus/types";
import { ThemeConfig } from "@docusaurus/preset-classic";
import { Options } from "@docusaurus/plugin-client-redirects";
import { readFileSync } from "fs";
import { resolve } from "path";

module.exports = {
createConfig,
};
type FooterLinks = NonNullable<NonNullable<ThemeConfig["footer"]>["links"]>;
type NavbarItems = NonNullable<NonNullable<ThemeConfig["navbar"]>["items"]>;
type Redirects = Options["redirects"];

/** @type {import('@docusaurus/types').DocusaurusConfig} */
function createConfig(options) {
const {
footerLinks = [],
navbarItems = [],
redirects = [],
rootPath,
title,
} = options;
export function createConfig({
footerLinks = [],
navbarItems = [],
redirects = [],
rootPath,
title,
}: {
rootPath: string;
title: string;

footerLinks?: FooterLinks;
navbarItems?: NavbarItems;
redirects?: Redirects;
}): Config {
const { description, homepage, repository } = JSON.parse(
readFileSync(resolve(rootPath, "../package.json")),
readFileSync(resolve(rootPath, "../package.json")).toString(),
);

const orgUrl = new URL(homepage);
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "./theme.d.ts";

export { createConfig } from "./create-config.js";
29 changes: 0 additions & 29 deletions src/main.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/preset.js

This file was deleted.

1 change: 0 additions & 1 deletion src/sandbox.d.ts

This file was deleted.

6 changes: 1 addition & 5 deletions src/sandbox.js → src/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
module.exports = {
buildUrl,
};

function buildUrl(id) {
export function buildSandboxUrl(id: string): string {
const url = new URL("https://codesandbox.io/");
url.pathname = `/s/${id}`;

Expand Down
5 changes: 5 additions & 0 deletions src/theme.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type Sandbox from "./theme/Sandbox.js";

declare module "@theme/Sandbox" {
export default Sandbox;
}
15 changes: 0 additions & 15 deletions src/theme.js

This file was deleted.

6 changes: 4 additions & 2 deletions src/theme/LayoutHead.js → src/theme/LayoutHead.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Head from "@docusaurus/Head";
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import ThemeLayoutHead from "@theme-init/LayoutHead";
import React from "react";
import React, { ComponentProps } from "react";

export default function LayoutHead(props) {
type Props = ComponentProps<typeof ThemeLayoutHead>;

export default function LayoutHead(props: Props) {
const {
siteConfig: { url },
} = useDocusaurusContext();
Expand Down
11 changes: 10 additions & 1 deletion src/theme/Sandbox.js → src/theme/Sandbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import styles from "./Sandbox.module.css";
const sandboxProp =
"allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts";

export default function Sandbox(props) {
type Props = {
codeMirror?: boolean;
height: number | string;
hideDevTools?: boolean;
id: string;
inline?: boolean;
previewWindow?: "browser" | "console" | "tests";
};

export default function Sandbox(props: Props) {
const {
codeMirror = true,
height,
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "artifacts/dist"
},
"include": ["src/**/*"]
}
3 changes: 1 addition & 2 deletions src/tsconfig.json → tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "@snout/tsconfig",
"compilerOptions": {
"alwaysStrict": false,
"sourceMap": false
"jsx": "react"
}
}

0 comments on commit d5e1910

Please sign in to comment.