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

Load favicon through html-loader. #428

Merged
merged 7 commits into from
Aug 12, 2016
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: 0 additions & 3 deletions config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ if (isInCreateReactAppSource) {
module.exports = {
appBuild: resolveOwn('../build'),
appHtml: resolveOwn('../template/index.html'),
appFavicon: resolveOwn('../template/favicon.ico'),
appPackageJson: resolveOwn('../package.json'),
appSrc: resolveOwn('../template/src'),
appNodeModules: resolveOwn('../node_modules'),
Expand All @@ -47,7 +46,6 @@ if (isInCreateReactAppSource) {
module.exports = {
appBuild: resolveApp('build'),
appHtml: resolveApp('index.html'),
appFavicon: resolveApp('favicon.ico'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
appNodeModules: resolveApp('node_modules'),
Expand All @@ -59,7 +57,6 @@ if (isInCreateReactAppSource) {
module.exports = {
appBuild: resolveApp('build'),
appHtml: resolveApp('index.html'),
appFavicon: resolveApp('favicon.ico'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
appNodeModules: resolveApp('node_modules'),
Expand Down
22 changes: 20 additions & 2 deletions config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,23 @@ module.exports = {
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
{
test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)(\?.*)?$/,
test: /\.(ico|jpg|png|gif|eot|svg|ttf|woff|woff2)(\?.*)?$/,
include: [paths.appSrc, paths.appNodeModules],
exclude: /\/favicon.ico$/,
loader: 'file',
query: {
name: 'static/media/[name].[ext]'
}
},
// A special case for favicon.ico to place it into build root directory.
{
test: /\/favicon.ico$/,
include: [paths.appSrc],
loader: 'file',
query: {
name: 'favicon.ico?[hash:8]'
}
},
// "url" loader works just like "file" loader but it also embeds
// assets smaller than specified size as data URLs to avoid requests.
{
Expand All @@ -143,6 +153,15 @@ module.exports = {
limit: 10000,
name: 'static/media/[name].[ext]'
}
},
// "html" loader is used to process template page (index.html) to resolve
// resources linked with <link href="./relative/path"> HTML tags.
{
test: /\.html$/,
loader: 'html',
query: {
attrs: ['link:href'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be extended with other tag:attribute combinations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh nice. Can you think up any other common combinations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't bother adding more at the moment but rather serve on per-request basis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think img:src is out of scope.

}
}
]
},
Expand All @@ -169,7 +188,6 @@ module.exports = {
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
favicon: paths.appFavicon,
}),
// Makes some environment variables available to the JS code, for example:
// if (process.env.NODE_ENV === 'development') { ... }. See `env.js`.
Expand Down
30 changes: 24 additions & 6 deletions config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,33 @@ module.exports = {
loader: ExtractTextPlugin.extract('style', 'css?-autoprefixer!postcss')
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
},
// JSON is not enabled by default in Webpack but both Node and Browserify
// allow it implicitly so we also enable it.
{
// JSON is not enabled by default in Webpack but both Node and Browserify
// allow it implicitly so we also enable it.
test: /\.json$/,
include: [paths.appSrc, paths.appNodeModules],
loader: 'json'
},
// "file" loader makes sure those assets end up in the `build` folder.
// When you `import` an asset, you get its filename.
{
// "file" loader makes sure those assets end up in the `build` folder.
// When you `import` an asset, you get its filename.
test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)(\?.*)?$/,
test: /\.(ico|jpg|png|gif|eot|svg|ttf|woff|woff2)(\?.*)?$/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally favicon.ico specifically should end up in root build output folder so that browser see it even for files requested from the domain. (Not a big deal but still.)

We could declare a loader entry specifically for it with hardcoded name. However we would have to exclude hash from its name—otherwise it defeats the point. But then we don’t have a way to bump cache. I wonder if Webpack understands name: 'favicon.ico?[hash:8].

Copy link
Contributor Author

@andreypopp andreypopp Aug 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if Webpack understands name: 'favicon.ico?[hash:8]

It does!

exclude: /\/favicon.ico$/,
include: [paths.appSrc, paths.appNodeModules],
loader: 'file',
query: {
name: 'static/media/[name].[hash:8].[ext]'
}
},
// A special case for favicon.ico to place it into build root directory.
{
test: /\/favicon.ico$/,
include: [paths.appSrc],
loader: 'file',
query: {
name: 'favicon.ico?[hash:8]'
}
},
// "url" loader works just like "file" loader but it also embeds
// assets smaller than specified size as data URLs to avoid requests.
{
Expand All @@ -153,6 +163,15 @@ module.exports = {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]'
}
},
// "html" loader is used to process template page (index.html) to resolve
// resources linked with <link href="./relative/path"> HTML tags.
{
test: /\.html$/,
loader: 'html',
query: {
attrs: ['link:href'],
}
}
]
},
Expand Down Expand Up @@ -181,7 +200,6 @@ module.exports = {
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
favicon: paths.appFavicon,
minify: {
removeComments: true,
collapseWhitespace: true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"filesize": "3.3.0",
"fs-extra": "0.30.0",
"gzip-size": "3.0.0",
"html-loader": "0.4.3",
"html-webpack-plugin": "2.22.0",
"http-proxy-middleware": "0.17.0",
"jest": "14.1.0",
Expand Down
3 changes: 3 additions & 0 deletions tasks/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ test -e build/*.html
test -e build/static/js/*.js
test -e build/static/css/*.css
test -e build/static/media/*.svg
test -e build/favicon.ico

# Run tests
npm run test
Expand Down Expand Up @@ -96,6 +97,7 @@ test -e build/*.html
test -e build/static/js/*.js
test -e build/static/css/*.css
test -e build/static/media/*.svg
test -e build/favicon.ico

# Run tests
npm run test
Expand All @@ -113,6 +115,7 @@ test -e build/*.html
test -e build/static/js/*.js
test -e build/static/css/*.css
test -e build/static/media/*.svg
test -e build/favicon.ico

# Run tests
npm run test
Expand Down
1 change: 1 addition & 0 deletions template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="./src/favicon.ico">
<title>React App</title>
</head>
<body>
Expand Down
File renamed without changes.