-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from moonbitlang/zhiyuan/esbuild
Zhiyuan/esbuild
- Loading branch information
Showing
17 changed files
with
806 additions
and
2,506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as chokidar from "chokidar"; | ||
import * as esbuild from "esbuild"; | ||
import { tailwindPlugin } from "esbuild-plugin-tailwindcss"; | ||
import * as fs from "fs/promises"; | ||
import * as path from "path"; | ||
import * as page from "./page"; | ||
|
||
const isDev = process.env["DEV"] === "true"; | ||
const isWatch = process.argv.includes("--watch"); | ||
const isBuild = process.argv.includes("--build"); | ||
|
||
const plugin = (): esbuild.Plugin => { | ||
return { | ||
name: "my-plugin", | ||
setup(build) { | ||
build.onStart(async () => { | ||
console.log("start build"); | ||
build.initialOptions.outdir && | ||
(await fs.rm(build.initialOptions.outdir, { | ||
recursive: true, | ||
force: true, | ||
})); | ||
}); | ||
build.onEnd(async () => { | ||
console.log("end build"); | ||
|
||
const names = ["lsp-server.js", "moonc-worker.js", "onig.wasm"]; | ||
await Promise.all( | ||
names.map((name) => { | ||
fs.copyFile( | ||
`./node_modules/@moonbit/moonpad-monaco/dist/${name}`, | ||
`./dist/${name}`, | ||
); | ||
}), | ||
); | ||
|
||
const template = await fs.readFile("index.html", "utf8"); | ||
|
||
const pages = await page.collectTourPage(); | ||
await Promise.all( | ||
pages.map(async (p) => { | ||
const content = page.render(template, p); | ||
await fs.mkdir(`./dist/${path.dirname(p.path)}`, { | ||
recursive: true, | ||
}); | ||
await fs.writeFile(`./dist/${p.path}`, content, { | ||
encoding: "utf8", | ||
}); | ||
}), | ||
); | ||
}); | ||
}, | ||
}; | ||
}; | ||
|
||
const ctx = await esbuild.context({ | ||
entryPoints: [ | ||
"./src/main.ts", | ||
"./node_modules/monaco-editor-core/esm/vs/editor/editor.worker.js", | ||
], | ||
bundle: true, | ||
minify: !isDev, | ||
format: "esm", | ||
outdir: "./dist", | ||
entryNames: "[name]", | ||
loader: { | ||
".ttf": "file", | ||
".woff2": "file", | ||
}, | ||
logLevel: "info", | ||
plugins: [tailwindPlugin(), plugin()], | ||
}); | ||
|
||
await ctx.rebuild(); | ||
|
||
if (isBuild) { | ||
process.exit(0); | ||
} | ||
|
||
if (isDev) { | ||
await ctx.serve({ | ||
servedir: "dist", | ||
}); | ||
} | ||
|
||
if (isWatch) { | ||
chokidar | ||
.watch(["index.html", "src", "tour"], { | ||
ignoreInitial: true, | ||
}) | ||
.on("all", async (e, path) => { | ||
console.log(`[watch] ${e} ${path}`); | ||
await ctx.rebuild(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as fs from "fs/promises"; | ||
import * as remark from "./remark"; | ||
import * as scan from "./scan-tour"; | ||
import * as shiki from "./shiki"; | ||
|
||
type Page = { | ||
title: string; | ||
markdown: string; | ||
code: string; | ||
back: string; | ||
next: string; | ||
path: string; | ||
}; | ||
|
||
export async function collectTourPage(): Promise<Page[]> { | ||
const pages: Page[] = []; | ||
const chapters = await scan.scanTour(); | ||
const lessons = chapters.flatMap((c) => c.lessons); | ||
|
||
const indexMd = await fs.readFile("tour/index.md", "utf8"); | ||
const indexMbt = await fs.readFile("tour/index.mbt", "utf8"); | ||
pages.push({ | ||
title: "MoonBit Language Tour", | ||
markdown: indexMd, | ||
code: indexMbt, | ||
back: `<span class="text-zinc-500">Back</span>`, | ||
next: `<a href="/${scan.slug(lessons[0])}/index.html">Next</a>`, | ||
path: "index.html", | ||
}); | ||
|
||
for (const [i, l] of lessons.entries()) { | ||
const p: Page = { | ||
title: `${l.lesson} - MoonBit Language Tour`, | ||
markdown: l.markdown, | ||
code: l.code, | ||
back: | ||
i === 0 | ||
? `<a href="/index.html">Back</a>` | ||
: `<a href="/${scan.slug(lessons[i - 1])}/index.html">Back</a>`, | ||
next: | ||
i === lessons.length - 1 | ||
? `<span class="text-zinc-500">Next</span>` | ||
: `<a href="/${scan.slug(lessons[i + 1])}/index.html">Next</a>`, | ||
path: `${scan.slug(l)}/index.html`, | ||
}; | ||
pages.push(p); | ||
} | ||
|
||
const toc = scan.generateTOC(chapters); | ||
pages.push({ | ||
title: `Table of Contents - MoonBit Language Tour`, | ||
markdown: toc.markdown, | ||
code: toc.code, | ||
back: `<span class="text-zinc-500">Back</span>`, | ||
next: `<span class="text-zinc-500">Next</span>`, | ||
path: "table-of-contents/index.html", | ||
}); | ||
return pages; | ||
} | ||
|
||
export function render(template: string, page: Page): string { | ||
return template | ||
.replace("%TITLE%", page.title) | ||
.replace("%MARKDOWN%", remark.mdToHtml(page.markdown)) | ||
.replace("%CODE%", shiki.renderMoonBitCode(page.code)) | ||
.replace("%BACK%", page.back) | ||
.replace("%NEXT%", page.next); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "*.css" {} |
Oops, something went wrong.