简体   繁体   English

Webpack预加载器如何工作?

[英]How do webpack pre-loaders work?

I'm building a node app and I'm using webpack to process my client side scripts. 我正在构建一个节点应用程序,并且正在使用webpack处理我的客户端脚本。 I'm using webpack node api and on my app bootstrap file I simply do this: 我正在使用webpack节点api,在我的应用程序引导文件中,我只是这样做:

    const webpack = require('webpack')
    const webpackConfig = require('./webpack.config.js')

    webpack(webpackConfig, (err, stats) => {
        if (err || stats.hasErrors()) {
           //do something with that
        }
    })

This works well until I try to write with ES6 syntax. 在我尝试使用ES6语法编写之前,此方法效果很好。 Although I have defined in my webpack.config.js that babel should be used to pre-process the files it simply doesn't work. 尽管我在webpack.config.js中定义了babel应该用于预处理文件,但它根本不起作用。 It says that there is a syntax error. 它表示存在语法错误。

this is my webpack.config.js 这是我的webpack.config.js

module.exports = {
    context: __dirname,
    entry: "./resources/assets/js/main.js",
    output: {
        path: __dirname + "/build",
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use : {
                    loader: "babel-loader",
                    options: {
                        presets: [
                            "es2015",
                            "es2016",
                            "stage-2"
                        ]
                    }
                }
            }
        ]
    }
}

this is my package.json 这是我的package.json

{
  "name": "topo",
  "version": "1.0.0",
  "description": "Node app test",
  "main": "index.js",
  "dependencies": {
    "nodemon": "^1.11.0"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^6.4.1",
    "babel-plugin-transform-object-rest-spread": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2016": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "pug": "^2.0.0-beta11",
    "webpack": "^2.3.3"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "António Quadrado",
  "license": "ISC"
}

I found multiple problems regarding webpack and ES6 with babel and I tried to work it out with the solutions that were given on those situations but I failed to make it work and that's why I posting my specific case, hoping that someone can point me to my error. 我发现有关babel的webpack和ES6的多个问题,我尝试使用针对这些情况的解决方案进行解决,但未能使其正常工作,这就是为什么我发布我的特定案例,希望有人可以指出我的意思错误。

I'm not just looking for the solution to this particular problem but also to understand how the webpack and its pre-loaders work so I can avoid situations like this in the future. 我不仅在寻找解决此特定问题的方法,而且还了解Webpack及其预加载器的工作方式,以便将来避免此类情况。

I'm using node v7.8.0 我正在使用节点v7.8.0

Thank you. 谢谢。

use expects an array of loaders to use for a particular rule. use期望将一组加载程序用于特定规则。 Since you only have a single loader in your rule, you should just put all of the loader properties directly inside that rule (dropping use entirely): 由于您的规则中只有一个加载程序,因此您应该将所有加载程序属性直接放在该规则内(完全删除use ):

rules: [
  {
    test: /\.js$/,
    loader: "babel-loader",
    options: {
      presets: [
        "es2015",
        "es2016",
        "stage-2"
      ]
    }
  }
]

If you had to use use , it would look like this instead: 如果必须使用use ,它将改为:

rules: [
  {
    test: /\.js$/,
    use: [
      {
        loader: "babel-loader",
        options: {
          presets: [
            "es2015",
            "es2016",
            "stage-2"
          ]
        }
      }
    ]
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM