1
- import {
2
- BuilderContext ,
3
- BuilderHandlerFn ,
4
- BuilderOutput ,
5
- BuilderOutputLike ,
6
- Target ,
7
- createBuilder ,
8
- targetFromTargetString ,
9
- } from '@angular-devkit/architect' ;
1
+ import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
10
2
import { JsonObject } from '@angular-devkit/core' ;
11
- import { from , of , throwError } from 'rxjs' ;
12
- import { catchError , map , mapTo , switchMap } from 'rxjs/operators' ;
13
3
import { sync as findUpSync } from 'find-up' ;
14
4
import { sync as readUpSync } from 'read-pkg-up' ;
15
- import { BrowserBuilderOptions , StylePreprocessorOptions } from '@angular-devkit/build-angular' ;
5
+ import { StylePreprocessorOptions } from '@angular-devkit/build-angular' ;
16
6
17
7
import { CLIOptions } from '@storybook/types' ;
18
8
import { getEnvConfig , versions } from '@storybook/core-common' ;
@@ -22,11 +12,13 @@ import { buildStaticStandalone, withTelemetry } from '@storybook/core-server';
22
12
import {
23
13
AssetPattern ,
24
14
SourceMapUnion ,
15
+ StyleClass ,
25
16
StyleElement ,
26
17
} from '@angular-devkit/build-angular/src/builders/browser/schema' ;
27
18
import { StandaloneOptions } from '../utils/standalone-options' ;
28
19
import { runCompodoc } from '../utils/run-compodoc' ;
29
20
import { errorSummary , printErrorDetails } from '../utils/error-handler' ;
21
+ import { setup } from '../utils/setup' ;
30
22
31
23
addToGlobalContext ( 'cliVersion' , versions . storybook ) ;
32
24
@@ -59,119 +51,86 @@ export type StorybookBuilderOptions = JsonObject & {
59
51
60
52
export type StorybookBuilderOutput = JsonObject & BuilderOutput & { [ key : string ] : any } ;
61
53
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
- } ;
54
+ type StandaloneBuildOptions = StandaloneOptions & { outputDir : string ; excludeChunks : string [ ] } ;
142
55
143
- export default createBuilder ( commandBuilder ) ;
56
+ const commandBuilder = async (
57
+ options : StorybookBuilderOptions ,
58
+ context : BuilderContext
59
+ ) : Promise < BuilderOutput > => {
60
+ const { tsConfig, angularBuilderContext, angularBuilderOptions } = await setup ( options , context ) ;
144
61
145
- async function setup ( options : StorybookBuilderOptions , context : BuilderContext ) {
146
- let browserOptions : ( JsonObject & BrowserBuilderOptions ) | undefined ;
147
- let browserTarget : Target | undefined ;
62
+ const docTSConfig = findUpSync ( 'tsconfig.doc.json' , { cwd : options . configDir } ) ;
148
63
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 )
64
+ if ( options . compodoc ) {
65
+ await runCompodoc (
66
+ { compodocArgs : options . compodocArgs , tsconfig : docTSConfig ?? tsConfig } ,
67
+ context
154
68
) ;
155
69
}
156
70
157
- return {
158
- tsConfig :
159
- options . tsConfig ??
160
- findUpSync ( 'tsconfig.json' , { cwd : options . configDir } ) ??
161
- browserOptions . tsConfig ,
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,
162
113
} ;
163
- }
164
114
165
- function runInstance ( options : StandaloneBuildOptions ) {
166
- return from (
167
- withTelemetry (
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 (
168
125
'build' ,
169
126
{
170
127
cliOptions : options ,
171
128
presetOptions : { ...options , corePresets : [ ] , overridePresets : [ ] } ,
172
129
printError : printErrorDetails ,
173
130
} ,
174
131
( ) => buildStaticStandalone ( options )
175
- )
176
- ) . pipe ( catchError ( ( error : any ) => throwError ( errorSummary ( error ) ) ) ) ;
132
+ ) ;
133
+ } catch ( error ) {
134
+ throw new Error ( errorSummary ( error ) ) ;
135
+ }
177
136
}
0 commit comments