import webpack from 'webpack' ; import { DigestConfig , defaultConfig } from './webpack.config' ; export async function digestStories ( digestConfig ) { console . log ( 'Bundling digest..' ) ; const defaultConfigs = defaultConfig ( digestConfig ) ; const webpackConfigs = [ ] ; defaultConfigs . forEach ( webpackConfig => { const mergedConfig = { ... webpackConfig , output : { ... webpackConfig . output , ... { path : digestConfig . outputDir } } , resolve : { ... webpackConfig . resolve , alias : { ... webpackConfig . resolve . alias , ... { stories : digestConfig . configDir } } } } ; webpackConfigs . push ( mergedConfig ) ; } ) ; const webpackTasks = webpackConfigs . map ( webpackConfig => new Promise ( ( resolve , reject ) => { webpack ( webpackConfig ) . run ( ( error , stats ) => { if ( error || ! stats || stats . hasErrors ( ) ) { console . error ( 'Failed to create bundle.' ) ; if ( error ) { console . error ( error . message ) ; } if ( stats && ( stats . hasErrors ( ) || stats . hasWarnings ( ) ) ) { const { warnings , errors } = stats . toJson ( ) ; errors . forEach ( e => console . error ( e ) ) ; warnings . forEach ( e => console . error ( e ) ) ; } process . exitCode = 1 ; reject ( error || stats ) ; return ; } stats . toJson ( ) . warnings . forEach ( e => console . warn ( e ) ) ; resolve ( stats ) ; } ) ; } ) ) ; return Promise . all ( webpackTasks ) ; } export function generateUrl ( baseUrl , kind , story , iterations ? ) { return `${ baseUrl }?selectedKind=${ kind }&selectedStory=${ story }${ iterations ? `&iterations=${ iterations }` : '' }` ; }