【问题标题】:what does the "modules:auto" means in @babel/preset-env field?@babel/preset-env 字段中的“modules:auto”是什么意思?
【发布时间】:2019-04-22 09:54:36
【问题描述】:

babel 文档说模块默认选项是自动,自动是什么意思?它是否将“导入”转换为“要求”? “模块:假”和“模块:自动”有什么区别?似乎它们是相同的结果;我正在学习 tree shaking,当我没有给我的@babel/preset-env 设置任何选项时,tree shaking 成功,当我设置 "modules: false" 时,tree shaking 也成功了;

  "presets": [
    [
      "@babel/preset-react"
    ],
    [
      "@babel/preset-env",{"modules": "false"}
    ]
  ],

【问题讨论】:

    标签: javascript webpack babeljs babel-preset-env


    【解决方案1】:

    似乎 auto 的文档在this 拉取请求中有所描述。根据那个公关:

    如果调用者已经支持 ES6 模块语法,默认 auto 将自动选择 false,否则将自动选择 "commonjs"

    因此,如果支持 ES6(这似乎是您的情况),将模块设置为 auto 与设置 false 相同。

    【讨论】:

      【解决方案2】:

      auto 表示default valuemodules 的默认值为false。见 - https://github.com/babel/babel/pull/8485/files#r236086742

      `Invalid Option: The 'modules' option must be one of \n` +
        ` - 'false' to indicate no module processing\n` +
        ` - a specific module type: 'commonjs', 'amd', 'umd', 'systemjs'` +
        ` - 'auto' (default) which will automatically select 'false' if the current\n` +
        `   process is known to support ES module syntax, or "commonjs" otherwise\n`,
      

      【讨论】:

        【解决方案3】:

        它可以将 ES6 模块语法转换为另一种模块类型。

        参考:https://babeljs.io/docs/en/babel-preset-env#modules

        【讨论】:

          猜你喜欢
          • 2019-10-24
          • 1970-01-01
          • 2018-04-27
          • 1970-01-01
          • 2020-10-13
          • 1970-01-01
          • 1970-01-01
          • 2019-02-18
          • 1970-01-01
          相关资源
          最近更新 更多