2015-11-01 47 views
0

我第一次尝试使用ES6模块,尝试使用Webpack进行基本捆绑。我想使用Babel 6(当前最新最好的)在Webpack加载器中进行转译。我读过thisthisthisGrunt/Webpack/Babel中的路径错误

的package.json包括:

"devDependencies": { 
    "babel": "~6.0.12", 
    "babel-core": "~6.0.12", 
    "babel-loader": "~6.0.0", 
    "babel-preset-es2015": "^6.0.14", 
    "grunt": "~0.4.5", 
    "grunt-webpack": "^1.0.11", 
    "webpack": "^1.12.2", 
    "webpack-dev-server": "^1.12.1" 
    }, 

文件结构:

myproject 
├── js 
│   ├── es6 
│   │   ├── app.js 
│   │   ├── lib 
│   │   │   ├── lib1.js 
│   │   │   ├── lib2.js 
│   │   └── modules 
│   │    └── _myModule.js 
├── node_modules 
├── index.html 
├── gruntfile.js 

gruntfile.js

module.exports = function(grunt) { 
    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 

     webpack: { 
      options: { 
       entry: "./js/es6/app.js", 
       output: { 
        path: __dirname, 
        filename: "webpacked.js", 
       }, 
       module : { 
        loaders: [ 
         { 

     // grunt --verbose says the problem is with the next line 
          include: path.resolve(__dirname, "es6"), 
          loader: 'babel',  
          query: { 
           presets: ['es2015'] 
          } 
         } 
        ] 
       }, 
       failOnError: true, 
       resolve: { 
        root: [ "js", "node_modules" ], 
       }  
      }, 
      build : { 
       devtool: "sourcemap", 
       debug: true 
      } 
     }, 

    }); 

    grunt.loadNpmTasks('babel'); 
    grunt.loadNpmTasks('babel-core'); 
    grunt.loadNpmTasks('babel-loader'); 
    grunt.loadNpmTasks('babel-preset-es2015'); 
    grunt.loadNpmTasks('grunt-minify-html'); 
    grunt.loadNpmTasks('grunt-webpack');  
    grunt.loadNpmTasks('grunt-sass'); 
    grunt.loadNpmTasks('grunt-contrib-watch'); 

    grunt.registerTask('default', ['webpack']); 
}; 

我显然缺少与路径处理很基本的东西。我甚至无法开始,而且我已经圈了几个小时,尝试了很多组合。错误是:

Loading "Gruntfile.js" tasks...ERROR 
>> ReferenceError: path is not defined 

任何帮助非常感激。

回答

6

你正在做

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

,并使用path模块,但是你有没有装好了。

var path = require('path'); 
+0

:)谢谢。现在我收到错误“Module parse failed:myproject/js/es6/app.js Line 1:Unexpected token:'Fling from Fling.js'您可能需要一个合适的加载器来处理这种文件类型。”看起来,转译的Babel加载器只能设置为在模块上工作,而不是在app.js“entry”文件本身中的ES6“导入”代码上。如何在构建的一开始就传输条目“app.js”文件,同时仍然只使用Webpack? – Ben

+0

'include'需要匹配任何ES6文件,所以如果你在'es6'文件夹外有一个文件夹,你需要扩展'include'以匹配它。 – loganfsmyth