13

I'm trying to find the issue with this Webpack configuration.

I cannot debug with original source in REACT.

Currently, I'm using Chrome dev tools.

Issue: enter image description here

Expected: enter image description here

And here my dependencies

"babel-loader": "^7.1.0",
"babel-plugin-lodash": "^3.2.11",
"babel-plugin-transform-react-jsx-source": "^6.22.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-plugin-react-transform": "^3.0.0",
"babel-plugin-transform-runtime": "^6.23.0",

/* global __dirname, require, module*/

const webpack = require('webpack');
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const env = require('yargs').argv.env;
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin');
const pkg = require('./package.json');

const WidgetName = pkg.main.match(/lib\/(.*).js/).pop();

let plugins = [
  new webpack.DefinePlugin({
    'process.env': {
      NODE_ENV:
        env === 'build'
          ? JSON.stringify('production')
          : JSON.stringify('development'),
    },
  }),
  new HtmlWebpackPlugin({
    title: WidgetName,
    template: '../index.html',
    minify: {
      removeComments: env === 'build' ? true : false,
      collapseWhitespace: false,
      removeRedundantAttributes: env === 'build' ? true : false,
      useShortDoctype: env === 'build' ? true : false,
      removeEmptyAttributes: env === 'build' ? true : false,
      removeStyleLinkTypeAttributes: env === 'build' ? true : false,
      keepClosingSlash: env === 'build' ? true : false,
      minifyJS: env === 'build' ? true : false,
      minifyCSS: env === 'build' ? true : false,
      minifyURLs: env === 'build' ? true : false,
    },
    inject: true,
  }),
  new DuplicatePackageCheckerPlugin({
    verbose: true,
  }),
  new LodashModuleReplacementPlugin()
];
let outputFile;

if (env === 'build') {
  plugins.push(
    new webpack.optimize.ModuleConcatenationPlugin(),
    new UglifyJsPlugin({
      mangle: true,
      compress: {
        warnings: false,
        pure_getters: true,
        unsafe: true,
        unsafe_comps: true,
        screw_ie8: true,
      },
      output: {
        comments: false,
      },
      exclude: [/\.min\.js$/gi],
    }),
    new CompressionPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: /\.(js|css|html|svg)$/,
      threshold: 10240,
      minRatio: 0.8,
    }),
    new webpack.BannerPlugin('$Rev: 1077 $')
  );
  outputFile = WidgetName + '.js';
} else {
  outputFile = WidgetName + '.js';
}

const SOURCE_PATH = path.resolve(__dirname, './src/');
const DIST_PATH = path.resolve(__dirname, './lib/');

const config = {
  context: SOURCE_PATH,
  entry: './index.js',
  devtool: 'source-map',
  output: {
    path: DIST_PATH,
    filename: outputFile,
    library: ['TEMP', TEMP],
    libraryTarget: 'umd',
    libraryExport: 'default',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
      {
        test: /\.svg$/i,
        loader: 'raw-loader',
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json'],
    modules: [SOURCE_PATH, 'node_modules', path.join(__dirname, "node_modules")],
  },
  plugins: plugins,
  devServer: {
    host: 'localhost',
    port: 3001,
    stats: {
      colors: true,
      errors: true,
    },
  },
};

module.exports = config;
taile
  • 2,738
  • 17
  • 29
  • What is your version of react js? – praveenkumar s Feb 01 '18 at 12:03
  • I'm using ^15.6.1 – taile Feb 01 '18 at 12:04
  • You couldn't see the error in your exact components jsx file. It's bundled in single js file. So debug is really tough in this case. Is this your concern? – praveenkumar s Feb 01 '18 at 12:09
  • It seems the babel compiled the code instead of keeping the original code as the second attached picture which I prefer – taile Feb 01 '18 at 12:12
  • 1
    ok, Just change 'devtool: "source-map"' to 'devtool: 'inline-source-map''. this help you to find the bug of your original code – praveenkumar s Feb 01 '18 at 12:15
  • While making it a production build, replace this to source-map, becoz it is not a secure way for live prod – praveenkumar s Feb 01 '18 at 12:24
  • For the second picture, I used 'eval-source-map' which is faster than ''inline-source-map'. But It didn't compile as I expected – taile Feb 01 '18 at 12:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164335/discussion-between-tai-le-and-praveenkumar-s). – taile Feb 01 '18 at 12:25
  • Please explain your problem a liitle bit more. I am not able to depict by the above two images. Thanks – Pravesh Khatri Jul 17 '18 at 17:54
  • Can you give more info? Browser, is it the code shown from webpack:// or webpack-internals:// (when you use source in the browser)? How does your source directories look like in the browser – SirPeople Jul 18 '18 at 13:26

1 Answers1

-2

Just add sourceMap: true to the UglifyJsPlugin options, the default has been changed by the team.

    new UglifyJsPlugin({
      sourceMap: true
      ...
    }),
Ishan Gulati
  • 187
  • 4