webpack中babel-loader的配置过程

真理: 官网才是王道

回合1

此次的是在网上看的blog做的 地址 其实博客写的很好,只是 webpack更新了.所以还是要向官网看齐.

npm i -D webpack webpack-cli
npm i -D babel-loader babel-core
npm i -D babel-preset-env 

此时编辑我的js文件(用es6的语法). 然后 用webpack编译

npx webpack   //报错

错误如下:

Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module '@babel/core'
 babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.

好像是让我安装babel-loader@7, 那就安装吧:

npm i -D babel-loader@7

此时在编译 没问题了

主要代码:

// .babelrc
{
  "presets": ["env"]
}
// package.json 中的 部分
 "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.23.0",
    "webpack-cli": "^3.1.2"
  }
// webpack.config.js中的部分
mode: 'development',  //此行不加 webpack会提示你要添加mode
module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader"
      }
    ]
  }

回合2

跟着官网来呗 babel-loader

安装有两个版本 都可用

  • one

    // webpack 4.x | babel-loader 8.x | babel 7.x
    npm install -D babel-loader @babel/core @babel/preset-env webpack
    • two

      // webpack 4.x | babel-loader 7.x | babel 6.x
      npm install -D babel-loader@7 babel-core babel-preset-env webpack

webpack.config.js配置

module: {
  rules: [
    {
      test: /\.m?js$/,
      exclude: /(node_modules|bower_components)/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env']
        }
      }
    }
  ]
}

一次成功. yes!~~ 更多用法详见官网 babel-loader


   转载规则


《webpack中babel-loader的配置过程》 lttztt 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
mvc思想 mvc思想
起源2010年, Backbone.js出现. 单词 increase 加 decrease 减 square 平方 cube 立方 意大利面条 思想是: 用动态页面,: 获取数据 > 更新数据 > 更改数据 > 更新数
2018-10-26
下一篇 
node.js简单练习-阮一峰 node.js简单练习-阮一峰
一直想自己写接口玩玩,今天公司不忙就在网上找找有没有合适的练手的demo,发现阮一峰老师有写过此类教程.github地址: ruanyf/jstrining这个项目是一个很全面的教程,我此次只练习的一部分,大家喜欢可以都试试. 阮一峰的笔记
2018-10-23
  目录