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

Allow assets to have dependencies (eg images in CSS) #2823

Closed
jakearchibald opened this issue Apr 25, 2019 · 10 comments
Closed

Allow assets to have dependencies (eg images in CSS) #2823

jakearchibald opened this issue Apr 25, 2019 · 10 comments

Comments

@jakearchibald
Copy link
Contributor

Feature Use Case

I'd like to use Rollup's asset system in non-JS files. Eg manifest.json which references icons, style.css which references images.

The hash of the asset should be influenced by its content, and the content of the assets it references.

Feature Proposal

A way to create and later replace placeholders feels sufficient.

@lukastaegert

This comment has been minimized.

@lukastaegert

This comment has been minimized.

@lukastaegert
Copy link
Member

Ah sorry, I misunderstood, so you want the placeholder inside the asset

@jakearchibald
Copy link
Contributor Author

Yeah, so if style.css contains:

.foo {
  background: url('bg.png');
}

I want a way to add bg.png to the build (that bit is easy), but then replace bg.png in the CSS with the eventual asset path.

If bg.png is updated, it should change the hash of both bg.png and style.css.

https://github.com/GoogleChromeLabs/graviton/blob/dea46e2eb5f14955c5f232f803f9c207da99900b/ejs-asset-plugin.js here's what I tried so far. Here I create my own placeholder which I later replace. However, this happens after the hash of the resource has been generated.

@lukastaegert
Copy link
Member

How about something like

this.emitAsset(assetName, source, {chunkDependencies: ['someChunkReferenceId'], assetDependencies: ['someAssetReferenceId']});

?

So leaving the actual replacing to the plugin but declaratively add dependencies. Should be doable. So the idea would be that when all hashes are generated, all dependency hashes are incorporated into the dependent hash.

@jakearchibald
Copy link
Contributor Author

Yeah, that seems totally reasonable.

@jakearchibald jakearchibald changed the title Make it easier to deal reference assets from other assets Make it easier to reference assets from other assets Apr 25, 2019
@tivac
Copy link
Contributor

tivac commented Apr 25, 2019

Omg please, please please. Every step we can take towards rollup managing assets means giant blocks of code I can delete from modular-css!

@philipwalton
Copy link

What about adding a new plugin hook that would allow plugins managing non-JS files to update their source contents based on the hash URLs of their dependencies?

As I described #2839, the hash URL problem can be solved in Rollup for JS dependencies, but it would be quite tricky for Rollup to solve it for all possible asset types. However, since Rollup is the only actor in the system with complete knowledge of the full dependency graph, it makes sense that Rollup must control when (and in what order) to determine the file hashes for each asset, but in some cases only a plugin can know how to update its contents to load its dependencies.

What about something like this, which is called with a mapping between each dependency chunk's name and hashed URL path.

resolveAssetURLs({chunkDependencies<name, urlPath>})

And this hook would be the last thing to run before the files were written to the file system, and it could run in reverse topologically sorted order to ensure dependent chunk hashes can be determined based on the final contents of their dependencies (including import paths).

@lukastaegert
Copy link
Member

Extending upon this and incorporating the idea with the topological sorting, there are my latest thoughts: #2872 (comment)

Then in the proposed renderAsset hook, plugins could use this.getAssetFileName and this.getChunkFileName similar to how this is possible today within the renderChunk hook.

@jakearchibald jakearchibald changed the title Make it easier to reference assets from other assets Allow assets to have dependencies (eg images in CSS) Jul 16, 2019
@shellscape
Copy link
Contributor

Let's move conversation over to #2872 which is the current issue for tracking asset bundling.

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

No branches or pull requests

5 participants