簡體   English   中英

如何使Sourcemap在Webpack中工作?

[英]How to get sourcemaps to work in Webpack?

我最近從grunt / bower移到了webpack,但是無法使sourcemaps工作。 我得到像debugger:// VM8967這樣的代碼引用,而不是像我以前那樣使用真實文件名。 我嘗試了多種組合

mode: 'development', devtool: 'cheap-module-source-map',

正如許多地方所建議的,例如這里這里,但沒有任何運氣。 這似乎是每個人都需要的非常標准的行為,所以我確定我犯了一些愚蠢的錯誤。 希望有人能幫忙。 我用:

"webpack": "^4.39.1",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.7.2"

我的webpack.config看起來像這樣:

const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: './app/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, { loader: 'css-loader' }],
      },
      {
        test: /\.(eot|woff|ttf|woff2|svg|png|jpg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'assets',
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new CopyWebpackPlugin([{ from: 'app/images', to: 'images' }]),
    new CopyWebpackPlugin([{ from: 'app/languages', to: 'languages' }]),
    new CopyWebpackPlugin([{ from: 'app/views', to: 'views' }]),
    new CopyWebpackPlugin([{ from: 'app/robots.txt' }]),
    new CopyWebpackPlugin([{ from: 'app/404.html' }]),
    new CopyWebpackPlugin([{ from: 'app/.htaccess' }]),
    new CopyWebpackPlugin([{ from: 'app/favicon.ico' }]),
    new CopyWebpackPlugin([{ from: 'app/apple-touch-icon.png' }]),
    new HtmlWebpackPlugin({
      template: 'app/index.html',
    }),
    new MiniCssExtractPlugin({
      filename: 'style.css',
    }),
  ],
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000,
  },
};

對於js源映射,您可以這樣做

module.exports = {
  // ...
  devtool: false,
  plugins: [
    new webpack.SourceMapDevToolPlugin({
        filename: '[name].js.map',
        exclude: ['vendor.js']
    })
  ]
};

連結這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM