feat: add hydrateInit compiler option

This commit is contained in:
Dylan Piercey 2023-08-09 11:36:04 -07:00 committed by Dylan Piercey
parent 17099cd8ff
commit 9aede281f9
5 changed files with 38 additions and 12 deletions

View File

@ -0,0 +1,6 @@
---
"@marko/translator-default": minor
"marko": minor
---
Add compiler option to disable initializing components when outputting hydrate code.

View File

@ -22,6 +22,7 @@ declare const Config: {
) => string)
| null;
hydrateIncludeImports?: RegExp | ((request: string) => boolean);
hydrateInit?: boolean;
optimize?: boolean;
cache?: Map<unknown, unknown>;
hot?: boolean;

View File

@ -136,6 +136,12 @@ const config = {
hydrateIncludeImports:
/\.(css|less|s[ac]ss|styl|png|jpe?g|gif|svg|ico|webp|avif|mp4|webm|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf)$/,
/**
* When compiling in hydrate mode, this option will cause the compiler to
* call the `marko/components.init` function to begin hydrating components.
*/
hydrateInit: true,
/**
* Set to true in order to bring in the hot module replacement runtime.
*/

View File

@ -296,6 +296,12 @@ import "./baz.wasm";
For `hydrate` output, with the default `hydrateIncludeImports`, would only cause `./foo.css` to be loaded in the browser.
#### `hydrateInit`
This option is only used for `output: "hydrate"`. It defaults to `true` and causes the hydrate output to include code which tells the Marko runtime to begin hydrating any registered components.
Setting to false will disable that `init` call and allow you to generate code which _just_ imports any hydrate dependencies for a template.
#### `cache`
Type: typeof [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) (specifically, `.get()` is required)<br>

View File

@ -9,7 +9,7 @@ import {
} from "@marko/babel-utils";
export default (entryFile, isHydrate) => {
const { resolveVirtualDependency, hydrateIncludeImports } =
const { resolveVirtualDependency, hydrateIncludeImports, hydrateInit } =
entryFile.markoOpts;
const hydratedFiles = new Set();
const program = entryFile.path;
@ -39,19 +39,26 @@ export default (entryFile, isHydrate) => {
t.importSpecifier(t.identifier("register"), t.identifier("register"))
);
}
markoComponentsImport.specifiers.push(t.importSpecifier(initId, initId));
if (hydrateInit) {
markoComponentsImport.specifiers.push(t.importSpecifier(initId, initId));
}
program.unshiftContainer("body", markoComponentsImport);
program.pushContainer(
"body",
t.expressionStatement(
t.callExpression(
initId,
entryFile.markoOpts.runtimeId
? [t.stringLiteral(entryFile.markoOpts.runtimeId)]
: []
if (hydrateInit) {
program.pushContainer(
"body",
t.expressionStatement(
t.callExpression(
initId,
entryFile.markoOpts.runtimeId
? [t.stringLiteral(entryFile.markoOpts.runtimeId)]
: []
)
)
)
);
);
}
}
function addHydrateDeps(file) {