KodersLab / react-native-for-web

A set of classes and react components to make work your react-native app in a browser. (with some limitations obviously)
MIT License
255 stars 17 forks source link

Support new RN 0.14 asset loading #12

Closed respectTheCode closed 9 years ago

respectTheCode commented 9 years ago

We need a way to load images using the new asset loading system in RN 0.14.

facebook/react-native#3545

We can now include images in the jsbundle and load them with require("./path/to/image.png").

respectTheCode commented 9 years ago

So making a loader that returns the base64 encoded image is pretty simple but this doesn't feel like the right approach.

module.exports = function (source) {
    var image = source.toString("base64");
    image = "data:image/png;base64," + image;

    var result = {
        uri: image,
        isStatic: true
    };

    return "module.exports = " + JSON.stringify(result);
};

// get a buffer instead of a string
module.exports.raw = true;
mattiamanzati commented 9 years ago

I think the correct way would be to continue to use an external resource resolver! :)

Your approach is a good one for smaller resource, maybe we can conditionally chose whenever to use data-uri or filename based on the file size! :)

You can have a try by modifying the default one that react-native-for-web provides and recreate the resource url based on context and on the request! https://github.com/KodersLab/react-native-for-web/blob/master/src/macro/image.js

mattiamanzati commented 9 years ago

We can then push isStatic: true when using data-urls and isStatic: false while using plain urls :)