Git Product home page Git Product logo

react-native-file-hash-plugin's Introduction

react-native-file-hash-plugin

create file hash and transform asset in react-native bundle! when you use react-native run-ios/run-android it will not work, it is just use in react-native bundle ...

Installation

npm

npm install --save-dev react-native-file-hash-plugin

metro

Add react-native-file-hash-plugin to the list of assetPlugins in your metro.config.js file under the transformer section.

for example :

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
    assetPlugins: ["react-native-file-hash-plugin"],
  },
};

Configuration

You can configure the plugin behaviour through the optional fileHashPlugin field in your metro.config.js file under the transformer section.

For example:

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
    assetPlugins: ["react-native-file-hash-plugin"],
  },
  fileHashPlugin: {
    // optional
    // the file type to hash,default is png,jpeg,jpg,gif
    types: ["png", "jpeg", "jpg", "gif"],
    // optional
    // the input params is file name and file path; the output is hash string ,default is react-native hash
    hashFunction: (fileName, path) => {
      return "your hash";
    },
    // optional
    // the  ignore file path regex
    ignoreRegex: null,
    // optional
    // add external options to asset, resturn must be object
    externalOptions: (fileName, path) => {
      return {
        key: "mrgaogang",
      };
    },
  },
};

Output

example:

react-native bundle --entry-file index.js --bundle-output ./bundle/ios.bundle --platform ios --reset-cache --assets-dest ./bundle --dev false

suggest to add --reset-cache params.

will output like this, and the file will add hash string.

├─assets
│ ├─app.json
│ ├─img
│ │ └─success-c64ef9278d09de068787ded3a56ed560.png
│ └─node_modules
│   └─react-native
│     └─Libraries
└─ios.bundle

Others

if you want to transform Images, also you can use Image.resolveAssetSource.setCustomSourceTransformer,

Image.resolveAssetSource.setCustomSourceTransformer((resolver) => {
  // do any thing you want
  if (Platform.OS === 'android') {
      return this.isLoadedFromFileSystem()
        ? this.drawableFolderInBundle()
        : this.resourceIdentifierWithoutScale();
    } else {
      return this.scaledAssetURLNearBundle();
    }
});

the resolver is:

 // node_modules/react-native/Libraries/Image/AssetSourceResolver.js
  serverUrl: ?string;
  // where the jsbundle is being run from
  jsbundleUrl: ?string;
  // the asset to resolve
  asset: PackagerAsset;
  
 // PackagerAsset is 
 export type PackagerAsset = {
  +__packager_asset: boolean,
  +fileSystemLocation: string,
  +httpServerLocation: string,
  +width: ?number,
  +height: ?number,
  +scales: Array<number>,
  +hash: string,
  +name: string,
  +type: string,
  ...
};
  

react-native-file-hash-plugin's People

Contributors

mrgaogang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

snlove

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.