【发布时间】:2023-01-31 00:39:11
【问题描述】:
我创建了一个 Next 13 应用程序,我在其中安装了一个包 @viral-loops/widgets
安装包后,当我运行应用程序时,出现以下错误
error - ./node_modules/@viral-loops/widgets/dist/react/Widget.jsx
Module parse failed: Unexpected token (34:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| return (
> <>
| {props.ucid ? (
| <form-widget ucid={props.ucid} popup={props.popup} />
这是我的 next.config.js
module.exports = {
webpack(config, { isServer }) {
const prefix = config.assetPrefix ?? config.basePath ?? '';
config.module.rules.push(
{
test: /\.svg$/,
use: ['@svgr/webpack', 'url-loader'],
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: 'url-loader',
},
],
},
{
test: /\.(mp4|webm|mov|ogg|swf|ogv)$/,
use: [
{
loader: require.resolve('file-loader'),
options: {
publicPath: `${prefix}/_next/static/videos/`,
outputPath: `${isServer ? '../' : ''}static/videos/`,
name: '[name]-[hash].[ext]',
},
},
],
},
);
return config;
},
images: {
disableStaticImages: true,
remotePatterns: [
{
protocol: 'https',
hostname: 'images.ctfassets.net',
pathname: '**',
},
],
},
sassOptions: {
includePaths: [
path.join(__dirname, 'styles'),
path.join(__dirname, 'styles/variables.scss'),
],
prependData: '@import "./styles/variables.scss";',
},
compilerOptions: {
baseUrl: '.',
paths: {
'@/components/*': ['components/*'],
},
},
};
我尝试在 next.config.js webpack 中设置如下所示的 babel-loader
module.exports = {
webpack(config, { isServer }) {
const prefix = config.assetPrefix ?? config.basePath ?? '';
config.module.rules.push(
**{test: /\.(js|jsx)$/, use: 'babel-loader'}**,
返回如下所示的新错误
Syntax error: Support for the experimental syntax 'jsx' isn't currently enabled (34:5):
32 |
33 | return (
> 34 | <>
| ^
35 | {props.ucid ? (
36 | <form-widget ucid={props.ucid} popup={props.popup} />
37 | ) : null}
Add @babel/preset-react (https://github.com/babel/babel/tree/main/packages/babel-preset-react) to the 'presets' section of your Babel config to enable transformation.
我不确定我必须在 Nextjs 应用程序中的何处添加 @babel/preset-react。
【问题讨论】:
标签: javascript reactjs webpack next.js server-side-rendering