storybook-addon-sdc 0.9.6 → 0.10.1
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 +36 -25
- package/dist/preset.js +192 -89
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ This addon streamlines the integration of Drupal Single Directory Components (SD
|
|
|
11
11
|
- [Features of the Addon](#features-of-the-addon)
|
|
12
12
|
- [Quickstart Guide](#quickstart-guide)
|
|
13
13
|
- [Configuration](#configuration)
|
|
14
|
+
- [Namespaces](#namespaces)
|
|
14
15
|
- [Creating Experimental Stories](#creating-experimental-stories)
|
|
15
16
|
- [Support for Single Story Files (\*.story.yml)](#support-for-single-story-files-storyyml)
|
|
16
17
|
- [Regular Storybook](#regular-storybook)
|
|
@@ -44,6 +45,7 @@ The SDC Storybook Addon simplifies the integration of Drupal Single Directory Co
|
|
|
44
45
|
- **Drupal Behavior Embedding**: Allows you to directly embed Drupal behaviors like `Drupal.attachBehaviors()` into Storybook previews, ensuring components behave similarly to their Drupal counterparts.
|
|
45
46
|
- **Custom and External Schema Definitions**: Supports custom and external JSON schema definitions to validate components based on Drupal-specific configurations (e.g., UI Patterns, Experience Builder).
|
|
46
47
|
- **Default and Custom Story Rendering**: Use `type: component` to nest components, `type: element` for HTML markup, and `type: image` for images within stories. Or create your own custom renderers.
|
|
48
|
+
- **Namespaces**: Supports multiple namespaces, allowing you to use components from different(parent/sibling) directories or packages.
|
|
47
49
|
|
|
48
50
|
## Quickstart Guide
|
|
49
51
|
|
|
@@ -106,24 +108,7 @@ const config = {
|
|
|
106
108
|
options: {
|
|
107
109
|
sdcStorybookOptions: {
|
|
108
110
|
twigLib: 'twig', // 'twig' for Twig.js or 'twing' for Twing.
|
|
109
|
-
namespace: 'umami', // Your namespace.
|
|
110
111
|
},
|
|
111
|
-
|
|
112
|
-
// vite-plugin-twig-drupal options. Comment if using Twing.
|
|
113
|
-
vitePluginTwigDrupalOptions: {
|
|
114
|
-
namespaces: {
|
|
115
|
-
umami: join(cwd(), './components'), // Your namespace and path to components.
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
|
|
119
|
-
// vite-plugin-twing-drupal options. Uncomment if using Twing.
|
|
120
|
-
// vitePluginTwingDrupalOptions: {
|
|
121
|
-
// namespaces: {
|
|
122
|
-
// umami: [join(cwd(), './components')],
|
|
123
|
-
// },
|
|
124
|
-
// },
|
|
125
|
-
|
|
126
|
-
jsonSchemaFakerOptions: {}, // json-schema-faker options.
|
|
127
112
|
},
|
|
128
113
|
},
|
|
129
114
|
// Any other addons.
|
|
@@ -137,6 +122,34 @@ const config = {
|
|
|
137
122
|
export default config
|
|
138
123
|
```
|
|
139
124
|
|
|
125
|
+
## Namespaces
|
|
126
|
+
|
|
127
|
+
- **Default Namespace**: The addon works out of the box without requiring namespace configuration and will use the current directory name as the default namespace, mirroring the behavior of a Drupal theme or module.
|
|
128
|
+
For example, if your `package.json` is in the `my-theme` directory, the default namespace will be `my-theme`, and this namespace will be linked to the `./components` directory.
|
|
129
|
+
|
|
130
|
+
- **Custom Namespace**: If you want to use a custom namespace or explicitly set the namespace, you can specify it in the plugin configuration:
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
sdcStorybookOptions: {
|
|
134
|
+
...
|
|
135
|
+
namespace: 'umami',
|
|
136
|
+
},
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
- **Multiple Namespaces**: The most useful feature is the ability to use multiple namespaces.
|
|
140
|
+
This allows you to use components from other directories (parent or sibling).
|
|
141
|
+
These directories must also contain a `components` directory with SDC components.
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import { resolve } from 'node:path'
|
|
145
|
+
sdcStorybookOptions: {
|
|
146
|
+
namespaces: {
|
|
147
|
+
'parent-namespace': resolve('../parent-namespace'),
|
|
148
|
+
'grandparent-namespace': resolve('../../grandparent-namespace'),
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
```
|
|
152
|
+
|
|
140
153
|
## Creating Experimental Stories
|
|
141
154
|
|
|
142
155
|
The `sdcStorybook` configuration in the SDC YAML file provides a flexible way to define custom stories for your components. This feature allows you to use predefined props, custom slots, or even reuse stories defined elsewhere. Here's how it works:
|
|
@@ -280,7 +293,7 @@ This means you can create independent stories for your components by simply addi
|
|
|
280
293
|
```yaml
|
|
281
294
|
# components/slider/slider.badges.story.yml
|
|
282
295
|
name: Badges
|
|
283
|
-
|
|
296
|
+
slots:
|
|
284
297
|
slides:
|
|
285
298
|
- type: component
|
|
286
299
|
component: 'umami:badge'
|
|
@@ -338,7 +351,11 @@ export const Basic = {}
|
|
|
338
351
|
|
|
339
352
|
## Configuration Options
|
|
340
353
|
|
|
341
|
-
|
|
354
|
+
- **vitePluginTwigDrupalOptions**: Options for [vite-plugin-twig-drupal](https://github.com/larowlan/vite-plugin-twig-drupal) (Twig.js). Use when `twigLib` is set to `'twig'`.
|
|
355
|
+
- **vitePluginTwingDrupalOptions**: Options for [vite-plugin-twing-drupal](https://github.com/christianwiedemann/vite-plugin-twing-drupal) (Twing). Use when `twigLib` is set to `'twing'`.
|
|
356
|
+
- **customDefs**: An object with custom JSON schema definitions for your components.
|
|
357
|
+
- **externalDefs**: An array of URLs or local file paths to external schema definition files.
|
|
358
|
+
- **validate**: A URL or path to a JSON schema for validating your SDC components.
|
|
342
359
|
|
|
343
360
|
### Twig.js with `vitePluginTwigDrupalOptions`
|
|
344
361
|
|
|
@@ -346,9 +363,6 @@ It is possible to pass options to the underlying `vite-plugin-twig-drupal` plugi
|
|
|
346
363
|
|
|
347
364
|
```js
|
|
348
365
|
vitePluginTwigDrupalOptions: {
|
|
349
|
-
namespaces: {
|
|
350
|
-
umami: join(cwd(), './components'), // Your namespace and path to components.
|
|
351
|
-
},
|
|
352
366
|
functions: {
|
|
353
367
|
// You can add custom functions - each is a function that is passed the active Twig instance and should call
|
|
354
368
|
// e.g. extendFunction to register a function
|
|
@@ -370,9 +384,6 @@ It is possible to pass options to the underlying `vite-plugin-twing-drupal` plug
|
|
|
370
384
|
|
|
371
385
|
```js
|
|
372
386
|
vitePluginTwingDrupalOptions: {
|
|
373
|
-
namespaces: {
|
|
374
|
-
umami: [join(cwd(), './components')],
|
|
375
|
-
},
|
|
376
387
|
// (Optional) With twing hooks you can adjust twing environment.
|
|
377
388
|
hooks: join(cwd(), '.storybook/twing-hooks.js'),
|
|
378
389
|
},
|
package/dist/preset.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
// src/vite-plugin-storybook-yaml-stories.ts
|
|
2
2
|
import { readdirSync as readdirSync2, readFileSync } from "fs";
|
|
3
3
|
import { parse as parseYaml } from "yaml";
|
|
4
|
-
import { join as join2, basename, dirname as dirname2, extname } from "path";
|
|
4
|
+
import { join as join2, basename as basename2, dirname as dirname2, extname } from "path";
|
|
5
5
|
import { globSync } from "glob";
|
|
6
6
|
|
|
7
|
+
// src/logger.ts
|
|
8
|
+
import pino from "pino";
|
|
9
|
+
var logger = pino({
|
|
10
|
+
transport: {
|
|
11
|
+
target: "pino-pretty",
|
|
12
|
+
options: {
|
|
13
|
+
colorize: true
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
7
18
|
// src/argsGenerator.ts
|
|
8
19
|
import { JSONSchemaFaker } from "json-schema-faker";
|
|
9
20
|
var generateArgs = (schema, defs) => {
|
|
@@ -25,10 +36,6 @@ var slotsToSchemaProperties = (slots) => {
|
|
|
25
36
|
};
|
|
26
37
|
function generateStorybookArgs(content, jsonSchemaFakerOptions) {
|
|
27
38
|
JSONSchemaFaker.option({
|
|
28
|
-
ignoreMissingRefs: true,
|
|
29
|
-
failOnInvalidTypes: false,
|
|
30
|
-
useExamplesValue: true,
|
|
31
|
-
useDefaultValue: true,
|
|
32
39
|
...jsonSchemaFakerOptions
|
|
33
40
|
});
|
|
34
41
|
const { props, slots, $defs } = content;
|
|
@@ -56,19 +63,8 @@ var argTypesGenerator_default = (content) => {
|
|
|
56
63
|
};
|
|
57
64
|
|
|
58
65
|
// src/utils.ts
|
|
59
|
-
import { readdirSync, existsSync } from "fs";
|
|
60
|
-
import { join, resolve } from "path";
|
|
61
66
|
var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
62
67
|
var convertToKebabCase = (str) => str.replace(/[-:]/g, "");
|
|
63
|
-
var getSubdirectories = (baseDir) => readdirSync(baseDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join(baseDir, entry.name));
|
|
64
|
-
var resolveComponentPath = (namespace, component) => {
|
|
65
|
-
const baseDir = resolve("./components");
|
|
66
|
-
const directories = [baseDir, ...getSubdirectories(baseDir)];
|
|
67
|
-
const possiblePaths = directories.map(
|
|
68
|
-
(dir) => join(dir, component, `${component}.component.yml`)
|
|
69
|
-
);
|
|
70
|
-
return possiblePaths.find(existsSync);
|
|
71
|
-
};
|
|
72
68
|
var toAttributes = (attrs) => {
|
|
73
69
|
if (!attrs) return "";
|
|
74
70
|
return " " + Object.entries(attrs).map(([key, value]) => {
|
|
@@ -169,9 +165,10 @@ var generateVariants = (variants) => {
|
|
|
169
165
|
|
|
170
166
|
// src/componentMetadata.ts
|
|
171
167
|
import { dirname, relative } from "path";
|
|
168
|
+
import { cwd } from "process";
|
|
172
169
|
var componentMetadata_default = (id, content) => {
|
|
173
170
|
return {
|
|
174
|
-
path: relative(
|
|
171
|
+
path: relative(cwd(), dirname(id)),
|
|
175
172
|
machineName: id,
|
|
176
173
|
status: content.status || "stable",
|
|
177
174
|
name: content.name,
|
|
@@ -182,19 +179,6 @@ var componentMetadata_default = (id, content) => {
|
|
|
182
179
|
// src/validateJson.ts
|
|
183
180
|
import { Validator } from "jsonschema";
|
|
184
181
|
import fetch from "node-fetch";
|
|
185
|
-
|
|
186
|
-
// src/logger.ts
|
|
187
|
-
import pino from "pino";
|
|
188
|
-
var logger = pino({
|
|
189
|
-
transport: {
|
|
190
|
-
target: "pino-pretty",
|
|
191
|
-
options: {
|
|
192
|
-
colorize: true
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
// src/validateJson.ts
|
|
198
182
|
var validator = new Validator();
|
|
199
183
|
var schemaCache = /* @__PURE__ */ new Map();
|
|
200
184
|
var fetchSchema = async (url) => {
|
|
@@ -232,6 +216,116 @@ ${validationResult.errors.map((error) => error.stack).join("\n")}
|
|
|
232
216
|
}
|
|
233
217
|
};
|
|
234
218
|
|
|
219
|
+
// src/namespaces.ts
|
|
220
|
+
import { readdirSync, existsSync } from "fs";
|
|
221
|
+
import { basename, join, resolve, sep, relative as relative2 } from "path";
|
|
222
|
+
import { cwd as cwd2 } from "process";
|
|
223
|
+
import { normalizePath } from "vite";
|
|
224
|
+
var getSubdirectories = (baseDir) => readdirSync(baseDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join(baseDir, entry.name));
|
|
225
|
+
var getProjectName = (p) => {
|
|
226
|
+
const fullPath = resolve(p);
|
|
227
|
+
const parts = fullPath.split(sep);
|
|
228
|
+
const i = parts.lastIndexOf("components");
|
|
229
|
+
if (i < 1) {
|
|
230
|
+
throw new Error(`Could not find 'components' folder in path: ${fullPath}`);
|
|
231
|
+
}
|
|
232
|
+
return parts[i - 1];
|
|
233
|
+
};
|
|
234
|
+
var resolveComponentPath = (namespace, component, namespaces) => {
|
|
235
|
+
const baseDir = namespaces.findPath(namespace);
|
|
236
|
+
const directories = [baseDir, ...getSubdirectories(baseDir)];
|
|
237
|
+
const possiblePaths = directories.map(
|
|
238
|
+
(dir) => join(dir, component, `${component}.component.yml`)
|
|
239
|
+
);
|
|
240
|
+
return possiblePaths.find(existsSync);
|
|
241
|
+
};
|
|
242
|
+
var Namespaces = class {
|
|
243
|
+
namespaces;
|
|
244
|
+
stripTrailingSlash = (p) => p.replace(/\/+$/g, "");
|
|
245
|
+
constructor(namespaceDefinition) {
|
|
246
|
+
this.namespaces = namespaceDefinition.namespaces ?? {};
|
|
247
|
+
if (namespaceDefinition.namespace) {
|
|
248
|
+
this.namespaces[namespaceDefinition.namespace] = cwd2();
|
|
249
|
+
} else {
|
|
250
|
+
this.namespaces[basename(cwd2())] = cwd2();
|
|
251
|
+
}
|
|
252
|
+
logger.info(`REGISTER NAMESPACES: ${JSON.stringify(this.namespaces)}`);
|
|
253
|
+
}
|
|
254
|
+
toViteAlias() {
|
|
255
|
+
const aliases = [];
|
|
256
|
+
Object.keys(this.namespaces).forEach((namespace) => {
|
|
257
|
+
aliases.push({
|
|
258
|
+
find: "@" + namespace,
|
|
259
|
+
replacement: normalizePath(
|
|
260
|
+
resolve(this.namespaces[namespace], "components")
|
|
261
|
+
)
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
logger.info(`REGISTER VITE ALIASES: ${JSON.stringify(aliases)}`);
|
|
265
|
+
return aliases;
|
|
266
|
+
}
|
|
267
|
+
toTwigJsNamespaces() {
|
|
268
|
+
let namespaces = {};
|
|
269
|
+
for (const [ns, path] of Object.entries(this.namespaces)) {
|
|
270
|
+
namespaces[ns] = join(path, "components");
|
|
271
|
+
}
|
|
272
|
+
return namespaces;
|
|
273
|
+
}
|
|
274
|
+
toTwingNamespaces() {
|
|
275
|
+
let namespaces = {};
|
|
276
|
+
for (const [ns, path] of Object.entries(this.namespaces)) {
|
|
277
|
+
namespaces[ns] = [join(path, "components")];
|
|
278
|
+
}
|
|
279
|
+
return namespaces;
|
|
280
|
+
}
|
|
281
|
+
findPath = (namespace) => {
|
|
282
|
+
return this.namespaces[namespace] || "";
|
|
283
|
+
};
|
|
284
|
+
find = (fsPath) => {
|
|
285
|
+
const fp = this.stripTrailingSlash(fsPath);
|
|
286
|
+
let bestPath;
|
|
287
|
+
let bestNs;
|
|
288
|
+
for (const [ns, path] of Object.entries(this.namespaces)) {
|
|
289
|
+
const base = this.stripTrailingSlash(path);
|
|
290
|
+
if (fp === base || fp.startsWith(base + "/")) {
|
|
291
|
+
if (!bestPath || base.length > bestPath.length) {
|
|
292
|
+
bestPath = base;
|
|
293
|
+
bestNs = ns;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return bestNs;
|
|
298
|
+
};
|
|
299
|
+
pathToNamespace(fsPath, makeComponentIdFormat = false) {
|
|
300
|
+
const ns = this.find(fsPath);
|
|
301
|
+
if (!ns) {
|
|
302
|
+
throw new Error(
|
|
303
|
+
`Could not find valid 'namespace' for folder: ${fsPath} in namespaces: ${JSON.stringify(this.namespaces)}`
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const rootPath = resolve(this.namespaces[ns]);
|
|
307
|
+
const componentsDir = join(rootPath, "components");
|
|
308
|
+
const isExact = fsPath === componentsDir;
|
|
309
|
+
const isChild = fsPath.startsWith(componentsDir + sep);
|
|
310
|
+
if (!isExact && !isChild) {
|
|
311
|
+
throw new Error(
|
|
312
|
+
`Could not find 'components' folder in path: ${fsPath}, namespace: ${ns} (${Object.values(this.namespaces).join(", ")})`
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
let rel = relative2(componentsDir, fsPath);
|
|
316
|
+
if (sep !== "/") {
|
|
317
|
+
rel = rel.split(sep).join("/");
|
|
318
|
+
}
|
|
319
|
+
if (makeComponentIdFormat) {
|
|
320
|
+
return `${ns}:${rel}`;
|
|
321
|
+
}
|
|
322
|
+
return `@${ns}/${rel}`;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
var toNamespaces = (namespaceDefinition) => {
|
|
326
|
+
return new Namespaces(namespaceDefinition ?? {});
|
|
327
|
+
};
|
|
328
|
+
|
|
235
329
|
// src/vite-plugin-storybook-yaml-stories.ts
|
|
236
330
|
var readSDC = (filePath, defs, validate) => {
|
|
237
331
|
const sdcSchema = {
|
|
@@ -243,17 +337,23 @@ var readSDC = (filePath, defs, validate) => {
|
|
|
243
337
|
}
|
|
244
338
|
return sdcSchema;
|
|
245
339
|
};
|
|
246
|
-
var generateImports = (directory) => readdirSync2(directory).filter(
|
|
340
|
+
var generateImports = (directory, namespaces) => readdirSync2(directory).filter(
|
|
247
341
|
(file) => [".css", ".js", ".mjs", ".twig", ".yml"].includes(extname(file))
|
|
248
342
|
).map((file) => {
|
|
249
343
|
const filePath = `./${file}`;
|
|
250
|
-
|
|
344
|
+
const namespace = namespaces.pathToNamespace(directory);
|
|
345
|
+
logger.info(`IMPORT ASSET ${directory}/${file}`);
|
|
346
|
+
return extname(file) === ".twig" ? `import COMPONENT from '${namespace}/${file}';` : `import '${filePath}';`;
|
|
251
347
|
}).join("\n");
|
|
252
|
-
var dynamicImports = (stories) => {
|
|
348
|
+
var dynamicImports = (stories, namespaces) => {
|
|
253
349
|
const imports = /* @__PURE__ */ new Set();
|
|
254
350
|
const importComponent = (item) => {
|
|
255
351
|
const [namespace, componentName] = item.component.split(":");
|
|
256
|
-
const resolvedPath = resolveComponentPath(
|
|
352
|
+
const resolvedPath = resolveComponentPath(
|
|
353
|
+
namespace,
|
|
354
|
+
componentName,
|
|
355
|
+
namespaces
|
|
356
|
+
);
|
|
257
357
|
const kebabCaseName = convertToKebabCase(item.component);
|
|
258
358
|
if (resolvedPath) {
|
|
259
359
|
imports.add(`import * as ${kebabCaseName} from '${resolvedPath}';`);
|
|
@@ -305,7 +405,8 @@ var createStoryIndex = (fileName, baseTitle, stories) => {
|
|
|
305
405
|
var vite_plugin_storybook_yaml_stories_default = ({
|
|
306
406
|
jsonSchemaFakerOptions = {},
|
|
307
407
|
sdcStorybookOptions = {},
|
|
308
|
-
globalDefs = {}
|
|
408
|
+
globalDefs = {},
|
|
409
|
+
namespaces = {}
|
|
309
410
|
}) => ({
|
|
310
411
|
name: "vite-plugin-storybook-yaml-stories",
|
|
311
412
|
async load(id) {
|
|
@@ -315,13 +416,13 @@ var vite_plugin_storybook_yaml_stories_default = ({
|
|
|
315
416
|
if (!id.endsWith("component.yml")) return;
|
|
316
417
|
try {
|
|
317
418
|
const content = readSDC(id, globalDefs, sdcStorybookOptions.validate);
|
|
318
|
-
const imports = generateImports(dirname2(id));
|
|
419
|
+
const imports = generateImports(dirname2(id), namespaces);
|
|
319
420
|
const previewsStories = {
|
|
320
421
|
...content.thirdPartySettings?.sdcStorybook?.stories || {},
|
|
321
422
|
...loadStoryFilesSync(id)
|
|
322
423
|
};
|
|
323
424
|
storyNodeRenderer.register(sdcStorybookOptions.storyNodesRenderer ?? []);
|
|
324
|
-
const storiesImports = dynamicImports(previewsStories);
|
|
425
|
+
const storiesImports = dynamicImports(previewsStories, namespaces);
|
|
325
426
|
const metadata = componentMetadata_default(id, content);
|
|
326
427
|
const argTypes = {
|
|
327
428
|
componentMetadata: { table: { disable: true } },
|
|
@@ -338,7 +439,7 @@ var vite_plugin_storybook_yaml_stories_default = ({
|
|
|
338
439
|
defaultAttributes: [
|
|
339
440
|
[
|
|
340
441
|
"data-component-id",
|
|
341
|
-
`${
|
|
442
|
+
`${namespaces.pathToNamespace(dirname2(id), true)}`
|
|
342
443
|
]
|
|
343
444
|
],
|
|
344
445
|
componentMetadata: metadata,
|
|
@@ -374,7 +475,7 @@ export const Basic = {
|
|
|
374
475
|
${stories}
|
|
375
476
|
`;
|
|
376
477
|
} catch (error) {
|
|
377
|
-
|
|
478
|
+
logger.error(`Error loading component YAML file: ${id}, ${error}`);
|
|
378
479
|
throw error;
|
|
379
480
|
}
|
|
380
481
|
}
|
|
@@ -384,13 +485,13 @@ var yamlStoriesIndexer = {
|
|
|
384
485
|
createIndex: async (fileName, { makeTitle }) => {
|
|
385
486
|
try {
|
|
386
487
|
const content = readSDC(fileName);
|
|
387
|
-
const baseTitle = makeTitle(
|
|
488
|
+
const baseTitle = makeTitle(`${getProjectName(fileName)}/${content.name}`);
|
|
388
489
|
const stories = content.thirdPartySettings?.sdcStorybook?.stories;
|
|
389
490
|
const storiesContent = loadStoryFilesSync(fileName);
|
|
390
491
|
const mergedStories = { ...stories, ...storiesContent };
|
|
391
492
|
return createStoryIndex(fileName, baseTitle, mergedStories);
|
|
392
493
|
} catch (error) {
|
|
393
|
-
|
|
494
|
+
logger.error(`Error creating index for YAML file: ${fileName}, ${error}`);
|
|
394
495
|
throw error;
|
|
395
496
|
}
|
|
396
497
|
}
|
|
@@ -401,7 +502,7 @@ var loadStoryFilesSync = (fileName) => {
|
|
|
401
502
|
return storyFiles.reduce(
|
|
402
503
|
(acc, file) => {
|
|
403
504
|
const content = readFileSync(file, "utf8");
|
|
404
|
-
const key =
|
|
505
|
+
const key = basename2(file).split(".")[1];
|
|
405
506
|
return {
|
|
406
507
|
...acc,
|
|
407
508
|
[key]: parseYaml(content)
|
|
@@ -413,10 +514,10 @@ var loadStoryFilesSync = (fileName) => {
|
|
|
413
514
|
|
|
414
515
|
// src/preset.ts
|
|
415
516
|
import { mergeConfig } from "vite";
|
|
416
|
-
|
|
417
|
-
|
|
517
|
+
|
|
518
|
+
// src/definitions.ts
|
|
418
519
|
import { parse } from "yaml";
|
|
419
|
-
import {
|
|
520
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
420
521
|
import fetch2 from "node-fetch";
|
|
421
522
|
async function loadExternalDef(defPath) {
|
|
422
523
|
try {
|
|
@@ -436,28 +537,6 @@ async function loadExternalDef(defPath) {
|
|
|
436
537
|
throw error;
|
|
437
538
|
}
|
|
438
539
|
}
|
|
439
|
-
function getComponentDirectories() {
|
|
440
|
-
return sync("./components/**/*.component.yml");
|
|
441
|
-
}
|
|
442
|
-
function resolveComponentPath2(namespace, component) {
|
|
443
|
-
const componentDirectories = getComponentDirectories();
|
|
444
|
-
const possiblePaths = componentDirectories.map(
|
|
445
|
-
(dir) => resolve2(`${dir}/${component}/${component}.component.yml`)
|
|
446
|
-
);
|
|
447
|
-
const resolvedPath = possiblePaths.find((path) => existsSync2(path));
|
|
448
|
-
if (!resolvedPath) {
|
|
449
|
-
logger.error(
|
|
450
|
-
`Component ${component} could not be resolved in namespace ${namespace}`
|
|
451
|
-
);
|
|
452
|
-
}
|
|
453
|
-
return resolvedPath;
|
|
454
|
-
}
|
|
455
|
-
var defaultOptions = {
|
|
456
|
-
validate: false,
|
|
457
|
-
twigLib: "twig"
|
|
458
|
-
// validate:
|
|
459
|
-
// 'https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json',
|
|
460
|
-
};
|
|
461
540
|
async function loadAndMergeDefinitions(externalDefs, customDefs) {
|
|
462
541
|
const globalDefs = {};
|
|
463
542
|
if (externalDefs) {
|
|
@@ -477,24 +556,60 @@ async function loadAndMergeDefinitions(externalDefs, customDefs) {
|
|
|
477
556
|
}
|
|
478
557
|
if (Object.keys(globalDefs).length > 0) {
|
|
479
558
|
logger.info(
|
|
480
|
-
`
|
|
559
|
+
`REGISTER CUSTOM DEFINITIONS ${JSON.stringify(Object.keys(globalDefs))}`
|
|
481
560
|
);
|
|
482
561
|
}
|
|
483
562
|
return globalDefs;
|
|
484
563
|
}
|
|
564
|
+
|
|
565
|
+
// src/constants.ts
|
|
566
|
+
var DEFAULT_ADDON_OPTIONS = {
|
|
567
|
+
sdcStorybookOptions: {
|
|
568
|
+
twigLib: "twig",
|
|
569
|
+
validate: false
|
|
570
|
+
// validate:
|
|
571
|
+
// 'https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json',
|
|
572
|
+
},
|
|
573
|
+
vitePluginTwingDrupalOptions: {
|
|
574
|
+
include: "/.twig(?.*)?$/"
|
|
575
|
+
},
|
|
576
|
+
vitePluginTwigDrupalOptions: {},
|
|
577
|
+
jsonSchemaFakerOptions: {
|
|
578
|
+
ignoreMissingRefs: true,
|
|
579
|
+
failOnInvalidTypes: false,
|
|
580
|
+
useExamplesValue: true,
|
|
581
|
+
useDefaultValue: true
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
// src/preset.ts
|
|
485
586
|
async function viteFinal(config, options) {
|
|
486
|
-
options
|
|
487
|
-
...
|
|
488
|
-
...options
|
|
587
|
+
options = {
|
|
588
|
+
...DEFAULT_ADDON_OPTIONS,
|
|
589
|
+
...options
|
|
489
590
|
};
|
|
490
|
-
const {
|
|
491
|
-
|
|
591
|
+
const {
|
|
592
|
+
sdcStorybookOptions,
|
|
593
|
+
vitePluginTwigDrupalOptions,
|
|
594
|
+
vitePluginTwingDrupalOptions
|
|
595
|
+
} = options;
|
|
596
|
+
const { customDefs, externalDefs } = sdcStorybookOptions;
|
|
492
597
|
const { nodePolyfills } = await import("vite-plugin-node-polyfills");
|
|
598
|
+
const globalDefs = await loadAndMergeDefinitions(externalDefs, customDefs);
|
|
599
|
+
const namespaces = toNamespaces(sdcStorybookOptions);
|
|
493
600
|
let twigPlugin = null;
|
|
494
|
-
if (
|
|
601
|
+
if (sdcStorybookOptions.twigLib === "twing") {
|
|
602
|
+
options.vitePluginTwingDrupalOptions = {
|
|
603
|
+
...vitePluginTwingDrupalOptions,
|
|
604
|
+
namespaces: { ...namespaces.toTwingNamespaces() }
|
|
605
|
+
};
|
|
495
606
|
const { default: twing } = await import("vite-plugin-twing-drupal");
|
|
496
607
|
twigPlugin = twing(options.vitePluginTwingDrupalOptions);
|
|
497
|
-
} else if (
|
|
608
|
+
} else if (sdcStorybookOptions.twigLib === "twig") {
|
|
609
|
+
options.vitePluginTwigDrupalOptions = {
|
|
610
|
+
...vitePluginTwigDrupalOptions,
|
|
611
|
+
namespaces: { ...namespaces.toTwigJsNamespaces() }
|
|
612
|
+
};
|
|
498
613
|
const { default: twig } = await import("vite-plugin-twig-drupal");
|
|
499
614
|
twigPlugin = twig(options.vitePluginTwigDrupalOptions);
|
|
500
615
|
}
|
|
@@ -504,22 +619,10 @@ async function viteFinal(config, options) {
|
|
|
504
619
|
include: ["buffer", "stream", "path"]
|
|
505
620
|
}),
|
|
506
621
|
...twigPlugin ? [twigPlugin] : [],
|
|
507
|
-
vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs })
|
|
622
|
+
vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs, namespaces })
|
|
508
623
|
],
|
|
509
624
|
resolve: {
|
|
510
|
-
alias: [
|
|
511
|
-
{
|
|
512
|
-
find: new RegExp(`${namespace}:(.*)`),
|
|
513
|
-
// Use namespace from options
|
|
514
|
-
replacement: (match, component) => {
|
|
515
|
-
const resolvedPath = resolveComponentPath2(namespace, component);
|
|
516
|
-
if (!resolvedPath) {
|
|
517
|
-
throw new Error(`Component ${component} could not be resolved.`);
|
|
518
|
-
}
|
|
519
|
-
return resolvedPath;
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
]
|
|
625
|
+
alias: [...namespaces.toViteAlias()]
|
|
523
626
|
}
|
|
524
627
|
});
|
|
525
628
|
}
|