Webpack 4开发工具选项无法与webpack-dev-server一起使用

15

在我决定发布这个问题之前,我进行了一些背景检查。
所以,我的问题是:

- 我使用 webpack v4.6.0 和 webpack-dev-server v3.1.3 - 它们在一起工作得很好,但现在我正在尝试为我的应用程序设置源映射,似乎 devtool选项 不起作用。

至少对于我来说,我已经尝试并测试了列表中的每个选项:

  • Webpack 4 - Sourcemaps: 该问题建议devtool:'source-map'应该可以直接使用,但对我来说并非如此
  • how to make webpack sourcemap to original files: 将devtoolModuleFilenameTemplate:info =>'file://' + path.resolve(info.absoluteResourcePath).replace(/ \ / g,'/')添加到我的输出配置中并没有帮助太多,它显示index.js而不是client.js,如下图所示:enter image description hereenter image description here
  • https://github.com/webpack/webpack/issues/6400:这不是我问题的准确描述,尝试这里的方法似乎也没有帮助我
  • 我尝试使用webpack.SourceMapDevToolPlugin,但它也无法与我的设置一起使用,即使我删除了devtools或将它们设置为false
  • 我这里没有使用UglifyJS插件
  • 我知道webpack-dev-server现在正在维护中,所以我尝试使用webpack-serve,但是它似乎也不能使用源映射
  • 我也尝试过source-map-support包,但也没有运气,像这里一样遇到了类似的情况:enter image description here

你知道是否有PR修复了这个问题或者你自己尝试解决它了吗?感谢任何提示或帮助!

我想要得到如此描述的输出,在博客文章中,具有指向我的文件和原始文件代码的直接链接。

我的 webpack.js

// webpack v4.6.0
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const stylish = require('eslint/lib/formatters/stylish');
const webpack = require('webpack');

module.exports = {
  entry: { main: './src/index.js' },
  output: {
    devtoolModuleFilenameTemplate: info =>
      'file://' + path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[hash].js'
  },
  devtool: 'source-map',
  devServer: {
    contentBase: './dist',
    hot: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          formatter: stylish
        }
      }
    ]
  },
  plugins: [
    // new webpack.SourceMapDevToolPlugin({
    //   filename: '[file].map',
    //   moduleFilenameTemplate: undefined,
    //   fallbackModuleFilenameTemplate: undefined,
    //   append: null,
    //   module: true,
    //   columns: true,
    //   lineToLine: false,
    //   noSources: false,
    //   namespace: ''
    // }),
    new CleanWebpackPlugin('dist', {}),
    new HtmlWebpackPlugin({
      inject: false,
      hash: true,
      template: './src/index.html',
      filename: 'index.html'
    }),
    new WebpackMd5Hash(),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin()
  ]
};

my package.json

{
  "name": "post",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "storybook": "start-storybook -p 9001 -c .storybook",
    "dev": "webpack-dev-server --mode development --open",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@storybook/addon-actions": "^3.4.3",
    "@storybook/react": "v4.0.0-alpha.4",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-runtime": "^6.26.0",
    "clean-webpack-plugin": "^0.1.19",
    "eslint": "^4.19.1",
    "eslint-config-prettier": "^2.9.0",
    "eslint-loader": "^2.0.0",
    "eslint-plugin-prettier": "^2.6.0",
    "eslint-plugin-react": "^7.7.0",
    "html-webpack-plugin": "^3.2.0",
    "prettier": "^1.12.1",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "webpack": "v4.6.0",
    "webpack-cli": "^2.0.13",
    "webpack-dev-server": "v3.1.3",
    "webpack-md5-hash": "0.0.6",
    "webpack-serve": "^0.3.1"
  },
  "dependencies": {
    "source-map-support": "^0.5.5"
  }
}

我的控制台输出是:在这里输入图片描述

编辑:

我看到了一个类似的问题here,但是似乎没有人回答。 这个错误是故意的! 这不仅适用于ESLint错误,还适用于我应用程序中的每个错误。 这是我的GITHUB库链接:https://github.com/marharyta/webpack-fast-development

更新01.05.2018

我创建了另一个干净的设置库:https://github.com/marharyta/webpack-4.6.0-test 我在这里详细说明了如何到达那里:https://medium.com/p/79fb676417f4/edit 一些建议由webpack开发人员提供,但对我仍然无效:https://github.com/marharyta/webpack-4.6.0-test/issues/1

更新02.05.2018

经过长时间的调查,我在下面发布了我的答案。问题是ESLint和可能是一些模式标记,因为我必须以CLI方式完成它。 我还在这里发布了一个ESLint加载程序问题:https://github.com/webpack-contrib/eslint-loader/issues/227 我还创建了一个带有更详细描述的帖子:https://medium.com/@riittagirl/how-to-solve-webpack-problems-the-practical-case-79fb676417f4

2个回答

10

经过长时间的尝试和错误,我最终从webpack维护者中得到了帮助。主要问题出在eslint上。如果将其作为加载器来加载,会导致意外的行为。通过从Webpack JS加载器中删除eslint,可以解决这个问题。

之前的Webpack设置:

// webpack v4
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const baseConfig = {
  entry: { main: './src/index.js' },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[hash].js'
  },
  devServer: {
    contentBase: './dist',
    hot: true,
    open: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        **use: [
          { loader: 'babel-loader' },
          {
            loader: 'eslint-loader',
            options: { formatter: require('eslint/lib/formatters/stylish') }
          }**
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin('dist'),
    new HtmlWebpackPlugin({
      inject: false,
      hash: true,
      template: './src/index.html',
      filename: 'index.html'
    }),
    new WebpackMd5Hash()
  ]
};

if (process.env.NODE_ENV === 'development') {
  baseConfig.devtool = 'inline-source-map';
}

module.exports = baseConfig

经过改进后可以正常工作的Webpack:

// webpack v4
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
  entry: { main: './src/index.js' },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[hash].js'
  },
  devtool: 'cheap-module-source-map',
  devServer: {
    contentBase: './dist',
    hot: true,
    open: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        **use: [{ loader: 'babel-loader' }]**
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin('dist'),
    new HtmlWebpackPlugin({
      inject: false,
      hash: true,
      template: './src/index.html',
      filename: 'index.html'
    }),
    new WebpackMd5Hash()
  ]
};

我的package.json文件看起来像这样:

{
  "name": "post",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack --mode=production",
    "start": "NODE_ENV=development webpack-dev-server --mode=development --hot"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-runtime": "^6.26.0",
    "clean-webpack-plugin": "^0.1.19",
    "eslint": "^4.19.1",
    "eslint-config-prettier": "^2.9.0",
    "eslint-loader": "^2.0.0",
    "eslint-plugin-prettier": "^2.6.0",
    "eslint-plugin-react": "^7.7.0",
    "html-webpack-plugin": "^3.2.0",
    "prettier": "^1.12.1",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.0.13",
    "webpack-md5-hash": "0.0.6"
  },
  "dependencies": {
    "webpack-dev-server": "^3.1.3"
  }
}

另外请参考在我的分支上创建的问题提出的建议: https://github.com/marharyta/webpack-4.6.0-test


1
非常感谢您!在移除ESlint并设置devtool:null后,我的捆绑包大小从8 MB解析为1.6 MB解析。在此之前,设置devtool:null会生成一个更小的生产捆绑包,但不能正常工作 :). 移除eslint是缺失的一步。没有您的答案,我永远不可能想到这一点。 - David Simic

0
在webpack4中,您需要在webpack配置中设置模式。请添加:
mode: "development"

添加到你的webpack配置文件中。

你可以移除NamedModulesPlugin,因为它已经在开发模式下使用了。

source-map选项是用于生产构建的。 对于你的开发构建,我会移除devtool属性,因为这样webpack会默认使用eval。 如果不起作用,请尝试:

devtool: 'cheap-module-eval-source-map'

这就是我使用的。最小化配置。

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const stylish = require('eslint/lib/formatters/stylish');
const webpack = require('webpack');

module.exports = {
  mode: 'development',
  entry: { main: './src/index.js' },
  output: {
    filename: '[name].[hash].js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  devtool: 'cheap-module-eval-source-map',
  devServer: {
    hot: true,
    open: true
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [
          { loader: 'babel-loader' },
          { loader: 'eslint-loader', options: { formatter: stylish } }
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin('dist'),
    new HtmlWebpackPlugin({
      template: './src/index.html'
    }),
    new webpack.HotModuleReplacementPlugin()
  ]
};

设置模式不起作用,很遗憾。 - margarita
你也移除了这个 devtoolModuleFilenameTemplate 吗? - Daniel
没有NamedModulesPlugin并不是问题。我只是想告诉你,它已经不再需要了。 - Daniel
此外,您的代码检查器会向您抛出错误。您应该先修复它们。 - Daniel
我故意制造了一些错误,以便查看浏览器如何处理它们。 - margarita
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接