Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core - Allow ignoring preview bundle building in core #5523

Merged
merged 1 commit into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/core/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
33 changes: 24 additions & 9 deletions lib/core/src/server/build-dev.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}` : ''}
`,
Expand All @@ -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'))}`);
}
Expand All @@ -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];
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
113 changes: 58 additions & 55 deletions lib/core/src/server/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/server/manager/manager-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down