reactjs - Next.js 勒纳 monrepo : Module parse failed: Unexpected token

标签 reactjs typescript next.js jsx lerna

我已将 next + typescript 项目添加到 lerna monorepo。

当我运行lerna build时它运行 tsc在我所有的包裹和 next build在我的 UI 包中。

tsc部分工作正常:

$ yarn build
yarn run v1.22.18
$ lerna run build
lerna notice cli v4.0.0
lerna info Executing command in 6 packages: "yarn run build"
lerna info run Ran npm script 'build' in '@xxx/config' in 1.1s:
$ tsc
lerna info run Ran npm script 'build' in '@xxx/domain' in 1.2s:
$ tsc
lerna info run Ran npm script 'build' in '@xxx/utils' in 1.2s:
$ tsc
lerna info run Ran npm script 'build' in '@xxx/components' in 1.0s:
$ tsc
lerna info run Ran npm script 'build' in '@xxx/pages' in 1.0s:
$ tsc

但是,我收到以下错误:

lerna ERR! yarn run build exited 1 in '@xxx/ui'
lerna ERR! yarn run build stdout:
$ next build
info  - Checking validity of types...
info  - Creating an optimized production build...
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
lerna ERR! yarn run build stderr:
Failed to compile.

../xxx-pages/lib/blog/blog.jsx
Module parse failed: Unexpected token (6:12)
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
| export var Blog = function (props) {
|     var data = props.data;
>     return (<Container>
|       <BlogPosts data={data}/>
|     </Container>);

看起来像next build JSX 有问题 <在:

>     return (<Container>

我的理解是,这应该是开箱即用的,我不应该尝试自定义 babel 配置。

最佳答案

使用next-transpile-modules部分解决了该问题:

const { dependencies } = require('./package.json')

function getModules() {
  return Object.keys(dependencies || [])
               .filter(dependency => dependency.startsWith('@jsdayie/'));
}

const modules = getModules();

const withTM = require('next-transpile-modules')(modules);

function getAliases(modulesArray) {
  modulesArray.reduce((prev, next) => {
    return {
      ...prev,
      ...{
        [module]: require.resolve(next)
      }
    };
  }, {
    resolveSymlinks: false
  });
}

module.exports = withTM({
  webpack: (config) => {
    config.resolve.alias = {
      ...config.resolve.alias,
      ...getAliases(modules)
    };
    return config;
  },
});

2023 年更新:

自 Nextjs 13.1 版本起支持内置模块转译:

const nextConfig = {
  transpilePackages: ['some-package', ...getAliases(modules)],
};

您可以阅读更多here .

关于reactjs - Next.js 勒纳 monrepo : Module parse failed: Unexpected token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72432298/

相关文章:

Javascript JSX,非法导入声明

java - 如何在 Tomcat 服务器上部署 reactJS 应用程序

javascript - 处理对本地状态的并发异步更新

reactjs - Material-UI v5 无法与 NextJS v12/React v18 一起编译(无法在模块外部使用 import 语句)

javascript - Three.js 中的帧速率异常高

javascript - 类型错误 : Cannot read property 'map' of undefined in react-redux

javascript - 最小化源代码文件后,JavaScript 源映射文件是否仍然有效?

javascript - TypeScript 在生成的 JavaScript 中列出导入的接口(interface)和类型

javascript - onSubmit onClick 的 nextjs 事件处理程序不起作用

javascript - Next JS 和 Cloudinary,图像未加载