postcss / postcss-url

PostCSS plugin to rebase url(), inline or copy asset.
MIT License
377 stars 59 forks source link

async url: function #123

Closed eele94 closed 6 years ago

eele94 commented 6 years ago

Hi people,

In my app for offline purposes I have to request the cache API while transforming the url.

So this basicly involves an async url().

Is it actually possible to use async url function? Help would be appreciated, thanks!

Example of the problem below:

const parsedCSS = postcss.parse(res);

postcss()
                  .use(url({
                    url: async (asset) => {
                      // extract urls from cache
                      const matchUrl = `${location.origin}/x/${asset.url}`;

                      const assetResponse = await cache.match(matchUrl);

                      // convert assetResponse to blob
                      const blobResponse = await assetResponse.blob();

                      // generate objectUrl
                      const dataUrl = URL.createObjectURL(blobResponse);

                      // replace url
                      return dataUrl || asset.url;
                    },
                  }))
                  .process(parsedCSS, {
                    parser: postcssJs,
                  })
                  .then((postcssRes) => {
                    // submit changed css
                    submitCSS(postcssRes.css);
                  });

Now the problem is that my postcssRes.css urls contains an object promise instead of the resolved result.