Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MML esbuild plugin #54

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Add MML esbuild plugin #54

wants to merge 11 commits into from

Conversation

deej-io
Copy link

@deej-io deej-io commented Aug 1, 2024

  • add Nix flake
  • add build system for local development
  • create combined build result and optionally clean up JS files
  • add comments MML plugin sections
  • Add file renamer and clean up empty directories

This PR adds an MML plugin that builds a React project and embeds the result into HTML files.

It uses a custom esbuild loader to ensure any references files are copied to the build directory, optionally re-writes the names, and then updates the import statements to contain a string for the document URL.

What kind of change does your PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Refactor
  • Tests
  • Other, please describe:

Does your PR fulfill the following requirements?

  • The title references the corresponding issue # (if relevant)

@deej-io deej-io requested a review from MarcusLongmuir August 1, 2024 13:00
@deej-io deej-io force-pushed the deej/mml-dev-tools branch from 4ac6b52 to c4f8d1c Compare August 1, 2024 13:02
Comment on lines +90 to +92
"import/no-unresolved": {
ignore: ['^mml:']
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just does not work for some reason... Which is why I've had to add the /*eslint ... */ directive at the top of the type script files that import MML documents...

@deej-io deej-io force-pushed the deej/mml-dev-tools branch from 6c7c5cd to 6732c30 Compare August 5, 2024 14:21
@@ -1,20 +1,249 @@
import lockfile from "proper-lockfile";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no default export lockfile in proper-lockfile.

This should probably be imported as

import { lockSync, unlockSync } from "proper-lockfile";

with the necessary adjustments where such methods are used.

build.initialOptions.metafile = true;
const outdir = resolve(__dirname, build.initialOptions.outdir ?? "build");

build.onStart(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async arrow func with no await inside (no methods returning promises in the block)

},
entryPoints: ["./src/playground/index.tsx"],
outdir,
outbase: "src",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should force

treeShaking: true,
minify: true,

in the buildOptions (reduces the built duck example from 1MB to 143KB.

@@ -28,7 +257,8 @@ const mode = args[0];

switch (mode) {
case buildMode:
esbuild.build(buildOptions).catch(() => process.exit(1));
esbuild
.build(buildOptions).catch(() => process.exit(1));
break;
case watchMode:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch mode will detect changes only if index.tsx is changed, but not when tsx MML documents are modified (like src/duck/index.tsx for example. We should probably monitor changes for those too, to re-trigger the build process.

if (!(htmlPath in outputs)) {
delete outputs[jsPath]
const js = await fsp.readFile(jsPath, { encoding: "utf8" });
const html = `<body></body><script>${js}</script>`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably brush off a few more bytes by removing the multi-line comments that the React build leaves in the built file

const cleanedJs = js.replace(/\/\*[\s\S]*?\*\//g, "");
const html = `<body></body><script>${cleanedJs}</script>`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants