【问题标题】:Babel loader doesn't work with webpack-dev-serverBabel 加载器不适用于 webpack-dev-server
【发布时间】:2018-07-22 23:17:34
【问题描述】:

我正在尝试设置我的 webpack 开发人员环境。您可以在下面找到我的 npm 依赖项和脚本部分:

"devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-register": "^6.26.0",
    "jshint": "^2.9.5",
    "jshint-loader": "^0.8.4",
    "node-libs-browser": "^2.1.0",
    "webpack": "^3.11.0",
    "webpack-dev-server": "^2.11.1"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "http-server",
    "server": "webpack-dev-server"
  },

还有我的 webpack.config.js:

    const path = require('path');
    let webpack = require('webpack');

    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'app.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            loaders: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    loader: 'jshint-loader'

                },
                {
                    test: /\.js$/,
                    loader: "babel-loader",
                    exclude: /node_modules/,
                    query: {
                        presets: ["env"]
                    }
                }
            ]
        },
        stats: {
            colors: true
        },
        devtool: 'source-map'
    };

当我运行 npm run server 时,我收到如下终端消息:

Project is running at http://localhost:8080/
webpack output is served from /
Hash: bd3defdad89370ccb56f
Version: webpack 3.11.0
Time: 1508ms
     Asset     Size  Chunks                    Chunk Names
    app.js   866 kB       0  [emitted]  [big]  main
app.js.map  1.05 MB       0  [emitted]         main

但我的输出目录中没有文件 (dist)。为什么我有这样的问题,我该如何解决它以将 babel 与 webpack watcher 一起使用?

更新 下面你可以看到我在执行webpack-dev-server后得到的完整输出

【问题讨论】:

  • 您想查看捆绑的文件吗?
  • 捆绑的文件根本没有生成,这是个问题
  • 是的,当你使用webpack-dev-sever时会发生这种情况
  • 试试我在下面提供的答案
  • 您是否尝试过使用 const 来要求 webpack 而不是 let 并且您是否尝试过设置 babelrc 文件而不是在 webpack 配置中使用 query 属性

标签: node.js webpack babeljs


【解决方案1】:

我的问题的解决方案非常简单,我用下一个方法解决它: 在我的 webpack.config.js 文件中,我添加了以下几行:

    module.exports = {
        //other options   
        ...
     watch: true,
        watchOptions: {
            aggregateTimeout: 300,
            poll: 1000
        },
      ...
       //other options

webpack 开始观察我的 js 代码并调用 babel 进行转译

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2016-11-19
    • 2019-09-21
    相关资源
    最近更新 更多