Skip to content

Commit

Permalink
Merge pull request #13245 from storybookjs/better-statics-dir
Browse files Browse the repository at this point in the history
Core: Improve handling of --static-dir option
  • Loading branch information
shilman committed Nov 25, 2020
2 parents 92e082f + 5e285bb commit 75d1302
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/core/src/server/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,25 @@ async function useStatics(router: any, options: { staticDir?: string[] }) {
if (staticDir && staticDir.length) {
await Promise.all(
staticDir.map(async (dir) => {
const [currentStaticDir, staticEndpoint] = dir.split(':').concat('/');
const localStaticPath = path.resolve(currentStaticDir);

if (!(await pathExists(localStaticPath))) {
logger.error(`Error: no such directory to load static files: ${localStaticPath}`);
process.exit(-1);
const [dirname, location = '/'] = dir.split(':');
const dirpath = path.resolve(dirname);
const endpoint = location.startsWith('/') ? location : `/${location}`;

if (!(await pathExists(dirpath))) {
logger.warn(`Failed to load static files, no such directory: ${dirpath}`);
logger.warn(`You should create this directory, or omit the -s (--static-dir) option.`);
return;
}

logger.info(
`=> Loading static files from: ${localStaticPath} and serving at ${staticEndpoint} .`
);
router.use(staticEndpoint, express.static(localStaticPath, { index: false }));

const faviconPath = path.resolve(localStaticPath, 'favicon.ico');
logger.info(`=> Loading static files from ${dirpath} and serving at ${endpoint}.`);
router.use(endpoint, express.static(dirpath, { index: false }));

if (await pathExists(faviconPath)) {
hasCustomFavicon = true;
router.use(favicon(faviconPath));
if (!hasCustomFavicon) {
const faviconPath = path.resolve(dirpath, 'favicon.ico');
if (await pathExists(faviconPath)) {
hasCustomFavicon = true;
router.use(favicon(faviconPath));
}
}
})
);
Expand Down

0 comments on commit 75d1302

Please sign in to comment.