Comparing version 0.0.1 to 1.0.0
{ | ||
"name": "assets", | ||
"version": "0.0.1", | ||
"description": "Asset API for Node.js", | ||
"homepage": "http://github.com/viatropos/assets.js", | ||
"main": "lib/assets.js", | ||
"version": "1.0.0", | ||
"description": "An asset manager for node", | ||
"keywords": [ | ||
"framework", | ||
"node" | ||
"assets", | ||
"base64", | ||
"image", | ||
"path", | ||
"size", | ||
"url" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/viatropos/assets.js/issues" | ||
"main": "./lib", | ||
"repository": "borodean/assets", | ||
"author": "Vadym Borodin <borodean@gmail.com>", | ||
"scripts": { | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls", | ||
"test": "eslint --ignore-path .gitignore . && nyc --reporter=text --reporter=html ./node_modules/ava/cli.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/viatropos/assets.js.git" | ||
"dependencies": { | ||
"async": "^1.5.0", | ||
"bluebird": "^3.0.6", | ||
"calipers": "^2.0.0", | ||
"calipers-gif": "^2.0.0", | ||
"calipers-jpeg": "^2.0.0", | ||
"calipers-png": "^2.0.0", | ||
"calipers-svg": "^2.0.0", | ||
"calipers-webp": "^2.0.0", | ||
"lodash": "^3.10.1", | ||
"mime": "^1.3.4" | ||
}, | ||
"engines": { "node": ">= 0.4.0" }, | ||
"dependencies": { | ||
"devDependencies": { | ||
"ava": "^0.7.0", | ||
"coveralls": "^2.11.4", | ||
"eslint": "^1.10.3", | ||
"nyc": "^4.0.1", | ||
"sinon": "^1.17.2" | ||
} | ||
} |
240
README.md
@@ -1,3 +0,239 @@ | ||
# Assets.js | ||
# Assets [![Unix Build Status][travis-badge]][travis] [![Windows Build Status][appveyor-badge]][appveyor] [![Coverage][coveralls-badge]][coveralls] | ||
> Nothing yet. | ||
[appveyor]: https://ci.appveyor.com/project/borodean/assets | ||
[appveyor-badge]: https://img.shields.io/appveyor/ci/borodean/assets.svg?label=windows | ||
[coveralls]: https://coveralls.io/github/borodean/assets | ||
[coveralls-badge]: https://img.shields.io/coveralls/borodean/assets.svg | ||
[travis]: https://travis-ci.org/borodean/assets | ||
[travis-badge]: https://img.shields.io/travis/borodean/assets.svg?label=unix | ||
Assets is an asset manager for node. It isolates assets from environmental changes, gets generates their URLs, retrieves image sizes and base64-encodes them. | ||
## Installation | ||
```bash | ||
npm install assets --save | ||
``` | ||
## Usage | ||
An instance of Assets should be created: | ||
```js | ||
var options = { loadPaths: ['fonts', 'images'] }; | ||
var resolver = new Assets(options); | ||
``` | ||
Each of the resolving methods returns a Promise: | ||
```js | ||
resolver.path('foobar.jpg') | ||
.then(function (resolvedPath) { | ||
// .... | ||
}); | ||
``` | ||
To use a node-style callback, pass it as the last argument to the resolving method: | ||
```js | ||
resolver.path('foobar.jpg', function (err, resolvedPath) { | ||
// ... | ||
}); | ||
``` | ||
## Resolving methods | ||
### `.path(path)` | ||
Resolve the absolute path for an asset. | ||
```js | ||
var resolver = new Assets({ loadPaths: ['assets'] }); | ||
resolver.path('patterns/knitwork.gif') | ||
.then(function (resolvedPath) { | ||
console.log(resolvedPath); // '/var/www/example.com/assets/patterns/knitwork.gif' | ||
}); | ||
``` | ||
### `.url(path)` | ||
Generates an URL for an asset. | ||
```js | ||
var resolver = new Assets({ loadPaths: ['assets/images'] }); | ||
resolver.url('page/background.jpg') | ||
.then(function (resolvedUrl) { | ||
console.log(resolvedUrl); // '/assets/images/page/background.jpg' | ||
}); | ||
``` | ||
### `.data(path)` | ||
Returns a base64-encoded content of an asset. SVG files would be non-encoded, because then [they benefit in size](http://css-tricks.com/probably-dont-base64-svg/). | ||
```js | ||
var resolver = new Assets(); | ||
resolver.data('icons/sabre.png') | ||
.then(function (resolvedData) { | ||
console.log(resolvedData); // 'data:image/png;base64,...' | ||
}); | ||
``` | ||
### `.size(path)` | ||
Return the size of an asset. | ||
```js | ||
var resolver = new Assets(); | ||
resolver.size('logo.png') | ||
.then(function (resolvedSize) { | ||
console.log(resolvedSize); // '{ width: 132, height: 48 }' | ||
}); | ||
``` | ||
Options | ||
------- | ||
Options are set by passing an options object to the constructor. Available options are: | ||
### `basePath` | ||
The path to the root of the project. | ||
For example: `"source/"`. | ||
Defaults to the current working directory. | ||
### `baseUrl` | ||
URL of the project when running withing the web server. | ||
For example: `"/wp-content/themes/twentyfourteen"`, `"http://example.com"`. | ||
Defaults to `"/"`. | ||
### `currentPath` | ||
The path to the currently processed file. | ||
There is no default `currentPath`. | ||
### `cachebuster` | ||
If cache should be busted. If set to `true`, Assets will bust assets cache, changing urls depending on asset’s modification date: | ||
```js | ||
var resolver = new Assets({ cachebuster: true, loadPaths: ['assets/images'] }); | ||
resolver.url('page/background.jpg') | ||
.then(function (resolvedUrl) { | ||
console.log(resolvedUrl); // '/assets/images/page/background.jpg?14a931c501f' | ||
}); | ||
``` | ||
To define a custom cachebuster pass a function as an option: | ||
```js | ||
var resolver = new Assets({ | ||
cachebuster: function (resolvedPath, pathname) { | ||
return fs.statSync(resolvedPath).mtime.getTime().toString(16); | ||
} | ||
}); | ||
``` | ||
If the returned value is falsy, no cache busting is done for the asset. | ||
If the returned value is an object the values of pathname and/or query are used to generate a cache busted path to the asset. | ||
If the returned value is a string, it is added as a query string. | ||
The returned values for query strings must not include the starting ?. | ||
Busting the cache via path: | ||
```js | ||
var resolver = new Assets({ | ||
cachebuster: function (resolvedPath, pathname) { | ||
var hash = fs.statSync(resolvedPath).mtime.getTime().toString(16); | ||
return { | ||
pathname: path.dirname(pathname) | ||
+ '/' + path.basename(pathname, path.extname(pathname)) | ||
+ hash + path.extname(pathname), | ||
query: false // you may omit this one | ||
} | ||
} | ||
}); | ||
``` | ||
Defaults to `false`. | ||
### `loadPaths` | ||
Specific directories to look for the files. | ||
For example: `["assets/fonts", "assets/images"]`. | ||
Defaults to an empty array. | ||
### `relativeTo` | ||
Directory to relate to when resolving URLs. When `false`, disables relative URLs. | ||
For example: `"assets/css"`. | ||
Defaults to `false`. | ||
Path resolution | ||
--------------- | ||
Assets provide a file path resolution algorithm similar to the one used by desktop file systems. | ||
This may come in handy when you have different directories for different types of assets, e.g. images, fonts. You add those to the list of load paths when configuring Assets: | ||
```js | ||
var resolver = new Assets({ | ||
loadPaths: ['assets/fonts', 'assets/images'] | ||
}); | ||
``` | ||
Now, instead of writing this: | ||
```js | ||
var url = '/assets/images/icons/create.png'; | ||
var url = '/assets/images/icons/read.png'; | ||
var url = '/assets/images/icons/update.png'; | ||
var url = '/assets/images/icons/delete.png'; | ||
``` | ||
You can write this: | ||
```js | ||
var url = resolver.path('icons/create.png'); | ||
var url = resolver.path('icons/read.png'); | ||
var url = resolver.path('icons/update.png'); | ||
var url = resolver.path('icons/delete.png'); | ||
``` | ||
Apart from the fact that these lines are just shorter, it gives you an opportunity to easily change the environment and the way the URLs are being output much quicker. | ||
For instance, if you move all the images from `assets/images` to `client/source/images` you wouldn't need to go through all of your stylesheets to replace the URLs, you would just need to edit the corresponding parameter inside your Assets config: | ||
```js | ||
var resolver = new Assets({ | ||
loadPaths: ['assets/fonts', 'client/source/images'] | ||
}); | ||
``` | ||
When resolving a path, Assets would look for it through every of the following paths in the listed order: | ||
* current path; | ||
* load paths; | ||
* base path. | ||
Path resolution also gives an opportunity to easily change the URL structure when the directory structure of the project on your computer is not exactly the same as it would be on the server. | ||
For instance, if you have a Wordpress theme project, you may want to append `/wp-content/themes/your-theme-name` to every URL inside of your stylesheet. This is done by providing a `baseUrl` parameter to Assets config: | ||
```js | ||
var resolver = new Assets({ | ||
baseUrl: '/wp-content/themes/your-theme-name' | ||
}); | ||
``` | ||
Now everything would be resolved relative to that base URL: | ||
```js | ||
resolver.url('images/create.png') | ||
.then(function (resolvedUrl) { | ||
console.log(resolvedUrl); // '/wp-content/themes/your-theme-name/images/create.png' | ||
}); | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
62651
34
0
1000
2
1
240
10
5
6
2
+ Addedasync@^1.5.0
+ Addedbluebird@^3.0.6
+ Addedcalipers@^2.0.0
+ Addedcalipers-gif@^2.0.0
+ Addedcalipers-jpeg@^2.0.0
+ Addedcalipers-png@^2.0.0
+ Addedcalipers-svg@^2.0.0
+ Addedcalipers-webp@^2.0.0
+ Addedlodash@^3.10.1
+ Addedmime@^1.3.4
+ Addedasync@1.5.2(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedcalipers@2.1.0(transitive)
+ Addedcalipers-gif@2.0.0(transitive)
+ Addedcalipers-jpeg@2.1.0(transitive)
+ Addedcalipers-png@2.1.0(transitive)
+ Addedcalipers-svg@2.0.1(transitive)
+ Addedcalipers-webp@2.0.0(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedmime@1.6.0(transitive)