Repository navigation
Description
Since Rollup is now a many -> many compiler with code splitting, asset emission plugins can no longer just drop a file next to the output.file path.
With updates to the transformBundle hook -> transformChunk hook we can provide plugins the list of chunks and their names and what modules are in them so that assets can be output properly, but a limitation with having plugins save files is that such plugins can't then work with in-memory builds.
For example, with CSS, one would want the ability to extract the CSS per chunk when doing server side rendering without necessarily having the plugin write a CSS file.
The idea of the asset emission API would be to be able to populate files in the "virtual code splitting file system" that are returned as keys of the output bundle, where some of the files are assets and not scripts.
In addition, it should be easy to have code embed references to asset URLs, regardless of where the actual asset is finally placed in the output. This is also tricky to do if we leave it up to plugins to fend for themselves here.
Here's the rough API I'd like to propose:
In any plugin hook, we add emitAsset to the plugin context, and getAssetUrl which only works in the transformBundle / transformChunk context:
function plugin () {
const sourceIdToCssMap = Object.create(null);
return {
transformChunk (code, options, chunk) {
let chunkCss = '';
for (let moduleId of chunk.modules) {
const css = getCssForModule[moduleId];
if (!css) continue;
chunkCss = mergeAndRenormalizeCss(chunkCss, css);
}
const assetId = this.emitAsset(chunk.filename, chunkCss);
return code + `injectCss(${this.getAssetUrl(assetId)})`;
}
};
}Assets can be located in the final output using an options.assetNames output option, where the default rule is say "[alias]-[hash].[ext]". Users might want to make this "asset-[ext]/[hash].[ext]" etc for example. If we want deeper customization of output paths, allowing these properties to be a function should do that too I think. Note that "alias" can have path separators in it (just like for chunks), but should be a "plain name" - that is of the form a/b and not URL-like (/a/b, https://a/b, ./, etc). We can add better failure validations for these edge cases.
The above example doesn't quite show all the use cases, other use cases captured by this API include:
- Writing plugins to support converting asset imports into their URLs -
import assetUrl from './asset.raw; img.src = assetUrl; // etc - Assets are assigned a hash Id to uniquely identify and dedupe them. If two plugins import the same asset source and give it the same alias, then they share the same hash and output file.
- Assets can be emitted at any point in the plugin system. The output name of the asset isn't known until rendering as we need the output options to do this. By having an internal hash Id, assets can be easily tracked and managed from early in the plugin lifecycle.
My hope is that this gets us CSS code splitting, raw asset URL imports and copying fonts and images that are dependencies of CSS files in a build, all while keeping the core of Rollup super small and focused on JS, while still supporting things like in-memory builds and server-side rendering.
There's a lot to digest here, but implementing the above API itself is not even a few lines change in Rollup - it will be low maintenance for us, leaving the hard problems to the plugin ecosystem still.
Thoughts and feedback welcome - I'm in the process of testing these approaches and am hoping to provide a PR along these lines soon.
Activity
tivac commentedon Apr 27, 2018
👍 from me, would love to see a PR for this so I could try porting
modular-css-rollupto use the new API.zerkalica commentedon Apr 27, 2018
How this api helps to detect output directory for writing images and font files? I don't want to use hashes, prefer unique and human-readable package names. I want to control assets naming and placing.
I create plugin-assets. This plugin grabs assets from imports directly, from imported libraries
dist/idirectory and copies them todist/i/<pkg.name>directory of application. It uses output.file or output.dir in transformBundle.This is demo project with two libraries and one application (site1).
site1/dist/i directory:
Plugin inserts relative urls into code:
guybedford commentedon Apr 27, 2018
@zerkalica the focus of the asset emission API is to relocate assets into the
output.dirfolder, and provide their URL references back to modules.The name can be customized with
output: { assetFileNames: '[name].[ext]' }where by default name can be a subpath likezerollup_demo_lib1/face_angel, outputting the asset at$outDir/zerollup_demo_lib1/face_angel.png. No hashes output at all in this example, but the default would likely beassetFileNames: '[name]-[hash].[ext]'.By default asset URLs would be relative URLs, but they could also have the base URL inlined with a possible
inlinePublicUrlor similar option.zerkalica commentedon Apr 28, 2018
How to copy assets from already builded node_modules packages?
Currently, i do a simple copy
node_modules/<pkg.name>/dist/itoapp/dist/i.But new api
this.emitAsset(chunk.filename, chunkCss);emits asset data, not asset source file path. I need to load all files fromnode_modules/<pkg.name>/dist/i/**and do emitAsset on each file in plugin?guybedford commentedon Apr 28, 2018
guybedford commentedon May 18, 2018
I cut this approach back quite significantly with a new suggestion via the
generateBundlehook in #2126 (comment) that is given full access to the output file map as well as a simple naming function. It's simply encouraging some shared conventions more than anything else.