简体   繁体   English

Next.js 13 项目引用路径别名抛出 webpack 错误

[英]Next.js 13 project references path aliases throwing webpack error

I'm migrating a small and old react project (and its nestjs server) to Next.js 13. I'm doing that by scaffolding from a CLI called Create T3 App .我正在将一个小而旧的 React 项目(及其 nestjs 服务器)迁移到 Next.js 13。我正在通过一个名为Create T3 App的 CLI 搭建脚手架来做到这一点。 I use project references a lot, in all projects I work on and I'm trying to do that here like this:我经常使用项目引用,在我从事的所有项目中,我在这里尝试这样做:

tsconfig.json: tsconfig.json:

{
  "compilerOptions": {
    "target": "es2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "noUncheckedIndexedAccess": true,

    "baseUrl": ".",
    "paths": {
      "@core/*": ["../../../../libs/core/src/*"]
    }
  },
  "references": [
    { "path": "../../../../libs/core/" }
  ],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.mjs"],
  "exclude": ["node_modules"]
}

But it keeps throwing this webpack loader error:但它一直抛出这个 webpack 加载程序错误:

error -../../../../libs/core/src/path/to/file.ts Module parse failed: Unexpected token (4:12) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.错误 -../../../../libs/core/src/path/to/file.ts 模块解析失败:意外令牌 (4:12) 您可能需要适当的加载程序来处理此文件类型,当前没有加载程序配置为处理此文件。 See https://webpack.js.org/concepts#loaders参见https://webpack.js.org/concepts#loaders

There are no errors marked red on the file, vscode's intellisense works fine.文件上没有标记为红色的错误,vscode 的智能感知工作正常。

I have tried adding the aliases to webpack, like so:我尝试将别名添加到 webpack,如下所示:

next.config.mjs: next.config.mjs:

// @ts-check
/**
 * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation.
 * This is especially useful for Docker builds.
 */
!process.env.SKIP_ENV_VALIDATION && (await import("./src/env/server.mjs"));
import path from 'path';

/** @type {import("next").NextConfig} */
const config = {
  reactStrictMode: true,
  swcMinify: true,
  i18n: {
    locales: ["en"],
    defaultLocale: "en",
  },
  webpack: (config, { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }) => {
    return {
      ...config, 
      resolve: { 
        ...config.resolve,
        alias: {
          ...config.resolve.alias,
          '@core': path.resolve('../../../../libs/core/src'),
        }
      }
    }
  },
};
export default config;

This yielded no changes to the error message.这不会对错误消息产生任何更改。

I have tried adding ts-loader to webpack but it throws errors in other parts of the app.我尝试将 ts-loader 添加到 webpack,但它会在应用程序的其他部分引发错误。 I'd like to solve this without using babel if at all possible.如果可能的话,我想在不使用 babel 的情况下解决这个问题。

Does anyone know how to make paths aliases for external projects work on Next.js 13?有谁知道如何使外部项目的路径别名在 Next.js 13 上工作?


ANSWER BREAKDOWN:答案分解:

Using the package that @Yilmaz suggested, called next-transpile-modules I was able to make the project references and their aliases work.使用@Yilmaz建议的 package,称为next-transpile-modules ,我能够使项目引用及其别名起作用。 This solution requires "baseUrl" and "paths" fields to be correctly set on tsconfig.json and these additions to next.config.mjs :此解决方案需要在 tsconfig.json 上正确设置“ tsconfig.json ”和“paths”字段,并在next.config.mjs中添加以下内容:

import transpile from 'next-transpile-modules';

const withModules = transpile([
  '../../../../libs/core'
]);

/** @type {import("next").NextConfig} */
const config = {
  ...
};

export default withModules(config);

your tsconfig.json does not meet default next13 typescript configuration.您的 tsconfig.json 不符合默认 next13 typescript配置。 I created a next app with --typescript and this is the default tsconfig.json .我使用 --typescript 创建了下一个应用程序,这是默认的tsconfig.json I did not touch it我没碰它

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ]
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

If you follow next-transpile-modules npm package and set it next.config.js如果您遵循next-transpile-modules npm package 并将其设置为 next.config.js

import transpile from 'next-transpile-modules';

const withModules = transpile([
  '../../../../libs/core'
]);

/** @type {import("next").NextConfig} */
const config = {
  ...
};

export default withModules(config);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM