为什么Webpack不处理node_modules文件?

goucqfw6  于 2023-06-30  发布在  Webpack
关注(0)|答案(1)|浏览(135)

我有任务-在工作的nodeJs项目中将node版本从12升级到16。节点升级后,其中一个存储库(在monorep中)开始在部署过程中失败。错误:

ERROR in <my project>/node_modules/@sailplane/injector/dist/esm/injector.js 197:23
Module parse failed: Unexpected token (197:23)
File was processed with these loaders:
 * ../../node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|   return function (target) {
|     let asName = undefined;
>     if (typeof options?.as === "function") {
|       // Validate that 'as' is a parent class
|       let found = false;
 @ ./src/discovery-gql.ts 1:0-47 20:25-33

正如你所看到的,它在node_modules中失败,并建议我为webpack使用适当的加载器。我当前的加载器(webpack.sls.config.js):

...
 module: {
    rules: [
      {
        test: /(?!:spec)\.tsx?$/,
        loader: 'ts-loader',
        exclude: /\.d\.ts$/,
      },
      {
        test: /\.d\.ts$/,
        loader: 'ignore-loader',
      },
      {
        test: /\.mjs$/,
        type: "javascript/auto"
      },
      {
        test: /\.js$/,          //this one I just added
        loader: 'babel-loader',
      },
      
    ],
  },
...

添加babel-loader后错误没有消失。为什么在node_modules / dist级别失败?我该怎么办?

xjreopfe

xjreopfe1#

是什么帮助了我:添加加载器:

{
        test: /\.js$/,
        exclude: /node_modules\/(?!(@sailplane\/injector)\/).*/,
        use: {
          loader: 'babel-loader',
        },
      }

并将plagins添加到babel:

plugins: [
        '@babel/plugin-proposal-optional-chaining',
        '@babel/plugin-proposal-nullish-coalescing-operator'
      ],

相关问题