【问题标题】:Does webpack support es6 module natively?webpack 是否原生支持 es6 模块?
【发布时间】:2016-04-29 13:22:11
【问题描述】:

我刚开始学习使用 webpack 作为打包器。但是要开始编写 es6 代码,它需要一个 babel-loader 来将 es6 翻译成 es5。

我的困惑是为什么 webpack 需要 babel-loader?为什么它不知道如何处理 es6 模块语法??

如果我写的是纯 es5 代码,那么它不需要任何模块加载器,这是为什么呢?

【问题讨论】:

    标签: webpack


    【解决方案1】:

    目前还不支持 ES2015 Native 模块。但是,Webpack 2 将利用 Native 模块加载,这将允许一些很棒的功能,例如 tree-shaking。

    您可以查看此设置的示例here

    【讨论】:

      【解决方案2】:

      如果你想在你的 webpack 配置中使用 ES6/ES2015,你必须创建一个快速服务器并运行“npm start”。确保您在 package.json 脚本中进行了设置。确保你也安装了 babel-cli 和 babel-core。这是我的 package.json 示例:

        "scripts": {
          "start": "better-npm-run start",
          "compile": "better-npm-run compile",
          "deploy": "better-npm-run deploy",
          "clean": "rm -rf dist && npm cache clean",
        },
        "betterScripts": {
          "start": {
            "command": "babel-node bin/server",
            "env": {
              "NODE_ENV": "development",
              "DEBUG": "app:*"
            }
          },
      

      如果您想查看使用 es6 进行 webpack 配置的示例,请查看此 git 存储库:https://github.com/codetony25/react-starter-boilerplate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-23
        • 1970-01-01
        • 2020-06-07
        • 2018-03-22
        • 2019-11-27
        • 2015-03-26
        • 2016-01-20
        • 2016-01-18
        相关资源
        最近更新 更多