gpt4 book ai didi

reactjs - 如何配置nextjs 9 与ant design 兼容性较差?

转载 作者:行者123 更新时间:2023-12-04 02:22:21 38 4
gpt4 key购买 nike

升级后react , react-domnextjs发生此错误:

Build error occurred /home/lenovo/.../node_modules/antd/lib/style/index.css:7 body { ^

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

at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10) at Module.load (internal/modules/cjs/loader.js:643:32) { type: 'SyntaxError', '$error': '$error' } events.js:180 throw er; // Unhandled 'error' event ^ Error: write EPIPE ... at processTicksAndRejections (internal/process/task_queues.js:77:11) Emitted 'error' event at: at internal/child_process.js:810:39 at processTicksAndRejections (internal/process/task_queues.js:75:11) { errno: 'EPIPE', code: 'EPIPE', syscall: 'write' } error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.



这是我的 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 设计似乎存在兼容性问题,我发现 some sources但没有得到它!

最佳答案

基于 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,
);

关于reactjs - 如何配置nextjs 9 与ant design 兼容性较差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57542802/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com