storybook-addon-sdc 0.8.3 → 0.9.2

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 CHANGED
@@ -37,7 +37,7 @@ You can view a [live example of the SDC Addon in Storybook](https://iberdinsky-s
37
37
 
38
38
  The SDC Storybook Addon simplifies the integration of Drupal Single Directory Components (SDC) into Storybook, offering several key features:
39
39
 
40
- - **Vite Plugin Integration**: Leverages the vite-plugin-twig-drupal plugin to seamlessly load and process Twig templates used in SDC components.
40
+ - **Vite Plugin Integration**: You can use either the vite-plugin-twig-drupal plugin (Twig.js) or the vite-plugin-twing-drupal plugin (Twing) to load and process Twig templates in SDC components.
41
41
  - **Dynamic Path Resolution**: Utilizes namespaces to dynamically discover components within your project structure, eliminating the need for manual configuration.
42
42
  - **Story Generation**: Automatically creates stories based on the YAML configurations of your SDC components, streamlining the story creation process.
43
43
  - **JSON Schema Support**: Supports JSON Schema for props and slots, enabling the generation of mock data for missing values and ensuring data consistency.
@@ -78,13 +78,14 @@ While solutions like [SDC Styleguide](https://www.drupal.org/project/sdc_stylegu
78
78
  - Embed **Drupal behaviors** (like `Drupal.attachBehaviors()`) directly into Storybook previews, ensuring consistent component behavior between Storybook and production.
79
79
  - Supports `drupalSettings` and `once.js`, so components in Storybook behave identically to their Drupal counterparts.
80
80
 
81
- ### 7. Twig.js vs Drupal Twig
81
+ ### 7. Twig.js, Twing, and Drupal Twig
82
82
 
83
- While using Drupal to render components offers tighter integration, there are strong reasons to continue using Twig.js in many scenarios:
83
+ While using Drupal to render components offers tighter integration, there are strong reasons to use Twig.js or Twing in many scenarios:
84
84
 
85
- - Many Components **Don’t Need Full Drupal Logic**. Basic components (buttons, cards, lists) rely on simple HTML and CSS, not on complex template logic. For such components, Twig.js provides sufficient rendering without the need for full Drupal preprocessing.
86
- - Twig.js Works Well for Frontend-Focused Use Cases.
87
- - Styling and Behavior Mismatches Can Be Managed Separately in Drupal implelentation phase.
85
+ - Many components **don’t need full Drupal logic**. Basic components (buttons, cards, lists) rely on simple HTML and CSS, not on complex template logic. For such components, Twig.js or Twing provide sufficient rendering without the need for full Drupal preprocessing.
86
+ - **Twig.js** works well for most frontend-focused use cases.
87
+ - **Twing** is a modern, actively maintained Twig implementation for Node.js that offers better compatibility with Drupal's Twig features and syntax.
88
+ - Styling and behavior mismatches can be managed separately in the Drupal implementation phase.
88
89
 
89
90
  ## Quickstart Guide
90
91
 
@@ -133,6 +134,9 @@ While using Drupal to render components offers tighter integration, there are st
133
134
  ## Configuration
134
135
 
135
136
  To configure the addon, update `.storybook/main.js` as shown below:
137
+ You can use this plugin either with [Twig.js](https://github.com/twigjs/twig.js) or [Twing.js](https://twing.nightlycommit.com/).
138
+
139
+ #### [Twig.js](https://github.com/twigjs/twig.js)
136
140
 
137
141
  ```js
138
142
  import { join } from 'node:path' // 1. Add dependencies.
@@ -167,6 +171,61 @@ const config = {
167
171
  export default config
168
172
  ```
169
173
 
174
+ #### [Twing.js](https://twing.nightlycommit.com/)
175
+
176
+ ```js
177
+ import { join } from 'node:path' // 1. Add dependencies.
178
+ import { cwd } from 'node:process'
179
+
180
+ const config = {
181
+ stories: ['../components/**/*.component.yml'], // 2. Set components glob.
182
+ addons: [
183
+ {
184
+ name: 'storybook-addon-sdc', // 3. Configure addon.
185
+ options: {
186
+ sdcStorybookOptions: {
187
+ twigLib: 'twing',
188
+ namespace: 'umami', // Your namespace.
189
+ },
190
+ vitePluginTwingDrupalOptions: {
191
+ namespaces: {
192
+ umami: [join(cwd(), './components')],
193
+ },
194
+ // (Optional) With twing hooks you can adjust twing environment.
195
+ hooks: join(cwd(), '.storybook/twing-hooks.js'),
196
+ },
197
+ jsonSchemaFakerOptions: {}, // json-schema-faker options.
198
+ },
199
+ },
200
+ // Any other addons.
201
+ '@chromatic-com/storybook',
202
+ ],
203
+ framework: {
204
+ name: '@storybook/html-vite',
205
+ options: {},
206
+ },
207
+ }
208
+ export default config
209
+ ```
210
+
211
+ Sample twing hook file. See [twing.js documentation](https://twing.nightlycommit.com/) for more infos.
212
+
213
+ ```js
214
+ import { createSynchronousFunction } from 'twing'
215
+
216
+ /**
217
+ * Simple test function.
218
+ */
219
+ function testFunction() {
220
+ return 'IT WORKS!'
221
+ }
222
+
223
+ export function initEnvironment(twingEnvironment, config = {}) {
224
+ const func = createSynchronousFunction('testFunction', testFunction, [])
225
+ twingEnvironment.addFunction(func)
226
+ }
227
+ ```
228
+
170
229
  ## Setting Default Values
171
230
 
172
231
  For `json-schema-faker` to generate reliable data, use `default` or `examples` in your SDC schema:
@@ -391,13 +450,15 @@ const config = {
391
450
 
392
451
  ## Dependencies
393
452
 
394
- - [vite-plugin-twig-drupal](https://github.com/larowlan/vite-plugin-twig-drupal): Loads Twig with Drupal functions.
453
+ - [vite-plugin-twig-drupal](https://github.com/larowlan/vite-plugin-twig-drupal): Loads Twig with Drupal functions in twig.js.
454
+ - [vite-plugin-twing-drupal](https://github.com/christianwiedemann/vite-plugin-twing-drupal): Loads Twig with Drupal functions in Twing.
395
455
  - [json-schema-faker](https://github.com/json-schema-faker/json-schema-faker): Generates mock data for missing props and
396
456
  - [JSON Schema validator](https://www.npmjs.com/package/jsonschema)
397
457
 
398
458
  ## Known Issues
399
459
 
400
460
  - The addon relies on [Experimental indexers](https://storybook.js.org/docs/api/main-config/main-config-indexers).
461
+ - Twing works in Node 23+
401
462
 
402
463
  ## UI Patterns
403
464
 
package/dist/preset.js CHANGED
@@ -345,7 +345,6 @@ var loadStoryFilesSync = (fileName) => {
345
345
  };
346
346
 
347
347
  // src/preset.ts
348
- import twig from "vite-plugin-twig-drupal";
349
348
  import { mergeConfig } from "vite";
350
349
  import { resolve as resolve2 } from "path";
351
350
  import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
@@ -387,7 +386,8 @@ function resolveComponentPath2(namespace, component) {
387
386
  return resolvedPath;
388
387
  }
389
388
  var defaultOptions = {
390
- validate: false
389
+ validate: false,
390
+ twigLib: "twig"
391
391
  // validate:
392
392
  // 'https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json',
393
393
  };
@@ -422,9 +422,21 @@ async function viteFinal(config, options) {
422
422
  };
423
423
  const { namespace, customDefs, externalDefs } = options.sdcStorybookOptions;
424
424
  const globalDefs = await loadAndMergeDefinitions(externalDefs, customDefs);
425
+ const { nodePolyfills } = await import("vite-plugin-node-polyfills");
426
+ let twigPlugin = null;
427
+ if (options.sdcStorybookOptions.twigLib === "twing") {
428
+ const { default: twing } = await import("vite-plugin-twing-drupal");
429
+ twigPlugin = twing(options.vitePluginTwingDrupalOptions);
430
+ } else if (options.sdcStorybookOptions.twigLib === "twig") {
431
+ const { default: twig } = await import("vite-plugin-twig-drupal");
432
+ twigPlugin = twig(options.vitePluginTwigDrupalOptions);
433
+ }
425
434
  return mergeConfig(config, {
426
435
  plugins: [
427
- twig(options.vitePluginTwigDrupalOptions),
436
+ nodePolyfills({
437
+ include: ["buffer", "stream", "path"]
438
+ }),
439
+ ...twigPlugin ? [twigPlugin] : [],
428
440
  vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs })
429
441
  ],
430
442
  resolve: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-addon-sdc",
3
- "version": "0.8.3",
3
+ "version": "0.9.2",
4
4
  "description": "Drupal Single Directory Components as stories",
5
5
  "keywords": [
6
6
  "SDC",
@@ -43,14 +43,15 @@
43
43
  "node-fetch": "^3.3.2",
44
44
  "pino": "^9.6.0",
45
45
  "pino-pretty": "^13.0.0",
46
- "vite-plugin-twig-drupal": "^1.6.0",
46
+ "vite-plugin-twing-drupal": "^1.0.13",
47
+ "vite-plugin-twig-drupal": "^1.6.1",
47
48
  "yaml": "^2.7.1"
48
49
  },
49
50
  "devDependencies": {
50
- "@storybook/addon-links": "^9.0.17",
51
- "@storybook/html-vite": "^9.0.17",
51
+ "@storybook/addon-links": "^9.1.2",
52
+ "@storybook/html-vite": "^9.1.2",
52
53
  "@types/json-schema": "^7.0.15",
53
- "@types/node": "^22.13.14",
54
+ "@types/node": "^24.2.1",
54
55
  "@types/node-fetch": "^2.6.12",
55
56
  "@types/twig": "^1.12.16",
56
57
  "auto": "^11.3.0",
@@ -60,7 +61,7 @@
60
61
  "prompts": "^2.4.2",
61
62
  "react": "^19.1.0",
62
63
  "react-dom": "^19.1.0",
63
- "storybook": "^9.0.17",
64
+ "storybook": "^9.1.2",
64
65
  "tsup": "^8.4.0",
65
66
  "typescript": "^5.8.2",
66
67
  "vite": "^6.2.3",