You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ts-loader/babel-loader转译TS项目时ESLint报无效转义错误

Unicode属性正则\p{Diacritic}转译后触发ESLint解析错误

我的TypeScript项目配置了target: ES6、module: ESNext(虽更倾向兼容ES5,但当前配置暂时可以接受)。使用ts-loader(也测试过babel-loader)将代码转译为ESM模块后,代码中使用Unicode属性语法\p{Diacritic}的正则表达式触发了ESLint解析错误:

Parsing error: Invalid regular expression: /\p{Diacritic}/: Invalid escape

原TypeScript代码

export const normalize = (str: string): string => {
  // Translates the Unicode Normalization Form of the string into the Normalization Form Canonical Composition.
  // Extra consideration for the letters ø,ð, Ð, which are not included in the NFKD normalization.
  return (
    str
      .normalize("NFKD")
      .replace(/\p{Diacritic}/gu, "")
      .replace("ø", "o")
      .replace("Ø", "O")
      .replace("ð", "d")
      .replace("Ð", "D")
  );
};

转译后的JavaScript代码

d = e => e.normalize("NFKD").replace(/\p{Diacritic}/gu, "").replace("ø", "o").replace("Ø", "O").replace("ð", "d").replace("Ð", "D"),

我认为转译后的代码是正确的,怀疑问题出在webpack、babel或ts-loader的目标配置,或是ESLint的配置上,同时不确定是否需要引入polyfill。以下是我的相关配置文件:

tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "lib": ["dom", "dom.iterable", "ES6", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": false,
    "esModuleInterop": true,
    "module": "ESNext",
    "outDir": "./dist/",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": "./src",
    "sourceMap": true,
    "declaration": true,
    "paths": {
      "@namesrhard/*": ["./*"]
    }
  },
  "include": ["**/*.ts", "tests/**/*.ts"],
  "exclude": ["node_modules", "jest.config.**", "webpack.config.js", "dist"]
}

webpack.config.js

const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");

const exclude = /(node_modules|tests|dist|build|coverage.vscode)/;

module.exports = {
  mode: "development",
  devtool: "inline-source-map",
  // context: path.resolve(__dirname, "../"),
  entry: {
    objedi: "./src/objedi.ts",
  },
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./dist/"),
  },
  resolve: {
    extensions: [".ts"],
  },
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        exclude,
      }),
    ],
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: {
          loader: "ts-loader",
          options: {
            configFile: "tsconfig.dist.json",
          },
        },
        exclude,
      },
    ],
  },
};

.babelrc

{
  "plugins": ["@babel/plugin-proposal-unicode-sets-regex"]
}

我分别用以下两条命令测试了两种转译方式:

rm -rf dist && webpack # yarn build:webpack
babel dist --out-dir dist --extensions "\.js" --source-maps inline # yarn build:babel

内容的提问来源于stack exchange,提问作者brandonscript

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 18:37:03