@sit-onyx/storybook-utils 1.0.0-alpha.133 → 1.0.0-alpha.134
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 +1 -1
- package/src/preview.spec.ts +4 -1
- package/src/preview.ts +17 -7
package/package.json
CHANGED
package/src/preview.spec.ts
CHANGED
|
@@ -6,11 +6,12 @@ import { replaceAll, sourceCodeTransformer } from "./preview";
|
|
|
6
6
|
import * as sourceCodeGenerator from "./source-code-generator";
|
|
7
7
|
|
|
8
8
|
describe("preview.ts", () => {
|
|
9
|
-
test("should transform source code and add icon imports", () => {
|
|
9
|
+
test("should transform source code and add icon/onyx imports", () => {
|
|
10
10
|
// ARRANGE
|
|
11
11
|
const generatorSpy = vi.spyOn(sourceCodeGenerator, "generateSourceCode")
|
|
12
12
|
.mockReturnValue(`<template>
|
|
13
13
|
<OnyxTest icon='${placeholder}' test='${bellRing}' :obj="{foo:'${replaceAll(calendar, '"', "\\'")}'}" />
|
|
14
|
+
<OnyxOtherComponent />
|
|
14
15
|
</template>`);
|
|
15
16
|
|
|
16
17
|
// ACT
|
|
@@ -19,6 +20,7 @@ describe("preview.ts", () => {
|
|
|
19
20
|
// ASSERT
|
|
20
21
|
expect(generatorSpy).toHaveBeenCalledOnce();
|
|
21
22
|
expect(sourceCode).toBe(`<script lang="ts" setup>
|
|
23
|
+
import { OnyxOtherComponent, OnyxTest } from "sit-onyx";
|
|
22
24
|
import bellRing from "@sit-onyx/icons/bell-ring.svg?raw";
|
|
23
25
|
import calendar from "@sit-onyx/icons/calendar.svg?raw";
|
|
24
26
|
import placeholder from "@sit-onyx/icons/placeholder.svg?raw";
|
|
@@ -26,6 +28,7 @@ import placeholder from "@sit-onyx/icons/placeholder.svg?raw";
|
|
|
26
28
|
|
|
27
29
|
<template>
|
|
28
30
|
<OnyxTest :icon="placeholder" :test="bellRing" :obj="{foo:calendar}" />
|
|
31
|
+
<OnyxOtherComponent />
|
|
29
32
|
</template>`);
|
|
30
33
|
});
|
|
31
34
|
});
|
package/src/preview.ts
CHANGED
|
@@ -148,7 +148,7 @@ export const sourceCodeTransformer = (
|
|
|
148
148
|
|
|
149
149
|
let code = generateSourceCode(ctx);
|
|
150
150
|
|
|
151
|
-
const
|
|
151
|
+
const additionalImports: string[] = [];
|
|
152
152
|
|
|
153
153
|
// add icon imports to the source code for all used onyx icons
|
|
154
154
|
Object.entries(ALL_ICONS).forEach(([iconName, iconContent]) => {
|
|
@@ -158,27 +158,37 @@ export const sourceCodeTransformer = (
|
|
|
158
158
|
|
|
159
159
|
if (code.includes(iconContent)) {
|
|
160
160
|
code = code.replace(new RegExp(` (\\S+)=['"]${iconContent}['"]`), ` :$1="${importName}"`);
|
|
161
|
-
|
|
161
|
+
additionalImports.push(`import ${importName} from "@sit-onyx/icons/${iconName}.svg?raw";`);
|
|
162
162
|
} else if (code.includes(singleQuotedIconContent)) {
|
|
163
163
|
// support icons inside objects
|
|
164
164
|
code = code.replace(singleQuotedIconContent, importName);
|
|
165
|
-
|
|
165
|
+
additionalImports.push(`import ${importName} from "@sit-onyx/icons/${iconName}.svg?raw";`);
|
|
166
166
|
} else if (code.includes(escapedIconContent)) {
|
|
167
167
|
// support icons inside objects
|
|
168
168
|
code = code.replace(escapedIconContent, importName);
|
|
169
|
-
|
|
169
|
+
additionalImports.push(`import ${importName} from "@sit-onyx/icons/${iconName}.svg?raw";`);
|
|
170
170
|
}
|
|
171
171
|
});
|
|
172
172
|
|
|
173
|
-
|
|
173
|
+
// add imports for all used onyx components
|
|
174
|
+
// Set is used here to only include unique components if they are used multiple times
|
|
175
|
+
const usedOnyxComponents = [
|
|
176
|
+
...new Set(Array.from(code.matchAll(/<Onyx\S+/g)).map((match) => match[0].replace("<", ""))),
|
|
177
|
+
].sort();
|
|
178
|
+
|
|
179
|
+
if (usedOnyxComponents.length > 0) {
|
|
180
|
+
additionalImports.unshift(`import { ${usedOnyxComponents.join(", ")} } from "sit-onyx";`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (additionalImports.length === 0) return code;
|
|
174
184
|
|
|
175
185
|
if (code.startsWith("<script")) {
|
|
176
186
|
const index = code.indexOf("\n");
|
|
177
|
-
return code.slice(0, index) +
|
|
187
|
+
return code.slice(0, index) + additionalImports.join("\n") + "\n" + code.slice(index);
|
|
178
188
|
}
|
|
179
189
|
|
|
180
190
|
return `<script lang="ts" setup>
|
|
181
|
-
${
|
|
191
|
+
${additionalImports.join("\n")}
|
|
182
192
|
</script>
|
|
183
193
|
|
|
184
194
|
${code}`;
|