@sit-onyx/storybook-utils 1.0.0-alpha.20 → 1.0.0-alpha.21
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/package.json +9 -7
- package/src/preview.spec.ts +18 -0
- package/src/preview.ts +24 -16
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sit-onyx/storybook-utils",
|
|
3
3
|
"description": "Storybook utilities for Vue",
|
|
4
|
-
"version": "1.0.0-alpha.
|
|
4
|
+
"version": "1.0.0-alpha.21",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Schwarz IT KG",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -23,17 +23,19 @@
|
|
|
23
23
|
"url": "https://github.com/SchwarzIT/onyx/issues"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@storybook/core-events": ">= 8.0.0-beta.
|
|
27
|
-
"@storybook/preview-api": ">= 8.0.0-beta.
|
|
28
|
-
"@storybook/theming": ">= 8.0.0-beta.
|
|
29
|
-
"@storybook/vue3": ">= 8.0.0-beta.
|
|
26
|
+
"@storybook/core-events": ">= 8.0.0-beta.5",
|
|
27
|
+
"@storybook/preview-api": ">= 8.0.0-beta.5",
|
|
28
|
+
"@storybook/theming": ">= 8.0.0-beta.5",
|
|
29
|
+
"@storybook/vue3": ">= 8.0.0-beta.5",
|
|
30
30
|
"storybook-dark-mode": ">= 3",
|
|
31
|
-
"sit-onyx": "^1.0.0-alpha.
|
|
31
|
+
"sit-onyx": "^1.0.0-alpha.20"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"deepmerge-ts": "^5.1.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
|
-
"build": "tsc --noEmit"
|
|
37
|
+
"build": "tsc --noEmit",
|
|
38
|
+
"test": "vitest",
|
|
39
|
+
"test:coverage": "vitest run --coverage"
|
|
38
40
|
}
|
|
39
41
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { sourceCodeTransformer } from "./preview";
|
|
3
|
+
|
|
4
|
+
describe("preview.ts", () => {
|
|
5
|
+
test("should transform source code", () => {
|
|
6
|
+
// ARRANGE
|
|
7
|
+
// mix double and single quotes to test that both are supported
|
|
8
|
+
const originalSourceCode = `<OnyxTest v-on:test="someFunction" @empty='()=>({})' v-bind="{}" :disabled='true' :readonly="false" test="true" />`;
|
|
9
|
+
|
|
10
|
+
const expectedOutput = `<OnyxTest @test="someFunction" disabled :readonly="false" test />`;
|
|
11
|
+
|
|
12
|
+
// ACT
|
|
13
|
+
const output = sourceCodeTransformer(originalSourceCode);
|
|
14
|
+
|
|
15
|
+
// ASSERT
|
|
16
|
+
expect(output).toBe(expectedOutput);
|
|
17
|
+
});
|
|
18
|
+
});
|
package/src/preview.ts
CHANGED
|
@@ -78,22 +78,7 @@ export const createPreview = <T extends Preview = Preview>(overrides?: T) => {
|
|
|
78
78
|
* we want it to look.
|
|
79
79
|
* @see https://storybook.js.org/docs/react/api/doc-block-source
|
|
80
80
|
*/
|
|
81
|
-
transform:
|
|
82
|
-
const replacements = [
|
|
83
|
-
// replace event bindings with shortcut
|
|
84
|
-
{ searchValue: "v-on:", replaceValue: "@" },
|
|
85
|
-
// remove empty event handlers, e.g. @click="()=>({})" will be removed
|
|
86
|
-
{ searchValue: / @.*['"]\(\)=>\({}\)['"]/g, replaceValue: "" },
|
|
87
|
-
// remove empty v-binds, e.g. v-bind="{}" will be removed
|
|
88
|
-
{ searchValue: / v-bind=['"]{}['"]/g, replaceValue: "" },
|
|
89
|
-
// replace boolean shortcuts for true, e.g. disabled="true" will be changed to just disabled
|
|
90
|
-
{ searchValue: /:(.*)=['"]true['"]/g, replaceValue: "$1" },
|
|
91
|
-
];
|
|
92
|
-
|
|
93
|
-
return replacements.reduce((code, replacement) => {
|
|
94
|
-
return replaceAll(code, replacement.searchValue, replacement.replaceValue);
|
|
95
|
-
}, sourceCode);
|
|
96
|
-
},
|
|
81
|
+
transform: sourceCodeTransformer,
|
|
97
82
|
},
|
|
98
83
|
},
|
|
99
84
|
darkMode: {
|
|
@@ -134,6 +119,29 @@ export const createPreview = <T extends Preview = Preview>(overrides?: T) => {
|
|
|
134
119
|
return deepmerge<[T, typeof defaultPreview]>(overrides ?? ({} as T), defaultPreview);
|
|
135
120
|
};
|
|
136
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Custom transformer for the story source code to better fit to our
|
|
124
|
+
* Vue.js code because storybook per default does not render it exactly how
|
|
125
|
+
* we want it to look.
|
|
126
|
+
* @see https://storybook.js.org/docs/react/api/doc-block-source
|
|
127
|
+
*/
|
|
128
|
+
export const sourceCodeTransformer = (sourceCode: string): string => {
|
|
129
|
+
const replacements = [
|
|
130
|
+
// replace event bindings with shortcut
|
|
131
|
+
{ searchValue: "v-on:", replaceValue: "@" },
|
|
132
|
+
// remove empty event handlers, e.g. @click="()=>({})" will be removed
|
|
133
|
+
{ searchValue: / @\S*['"]\(\)=>\({}\)['"]/g, replaceValue: "" },
|
|
134
|
+
// // remove empty v-binds, e.g. v-bind="{}" will be removed
|
|
135
|
+
{ searchValue: / v-bind=['"]{}['"]/g, replaceValue: "" },
|
|
136
|
+
// // replace boolean shortcuts for true, e.g. disabled="true" will be changed to just disabled
|
|
137
|
+
{ searchValue: /:?(\S*)=['"]true['"]/g, replaceValue: "$1" },
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
return replacements.reduce((code, replacement) => {
|
|
141
|
+
return replaceAll(code, replacement.searchValue, replacement.replaceValue);
|
|
142
|
+
}, sourceCode);
|
|
143
|
+
};
|
|
144
|
+
|
|
137
145
|
/**
|
|
138
146
|
* Custom String.replaceAll implementation using a RegExp
|
|
139
147
|
* because String.replaceAll() is not available in our specified EcmaScript target in tsconfig.json
|