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

feat: shorter registry ids in production builds #166

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/four-planes-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Enable optimizeKnownTemplates config for Marko compiler to have shorter registry ids in production builds.
44 changes: 30 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as compiler from "@marko/compiler";
import fs from "fs";
import path from "path";
import crypto from "crypto";
import glob from "fast-glob";
import anyMatch from "anymatch";
import { pathToFileURL } from "url";

Expand Down Expand Up @@ -48,7 +49,6 @@ interface BrowserManifest {
}

interface ServerManifest {
// optimizedRegistryIds?: { [id: string]: string };
entries: {
[entryId: string]: string;
};
Expand Down Expand Up @@ -101,7 +101,7 @@ const babelCaller = {
supportsTopLevelAwait: true,
supportsExportNamespaceFrom: true,
};
// const optimizedRegistryIds: Map<string, string> = new Map();
const optimizeKnownTemplatesForRoot = new Map<string, string[]>();
let registeredTagLib = false;

// This package has a dependency on @parcel/source-map which uses native addons.
Expand Down Expand Up @@ -187,14 +187,15 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
config.base = process.env.BASE_URL;
}

root = normalizePath(config.root || process.cwd());
baseConfig = {
cache,
optimize,
// optimizedRegistryIds:
// optimize && linked ? optimizedRegistryIds : undefined,
runtimeId,
sourceMaps: true,
writeVersionComment: false,
optimizeKnownTemplates:
optimize && linked ? await getKnownTemplates(root) : undefined,
babelConfig: opts.babelConfig
? {
...opts.babelConfig,
Expand Down Expand Up @@ -240,7 +241,6 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
};

compiler.configure(baseConfig);
root = normalizePath(config.root || process.cwd());
devEntryFile = path.join(root, "index.html");
devEntryFilePosix = normalizePath(devEntryFile);
isTest = env.mode === "test";
Expand Down Expand Up @@ -449,6 +449,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
handleHotUpdate(ctx) {
compiler.taglib.clearCaches();
baseConfig.cache!.clear();
optimizeKnownTemplatesForRoot.clear();

for (const [, cache] of configsByFileSystem) {
cache.clear();
Expand All @@ -465,15 +466,6 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
if (isBuild && linked && !isSSRBuild) {
try {
serverManifest = await store.read();
// if (serverManifest.optimizedRegistryIds) {
// for (const id in serverManifest.optimizedRegistryIds) {
// optimizedRegistryIds.set(
// id,
// serverManifest.optimizedRegistryIds[id],
// );
// }
// }

inputOptions.input = toHTMLEntries(root, serverManifest.entries);
for (const entry in serverManifest.entrySources) {
const id = normalizePath(path.resolve(root, entry));
Expand Down Expand Up @@ -983,3 +975,27 @@ function getConfigForFileSystem(

return configForFileSystem;
}

function getKnownTemplates(root: string) {
let knownTemplates = optimizeKnownTemplatesForRoot.get(root);
if (!knownTemplates) {
optimizeKnownTemplatesForRoot.set(
root,
(knownTemplates = glob.globSync("**/*.marko", {
absolute: true,
cwd: root,
ignore: [
"**/*test*/**",
"**/*example*/**",
"**/*stories*/**",
"**/*coverage*/**",
"**/*snapshots*/**",
"**/node_modules/**",
"**/*.d.marko",
],
})),
);
}

return knownTemplates;
}
Loading