javascript - Grunt/Webpack/Babel 中的路径错误

标签 javascript gruntjs webpack babeljs

我第一次尝试使用 ES6 模块,尝试使用 Webpack 进行基本的捆绑。我想使用 Babel 6(目前最新最好的)在 Webpack 加载器中进行转译。我读过thisthisthis .

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

非常感谢任何帮助。

最佳答案

你在做什么

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

并使用path模块,但您尚未加载它。

var path = require('path');

关于javascript - Grunt/Webpack/Babel 中的路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33467388/

相关文章:

javascript - 延期/ promise 会促进违反得墨忒耳法则吗?

css - 是否有向文件夹中的所有图像添加背景图像的 Grunt 任务?

javascript - 如何使用 LESS 在 vue/webpack 中创建主题

angular - 将 webpack production 指向不同的 API url

javascript - AppCode IDE 中的 HTML/JavaScript 元素

javascript - 基于发布值的 XMLHttpRequest 响应状态

angularjs - 使用 Grunt 为 AngularJS 生成 HTML 文件

webpack - SCSS : concat files but not compile

javascript - 如何用另一个变量定义一个变量?

javascript - 在 Protractor Selenium 中运行单一测试