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

时间:2018-04-30 17:52:29

标签: reactjs npm webpack webpack-dev-server source-maps

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

- 我使用webpack v4.6.0和webpack-dev-server v3.1.3 - 它们一起工作正常,但现在我正在尝试为我的应用程序设置源地图,似乎devtool option不起作用。

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

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

您是否知道某个公关是否会修复该问题或您是否尝试自行解决?任何提示或帮助表示赞赏!

我想获得此处所述的输出,in 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()
  ]
};

我的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"
  }
}

我在控制台中的输出是:enter image description here

修改

我看到了一个类似的问题here,但似乎没有人回答。 错误是故意制作的! 这不仅适用于linting错误,而且适用于我的应用程序的每个错误。 的 Here is a link to my GITHUB repo: 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 个答案:

答案 0 :(得分:4)

所以,经过长时间的尝试和错误,我终于得到了一位webpack mainteiners的帮助。 主要问题是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




以后工作的网络包:



// 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()
  ]
};




我的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"
  }
}




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

答案 1 :(得分:0)

在webpack4中,您需要在webpack配置中设置模式。请添加

mode: "development"

到你的webpack配置。

您可以删除NamedModulesPlugin,因为它已在开发模式下使用。

选项source-map适用于生产版本。 对于你的开发构建,我会删除devtool道具,因为那时,webpack默认使用eval。 如果它不起作用,那么试试:

devtool: 'eval-cheap-module-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: 'eval-cheap-module-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()
  ]
};