forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for static image imports (vercel#24993)
Co-authored-by: Steven <[email protected]> Co-authored-by: Tim Neutkens <[email protected]> Co-authored-by: Tim Neutkens <[email protected]>
- Loading branch information
1 parent
9d14dd8
commit 9b295f5
Showing
18 changed files
with
422 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import loaderUtils from 'next/dist/compiled/loader-utils' | ||
import sizeOf from 'image-size' | ||
import { processBuffer } from '../../../next-server/server/lib/squoosh/main' | ||
|
||
const PLACEHOLDER_SIZE = 6 | ||
|
||
async function nextImageLoader(content) { | ||
const context = this.rootContext | ||
const opts = { context, content } | ||
const interpolatedName = loaderUtils.interpolateName( | ||
this, | ||
'/static/image/[path][name].[hash].[ext]', | ||
opts | ||
) | ||
|
||
let extension = loaderUtils.interpolateName(this, '[ext]', opts) | ||
if (extension === 'jpg') { | ||
extension = 'jpeg' | ||
} | ||
|
||
const imageSize = sizeOf(content) | ||
let placeholder | ||
if (extension === 'jpeg' || extension === 'png') { | ||
// Shrink the image's largest dimension to 6 pixels | ||
const resizeOperationOpts = | ||
imageSize.width >= imageSize.height | ||
? { type: 'resize', width: PLACEHOLDER_SIZE } | ||
: { type: 'resize', height: PLACEHOLDER_SIZE } | ||
const resizedImage = await processBuffer( | ||
content, | ||
[resizeOperationOpts], | ||
extension, | ||
0 | ||
) | ||
placeholder = `data:image/${extension};base64,${resizedImage.toString( | ||
'base64' | ||
)}` | ||
} | ||
|
||
const stringifiedData = JSON.stringify({ | ||
src: '/_next' + interpolatedName, | ||
height: imageSize.height, | ||
width: imageSize.width, | ||
placeholder, | ||
}) | ||
|
||
this.emitFile(interpolatedName, content, null) | ||
|
||
return `${'export default '} ${stringifiedData};` | ||
} | ||
export const raw = true | ||
export default nextImageLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.