Search code examples
javascriptnpmbabeljswebpack-2

webpack 2 + Babel is not transpiling to es2015


I have created sample project of webpack, started to use of babel with following configuration , which I tried from various sites but they doesn't seems to working for me

Here is my webpack.config.js

const path=require('path');

const config={

    entry:'./src/index.js',
    output:{
        path:path.resolve(__dirname,'build'),
        filename:'bundle.js'
    },
    module: {
        loaders:[
            {
                test:'/\.js?$/',
                loader: "babel",
                exclude: /node_modules/,
                query: {
                    presets: ['es2015'],
                }
            }
        ]
    }

};


module.exports=config;

.babelrc

{
    "presets":["es2015"]
}

package.json

{
  "name": "webpack2",
  "version": "1.0.0",
  "description": "This is first Webpack 2 project",
  "main": "index.js",
  "scripts": {
    "build": "webpack"
  },
  "author": "Viraj Nimbalkar",
  "license": "",
  "devDependencies": {
    "babel-core": "^6.23.1",
    "babel-loader": "^6.4.0",
    "babel-preset-env": "^1.2.1",
    "babel-preset-es2015": "^6.22.0",
    "webpack": "^2.2.1"
  }
}

Output in bundle.js

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;
/******/
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // identity function for calling harmony imports with the correct context
/******/    __webpack_require__.i = function(value) { return value; };
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {


const sum=(a,b)=>a+b;

module.exports=sum;


/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {


const sum=__webpack_require__(0);

const total=sum(10,5);

console.log('Total='+total);

/***/ })
/******/ ]);

Can any one help me here to identify, whats wrong I am doing ?

As well as I don't understand difference of .babelrc and query inside loader config.


Solution

  • Notice that loaders are now rules in module config for webpack 2.0 - Change your webpack config to like below to reflect that.

    const config={
    
        entry:'./src/index.js',
        output:{
            path:path.resolve(__dirname,'build'),
            filename:'bundle.js'
        },
        module: {
            rules: [
                {
                  test: /\.js$/,
                  exclude: [/node_modules/],
                  use: {
                    loader: 'babel-loader',
                    options: {
                      presets: ['es2015']
                    }
                  }
                }
            ]
        }
    
    };
    
    
    module.exports=config;