@analogjs/storybook-angular 1.20.0 → 1.20.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 CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  Integration package for Storybook using Angular & Vite.
4
4
 
5
+ > This is a community integration not maintained by the Storybook team. If you have issues,
6
+ > file an issue in our [GitHub repo](https://github.com/analogjs/analog/issues).
7
+
5
8
  ## Setup
6
9
 
7
10
  If you don't have Storybook setup already, run the following command to initialize Storybook for your project:
@@ -22,33 +25,7 @@ npm install @analogjs/storybook-angular --save-dev
22
25
 
23
26
  ## Configuring Storybook
24
27
 
25
- Add the `zone.js` import to the top of the `.storybook/preview.ts` file.
26
-
27
- ```ts
28
- import 'zone.js';
29
- import { applicationConfig, type Preview } from '@analogjs/storybook-angular';
30
- import { provideNoopAnimations } from '@angular/platform-browser/animations';
31
-
32
- const preview: Preview = {
33
- decorators: [
34
- applicationConfig({
35
- providers: [provideNoopAnimations()],
36
- }),
37
- ],
38
- parameters: {
39
- controls: {
40
- matchers: {
41
- color: /(background|color)$/i,
42
- date: /Date$/i,
43
- },
44
- },
45
- },
46
- };
47
-
48
- export default preview;
49
- ```
50
-
51
- Next, update the `.storybook/main.ts` file to use the `StorybookConfig`. Also update the `framework` to use the `@analogjs/storybook-angular` package.
28
+ Update the `.storybook/main.ts` file to use the `StorybookConfig` type. Also update the `framework` to use the `@analogjs/storybook-angular` package.
52
29
 
53
30
  ```ts
54
31
  import { StorybookConfig } from '@analogjs/storybook-angular';
@@ -60,11 +37,13 @@ const config: StorybookConfig = {
60
37
  options: {},
61
38
  },
62
39
  };
40
+
41
+ export default config;
63
42
  ```
64
43
 
65
44
  Remove the existing `webpackFinal` config function if present.
66
45
 
67
- Next, update the Storybook builders in the `angular.json` or `project.json`.
46
+ Next, update the Storybook targets in the `angular.json` or `project.json`
68
47
 
69
48
  ```json
70
49
  "storybook": {
@@ -77,7 +56,38 @@ Next, update the Storybook builders in the `angular.json` or `project.json`.
77
56
 
78
57
  Remove any `webpack` specific options and remove the `browserTarget` option.
79
58
 
80
- Add the `/storybook-static` folder to your `.gitignore` file.
59
+ Add the `/storybook-static` folder to the `.gitignore` file.
60
+
61
+ ## Setting up CSS
62
+
63
+ To register global styles, add them to the `@analogjs/storybook-angular` builder options in the `angular.json` or `project.json`.
64
+
65
+ ```json
66
+ "storybook": {
67
+ "builder": "@analogjs/storybook-angular:start-storybook",
68
+ "options": {
69
+ // ... other options
70
+ "styles": [
71
+ "src/styles.css"
72
+ ],
73
+ "stylePreprocessorOptions": {
74
+ "loadPaths": ["libs/my-lib/styles"]
75
+ }
76
+ }
77
+ },
78
+ "build-storybook": {
79
+ "builder": "@analogjs/storybook-angular:build-storybook",
80
+ "options": {
81
+ // ... other options
82
+ "styles": [
83
+ "src/styles.css"
84
+ ],
85
+ "stylePreprocessorOptions": {
86
+ "loadPaths": ["libs/my-lib/styles"]
87
+ }
88
+ }
89
+ }
90
+ ```
81
91
 
82
92
  ## Running Storybook
83
93
 
@@ -95,27 +105,6 @@ Run the command for building the storybook.
95
105
  npm run build-storybook
96
106
  ```
97
107
 
98
- ## Using shared CSS paths
99
-
100
- To load shared CSS paths, configure them using `loadPaths` css option in the `viteFinal` function.
101
-
102
- ```ts
103
- import path from 'node:path';
104
- import { UserConfig, mergeConfig } from 'vite';
105
-
106
- export async function viteFinal(config: UserConfig) {
107
- return mergeConfig(config, {
108
- css: {
109
- preprocessorOptions: {
110
- scss: {
111
- loadPaths: `${path.resolve(__dirname, '../src/lib/styles')}`,
112
- },
113
- },
114
- },
115
- });
116
- }
117
- ```
118
-
119
108
  ## Using TypeScript Config Path Aliases
120
109
 
121
110
  If you are using `paths` in your `tsconfig.json`, support for those aliases can be added to the `vite.config.ts`.
@@ -134,11 +123,18 @@ Next, add the plugin to the `plugins` array in the `.storybook/main.ts`.
134
123
  import viteTsConfigPaths from 'vite-tsconfig-paths';
135
124
  import { UserConfig, mergeConfig } from 'vite';
136
125
 
137
- export async function viteFinal(config: UserConfig) {
138
- return mergeConfig(config, {
139
- plugins: [viteTsConfigPaths()],
140
- });
141
- }
126
+ import type { StorybookConfig } from '@analogjs/storybook-angular';
127
+
128
+ const config: StorybookConfig = {
129
+ // ... other config, addons, etc.
130
+ async viteFinal(config: UserConfig) {
131
+ return mergeConfig(config, {
132
+ plugins: [viteTsConfigPaths()],
133
+ });
134
+ },
135
+ };
136
+
137
+ export default config;
142
138
  ```
143
139
 
144
140
  ### With Nx
@@ -149,11 +145,18 @@ For Nx workspaces, import and use the `nxViteTsPaths` plugin from the `@nx/vite`
149
145
  import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
150
146
  import { UserConfig, mergeConfig } from 'vite';
151
147
 
152
- export async function viteFinal(config: UserConfig) {
153
- return mergeConfig(config, {
154
- plugins: [nxViteTsPaths()],
155
- });
156
- }
148
+ import type { StorybookConfig } from '@analogjs/storybook-angular';
149
+
150
+ const config: StorybookConfig = {
151
+ // ... other config, addons, etc.
152
+ async viteFinal(config: UserConfig) {
153
+ return mergeConfig(config, {
154
+ plugins: [nxViteTsPaths()],
155
+ });
156
+ },
157
+ };
158
+
159
+ export default config;
157
160
  ```
158
161
 
159
162
  ## Using File Replacements
@@ -166,18 +169,25 @@ Import the plugin and set it up:
166
169
  import { replaceFiles } from '@nx/vite/plugins/rollup-replace-files.plugin';
167
170
  import { UserConfig, mergeConfig } from 'vite';
168
171
 
169
- export async function viteFinal(config: UserConfig) {
170
- return mergeConfig(config, {
171
- plugins: [
172
- replaceFiles([
173
- {
174
- replace: './src/one.ts',
175
- with: './src/two.ts',
176
- },
177
- ]),
178
- ],
179
- });
180
- }
172
+ import type { StorybookConfig } from '@analogjs/storybook-angular';
173
+
174
+ const config: StorybookConfig = {
175
+ // ... other config, addons, etc.
176
+ async viteFinal(config: UserConfig) {
177
+ return mergeConfig(config, {
178
+ plugins: [
179
+ replaceFiles([
180
+ {
181
+ replace: './src/one.ts',
182
+ with: './src/two.ts',
183
+ },
184
+ ]),
185
+ ],
186
+ });
187
+ },
188
+ };
189
+
190
+ export default config;
181
191
  ```
182
192
 
183
193
  Adding the replacement files to `files` array in the `tsconfig.app.json` may also be necessary.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@analogjs/storybook-angular",
3
- "version": "1.20.0",
3
+ "version": "1.20.1",
4
4
  "description": "Storybook Integration for Angular & Vite",
5
5
  "keywords": [
6
6
  "storybook",
package/preset.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { dirname, join } from 'node:path';
1
+ import { dirname, join, resolve } from 'node:path';
2
2
  import { createRequire } from 'node:module';
3
3
  import { core as PresetCore } from '@storybook/angular/dist/preset.js';
4
4
  const require = createRequire(import.meta.url);
@@ -21,7 +21,7 @@ export const viteFinal = async (config, options) => {
21
21
  .filter((plugin) => !plugin.name.includes('analogjs'));
22
22
 
23
23
  // Merge custom configuration into the default config
24
- const { mergeConfig } = await import('vite');
24
+ const { mergeConfig, normalizePath } = await import('vite');
25
25
  const { default: angular } = await import('@analogjs/vite-plugin-angular');
26
26
  // @ts-ignore
27
27
  const framework = await options.presets.apply('framework');
@@ -35,7 +35,9 @@ export const viteFinal = async (config, options) => {
35
35
  '@angular/platform-browser',
36
36
  '@angular/platform-browser/animations',
37
37
  'tslib',
38
- 'zone.js',
38
+ ...(options?.angularBuilderOptions?.experimentalZoneless
39
+ ? []
40
+ : ['zone.js']),
39
41
  ],
40
42
  },
41
43
  plugins: [
@@ -54,6 +56,7 @@ export const viteFinal = async (config, options) => {
54
56
  ? framework.options?.inlineStylesExtension
55
57
  : 'css',
56
58
  }),
59
+ angularOptionsPlugin(options, { normalizePath }),
57
60
  ],
58
61
  define: {
59
62
  STORYBOOK_ANGULAR_OPTIONS: JSON.stringify({
@@ -63,5 +66,64 @@ export const viteFinal = async (config, options) => {
63
66
  },
64
67
  });
65
68
  };
69
+
70
+ function angularOptionsPlugin(options, { normalizePath }) {
71
+ return {
72
+ name: 'analogjs-storybook-options-plugin',
73
+ config() {
74
+ const loadPaths =
75
+ options?.angularBuilderOptions?.stylePreprocessorOptions?.loadPaths;
76
+
77
+ if (Array.isArray(loadPaths)) {
78
+ return {
79
+ css: {
80
+ preprocessorOptions: {
81
+ scss: {
82
+ loadPaths: loadPaths.map(
83
+ (loadPath) =>
84
+ `${resolve(options.angularBuilderContext.workspaceRoot, loadPath)}`,
85
+ ),
86
+ },
87
+ },
88
+ },
89
+ };
90
+ }
91
+
92
+ return;
93
+ },
94
+ transform(code, id) {
95
+ if (
96
+ normalizePath(id).endsWith(
97
+ normalizePath(`${options.configDir}/preview.ts`),
98
+ )
99
+ ) {
100
+ const imports = [];
101
+ const styles = options?.angularBuilderOptions?.styles;
102
+
103
+ if (Array.isArray(styles)) {
104
+ styles.forEach((style) => {
105
+ imports.push(style);
106
+ });
107
+ }
108
+
109
+ const zoneless = options?.angularBuilderOptions?.experimentalZoneless;
110
+
111
+ if (!zoneless) {
112
+ imports.push('zone.js');
113
+ }
114
+
115
+ return {
116
+ code: `
117
+ ${imports.map((extraImport) => `import '${extraImport}';`).join('\n')}
118
+ ${code}
119
+ `,
120
+ };
121
+ }
122
+
123
+ return;
124
+ },
125
+ };
126
+ }
127
+
66
128
  export { addons, previewAnnotations } from '@storybook/angular/dist/preset.js';
67
129
  //# sourceMappingURL=preset.js.map
@@ -69,6 +69,28 @@
69
69
  "type": "boolean",
70
70
  "description": "Experimental: Use zoneless change detection.",
71
71
  "default": false
72
+ },
73
+ "styles": {
74
+ "type": "array",
75
+ "description": "Global styles to be included in the build.",
76
+ "default": [],
77
+ "items": {
78
+ "type": "string"
79
+ }
80
+ },
81
+ "stylePreprocessorOptions": {
82
+ "description": "Options to pass to style preprocessors.",
83
+ "type": "object",
84
+ "properties": {
85
+ "loadPaths": {
86
+ "description": "Paths to include.",
87
+ "type": "array",
88
+ "items": {
89
+ "type": "string"
90
+ }
91
+ }
92
+ },
93
+ "additionalProperties": false
72
94
  }
73
95
  },
74
96
  "additionalProperties": false
@@ -105,6 +105,28 @@
105
105
  "type": "boolean",
106
106
  "description": "Experimental: Use zoneless change detection.",
107
107
  "default": false
108
+ },
109
+ "styles": {
110
+ "type": "array",
111
+ "description": "Global styles to be included in the build.",
112
+ "default": [],
113
+ "items": {
114
+ "type": "string"
115
+ }
116
+ },
117
+ "stylePreprocessorOptions": {
118
+ "description": "Options to pass to style preprocessors.",
119
+ "type": "object",
120
+ "properties": {
121
+ "loadPaths": {
122
+ "description": "Paths to include.",
123
+ "type": "array",
124
+ "items": {
125
+ "type": "string"
126
+ }
127
+ }
128
+ },
129
+ "additionalProperties": false
108
130
  }
109
131
  },
110
132
  "additionalProperties": false