Skip to content

Adding asset to compilation.assets array problem #1287

Closed

Description

@lgordey

Hi everyone!
I have a problem with adding new asset to compilations.assets array.
Last week i solved some problems with it but i have the main and the last one (I hope so).

I have a plugin that generates new asset:

var sprite = oneSvg + secondSvg;

compiler.plugin('emit', function(compilation, callback) {
  compilation.assets['svg/my-sprite.' + self.hash(sprite) + '.svg'] = {
    source: function() { return new Buffer(sprite) },
    size: function() { return Buffer.byteLength(this.source(), 'utf8'); }
  }
  callback();
});

After plugin work I have my-sprite.svg - it have hash (I wrote simple crypto function for it), it added to compilation.assets webpack array and it appeared in destination path in hashed view like my-sprite.e5b4f3c18671eebfefa8bae79a760728.svg.

So it seems that everything is ok but my new asset doesn't appear in manifest.json that i collect by manifest-revision-webpack-plugin.

This plugin makes manifest of files and you can set format to it.
But it takes asset from compilations.stats object like:

compiler.plugin('done', function (stats) {
  var data = stats.toJson(options);
  var parsedAssets = self.parsedAssets(data.modules);

And now problems:

Assets added by code like I wrote above (compiler.plugin('emit'...) adding assets to stats.assets array but not to stats.modules. I made the issue with it.

So at first I decided to make a pull request to this repository to change data.modules to data.stats and to make some fixes in parsedAssets function and solve the problem.

But then i see that stats.modules had the name of asset in one of fields without hash - and this is the main thing.

Because I want manifest.json to look like this:
image

And i didn't have it (names without hashes) in stats.assets array:

So 2 things:

  1. I have my added asset in stats.assets but not in stats.modules
  2. I have names of assets without hashes in stats.modules but not in stats.assets

And the link with repo for describing this situation, I made it specially for it:

https://github.com/lgordey/webpack-plugin

Thank you!

Activity

  1. sokra commented on Jul 23, 2015

    @sokra
    Member

    This is a manifest-revision-webpack-plugin issue.

  2. lgordey commented on Jul 23, 2015

    @lgordey
    Author

    No, Tobias, that's not quite right.

    Sure I'll send pull request to manifest-revision-webpack-plugin, but at first I shoud have field in compilations.assets to have file.name or file paths without hash.

    Cause if I generate name with hash myself I should send it to compilation.assets array, otherwise I can't use it in manifest in my case.

    Now I have only this fields:

    { 
        name: 'svg/bbb.b4daae4c895c3277eba140e5aad5c3d4.svg',
        size: 1537,
        chunks: [],
        chunkNames: [],
        emitted: true 
    }

    Can we add one more field?
    For example I can add it from my plugin like:

    compilation.assets['svg/my-sprite.' + self.hash(sprite) + '.svg'] = {
        filePath: function() { return 'svg/my-sprite.svg' },
        source: function() { return new Buffer(sprite) },
        size: function() { return Buffer.byteLength(this.source(), 'utf8'); }
    }

    Now of course this does not work because I see in Stats.js:

    var obj = {
        name: asset,
        size: compilation.assets[asset].size(),
        chunks: [],
        chunkNames: [],
        emitted: compilation.assets[asset].emitted
    };

    So we can add one more field to obj and after that I could change manifest-revision-webpack-plugin to take assets array from it.

    Because now there is now way to send to versions of file name (with and without hash) and use it after this in other plugins as:

    "svg/aaa.svg": "svg/aaa.fa45a4659adad6648e03653367c28752.svg",
  3. lgordey commented on Jul 24, 2015

    @lgordey
    Author

    We need smth like name in module section

    name: module.readableIdentifier(requestShortener),

    but for the compilations.assets object

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions