【问题标题】:How to configure nextjs 9 and ant design less compatibility?如何配置nextjs 9和ant design兼容性差?
【发布时间】:2019-08-18 08:14:17
【问题描述】:

升级reactreact-domnextjs 后会出现此错误:

发生构建错误 /home/lenovo/.../node_modules/antd/lib/style/index.css:7 身体 { ^

SyntaxError: Unexpected token { 在 Module._compile (internal/modules/cjs/loader.js:720:22)

在 Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10) 在 Module.load (internal/modules/cjs/loader.js:643:32) { 类型:'语法错误', “$错误”:“$错误” } 事件.js:180 投掷者; // 未处理的“错误”事件 ^ 错误:写入 EPIPE ... 在 processTicksAndRejections (internal/process/task_queues.js:77:11) 在以下位置发出“错误”事件: 在内部/child_process.js:810:39 在 processTicksAndRejections (internal/process/task_queues.js:75:11) { 错误号:'EPIPE', 代码:'EPIPE', 系统调用:'写' } 错误命令失败,退出代码为 1。 info 访问https://yarnpkg.com/en/docs/cli/run 获取有关此命令的文档。

这是我的 next.config.js

const nextConfig = {
    distDir: '_next',

    onDemandEntries: {
        maxInactiveAge: 1000 * 60 * 60,
        pagesBufferLength: 5,
    },

    webpack: (config, { dev }) => {
        !dev &&
        config.plugins.push(
            new BrotliPlugin({
                asset: '[path].br[query]',
                test: /\.js$|\.css$|\.html$/,
                threshold: 10240,
                minRatio: 0.7,
            }),
        );

        !dev &&
        config.plugins.push(
            new CompressionPlugin({
                filename: '[path].gz[query]',
                algorithm: 'gzip',
                test: /\.js$|\.css$|\.html$/,
                threshold: 10240,
                minRatio: 0.7,
            }),
        );
        return config;
    },
};

module.exports = withPlugins(
    [
        [withImages],
        [withCss],
        [
            withSass,
            {
                cssModules: true,
                cssLoaderOptions: {
                    localIdentName: '[path]___[local]___[hash:base64:5]',
                },
            },
        ],
        [withBundleAnalyzer],
    ],
    nextConfig,
);

你知道这有什么问题吗?

编辑

ant design 似乎存在兼容性问题,我找到了some sources,但没有得到它!

【问题讨论】:

  • 这没有帮助。问题是 ant 设计和 nextjs 兼容性!

标签: reactjs next.js antd react-dom


【解决方案1】:

基于 Next.js 存储库中的这个示例 https://github.com/zeit/next.js/tree/canary/examples/with-ant-design

要解决此问题,请将这些行添加到您的next.config.js

const nextConfig = {
  webpack: (config, { isServer }) => {
    if (isServer) {
      const antStyles = /antd\/.*?\/style\/css.*?/;
      const origExternals = [...config.externals];
      config.externals = [ // eslint-disable-line
        (context, request, callback) => { // eslint-disable-line
          if (request.match(antStyles)) return callback();
          if (typeof origExternals[0] === 'function') {
            origExternals[0](context, request, callback);
          } else {
            callback();
          }
        },
        ...(typeof origExternals[0] === 'function' ? [] : origExternals),
      ];

      config.module.rules.unshift({
        test: antStyles,
        use: 'null-loader',
      });
    }
    return config;
  },
};

next.config.js 文件的示例:

const withPlugins = require('next-compose-plugins');
const withCss = require('@zeit/next-css');
const withSass = require('@zeit/next-sass');

if (typeof require !== 'undefined') {
  require.extensions['.css'] = file => {};
}

const nextConfig = {
  webpack: (config, { isServer }) => {
    if (isServer) {
      const antStyles = /antd\/.*?\/style\/css.*?/;
      const origExternals = [...config.externals];
      config.externals = [ // eslint-disable-line
        (context, request, callback) => { // eslint-disable-line
          if (request.match(antStyles)) return callback();
          if (typeof origExternals[0] === 'function') {
            origExternals[0](context, request, callback);
          } else {
            callback();
          }
        },
        ...(typeof origExternals[0] === 'function' ? [] : origExternals),
      ];

      config.module.rules.unshift({
        test: antStyles,
        use: 'null-loader',
      });
    }
    return config;
  },
};

module.exports = withPlugins(
  [
    [withCss],
    [
      withSass,
      {
        cssModules: true,
        cssLoaderOptions: {
          localIdentName: '[path]___[local]___[hash:base64:5]',
        },
      },
    ],
  ],
  nextConfig,
);

【讨论】:

    猜你喜欢
    • 2018-07-27
    • 2020-03-31
    • 2014-08-18
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2020-06-02
    相关资源
    最近更新 更多