首页 > webpack+babel配置

webpack+babel配置

我用webpack去编译es6语法编译不了,控制台没报错,这些算是坑么!!!!

这是我的目录结构

webpack
    |___app
        |___base.js
        |___index.js
        |___style.css
        |___css
            |___style.css
    |___build
    |___es6
        |___index.js
    |___build_es6
        |___build.js
        |___index.js        
    |___node_modules
    |___index.html
    |___package.json
    |___webpack.config.js
    |___webpack.es6.config.js

这个我的package.json,所有要加载的都加载了

 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack-dev-server --hot --inline",
    "start_es6": "webpack --config webpack.es6.config.js",
    "build_es6": "webpack-dev-server --hot --inline --config webpack.es6.config.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.9.0",
    "babel-core": "^6.9.1",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.5.0",
    "html-webpack-plugin": "^2.19.0",
    "react": "^15.1.0",
    "react-dom": "^15.1.0",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },

不过在生成的build_es6下的build.js他编译后没成功是这样的,index.html文件根本运行不了

/******/ (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] = {
/******/             exports: {},
/******/             id: moduleId,
/******/             loaded: false
/******/         };

/******/         // Execute the module function
/******/         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/         // Flag the module as loaded
/******/         module.loaded = 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;

/******/     // __webpack_public_path__
/******/     __webpack_require__.p = "";

/******/     // Load entry module and return exports
/******/     return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {

    var fn = () => {
        console.log(456);
    }

    fn();

    class Student {
        hello(){
            console.log("hello")
        }
    }

    var s = new Student;

    s.hello();

    class leo extends Student{

    }

    var l = new leo;

    l.hello();

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

这个我的webpack.es6.config.js

var htmlWebpackPlugin = require('html-webpack-plugin');
var path = require("path");
module.exports = {
    entry:{
        build:'./es6/index.js'
    },//要打包的对象
    output:{//输出
        path:"./build_es6",
        //path: path.resolve(__dirname, "build_es6"),
        //publicPath: "/build_es6/",
        filename: "[name].js"
    },
    module:{//添加模块
        loaders:[//模块加载器
            {
                test:/.css$/,//搜索以css后缀名的正则
                loaders:['style','css'],
                exclude:"/node_modules/"//打包时过滤掉这个文件
            },
            {//es6的加载器
                test:/.js$/,
                loaders:['babel?{"presets":["es2015"]}'],
                exclude:"/node_modules/",
                include:path.resolve(__dirname, "/es6/")
            }
        ]
    },
    resolve:{
        extensions:['','.js','.css','jsx']//自动补全后缀,所以在页面引用的时候不用写这些后缀名
    },
    plugins:[
        new htmlWebpackPlugin({
            title:'reactjs',
            filename:'index.html'
        })
    ]
}

  1. 你使用npm run start_es6编译的么?(命令命名怎么这么怪?)

还有把这句话去掉吧:

include:path.resolve(__dirname, "/es6/")

你这个场景里没作用不说,看着真碍眼


新建一个.babelrc文件吧,一看你就是这个没配好,建议好好看看教程吧。

【热门文章】
【热门文章】