Skip to content

Commit

Permalink
Merge pull request #9646 from storybookjs/fix/9443-assets-in-manager
Browse files Browse the repository at this point in the history
Core: Fix manager assets to be routed in express
  • Loading branch information
shilman committed Mar 31, 2020
1 parent 6fb4266 commit b633e48
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
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 @@ -5,8 +5,14 @@ import '@storybook/addon-preview-wrapper/register';
import { addons } from '@storybook/addons';
import { themes } from '@storybook/theming';

import logo from './logo.svg';

addons.setConfig({
theme: themes.dark,
theme: {
brandImage: logo,
brandTitle: 'Custom - Storybook',
...themes.dark,
},
panelPosition: 'bottom',
selectedPanel: 'storybook/roundtrip',
});
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 @@ -41,35 +41,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 @@ -109,17 +113,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 @@ -130,16 +135,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 @@ -152,10 +168,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

0 comments on commit b633e48

Please sign in to comment.