johnagan / clean-webpack-plugin

A webpack plugin to remove your build folder(s) before building
MIT License
1.96k stars 136 forks source link

Remove source maps after build #177

Closed jdavidferreira closed 4 years ago

jdavidferreira commented 4 years ago

Issue description or question

Is it possible to remove the source maps after run the build? I tried this to remove all the source maps inside the dist folder:

Webpack Config

{
  mode: 'production',
  devtool: 'hidden-source-map',
  entry: './src/index.js',
  output: {
    filename: 'main.js', 
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/dist/'
  },
  plugins: [
    new CleanWebpackPlugin({
      cleanAfterEveryBuildPatterns: ['dist/*.map']
    })
  ]
}

But its not working.

Environment

System: OS: Windows 10 10.0.18363 CPU: (12) x64 Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz Memory: 6.13 GB / 15.93 GB Binaries: Node: 13.12.0 - C:\Program Files\nodejs\node.EXE npm: 6.14.4 - C:\Program Files\nodejs\npm.CMD npmPackages: clean-webpack-plugin: ^3.0.0 => 3.0.0 webpack: ^4.41.5 => 4.41.5

chrisblossom commented 4 years ago

You should just tell webpack not to generate sourcemaps via devtool: false or devtool: 'none'. See https://webpack.js.org/configuration/devtool/

But to solve your issue, cleanAfterEveryBuildPatterns are relative to the build directory. Try cleanAfterEveryBuildPatterns: ['**/*.map'].

{
  mode: 'production',
  devtool: 'hidden-source-map',
  entry: './src/index.js',
  output: {
    filename: 'main.js', 
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/dist/'
  },
  plugins: [
    new CleanWebpackPlugin({
      cleanAfterEveryBuildPatterns: ['**/*.map']
    })
  ]
}
jdavidferreira commented 4 years ago

I'm using a plugin that uploads the source maps to a server. I want to remove them after that.

No, it still does not work. 😕

chrisblossom commented 4 years ago

I forgot protectWebpackAssets: false.

const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const path = require('path');

module.exports = {
    mode: 'production',
    devtool: 'hidden-source-map',
    entry: './src/index.js',
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist/',
    },
    plugins: [
        new CleanWebpackPlugin({
            cleanAfterEveryBuildPatterns: ['**/*.map'],
            protectWebpackAssets: false,
        }),
    ],
};
jdavidferreira commented 4 years ago

It works! Thank you. 😁

This is something I've been searching for and I noticed that many people do too, and I wanted to avoid doing the script "webpack --config webpack.prod.js && rm ./dist/*.js.map". I like this solution more.

UmbraCi commented 3 years ago
protectWebpackAssets: false,

good job,because i want use sentry to record web error,first, i will upload source map to sentry.So , i need to remove them,after builded