From 27d1cddda1a67a3c1350455673b7c3caacb97b12 Mon Sep 17 00:00:00 2001 From: igor Date: Fri, 8 Feb 2019 16:53:21 +0200 Subject: [PATCH] Allow ignoring preview bundle in core --- lib/core/server.js | 10 +- lib/core/src/server/build-dev.js | 33 +++-- lib/core/src/server/dev-server.js | 113 +++++++++--------- lib/core/src/server/manager/manager-preset.js | 4 +- 4 files changed, 93 insertions(+), 67 deletions(-) diff --git a/lib/core/server.js b/lib/core/server.js index 95d61363f855..4a2b60a83290 100644 --- a/lib/core/server.js +++ b/lib/core/server.js @@ -3,4 +3,12 @@ const serverUtils = require('./dist/server/utils/template'); const buildStatic = require('./dist/server/build-static'); const buildDev = require('./dist/server/build-dev'); -module.exports = Object.assign({}, defaultWebpackConfig, buildStatic, buildDev, serverUtils); +const managerPreset = require.resolve('./dist/server/manager/manager-preset'); + +module.exports = Object.assign( + { managerPreset }, + defaultWebpackConfig, + buildStatic, + buildDev, + serverUtils +); diff --git a/lib/core/src/server/build-dev.js b/lib/core/src/server/build-dev.js index 9f29d6e281d4..b67ee9ce7644 100644 --- a/lib/core/src/server/build-dev.js +++ b/lib/core/src/server/build-dev.js @@ -1,5 +1,6 @@ import express from 'express'; import https from 'https'; +import http from 'http'; import ip from 'ip'; import favicon from 'serve-favicon'; import path from 'path'; @@ -36,7 +37,7 @@ const writeStats = async (name, stats) => { async function getServer(app, options) { if (!options.https) { - return app; + return http.createServer(app); } if (!options.sslCert) { @@ -199,15 +200,18 @@ function outputStartupInformation(options) { ['On your network:', chalk.cyan(networkAddress)] ); + const timeStatement = previewTotalTime + ? `${chalk.underline(prettyTime(managerTotalTime))} for manager and ${chalk.underline( + prettyTime(previewTotalTime) + )} for preview` + : `${chalk.underline(prettyTime(managerTotalTime))}`; + // eslint-disable-next-line no-console console.log( boxen( stripIndents` ${colors.green(`Storybook ${chalk.bold(version)} started`)} - ${chalk.gray(stripIndents` - ${chalk.underline(prettyTime(managerTotalTime))} for manager and ${chalk.underline( - prettyTime(previewTotalTime) - )} for preview`)} + ${chalk.gray(timeStatement)} ${serveMessage.toString()}${updateMessage ? `\n\n${updateMessage}` : ''} `, @@ -217,7 +221,7 @@ function outputStartupInformation(options) { } async function outputStats(previewStats, managerStats) { - await writeStats('preview', previewStats); + if (previewStats) await writeStats('preview', previewStats); await writeStats('manager', managerStats); logger.info(`stats written to => ${chalk.cyan(path.join(cacheDir, '[name].json'))}`); } @@ -234,7 +238,7 @@ function openInBrowser(address) { export async function buildDevStandalone(options) { try { - const { port, host } = options; + const { port, host, extendServer } = options; // Used with `app.listen` below const listenAddr = [port]; @@ -246,6 +250,10 @@ export async function buildDevStandalone(options) { const app = express(); const server = await getServer(app, options); + if (typeof extendServer === 'function') { + extendServer(server); + } + await applyStatic(app, options); const { @@ -279,8 +287,15 @@ export async function buildDevStandalone(options) { if (options.smokeTest) { await outputStats(previewStats, managerStats); - process.exit(previewStats.toJson().warnings.length ? 1 : 0); - process.exit(managerStats.toJson().warnings.length ? 1 : 0); + let warning = 0; + + if (!options.ignorePreview) { + warning += previewStats.toJson().warnings.length; + } + + warning += managerStats.toJson().warnings.length; + + process.exit(warning ? 1 : 0); } else if (!options.ci) { openInBrowser(address); } diff --git a/lib/core/src/server/dev-server.js b/lib/core/src/server/dev-server.js index fa212aa11c11..c2a5cfb557c0 100644 --- a/lib/core/src/server/dev-server.js +++ b/lib/core/src/server/dev-server.js @@ -71,63 +71,66 @@ export default function(options) { }) ); - const previewPromise = loadConfig({ - configType, - outputDir, - cache, - corePresets: [require.resolve('./preview/preview-preset.js')], - overridePresets: [require.resolve('./preview/custom-webpack-preset.js')], - ...options, - }).then(previewConfig => { - const middlewareFn = getMiddleware(configDir); - - // remove the leading '/' - let { publicPath } = previewConfig.output; - if (publicPath[0] === '/') { - publicPath = publicPath.slice(1); - } - - const previewCompiler = webpack(previewConfig); - const devMiddlewareOptions = { - publicPath: previewConfig.output.publicPath, - watchOptions: { - aggregateTimeout: 1, - ignored: /node_modules/, - ...(previewConfig.watchOptions || {}), - }, - // this actually causes 0 (regular) output from wdm & webpack - logLevel: 'warn', - clientLogLevel: 'warning', - noInfo: true, - ...previewConfig.devServer, - }; - - const webpackDevMiddlewareInstance = webpackDevMiddleware( - previewCompiler, - devMiddlewareOptions - ); - router.use(webpackDevMiddlewareInstance); - router.use(webpackHotMiddleware(previewCompiler)); - - // custom middleware - middlewareFn(router); - - return new Promise((resolve, reject) => { - previewReject = reject; - webpackDevMiddlewareInstance.waitUntilValid(stats => { - previewTotalTime = process.hrtime(startTime); - - if (!stats) { - reject(new Error('no stats after building preview')); - } else if (stats.hasErrors()) { - reject(stats); - } else { - resolve(stats); + const previewPromise = options.ignorePreview + ? new Promise(resolve => resolve()) + : loadConfig({ + configType, + outputDir, + cache, + corePresets: [require.resolve('./preview/preview-preset.js')], + overridePresets: [require.resolve('./preview/custom-webpack-preset.js')], + ...options, + }).then(previewConfig => { + // remove the leading '/' + let { publicPath } = previewConfig.output; + if (publicPath[0] === '/') { + publicPath = publicPath.slice(1); } + + const previewCompiler = webpack(previewConfig); + + const devMiddlewareOptions = { + publicPath: previewConfig.output.publicPath, + watchOptions: { + aggregateTimeout: 1, + ignored: /node_modules/, + ...(previewConfig.watchOptions || {}), + }, + // this actually causes 0 (regular) output from wdm & webpack + logLevel: 'warn', + clientLogLevel: 'warning', + noInfo: true, + ...previewConfig.devServer, + }; + + const webpackDevMiddlewareInstance = webpackDevMiddleware( + previewCompiler, + devMiddlewareOptions + ); + + router.use(webpackDevMiddlewareInstance); + router.use(webpackHotMiddleware(previewCompiler)); + + return new Promise((resolve, reject) => { + previewReject = reject; + webpackDevMiddlewareInstance.waitUntilValid(stats => { + previewTotalTime = process.hrtime(startTime); + + if (!stats) { + reject(new Error('no stats after building preview')); + } else if (stats.hasErrors()) { + reject(stats); + } else { + resolve(stats); + } + }); + previewProcess = webpackDevMiddlewareInstance; + }); }); - previewProcess = webpackDevMiddlewareInstance; - }); - }); + + // custom middleware + const middlewareFn = getMiddleware(configDir); + middlewareFn(router); return Promise.all([managerPromise, previewPromise]).then(([managerStats, previewStats]) => { resolved = true; diff --git a/lib/core/src/server/manager/manager-preset.js b/lib/core/src/server/manager/manager-preset.js index ce09dfb65df3..7b4906d45ea6 100644 --- a/lib/core/src/server/manager/manager-preset.js +++ b/lib/core/src/server/manager/manager-preset.js @@ -6,7 +6,7 @@ export async function managerWebpack(_, options) { } export async function managerEntries(_, options) { - const { presets } = options; + const { presets, managerEntry = '../../client/manager' } = options; const entries = [require.resolve('../common/polyfills')]; const installedAddons = await presets.apply('addons', [], options); @@ -15,7 +15,7 @@ export async function managerEntries(_, options) { entries.push(...installedAddons); } - entries.push(require.resolve('../../client/manager')); + entries.push(require.resolve(managerEntry)); return entries; }