如何在现有的next.config.js文件中配置next-images。

huangapple go评论68阅读模式
英文:

How to Config next-images with existing next.config.js file

问题

我想在我的Next.js应用程序中使用npm包next-images。

在阅读next-images文档后,它说您需要创建一个next.config.js文件,并添加以下代码:

const withImages = require('next-images')
module.exports = withImages()

然而,我已经有一个next.config.js文件,目前它包含如下代码:

var fs = require('fs');

const nextConfig = {
    reactStrictMode: true,
    images: {
        remotePatterns: [
            {
                protocol: "http",
                hostname: "**",
            },
            {
                protocol: "https",
                hostname: "**",
            },
        ],
    },
    env: {
        customSnipcartJS: fs.readFileSync('public/file2.js').toString(),
        snipcartInstallJS: fs.readFileSync('public/file1.js').toString()
    }
}

module.exports = nextConfig

所以我的问题是,如何将next-images所需的配置代码与我已经在next.config.js中拥有的现有配置合并?

英文:

I want to use the npm package next-images in my nextjs app.

After reading the documentation for next-images, it says you need to create a next.config.js file with the following code:

const withImages = require('next-images')
module.exports = withImages()

However I already have a next.config.js file, currently it has code inside it that looks like this:

var fs = require('fs');

const nextConfig = {
    reactStrictMode: true,
    images: {
        remotePatterns: [
            {
                protocol: "http",
                hostname: "**",
            },
            {
                protocol: "https",
                hostname: "**",
            },
        ],
    },
    env: {
        customSnipcartJS: fs.readFileSync('public/file2.js').toString(),
        snipcartInstallJS: fs.readFileSync('public/file1.js').toString()
    }
}

module.exports = nextConfig

So my question is, how do I merge the required config code for next-images with my existing configuration I already have in my next.config.js

答案1

得分: 0

以下是翻译好的部分:

"In case someone else runs into something like this I found a solution.":
如果其他人遇到类似情况,我找到了一个解决方案。

"I managed to get this to work, you can pass your custom next config into the withImages method.":
我成功让这个工作了,你可以将你的自定义next配置传递给withImages方法。

"So this now works.":
所以现在这个可以工作了。

"var fs = require('fs');":
var fs = require('fs');

"const withImages = require('next-images');":
const withImages = require('next-images');

"module.exports =":
module.exports =

"reactStrictMode: true,":
reactStrictMode: true,

"images: {":
images: {

"disableStaticImages: true,":
disableStaticImages: true,

"remotePatterns: [":
remotePatterns: [

"protocol: 'http',":
protocol: 'http',

"hostname: '',":
hostname: '
',

"protocol: 'https',":
protocol: 'https',

"hostname: '',":
hostname: '
',

"env: {":
env: {

"customSnipcartJS: fs.readFileSync('public/file2.js').toString(),":
customSnipcartJS: fs.readFileSync('public/file2.js').toString(),

"snipcartInstallJS: fs.readFileSync('public/file1.js.js').toString()":
snipcartInstallJS: fs.readFileSync('public/file1.js.js').toString()

英文:

In case someone else runs in to something like this I found a solution.

I managed to get this to work, you can pass your custom next config in to the withImages method.

So this now works.

var fs = require('fs');
const withImages = require('next-images');

module.exports = withImages({
    reactStrictMode: true,
    images: {
            disableStaticImages: true,
            remotePatterns: [
            {
                protocol: "http",
                hostname: "**",
            },
            {
                protocol: "https",
                hostname: "**",
            },
        ],
    },
    env: {
        customSnipcartJS: fs.readFileSync('public/file2.js').toString(),
        snipcartInstallJS: fs.readFileSync('public/file1.js.js').toString()
    }
})

huangapple
  • 本文由 发表于 2023年1月10日 07:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75063935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定