Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
fix: handle only image file extensions with the next image loader
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
RyanClementsHax committed Feb 3, 2022
1 parent c468096 commit 64cc0d0
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/nextv12/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function Home() {
Click on any one of these links to see supported features in action
</h1>
<Links />
<p className="icons"> </p>
</main>
</div>
)
Expand Down
Binary file added examples/nextv12/public/font/iconfont.eot
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/nextv12/public/font/iconfont.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/nextv12/public/font/iconfont.ttf
Binary file not shown.
Binary file added examples/nextv12/public/font/iconfont.woff
Binary file not shown.
Binary file added examples/nextv12/public/font/iconfont.woff2
Binary file not shown.
17 changes: 17 additions & 0 deletions examples/nextv12/styles/globals.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
main {
font-size: 1.25rem;
}

@font-face {
font-family: 'iconfont';
src: url('../public/font/iconfont.eot?72359833');
src: url('../public/font/iconfont.eot?72359833#iefix')
format('embedded-opentype'),
url('../public/font/iconfont.woff2?72359833') format('woff2'),
url('../public/font/iconfont.woff?72359833') format('woff'),
url('../public/font/iconfont.ttf?72359833') format('truetype'),
url('../public/font/iconfont.svg?72359833#iconfont') format('svg');
font-weight: normal;
font-style: normal;
}

.icons {
font-family: 'iconfont';
}
19 changes: 17 additions & 2 deletions src/css/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ export const configureCss = (
{
loader: 'css-loader',
options: {
modules: { auto: true, getLocalIdent: getCssModuleLocalIdent }
// this is what nextjs is doing
// https://github.com/vercel/next.js/blob/b7725133f867b5e530dd4bb5d1fd8d5d389e3364/packages/next/build/webpack/config/blocks/css/loaders/global.ts#L32
// https://github.com/vercel/next.js/blob/b7725133f867b5e530dd4bb5d1fd8d5d389e3364/packages/next/build/webpack/config/blocks/css/loaders/file-resolve.ts
url: (url: string) => {
if (url.startsWith('/')) {
return false
}
if (/^[a-z][a-z0-9+.-]*:/i.test(url)) {
return false
}
return true
},
modules: {
auto: true,
getLocalIdent: getCssModuleLocalIdent
}
}
},
'postcss-loader'
Expand All @@ -35,7 +50,7 @@ export const configureCss = (
{
loader: 'css-loader',
options: {
modules: { auto: true, getLocalIdent: getCssModuleLocalIdent}
modules: { auto: true, getLocalIdent: getCssModuleLocalIdent }
}
},
'postcss-loader',
Expand Down
30 changes: 15 additions & 15 deletions src/images/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import semver from 'semver'
import { Configuration as WebpackConfig } from 'webpack'
import { Configuration as WebpackConfig, RuleSetRule } from 'webpack'
import { addScopedAlias, getNextjsVersion } from '../utils'

export const configureImages = (baseConfig: WebpackConfig): void => {
Expand All @@ -12,23 +12,23 @@ const configureStaticImageImport = (baseConfig: WebpackConfig): void => {
if (semver.lt(version, '11.0.0')) return

const rules = baseConfig.module?.rules
rules?.forEach((rule, i) => {
if (
const assetRule = rules?.find(
rule =>
typeof rule !== 'string' &&
rule.test instanceof RegExp &&
rule.test.test('test.jpg')
) {
rules[i] = {
test: rule.test,
use: [
{
loader: require.resolve('./next-image-loader-stub'),
options: {
filename: rule.generator?.filename
}
}
]
) as RuleSetRule
assetRule.test = /\.(apng|eot|otf|ttf|woff|woff2|cur|ani|pdf)(\?.*)?$/
rules?.push({
test: /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i,
issuer: { not: /\.(css|scss|sass)$/ },
use: [
{
loader: require.resolve('./next-image-loader-stub'),
options: {
filename: assetRule.generator?.filename
}
}
}
]
})
}

0 comments on commit 64cc0d0

Please sign in to comment.