Giter VIP home page Giter VIP logo

load_assets's Introduction

load_assets

A command that generates a JavaScript file which contains an object that maps to loaded assets from a directory, including static images files, using require(...). This was built to support writing many imports/require statements for bundlers that load static assets/resources like webpack.

Install

npm install -g @ayang4114/load_assets

Usage

On the command line, execute the following command

load_assets <directory_of_assets>

Flags

Any arguments passed without a flag is treated as an argument for the --input flag, i.e. it treated as the input directory.

Flag Description Default
--input The path to the directory to be converted into an asset file. None
--output The output path name of the converted asset file assets.js or assets.ts
--targetLang The target language of the output asset file: either 'ts' or 'js' 'js'
--indents The number of indents in output file. 2
--excludeExt A space-separated sequence of all filetype extensions to ignore. None
--includeExt A space-separated sequence of all filetype extensions to include. None

Supported Type Extensions

By default, all files with the extensions jpg, jpeg, png, and gif are included when traversing through the assets directory. These files can be ignored by adding the unwanted extensions as arguments in --excludeExt.

Example

Consider the following tree:

assets
├── moon.png
├── sun.png
├── nested_assets_1
│   ├── moon.jpg
│   └── sun.jpg
└── nested_assets_2
    ├── moon.jpg
    ├── nested_assets
    │   ├── moon.jpg
    │   └── sun.jpg
    └── sun.jpg

Then, load_assets assets will produce the following output in assets.js:

module.asset = {
  jpg: {
    "nested_assets_1": {
      "moon": require("./assets/nested_assets_1/moon.jpg"),
      "sun": require("./assets/nested_assets_1/sun.jpg"),
    },
    "nested_assets_2": {
      "moon": require("./assets/nested_assets_2/moon.jpg"),
      "nested_assets": {
        "moon": require("./assets/nested_assets_2/nested_assets/moon.jpg"),
        "sun": require("./assets/nested_assets_2/nested_assets/sun.jpg"),
      },
      "sun": require("./assets/nested_assets_2/sun.jpg"),
    },
  },
  png: {
    "moon": require("./assets/moon.png"),
    "sun": require("./assets/sun.png"),
  }
};

Contributing

Feel free to add issues or suggestions in the repository.

You can also make PRs:

  1. Fork the repository.
  2. Create a feature branch in the forked repository.
  3. Push changes to the feature branch.
  4. Submit a PR request.

License

ISC License

load_assets's People

Contributors

anthonyyangdev avatar

Watchers

 avatar

load_assets's Issues

throws error on invalid directory name

internal/fs/utils.js:230
throw err;
^

Error: ENOENT: no such file or directory, lstat 'dw.wd'
at Object.lstatSync (fs.js:1000:3)
at Object.generateRequireAllFiles [as default] (/Users/ayang/WebstormProjects/load_assets/dist/load_assets.js:69:29)
at Object. (/Users/ayang/WebstormProjects/load_assets/dist/load_assets-cli.js:27:35)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
errno: -2,
syscall: 'lstat',
code: 'ENOENT',
path: 'dw.wd'
}

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.