Babel不会从"node_modules"中转入已导入的模块 [英] Babel does not transpile imported modules from 'node_modules'

查看:658
本文介绍了Babel不会从"node_modules"中转入已导入的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从node_modules导入的模块进行编译存在问题. Babel出于某种原因不会转换从node_modules导入的模块,而是转换从src导入的模块.

I got a problem with transpiling imported modules from node_modules. Babel for some reason doesn't transpile imported module from node_modules, but transpile modules imported from src.

以下是示例存储库: https://github.com/NikitaKA/babeltest

main.js

// result code contains const and let, but it shouldn't. :(

index.js

import qs from 'query-string; // not transpiled
import lib from './lib' // transpiled

const query = qs.parse(window.location.search);

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
};

.babelrc

{
  "presets": [
    ["@babel/preset-env", {
      "modules": false,
      "targets": {
        "chrome": 39
      }
    }],
    ["@babel/preset-stage-1", {
      "modules": false,
      "decoratorsLegacy": true,
      "pipelineProposal": "minimal"
    }]
  ],
  "plugins": [
    "transform-es2015-constants",
    "@babel/plugin-transform-block-scoping",
    "@babel/plugin-transform-runtime"
  ]
}

推荐答案

展开我的评论:

您真的不想转译所有node_modules –这将花费很长时间,并且大多数代码中应该已经有ES5(除非它们实际上是ES6模块,在这种情况下为ES6条目)点在package.json清单中声明为"module".

You really don't want to transpile all of node_modules – it'll take a long time, and most of the code there should already be ES5 (unless they're actually ES6 modules, in which case the ES6 entry point is announced as "module" in the package.json manifest).

query-string@6.x 不是,它在其自述文件中如此表示:

此模块针对Node.js 6或更高版本以及最新版本的Chrome,Firefox和Safari.如果要支持较旧的浏览器,请使用版本5:npm install query-string@5.

这篇关于Babel不会从"node_modules"中转入已导入的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆