Webpack 4 devtool选项不适用于webpack-dev-server

mar*_*ita 10 npm source-maps reactjs webpack webpack-dev-server

在我决定发布这个问题之前,我做了很多事情作为背景调查.所以,我的问题是:

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

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

  • Webpack 4 - Sourcemaps:这个问题表明devtool: 'source-map'应该开箱即用,但对我来说情况并非如此
  • 如何使webpack源码映射到原始文件:添加 devtoolModuleFilenameTemplate: info =>'file://' + path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')到我的输出配置没有多大帮助,而不是client.js它为我显示index.js虽然在此输入图像描述在此输入图像描述
  • https://github.com/webpack/webpack/issues/6400:这个不是我的问题的准确描述,通过尝试这里的方法似乎没有帮助我
  • 我尝试使用webpack.SourceMapDevToolPlugin但它也不适用于我的设置,即使我删除devtools或将它们设置为false
  • 我这里不使用UglifyJS插件
  • 我知道webpack-dev-server现在处于维护状态,所以我尝试了webpack-serve,但是看起来源映射不适用于它
  • 我曾尝试源地图支持包为好,但没有运气或者,作为有类似的情况在这里:在此输入图像描述

你知道这个问题是由一些PR修复还是你自己尝试解决?任何提示或帮助表示赞赏!

我想获得这里描述的输出,在blogpost中直接链接到我的文件和原始文件代码.

我的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()
  ]
};
Run Code Online (Sandbox Code Playgroud)

我的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

我在控制台中的输出是: 在此输入图像描述

编辑:

我在这里看到了类似的问题,但似乎没有人回答. 错误是故意的! 这不仅适用于linting错误,而且适用于我的应用程序的每个错误. 这是我的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 /如何对解决-的WebPack-问题最实际情况,79fb676417f4

mar*_*ita 9

所以,经过长时间的尝试和错误,我终于得到了一位 webpack 维护者的帮助。主要问题是 eslint。如果您将其作为加载程序加载,则会产生意外行为。通过从 js 的 webpack 加载器中删除 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
Run Code Online (Sandbox Code Playgroud)

在以下之后工作的 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()
  ]
};
Run Code Online (Sandbox Code Playgroud)

我的 packeje.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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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