简体   繁体   English

如何在 Next JS 中启用 SWC 别名导入?

[英]How to enable SWC Aliased imports in Next JS?

Upgrading from babel to SWC.从 babel 升级到 SWC。 I deleted the .babelrc file but now the path aliases are no longer recognized in my project.我删除了 .babelrc 文件,但现在我的项目中不再识别路径别名。 Tried to restore this with a .swrc试图用 .swrc 恢复它

.swrc .swrc

{
    "jsc": {
        "target": "ES2017",
        "baseUrl": ".",
        "paths": {
            "@components/*": ["./components/*"]
        }
    }
}

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["es6", "dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "ESNEXT",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "@components/*": ["./components/*"],
    }
  },
  "exclude": ["node_modules", "**/*.stories.tsx"],
  "include": ["**/*.ts", "**/*.tsx", "next-env.d.ts", "next.config.js"]
}

14:24 Error: Unable to resolve path to module '@components/NavBar'

.eslintrc.json .eslintrc.json

{
    "root": true,
    "parser": "@typescript-eslint/parser",
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:react/recommended",
        "plugin:import/errors",
        "plugin:import/warnings",
        "plugin:jsx-a11y/recommended",
        "plugin:react-hooks/recommended"
    ],
    "env": {
        "browser": true,
        "commonjs": true,
        "es6": true,
        "node": true
    },
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true
        }
    },
    "plugins": [
        "@typescript-eslint", "react", "prettier"
    ],
    "rules": {
        "prefer-const": "off",
        "import/extensions": "off",
        "jsx-a11y/anchor-is-valid": "off",
        "import/no-cycle": [0, { "ignoreExternal": true }],
        "prettier/prettier": ["error", { "endOfLine": "auto" }],
        "react/prop-types": "off",
        "react-hooks/exhaustive-deps": "off",
        "react/no-unescaped-entities": "off",
        "react/jsx-filename-extension": [1, { "extensions": [".ts", ".tsx"] }],
        "react/jsx-props-no-spreading": ["error", { "html": "ignore", "custom": "ignore", "exceptions": [""] }],

        // needed because of https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-use-before-define.md#how-to-use & https://stackoverflow.com/questions/63818415/react-was-used-before-it-was-defined
        "no-use-before-define": "off",
        "@typescript-eslint/no-use-before-define": [
          "error",
          { "functions": false, "classes": false, "variables": true }
        ],
        "@typescript-eslint/ban-ts-comment": "off",
        "@typescript-eslint/no-explicit-any": "off"
    },
    "settings": {
        "import/resolver": {
            "babel-module": {
                "extensions": [".js", ".jsx", ".ts", ".tsx"]
            }
        }
    }
}

old .babelrc旧的.babelrc

{
    "presets": ["next/babel"],
    "plugins": [
      [
        "module-resolver",
        {
          "root": ["./"],
          "alias": {
            "@components": "./components",
            "@api": "./api",
            "@models": "./models",
            "@screens": "./screens",
            "@hooks": "./hooks",
            "@services": "./services",
            "@constants": "./constants",
            "@context": "./context",
            "@queries": "./queries",
            "@data": "./data",
            "@typeDefs": "./types",
            "@generated": "./generated",
            "@utils": "./utils",
            "@pages": "./pages",
            "@public": "./public"
          }
        }
      ]
    ]
  }

Was able to solve this with a few tweaks.能够通过一些调整来解决这个问题。

  1. Remove the import/resolver section of .eslintrc.json删除.eslintrc.jsonimport/resolver部分
  2. Extend next in eslint.在 eslint 中扩展next

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

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