@storyblok/astro 4.1.0-alpha.2 → 4.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -1
- package/dist/storyblok-astro.js +9 -9
- package/dist/storyblok-astro.mjs +441 -429
- package/dist/types/utils/generateFinalBridgeObject.d.ts +3 -8
- package/dist/types/utils/parseAstCode.d.ts +7 -1
- package/dist/types/vite-plugins/vite-plugin-storyblok-bridge.d.ts +10 -0
- package/live-preview/handleStoryblokMessage.ts +2 -1
- package/package.json +1 -1
- package/utils/generateFinalBridgeObject.ts +11 -20
- package/utils/parseAstCode.ts +41 -18
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
apiOptions?: ISbStoriesParams;
|
|
5
|
-
bridgeOptions?: StoryblokBridgeConfigV2;
|
|
6
|
-
};
|
|
7
|
-
}
|
|
8
|
-
export declare function generateFinalBridgeObject(rawCode: RawCodeItem[]): StoryblokBridgeConfigV2;
|
|
1
|
+
import type { StoryblokBridgeConfigV2 } from "@storyblok/js";
|
|
2
|
+
import type { RawCode } from "../vite-plugins/vite-plugin-storyblok-bridge";
|
|
3
|
+
export declare function generateFinalBridgeObject(rawCode: RawCode): StoryblokBridgeConfigV2;
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import type { RawCodeItemOptions } from "../vite-plugins/vite-plugin-storyblok-bridge";
|
|
2
|
+
import type { Rollup } from "vite";
|
|
3
|
+
/**
|
|
4
|
+
* Parses through the Abstract Syntax Tree (AST) code to locate the 'useStoryblok' function and its properties.
|
|
5
|
+
* This functionality is crucial for generating a virtual module that can be utilized during the initialization of the Storyblok bridge.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseAstRawCode(astCode: Rollup.ProgramNode): RawCodeItemOptions;
|
|
@@ -1,2 +1,12 @@
|
|
|
1
|
+
import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
|
|
1
2
|
import type { Plugin } from "vite";
|
|
3
|
+
export interface RawCodeItem {
|
|
4
|
+
url: string;
|
|
5
|
+
options?: RawCodeItemOptions;
|
|
6
|
+
}
|
|
7
|
+
export type RawCode = RawCodeItem[];
|
|
8
|
+
export interface RawCodeItemOptions {
|
|
9
|
+
apiOptions?: ISbStoriesParams;
|
|
10
|
+
bridgeOptions?: StoryblokBridgeConfigV2;
|
|
11
|
+
}
|
|
2
12
|
export declare function vitePluginStoryblokBridge(experimentalLivePreview: boolean, output: string): Plugin;
|
|
@@ -17,7 +17,7 @@ export async function handleStoryblokMessage(event: {
|
|
|
17
17
|
// Debounce the getNewHTMLBody function
|
|
18
18
|
const debouncedGetNewHTMLBody = async () => {
|
|
19
19
|
const newBody = await getNewHTMLBody(story);
|
|
20
|
-
const currentBody = document.body
|
|
20
|
+
const currentBody = document.body;
|
|
21
21
|
if (newBody.outerHTML === currentBody.outerHTML) return;
|
|
22
22
|
// Get current focused element in Storyblok
|
|
23
23
|
const focusedElem = document.querySelector('[data-blok-focused="true"]');
|
|
@@ -58,6 +58,7 @@ function updateDOMWithNewBody(
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
async function getNewHTMLBody(story: ISbStoryData) {
|
|
61
|
+
//TODO How to handel (50x, 405, etc.)
|
|
61
62
|
const result = await fetch(location.href, {
|
|
62
63
|
method: "POST",
|
|
63
64
|
body: JSON.stringify({
|
package/package.json
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { StoryblokBridgeConfigV2 } from "@storyblok/js";
|
|
2
|
+
import type { RawCode } from "../vite-plugins/vite-plugin-storyblok-bridge";
|
|
2
3
|
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
apiOptions?: ISbStoriesParams;
|
|
6
|
-
bridgeOptions?: StoryblokBridgeConfigV2;
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
export function generateFinalBridgeObject(
|
|
10
|
-
rawCode: RawCodeItem[]
|
|
11
|
-
): StoryblokBridgeConfigV2 {
|
|
12
|
-
let mergedOptions = {
|
|
4
|
+
export function generateFinalBridgeObject(rawCode: RawCode) {
|
|
5
|
+
let mergedOptions: StoryblokBridgeConfigV2 = {
|
|
13
6
|
resolveRelations: [],
|
|
14
7
|
};
|
|
15
8
|
|
|
16
|
-
function addToResolveRelations(
|
|
17
|
-
resolveRelations
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
mergedOptions.resolveRelations.push(resolveRelations);
|
|
24
|
-
}
|
|
9
|
+
function addToResolveRelations(resolveRelations?: string[] | string) {
|
|
10
|
+
if (resolveRelations && Array.isArray(mergedOptions.resolveRelations)) {
|
|
11
|
+
mergedOptions.resolveRelations.push(
|
|
12
|
+
...(Array.isArray(resolveRelations)
|
|
13
|
+
? resolveRelations
|
|
14
|
+
: [resolveRelations])
|
|
15
|
+
);
|
|
25
16
|
}
|
|
26
17
|
}
|
|
27
18
|
|
package/utils/parseAstCode.ts
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
import mergeWith from "lodash.mergewith";
|
|
2
|
+
import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
|
|
3
|
+
import type { RawCodeItemOptions } from "../vite-plugins/vite-plugin-storyblok-bridge";
|
|
4
|
+
import type { Rollup } from "vite";
|
|
5
|
+
import type { SpreadElement, Property } from "estree";
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Parses through the Abstract Syntax Tree (AST) code to locate the 'useStoryblok' function and its properties.
|
|
9
|
+
* This functionality is crucial for generating a virtual module that can be utilized during the initialization of the Storyblok bridge.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function parseAstRawCode(astCode: Rollup.ProgramNode) {
|
|
13
|
+
let obj: RawCodeItemOptions = {};
|
|
14
|
+
|
|
15
|
+
function customizer(_: any, srcValue: any) {
|
|
6
16
|
if (
|
|
7
17
|
srcValue?.type === "AwaitExpression" &&
|
|
8
|
-
srcValue
|
|
18
|
+
srcValue.argument.type === "CallExpression" &&
|
|
19
|
+
srcValue.argument.callee.type === "Identifier" &&
|
|
20
|
+
srcValue.argument.callee.name === "useStoryblok"
|
|
9
21
|
) {
|
|
10
|
-
const props = srcValue
|
|
11
|
-
if (props[1]
|
|
12
|
-
const apiOptions = getAstPropToObj(props[1]
|
|
22
|
+
const props = srcValue.argument.arguments;
|
|
23
|
+
if (props && props[1].type === "ObjectExpression") {
|
|
24
|
+
const apiOptions = getAstPropToObj(props[1].properties);
|
|
13
25
|
obj = {
|
|
14
26
|
...obj,
|
|
15
27
|
apiOptions,
|
|
16
28
|
};
|
|
17
29
|
}
|
|
18
|
-
if (props[2]
|
|
19
|
-
const bridgeOptions = getAstPropToObj(props[2]
|
|
30
|
+
if (props && props[2].type === "ObjectExpression") {
|
|
31
|
+
const bridgeOptions = getAstPropToObj(props[2].properties);
|
|
20
32
|
obj = {
|
|
21
33
|
...obj,
|
|
22
34
|
bridgeOptions,
|
|
@@ -29,15 +41,26 @@ export function parseAstRawCode(astCode) {
|
|
|
29
41
|
return obj;
|
|
30
42
|
}
|
|
31
43
|
|
|
32
|
-
function getAstPropToObj(properties) {
|
|
33
|
-
|
|
44
|
+
function getAstPropToObj(properties: (SpreadElement | Property)[]) {
|
|
45
|
+
const option: ISbStoriesParams | StoryblokBridgeConfigV2 = {};
|
|
46
|
+
|
|
47
|
+
return properties.reduce((options, property) => {
|
|
48
|
+
if (property.type !== "Property") return options;
|
|
49
|
+
const { key, value } = property;
|
|
34
50
|
const { type } = value;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
if (key.type !== "Identifier") return options;
|
|
52
|
+
|
|
53
|
+
if (type === "Literal") {
|
|
54
|
+
options[key.name] = value.value;
|
|
55
|
+
} else if (type === "ArrayExpression") {
|
|
56
|
+
const arrayValues = value.elements.reduce((acc, element) => {
|
|
57
|
+
if (element.type === "Literal" && element.value) {
|
|
58
|
+
return [...acc, element.value];
|
|
59
|
+
}
|
|
60
|
+
return acc;
|
|
61
|
+
}, []);
|
|
62
|
+
options[key.name] = arrayValues;
|
|
63
|
+
}
|
|
41
64
|
return options;
|
|
42
|
-
},
|
|
65
|
+
}, option);
|
|
43
66
|
}
|