javascript - webpack 2 + Babel 没有转译为 es2015

标签 javascript npm babeljs webpack-2

我已经创建了 webpack 示例项目,开始使用具有以下配置的 babel,我在各个网站上尝试过,但它们似乎不适合我

这是我的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"
  }
}

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);

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

任何人都可以帮我找出我做错了什么吗?

我也不明白加载器配置中 .babelrcquery 的区别。

最佳答案

请注意,loader 现在是 webpack 2.0 模块配置中的rules - 将您的 webpack 配置更改为如下所示以反射(reflect)这一点。

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;

关于javascript - webpack 2 + Babel 没有转译为 es2015,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42710853/

相关文章:

javascript - Webpack - 即使模块存在也找不到模块

javascript - 在 AngularJS 中为列表项加载部分 *没有* 路由

javascript - Bower 和 grunt 是全局还是本地?

asp.net-mvc - ReactJs.Net、Gulp、Babel、Browserify MVC

node.js - 直接在浏览器中使用 NPM 模块

node.js - 部署 AWS Beanstalk 后执行命令

javascript - 如何以编程方式将选项传递给 Babel 转换/插件?

javascript - 在每个图像顶部动态显示图标

javascript - Internet Explorer 中的 HTML5 元素 : runtime insertion

javascript - 使用 AngularJS 发送 HTTP header 请求