Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions catalog-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: backstage.io/v1beta1
kind: Component
metadata:
name: assets-loader
namespace: economist-impact
annotations:
github.com/project-slug: signal-noise/assets-loader
backstage.io/techdocs-ref: dir:.
spec:
type: website
lifecycle: production
owner: group:economist-impact/signal-noise
128 changes: 66 additions & 62 deletions dist/assets-loader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dist/assets-loader.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/assets-loader.min.js

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# assets-loader

[![NPM version](https://badge.fury.io/js/assets-loader.svg)](http://badge.fury.io/js/assets-loader) [![Bower version](https://badge.fury.io/bo/assets-loader.svg)](http://badge.fury.io/bo/assets-loader) [![Build Status](https://secure.travis-ci.org/ianmcgregor/assets-loader.png)](https://travis-ci.org/ianmcgregor/assets-loader)

A simple batch assets loader.

<http://ianmcgregor.github.io/assets-loader/examples/>

### Installation

npm:
```
npm install assets-loader --save-dev
```
bower:
```
bower install assets-loader --save-dev
```

### Usage

```javascript
var assetsLoader = require('assets-loader');

// load some assets:

var loader = assetsLoader({
assets: [
// image
'/images/picture.png',
// image with crossorigin
{ url: '/images/picture.jpg', crossOrigin: 'anonymous' },
// image without extension
{ url: 'http://lorempixel.com/100/100', type: 'jpg' },
// image as blob
{ url: '/images/picture.webp', blob: true },
// specify id for retrieval
{ id: 'picture', url: '/images/picture.jpg' },
// json
'data.json',
{ url: 'data.json' },
{ url: '/endpoint', type: 'json' },
// video
'video.webm',
{ url: 'video.webm' },
{ url: 'video.mp4', blob: true },
// audio
'audio.ogg',
{ url: 'audio.ogg', blob: true },
{ url: 'audio.mp3', webAudioContext: audioContext },
// binary / arraybuffer
'binary_file.bin',
{ url: 'binary_file', type: 'bin' },
// text
'text_file.txt',
{ url: 'text_file', type: 'text' }
]
})
.on('error', function(error) {
console.error(error);
})
.on('progress', function(progress) {
console.log((progress * 100).toFixed() + '%');
})
.on('complete', function(assets) {
assets.forEach(function(asset) {
console.log(asset);
});
// get by id from loader instance
console.log(loader.get('picture'));
})
.start();

// add assets in separate steps

var loader = assetsLoader()
.add('audio.mp3')
.add('picture.jpg')
.add([
'a.png',
'b.png'
])
.add({
id: 'video',
url: 'video.webm'
})
.add({
id: 'sounds',
assets: [
{ id: 'a', url: 'a.mp3' },
{ id: 'b', url: 'b.mp3' }
]
})
.on('complete', function(assets) {
console.log(assets);
console.log(loader.get('video'));
console.log(loader.get('sounds'));
})
.start();

// configure values for every file

var loader = assetsLoader({
blob: true, // only works if browser supports
crossOrigin: 'anonymous',
webAudioContext: audioContext,
assets: [
{ id: 'a', url: 'a.mp3' },
{ id: 'b', url: 'b.jpg' },
// override blob setting for this file
{ id: 'c', url: 'c.jpg', blob: false }
]
});

// destroy

loader.destroy();
loader.getLoader('groupId').destroy();

// stats

console.log(assetsLoader.stats.getMbps()); // e.g. 3.2
assetsLoader.stats.log(); // e.g. Total loaded: 2.00mb time: 2.00s speed: 1.00mbps
```

### Create an `assets.json` file
Sometimes you may need to load a lot of assets. A simple solution for that is to generate an `assets.json` file listing all your assets. Here is a bash script to do that:

```bash
#!/bin/bash

# Assign found results to an array
# Source: https://stackoverflow.com/a/23357277/616095
assets=()
while IFS=read -r -d $'\0'; do assets+=("${REPLY//static\//}")

# Filter results (excluding static/fonts folder)
# Source: http://www.liamdelahunty.com/tips/linux_find_exclude_multiple_directories.php
done < <(find static \( -path static/fonts -o -name ".*" \) -prune -o -type f -print0)

# Format an array to JSON (require https://github.com/stedolan/jq)
# Source: https://stackoverflow.com/a/26809318/616095
printf '%s\n' "${assets[@]}" | jq -R . | jq -s . > dest/assets.json
```

This script assumed that your assets are located in a `static/` folder and write the result to `dest/assets.json`. After running the script you just have to require your JSON file:

```javascript
new assetsLoader({
assets: require('dest/assets.json')
})
```

### Dev setup

To install dependencies:

```
$ npm install
```

To run tests:

```
$ npm install -g karma-cli
$ karma start
```
5 changes: 5 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
docs_dir: docs
nav:
- Main: index.md
plugins:
- techdocs-core
Loading