Git Product home page Git Product logo

pre-require's Introduction

Pre-require

Pre-require is a small global script, which helps you create a module of objects with required assets from the folder that you direct it to.

This small trick helps you in situations that you might need to use variables while fetching assets.

For Example:

Lets say I have 10 images named as:

  • image1.png
  • image2.png
  • ...
  • image10.png
let i = 1;
let imageArray = [];
while(i <= 10){
  imageArray.push(require("image"+ i +".png"));
  // Won't work.
  i++;
}

Not only that, now pre-require works for folder inside a folder as well. When pre-require sees a folder inside there, it will create an object based on it.

For Example:

  • jpegimage
    • image1.jpg
    • image2.jpg
    • image3.jpg
    • image4.jpg
    • image5.jpg
  • pngimage
    • image1.png
    • image2.png
    • image3.png
    • image4.png
    • image5.png

Require works before the logic of your script starts working, so using variables in require() is not the optimal direction. But, as you might know, using require() in React works really well and you will inevitably have situations where if only require would support variables, it would be perfect for your needs.

Pre-require, requires all the files and creates an array from the folder you direct it to, so you can import this array and use is the way you would use require, but with variables. Additionally, you can do array searches in your assets.

How to use?

First install the pre-require globally:

npm install pre-require -g

Pre-require takes 2 parameters: First parameter is the path to the folder that your assets exist in. The second parameter is the output Javascript file that you will be importing to your script which is returning a required asset map.

pre-require images/ assets.js

You might use this manually, when you add new assets to the destination file, or bind this command to your file watcher script, or webpack config or just add it to your npm run build command from your package.json

Once more, an example of rewriting image1 to image10 with pre-require:

import Assets from './assets.js'
// assets.js would be the path of your output file, that pre-require command created.

let i = 1;
let imageArray = [];
while(i <= 10){
  imageArray.push(Assets["image"+ i +"_png"]);
  i++;
}

What about the second example with 2 image type folder with pre-require?
yes of course you can, the folder will be available as an object with folder name as the key. In this case will be something like this

{
  "pngimage": {
    "image1_png":require('./pngimage/image1.png'),
    ...,
    "image5_png":require('./pngimage/image5.png')
  },
  "jpegimage": {
    "image1_jpg":require('./pngimage/image1.jpg'),
    ...,
    "image5_jpg":require('./pngimage/image5.jpg')
  }
}
import Assets from './assets.js'
// assets.js would be the path of your output file, that pre-require command created.

let i = 1;
let imageJpgs = [];
let imagePngs = [];
while(i <= 5){
  imagePngs.push(Assets["pngimage"]["image"+ i +"_png"]);
  imageJpgs.push(Assets["jpegimage"]["image"+ i +"_jpg"]);
  i++;
}

Methods

The asset.js file exposes a range of methods for interacting with the data structure

Assets.search

This will return an array of assets matching the given pattern, currently this is just the matched asset name. In the future this will accept a regexp or partial match.

import Assets from './assets.js'

let thirdImage = Assets.search("image3");

Assets.format

Similar to Assets.search this will return all assets matching a given filetype

import Assets from './assets.js'

let pngs = Assets.format("png");

For a full list of methods and usage see the API reference

pre-require -h

NOTE: Be careful that file extension dot (.) is changed into underscore ( _ ). Also if your asset file has hyphen (-), it will automatically be transformed into an underscore.

You can use this module with any file that require() supports; image files (png, jpg, svg, etc), json files or, even in some extreme cases, Javascript files.

NOTE: There is a lot to do, for example;

  • Built-in asset search
  • Choosing the type of the asset that (eg: regex folder parameter)
  • Adding -h info. (done)

Help me make it better with your pull requests, they are welcome.

Contributing

Development of pre-require happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. If you intend to make any non-trivial changes to pre-require's implementation, we recommend filing an issue. This lets us reach an agreement on your proposal before you put significant effort into it. If you’re only fixing a bug, it’s fine to submit a pull request right away but we still recommend to file an issue detailing what you’re fixing.

Development

Working on a feature or bug fix? We sugges that you follow the following pattern:

  1. Fork the repository and create your branch from master.
  2. Write some code! Use npm run start to transpile and run index.js. If you would like the output of the transpiled code run npm run transpile and view the output at transpiled.js.
  3. If you’ve added code that should be tested, add tests!
  4. Make to update the appropriate documentation if needed.
  5. Ensure the test suite passes.

Testing

We use Jest for tests, to start tests locally;

npm run test

To watch the changes made in the test scripts, use;

npm run test:watch
Code Coverage

Jest includes the ability to analyze test code coverage. When running the test tasks, coverage reports are generated and output to the /coverage directory. To view the HTML report, open /coverage/index.html in a browser.

Licence

MIT

pre-require's People

Contributors

btk avatar devlab2425 avatar ericjperry avatar faraazahmad avatar flopivg avatar gatukgl avatar hanstf avatar hummusonrails avatar simonbynd avatar sloansparger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pre-require's Issues

Check English grammar of readme file

Hey, It would be really good if a native English speaker can check any typos or grammar mistakes possibly made in the readme file and documentation.

pull-requests are welcome.

Development Section in Documentation

We should update the development section of the documentation (readme.md)

I started writing about tests in the documentation that thought maybe someone would like to contribute/help since it is Hacktoberfest 🥇

Basically we need to explain the package.json scripts and how to use them while contributing or doing developments.

Thanks!

Documentation update for new changes

pre-require -h needs to be added in the documentation. (#5)

Also we have new features in #3 and #4, these new methods (.search() and .format()) needs to be added to documentation, which we store in our readme file.

Any contribution is welcome.

Documentation improvements for recursive file system

I proposed an improvement at #28

And it has implemented in #30

Now we need to change the documentation (Readme.md) relative to the changes. And we should add an example use of the recursive system.

Its a good first task to take on for Hacktoberfest 🥇

Select asset files recursively

Currently, our logic is to list all the assets in the top level of given directory path.

We need to list and add the assets to our generated map in a recursive way;

Eg;

assets/
  - subasset/
     - subasset1.png
     - subasset2.png
  - asset1.png
  - asset2.png

Mapping this like;

{
  subasset_subasset1_png: ...,
  subasset_subasset2_png: ...,
  asset1_png: ...,
  asset2_png: ...,
}

Seems like a general solution, we should implement it this way. Also we might give user the preference to only list the current top level folder path in the future.

Is there anyone that would like to implement this? Thank you.

Adding -h info.

The console application should return an help information text when "-h" is typed after the command explaining how to use the tool.

Built-in asset search

Allow user to do a simple array search in the assets object created. This can be implemented in the asset module as a method.

Writing tests for loopDirectory

Function loopDirectory() as implemented by @hanstf might create problems in the future as it does an important task and might have complicated cases.

We should write test for this function especially. Also since it is implemented in an abstract by (thanks to @hanstf) it would be easy to write tests for.

We use jest for tests, If you like to take this on check example tests written by @gatukgl in ./tests/search-file-name.test.js file.

Creating a js file called loop-directory.test.js in tests folder would be good start.

Thank you!

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.