Skip to content

Commit 0c7d010

Browse files
feat(favicon): change default favicon, handle custom favicon
1 parent 2c51de3 commit 0c7d010

9 files changed

+57
-27
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,19 @@ module.exports = [
192192
]
193193
```
194194

195+
195196
#### Use cases
196197

197198
- Mock ajax calls
198199
- Redirect ajax calls to another endpoint
199200
- Base authorisation
200201
- etc
201202

203+
204+
### `favicon.ico`
205+
206+
Custom project icon
207+
202208
----
203209

204210
## Addons

build_storybook.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const path = require('path');
77
const fs = require('fs');
88

99
const customFs = require('./lib/fs');
10-
const { CUSTOM_PREVIEW_HEADER } = require('./config').paths;
10+
const { CUSTOM_PREVIEW_HEADER, FAVICON_FILE, FALLBACK_FAVICON_FILE } = require('./config').paths;
1111

1212
const getWebpackConfig = require('./lib/get_webpack_config');
1313

@@ -27,7 +27,8 @@ program
2727
Promise.all([
2828
customFs.readFile(CUSTOM_PREVIEW_HEADER),
2929
getWebpackConfig(),
30-
]).then(([header, webpackConfig]) => {
30+
customFs.checkFile(FAVICON_FILE),
31+
]).then(([header, webpackConfig, faviconFileExists]) => {
3132
if (program.output) {
3233
if (!fs.existsSync(program.output)) {
3334
// Make output Directory
@@ -61,6 +62,11 @@ Promise.all([
6162
assets: [].concat(statsJson.assetsByChunkName.preview).map(asset => `${statsJson.publicPath}${asset}`),
6263
}));
6364

65+
customFs.copyFile(
66+
faviconFileExists ? FAVICON_FILE : FALLBACK_FAVICON_FILE,
67+
path.resolve(BUILD_DIR, 'favicon.ico')
68+
);
69+
6470
});
6571
}).catch(console.log);
6672

config.js

+3
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@ module.exports = {
4545

4646
MANAGER_APP: path.resolve(PROJECT_DIR, 'js/manager.js'),
4747
PREVIEW_APP: path.resolve(PROJECT_DIR, 'js/preview.js'),
48+
49+
FAVICON_FILE: path.resolve(TARGET_DIR, storiesConfig.storybookDir, 'favicon.ico'),
50+
FALLBACK_FAVICON_FILE: path.resolve(PROJECT_DIR, 'favicon.ico'),
4851
}
4952
}

favicon.ico

5.68 KB
Binary file not shown.

lib/fs.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ const saveFile = (file, content) => new Promise((resolve, reject) => {
2424
});
2525
});
2626

27+
const copyFile = (source, target) => new Promise((resolve, reject) => {
28+
fs.copyFile(source, target, (err) => {
29+
if(err){
30+
return reject(err);
31+
}
32+
resolve(true);
33+
});
34+
});
35+
2736

28-
module.exports = { readFile, checkFile, saveFile };
37+
module.exports = { readFile, checkFile, saveFile, copyFile };

package-lock.json

+19-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"js-beautify": "^1.7.5",
2929
"modulor": "github:modulor-js/modulor",
3030
"path-to-regexp": "^2.1.0",
31+
"serve-favicon": "^2.5.0",
3132
"split.js": "^1.3.5",
3233
"webpack-dev-middleware": "^3.1.2"
3334
},

server.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
const express = require('express');
44
const webpack = require('webpack');
5+
const favicon = require('serve-favicon');
56
const program = require('commander');
67
const pack = require('./package');
78

89
const customFs = require('./lib/fs');
910
const getWebpackConfig = require('./lib/get_webpack_config');
1011

11-
const { CUSTOM_PREVIEW_HEADER, MIDDLEWARES_FILE } = require('./config').paths;
12+
const { CUSTOM_PREVIEW_HEADER, MIDDLEWARES_FILE, FAVICON_FILE, FALLBACK_FAVICON_FILE } = require('./config').paths;
1213

1314
const managerPageTemplate = require('./templates/manager_page.html');
1415
const previewPageTemplate = require('./templates/preview_page.html');
@@ -38,9 +39,15 @@ Promise.all([
3839
customFs.readFile(CUSTOM_PREVIEW_HEADER),
3940
getWebpackConfig(),
4041
customFs.checkFile(MIDDLEWARES_FILE),
41-
]).then(([header, webpackConfig, customMiddlewaresExist]) => {
42+
customFs.checkFile(FAVICON_FILE),
43+
]).then(([header, webpackConfig, customMiddlewaresExist, faviconFileExists]) => {
4244
const compiler = webpack(webpackConfig);
4345

46+
if(faviconFileExists){
47+
console.log('using custon favicon');
48+
}
49+
app.use(favicon(faviconFileExists ? FAVICON_FILE : FALLBACK_FAVICON_FILE));
50+
4451
app.use(webpackMiddleware(compiler, {
4552
serverSideRender: true,
4653
stats: 'minimal',

templates/common.html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = scope => `
22
<html>
33
<head>
4-
<link rel="icon" href="//www.iconsdb.com/icons/download/black/square-dashed-16.ico" type="image/x-icon" />
4+
<link rel="icon" href="favicon.ico" type="image/x-icon" />
55
${scope.headContent}
66
</head>
77
<body style="margin:0">

0 commit comments

Comments
 (0)