mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
feat: add hydrateInit compiler option
This commit is contained in:
parent
17099cd8ff
commit
9aede281f9
6
.changeset/hip-kings-tickle.md
Normal file
6
.changeset/hip-kings-tickle.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"@marko/translator-default": minor
|
||||
"marko": minor
|
||||
---
|
||||
|
||||
Add compiler option to disable initializing components when outputting hydrate code.
|
||||
1
packages/compiler/config.d.ts
vendored
1
packages/compiler/config.d.ts
vendored
@ -22,6 +22,7 @@ declare const Config: {
|
||||
) => string)
|
||||
| null;
|
||||
hydrateIncludeImports?: RegExp | ((request: string) => boolean);
|
||||
hydrateInit?: boolean;
|
||||
optimize?: boolean;
|
||||
cache?: Map<unknown, unknown>;
|
||||
hot?: boolean;
|
||||
|
||||
@ -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.
|
||||
*/
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user