首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在next.config.js文件中组合多个插件?

如何在next.config.js文件中组合多个插件?
EN

Stack Overflow用户
提问于 2021-01-06 23:01:24
回答 2查看 2.6K关注 0票数 3

当我将“下一步-视频”加载程序添加到next.config.js中时,只要module.exports执行最后一个操作,它就能很好地工作:

代码语言:javascript
复制
module.exports = withVideos()

另一方面,它破坏了位于上面的另一个module.exports实例:

代码语言:javascript
复制
module.exports = {
   images: {
     domains: ['cdn.shopify.com'],
   },
};

基本上,它覆盖了以前的每个module.exports。合并这些出口的规则是什么?我想我需要把它们放在一个模块下,但是规则是什么呢?我搞砸了语法,每次尝试把它重新定位到一个module.exports下,都会出现新的错误。

只是总结问题:

  1. 在单个导出中组合模块的规则是什么,以及如何将其与所有以前的module.exports?组合起来?
  2. ,我真的需要离开“webpack(配置)”部分吗?在next.config中复制上面相同的部分吗?,我从不同的来源收集了它,现在试图弄清楚它是如何工作的。
代码语言:javascript
复制
webpack(config) { 
  config.module.rules.push(

next.config.js的内容

代码语言:javascript
复制
const withPlugins = require('next-compose-plugins');
const withSass = require('@zeit/next-sass');
const withCSS = require('@zeit/next-css');
const withImages = require('next-images');
const withVideos = require('next-videos');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
    enabled: process.env.ANALYZE === 'true',
});

// next.config.js
module.exports = withPlugins(
    [[withImages], [withSass], [withCSS], [withVideos]],
    {
        plugins: ['postcss-import', 'tailwindcss', 'autoprefixer'],
        serverRuntimeConfig: {
            mySecret: 'secret',
            secondSecret: process.env.SECOND_SECRET, // Pass through env variables
        },
        publicRuntimeConfig: {
            // Will be available on both server and client
            staticFolder: '/public',
        },
        module: {
            loaders: [
                {
                    test: /.jsx?$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/,
                },
                {
                    test: /\.css$/,
                    loader: 'style-loader!css-loader',
                },
                {
                    test: /\.jsx?$/,
                    use: ['babel-loader', 'astroturf/loader'],
                },
                {
                    test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
                    loader: 'url-loader?limit=100000',
                },
                {
                    test: /\.(eot|woff|woff2|otf|ttf|svg|png|jpg|gif|webm)$/,
                    use: {
                        loader: 'url-loader',
                        options: {
                            limit: 100000,
                            name: '[name].[ext]',
                        },
                    },
                },
                {
                    test: /\.mp4$/,
                    use: 'file-loader?name=videos/[name].[ext]',
                },
                {
                    test: /\.style.js$/,
                    use: [
                        'style-loader',
                        {
                            loader: 'css-loader',
                            options: { importLoaders: 1 },
                        },
                        {
                            loader: 'postcss-loader',
                            options: { parser: 'sugarss', exec: true },
                        },
                    ],
                },
            ],
        },
        webpack(config) {
            config.module.rules.push(
                {
                    test: /\.(eot|woff|woff2|otf|ttf|svg|png|jpg|gif)$/,
                    use: {
                        loader: 'url-loader',
                        options: {
                            limit: 100000,
                            name: '[name].[ext]',
                        },
                    },
                },
                {
                    test: /\.style.js$/,
                    use: [
                        'style-loader',
                        'css-loader',
                        {
                            loader: 'css-loader',
                            options: { importLoaders: 1 },
                        },
                        {
                            loader: 'postcss-loader',
                            options: { parser: 'sugarss', exec: true },
                        },
                    ],
                },
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    use: ['babel-loader', 'eslint-loader'],
                },
            );
            withBundleAnalyzer({});
            return config;
        },
    },
    {
        images: {
            domains: ['cdn.shopify.com'],
        },
    },
    withVideos(),
);

module.exports = {
    extends: 'airbnb-base',
    rules: {
        'arrow-body-style': 0,
    },
};

module.exports = {
    images: {
        domains: ['cdn.shopify.com'],
    },
};

module.exports = withVideos();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-07 12:38:50

您似乎在Next.js配置文件中不正确地混合了几个信任项。

next-compose-plugins

没有必要使用next-compose-plugins。Next.js插件是构建成可链接的,您可以将每个插件调用嵌套在下一个插件中,并在最后一个调用中传递实际的配置对象。您的next.config.js文件也应该只有一个module.exports

代码语言:javascript
复制
// next.config.js

// Omitting requires for simplicity
 
module.exports = withImages(
    withSass(
        withCSS(
            withVideos(
                withBundleAnalyzer({
                    images: {
                        domains: ['cdn.shopify.com']
                    },
                    serverRuntimeConfig: {
                        mySecret: "secret",
                        secondSecret: process.env.SECOND_SECRET
                    },
                    publicRuntimeConfig: {
                        staticFolder: "/public"
                    },
                    // From here everything that's Webpack-related
                    webpack(config) {
                        // Add your custom Webpack configs
                        return config;
                    }
                })
            )
        )
    )
);

next-compose-plugins

如果您使用的是next-compose-plugins,它应该大致遵循以下结构:

代码语言:javascript
复制
// next.config.js

// Omitting requires for simplicity
 
module.exports = withPlugins(
    [withImages, withSass, withCSS, withVideos, withBundleAnalyzer], // All next plugins go here
    // Below is the main Next.js config object
    { 
        images: {
            domains: ['cdn.shopify.com']
        },
        serverRuntimeConfig: {
            mySecret: "secret",
            secondSecret: process.env.SECOND_SECRET
        },
        publicRuntimeConfig: {
          staticFolder: "/public"
        },
        // From here everything that's Webpack-related
        webpack(config) {
            // Add your custom Webpack configs
            return config;
        }
    }
);

最后,以下配置属于您的ESlint配置文件,而不是Next.js配置。

代码语言:javascript
复制
// .eslintrc.js
module.exports = {
    extends: 'airbnb-base',
    rules: {
        'arrow-body-style': 0,
    },
};
票数 7
EN

Stack Overflow用户

发布于 2022-06-21 09:35:51

在尝试将节点包中的全局css文件访问到下一个JS页面时,我遇到了一个类似的问题,经过多次尝试,我通过更改以下内容解决了问题:

当需要对插件使用多个插件或普通的JSON配置时,您需要在插件中包装原始配置。

在你的情况下

代码语言:javascript
复制
module.exports = withVideos(
images: {
     domains: ['cdn.shopify.com'],
   },
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65604469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档