Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcemaps are not emitted when excluded with deleteOriginalAssets option #215

Closed
ghost opened this issue Nov 6, 2020 · 15 comments · Fixed by #216
Closed

Sourcemaps are not emitted when excluded with deleteOriginalAssets option #215

ghost opened this issue Nov 6, 2020 · 15 comments · Fixed by #216

Comments

@ghost
Copy link

ghost commented Nov 6, 2020

  • Operating System: MacOs Catalina
  • Node Version: 14.15
  • NPM Version: 6.14.8
  • webpack Version: 5.4.0
  • compression-webpack-plugin Version: 6.0.5

Expected Behavior

Sourcemap should be emitted with deleteOriginalAssets when excluded from compression.

Actual Behavior

When deleteOriginalAssets is enabled and map files are excluded from the compression. It is expected that js files are compressed with the same filename and map files are emitted as it is. but the map files are not emitted.

Code

// webpack.config.js
const CompressionPlugin = require('compression-webpack-plugin')
const FileManagerPlugin = require('filemanager-webpack-plugin')

module.exports = {
  mode: 'production',
  devtool: 'source-map',
  plugins: [
    new FileManagerPlugin({
      events: {
        onStart: {
          delete: ['dist/']
        }
      }
    }),
    new CompressionPlugin({
      filename: '[file]',
      exclude: /.map$/,
      minRatio: 10,
      deleteOriginalAssets: true
    })
  ]
}

How Do We Reproduce?

Run webapck with the given config. the dist folder should have a compressed main.js and uncompressed main.js.map, but the map file is not emitted.

@alexander-akait
Copy link
Member

Expected, remove original source means remove source maps too

@ghost
Copy link
Author

ghost commented Nov 6, 2020

Thanks @evilebottnawi .

Anyway we could achieve this? I believe this is a general expectation. If this can't be fixed. Compression has to be done outside webpack I guess.

@ghost
Copy link
Author

ghost commented Nov 6, 2020

I was just thinking deleteOriginalAssets should have been something like overwriteOriginalAssets 🤔 .

@alexander-akait
Copy link
Member

alexander-akait commented Nov 6, 2020

No new options, we can improve this, deleteOriginalAssets: true | false | 'keep-source-map', but it will not easy task

@ghost
Copy link
Author

ghost commented Nov 6, 2020

Sounds good. I can give it a try. Can you put out a help wanted label. Incase you are busy. Someone can take this?

@alexander-akait
Copy link
Member

Yes, but nobody will take 😄 because it OSS

@alexander-akait
Copy link
Member

alexander-akait commented Nov 6, 2020

I think I was wrong here, look at https:/webpack-contrib/compression-webpack-plugin/blob/master/src/index.js#L303, it is easy:

if (this.options.deleteOriginalAssets) {
  if (this.options.deleteOriginalAssets === 'keep-source-map) { 
     CompressionPlugin.updateAsset(
              compilation,
              name,
              inputSource,
              // Here we have link on source map file, we should just remove it from object and webpack keep source maps
              {}
    );

  }

  CompressionPlugin.deleteAsset(compilation, name);
}

@alexander-akait
Copy link
Member

You can try, it should be really easy

@ghost
Copy link
Author

ghost commented Nov 6, 2020

Sure. Thanks for the tip.

@ghost
Copy link
Author

ghost commented Nov 6, 2020

const newAssetInfo = {
  ...info,
  related: { ...info.related, sourceMap: null },
};

CompressionPlugin.updateAsset(compilation, name, inputSource, newAssetInfo);

@evilebottnawi . removing the sourceMap property doesn't work. some value has to be set for the sourceMap else the default gets applied. I am setting it to null. Is that fine?

@alexander-akait
Copy link
Member

alexander-akait commented Nov 6, 2020

Let's use delete newAssetInfo.related.sourceMap, you should do it before https:/webpack-contrib/compression-webpack-plugin/blob/master/src/index.js#L303 and webpack will not touch source map files

@ghost
Copy link
Author

ghost commented Nov 6, 2020

Actually thats what I did first.

if (this.options.deleteOriginalAssets) {
  if (this.options.deleteOriginalAssets === "keep-source-map") {
    const { sourceMap, ...related } = info.related || {}; // <--- Removing the source map property.
    const newOriginalInfo = {
      ...info,
      related,
    };

    CompressionPlugin.updateAsset(
      compilation,
      name,
      inputSource,
      newOriginalInfo
    );

    console.log(newOriginalInfo);
  }

  const currentAsset = CompressionPlugin.getAsset(compilation, name);
  console.log(currentAsset.info); // <--- Still having source-map property in related info

  CompressionPlugin.deleteAsset(compilation, name);
} else {
  // existing logic
}

only setting a new value to it works. else the property is added back again.

@alexander-akait
Copy link
Member

Do it work?

@ghost
Copy link
Author

ghost commented Nov 6, 2020

Setting it to null works. but I am just little not sure if it, whether it will create any unintended sideeffects?

@alexander-akait
Copy link
Member

No side effect, here simple logic, when something removed webpack automatically remove all related, so if you want keep something just remove it from related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant