【问题标题】:webpack 2 + Babel is not transpiling to es2015webpack 2 + Babel 没有转译为 es2015
【发布时间】:2017-03-10 05:22:45
【问题描述】:

我已经创建了 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 在加载器配置中的区别。

【问题讨论】:

  • 语法不适合 webpack 2(见 blog.madewithenvy.com/…
  • 比如把 babel-loader 给 loader 参数。
  • Matthew,是的,存在语法问题,我在测试正则表达式中使用了单引号,这是错误的,不应该。

标签: javascript npm babeljs webpack-2


【解决方案1】:

注意 loaders 现在在 webpack 2.0 的模块配置中是 rules - 将您的 webpack 配置更改为如下所示。

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;

【讨论】:

    【解决方案2】:

    如果 .babelrc 文件存在,它总是被首先读取

    npm install babel-plugin-transform-runtime

    插入到 .babelrc

    {
      "plugins": [
        "transform-runtime"
      ],
      "presets": [
        "es2015"
      ]
    }
    

    【讨论】:

    • 感谢您告诉我对 .babelrc 的偏好
    【解决方案3】:
    module: {
      rules: [
        { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" }
      ]
    }
    

    babel-loader 试试这个选项 more info

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 2018-02-23
      • 1970-01-01
      • 2017-06-23
      • 2019-10-18
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多