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: Fix manager assets to be routed in express #9646

Merged
merged 6 commits into from
Feb 6, 2020
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
3 changes: 3 additions & 0 deletions examples/dev-kits/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion examples/dev-kits/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import { useAddonState, useStoryState } from '@storybook/api';
import { themes } from '@storybook/theming';
import { AddonPanel } from '@storybook/components';

import logo from './logo.svg';

addons.setConfig({
theme: themes.dark,
theme: {
brandImage: logo,
brandTitle: 'Custom - Storybook',
...themes.dark,
},
panelPosition: 'bottom',
selectedPanel: 'storybook/roundtrip',
});
Expand Down
84 changes: 48 additions & 36 deletions lib/core/src/server/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,39 @@ export default function(options) {
if (options.debugWebpack) {
logConfig('Manager webpack config', config, logger);
}
const managerCompiler = webpack(config);

const devMiddlewareOptions = {
publicPath: config.output.publicPath,
watchOptions: {
aggregateTimeout: 2000,
ignored: /node_modules/,
},
// this actually causes 0 (regular) output from wdm & webpack
logLevel: 'warn',
clientLogLevel: 'warning',
noInfo: true,
};

const managerDevMiddlewareInstance = webpackDevMiddleware(
managerCompiler,
devMiddlewareOptions
);

router.use(managerDevMiddlewareInstance);

return new Promise((resolve, reject) => {
webpack(config).watch(
{
aggregateTimeout: 1,
ignored: /node_modules/,
},
(err, stats) => {
managerTotalTime = process.hrtime(startTime);
if (!resolved && (err || stats.hasErrors())) {
const error = new Error('Manager build is broken');
error.error = err;
error.close = true;
error.stats = stats;
logger.line();
logger.line();
try {
previewReject(error);
previewProcess.close();
logger.warn('force closed preview build');
} catch (e) {
logger.warn('Unable to close preview build!');
}
logger.line();
reject(error);
} else {
resolve(stats);
}
managerDevMiddlewareInstance.waitUntilValid(stats => {
managerTotalTime = process.hrtime(startTime);

if (!stats) {
reject(new Error('no stats after building preview'));
} else if (stats.hasErrors()) {
reject(stats);
} else {
resolve(stats);
}
);
});
});
});

Expand Down Expand Up @@ -110,17 +114,18 @@ export default function(options) {
...previewConfig.devServer,
};

const webpackDevMiddlewareInstance = webpackDevMiddleware(
const previewDevMiddlewareInstance = webpackDevMiddleware(
previewCompiler,
devMiddlewareOptions
);

router.use(webpackDevMiddlewareInstance);
router.use(previewDevMiddlewareInstance);
router.use(webpackHotMiddleware(previewCompiler));

return new Promise((resolve, reject) => {
previewReject = reject;
webpackDevMiddlewareInstance.waitUntilValid(stats => {
previewDevMiddlewareInstance.waitUntilValid(stats => {
resolved = true;
previewTotalTime = process.hrtime(startTime);

if (!stats) {
Expand All @@ -131,16 +136,27 @@ export default function(options) {
resolve(stats);
}
});
previewProcess = webpackDevMiddlewareInstance;
previewProcess = previewDevMiddlewareInstance;
});
});

// custom middleware
const middlewareFn = getMiddleware(configDir);
middlewareFn(router);

managerPromise.catch(e => {
try {
if (!resolved) {
previewReject();
}
previewProcess.close();
logger.warn('force closed preview build');
} catch (err) {
logger.warn('Unable to close preview build!');
}
});

return Promise.all([managerPromise, previewPromise]).then(([managerStats, previewStats]) => {
resolved = true;
router.get('/', (request, response) => {
response.set('Content-Type', 'text/html');
response.sendFile(path.join(`${outputDir}/index.html`));
Expand All @@ -153,10 +169,6 @@ export default function(options) {
response.set('Content-Type', 'text/html');
response.sendFile(path.join(`${dllPath}/${request.params[0]}`));
});
router.get(/(.+\.js)$/, (request, response) => {
response.set('Content-Type', 'text/javascript');
response.sendFile(path.join(`${outputDir}/${request.params[0]}`));
});

return { previewStats, managerStats, managerTotalTime, previewTotalTime, router };
});
Expand Down