1
- import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
1
+ import {
2
+ BuilderContext ,
3
+ BuilderHandlerFn ,
4
+ BuilderOutput ,
5
+ BuilderOutputLike ,
6
+ Target ,
7
+ createBuilder ,
8
+ targetFromTargetString ,
9
+ } from '@angular-devkit/architect' ;
2
10
import { JsonObject } from '@angular-devkit/core' ;
11
+ import { from , of , throwError } from 'rxjs' ;
12
+ import { catchError , map , mapTo , switchMap } from 'rxjs/operators' ;
3
13
import { sync as findUpSync } from 'find-up' ;
4
14
import { sync as readUpSync } from 'read-pkg-up' ;
5
- import { StylePreprocessorOptions } from '@angular-devkit/build-angular' ;
15
+ import { BrowserBuilderOptions , StylePreprocessorOptions } from '@angular-devkit/build-angular' ;
6
16
7
17
import { CLIOptions } from '@storybook/types' ;
8
18
import { getEnvConfig , versions } from '@storybook/core-common' ;
@@ -12,13 +22,11 @@ import { buildStaticStandalone, withTelemetry } from '@storybook/core-server';
12
22
import {
13
23
AssetPattern ,
14
24
SourceMapUnion ,
15
- StyleClass ,
16
25
StyleElement ,
17
26
} from '@angular-devkit/build-angular/src/builders/browser/schema' ;
18
27
import { StandaloneOptions } from '../utils/standalone-options' ;
19
28
import { runCompodoc } from '../utils/run-compodoc' ;
20
29
import { errorSummary , printErrorDetails } from '../utils/error-handler' ;
21
- import { setup } from '../utils/setup' ;
22
30
23
31
addToGlobalContext ( 'cliVersion' , versions . storybook ) ;
24
32
@@ -51,86 +59,119 @@ export type StorybookBuilderOptions = JsonObject & {
51
59
52
60
export type StorybookBuilderOutput = JsonObject & BuilderOutput & { [ key : string ] : any } ;
53
61
54
- type StandaloneBuildOptions = StandaloneOptions & { outputDir : string ; excludeChunks : string [ ] } ;
62
+ type StandaloneBuildOptions = StandaloneOptions & { outputDir : string } ;
63
+
64
+ const commandBuilder : BuilderHandlerFn < StorybookBuilderOptions > = (
65
+ options ,
66
+ context
67
+ ) : BuilderOutputLike => {
68
+ const builder = from ( setup ( options , context ) ) . pipe (
69
+ switchMap ( ( { tsConfig } ) => {
70
+ const docTSConfig = findUpSync ( 'tsconfig.doc.json' , { cwd : options . configDir } ) ;
71
+ const runCompodoc$ = options . compodoc
72
+ ? runCompodoc (
73
+ { compodocArgs : options . compodocArgs , tsconfig : docTSConfig ?? tsConfig } ,
74
+ context
75
+ ) . pipe ( mapTo ( { tsConfig } ) )
76
+ : of ( { } ) ;
77
+
78
+ return runCompodoc$ . pipe ( mapTo ( { tsConfig } ) ) ;
79
+ } ) ,
80
+ map ( ( { tsConfig } ) => {
81
+ getEnvConfig ( options , {
82
+ staticDir : 'SBCONFIG_STATIC_DIR' ,
83
+ outputDir : 'SBCONFIG_OUTPUT_DIR' ,
84
+ configDir : 'SBCONFIG_CONFIG_DIR' ,
85
+ } ) ;
86
+
87
+ const {
88
+ browserTarget,
89
+ stylePreprocessorOptions,
90
+ styles,
91
+ configDir,
92
+ docs,
93
+ loglevel,
94
+ test,
95
+ outputDir,
96
+ quiet,
97
+ enableProdMode = true ,
98
+ webpackStatsJson,
99
+ statsJson,
100
+ debugWebpack,
101
+ disableTelemetry,
102
+ assets,
103
+ previewUrl,
104
+ sourceMap = false ,
105
+ } = options ;
106
+
107
+ const standaloneOptions : StandaloneBuildOptions = {
108
+ packageJson : readUpSync ( { cwd : __dirname } ) . packageJson ,
109
+ configDir,
110
+ ...( docs ? { docs } : { } ) ,
111
+ loglevel,
112
+ outputDir,
113
+ test,
114
+ quiet,
115
+ enableProdMode,
116
+ disableTelemetry,
117
+ angularBrowserTarget : browserTarget ,
118
+ angularBuilderContext : context ,
119
+ angularBuilderOptions : {
120
+ ...( stylePreprocessorOptions ? { stylePreprocessorOptions } : { } ) ,
121
+ ...( styles ? { styles } : { } ) ,
122
+ ...( assets ? { assets } : { } ) ,
123
+ sourceMap,
124
+ } ,
125
+ tsConfig,
126
+ webpackStatsJson,
127
+ statsJson,
128
+ debugWebpack,
129
+ previewUrl,
130
+ } ;
131
+
132
+ return standaloneOptions ;
133
+ } ) ,
134
+ switchMap ( ( standaloneOptions ) => runInstance ( { ...standaloneOptions , mode : 'static' } ) ) ,
135
+ map ( ( ) => {
136
+ return { success : true } ;
137
+ } )
138
+ ) ;
139
+
140
+ return builder as any as BuilderOutput ;
141
+ } ;
55
142
56
- const commandBuilder = async (
57
- options : StorybookBuilderOptions ,
58
- context : BuilderContext
59
- ) : Promise < BuilderOutput > => {
60
- const { tsConfig, angularBuilderContext, angularBuilderOptions } = await setup ( options , context ) ;
143
+ export default createBuilder ( commandBuilder ) ;
61
144
62
- const docTSConfig = findUpSync ( 'tsconfig.doc.json' , { cwd : options . configDir } ) ;
145
+ async function setup ( options : StorybookBuilderOptions , context : BuilderContext ) {
146
+ let browserOptions : ( JsonObject & BrowserBuilderOptions ) | undefined ;
147
+ let browserTarget : Target | undefined ;
63
148
64
- if ( options . compodoc ) {
65
- await runCompodoc (
66
- { compodocArgs : options . compodocArgs , tsconfig : docTSConfig ?? tsConfig } ,
67
- context
149
+ if ( options . browserTarget ) {
150
+ browserTarget = targetFromTargetString ( options . browserTarget ) ;
151
+ browserOptions = await context . validateOptions < JsonObject & BrowserBuilderOptions > (
152
+ await context . getTargetOptions ( browserTarget ) ,
153
+ await context . getBuilderNameForTarget ( browserTarget )
68
154
) ;
69
155
}
70
156
71
- getEnvConfig ( options , {
72
- staticDir : 'SBCONFIG_STATIC_DIR' ,
73
- outputDir : 'SBCONFIG_OUTPUT_DIR' ,
74
- configDir : 'SBCONFIG_CONFIG_DIR' ,
75
- } ) ;
76
-
77
- const {
78
- configDir,
79
- docs,
80
- loglevel,
81
- test,
82
- outputDir,
83
- quiet,
84
- enableProdMode = true ,
85
- webpackStatsJson,
86
- statsJson,
87
- debugWebpack,
88
- disableTelemetry,
89
- previewUrl,
90
- } = options ;
91
-
92
- const standaloneOptions : StandaloneBuildOptions = {
93
- packageJson : readUpSync ( { cwd : __dirname } ) . packageJson ,
94
- configDir,
95
- ...( docs ? { docs } : { } ) ,
96
- excludeChunks : angularBuilderOptions . styles
97
- ?. filter ( ( style ) => typeof style !== 'string' && style . inject === false )
98
- . map ( ( s : StyleClass ) => s . bundleName ) ,
99
- loglevel,
100
- outputDir,
101
- test,
102
- quiet,
103
- enableProdMode,
104
- disableTelemetry,
105
- angularBrowserTarget : options . browserTarget ,
106
- angularBuilderContext,
107
- angularBuilderOptions,
108
- tsConfig,
109
- webpackStatsJson,
110
- statsJson,
111
- debugWebpack,
112
- previewUrl,
157
+ return {
158
+ tsConfig :
159
+ options . tsConfig ??
160
+ findUpSync ( 'tsconfig.json' , { cwd : options . configDir } ) ??
161
+ browserOptions . tsConfig ,
113
162
} ;
163
+ }
114
164
115
- await runInstance ( { ...standaloneOptions , mode : 'static' } ) ;
116
-
117
- return { success : true } ;
118
- } ;
119
-
120
- export default createBuilder ( commandBuilder ) ;
121
-
122
- async function runInstance ( options : StandaloneBuildOptions ) {
123
- try {
124
- await withTelemetry (
165
+ function runInstance ( options : StandaloneBuildOptions ) {
166
+ return from (
167
+ withTelemetry (
125
168
'build' ,
126
169
{
127
170
cliOptions : options ,
128
171
presetOptions : { ...options , corePresets : [ ] , overridePresets : [ ] } ,
129
172
printError : printErrorDetails ,
130
173
} ,
131
174
( ) => buildStaticStandalone ( options )
132
- ) ;
133
- } catch ( error ) {
134
- throw new Error ( errorSummary ( error ) ) ;
135
- }
175
+ )
176
+ ) . pipe ( catchError ( ( error : any ) => throwError ( errorSummary ( error ) ) ) ) ;
136
177
}
0 commit comments