Coder Social home page Coder Social logo

reiniiriarios / asset-resizer Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 303 KB

A minimal package to easily automate image asset resizing as a part of a build process.

Home Page: https://npmjs.com/package/asset-resizer

License: GNU General Public License v3.0

JavaScript 52.63% TypeScript 47.37%
assets build-process build-tool image-processing image-tools npm-package

asset-resizer's Introduction

asset-resizer

latest version downloads types: Typescript build

A minimal package to easily automate image asset resizing as a part of a build process. Uses sharp.

Getting Started

Install asset-resizer with:

npm i asset-resizer --save-dev

Then add assetresizer.config.js to your root directory, specifying what assets should be resized and where. Alternatively, you may specify a custom config file.

See example config.

// assetresizer.config.js
// alt: module.exports = {
export default {
  baseUrl: ".",
  inputDir: "assets",
  outputDir: "build",
  flatten: true,
  assets: [
    {
      file: "icon.png",
      output: [
        {
          file: "icon512.png",
          width: 512,
        },
        {
          file: "icon32.png",
          width: 32,
        },
        //...
      ],
    },
    //...
  ],
};

Command Line Usage

Run:

asset-resizer parse
asset-resizer parse --config=custom.config.js

Validate config file:

asset-resizer config
asset-resizer config --config=custom.config.js

Add these lines to your package.json to integrate into your build workflow.

Programmatic Usage

Alternatively, you can use this package programmatically. See examples.

TypeScript / ESModule

import { parseAllAssets, AssetResizerConfig } from "asset-resizer";

const config: AssetResizerConfig = {
  baseUrl: ".",
  inputDir: "assets",
  outputDir: "build",
  flatten: true,
  assets: [
    //...
  ],
};

parseAllAssets(config).then(() => {
  console.log("done");
});

CommonJS

const { parseAllAssets } = require("asset-resizer");

parseAllAssets({
  baseUrl: ".",
  inputDir: "assets",
  outputDir: "build",
  flatten: true,
  assets: [
    //...
  ],
}).then(() => {
  console.log("done");
});

Configuration Options

AssetResizerConfig

Option Type Description Default
baseUrl string Base url to run resizer from .
inputDir string Directory of input files assets
outputDir string Directory to build output files build
assets Array<AssetResizerAsset> Array of source assets

AssetResizerAsset

Option Type Description
file string Path to source asset relative to inputDir
output Array<AssetResizerOutput> Array of output files to create

AssetResizerOutput

Option Type Description
file string Name of file to create. Extension may be one of: jpg, png, webp, avif, tiff, gif, dzi, v.
width number? Width, in pixels, of the target output file. One of width or copy must be present.
height number? (optional) Height, in pixels, of the target output file. If not specified, uses width.
copy boolean? Optionally copy the file instead of resizing. One of width or copy must be present.
fit string? One of cover, contain, fill, inside, outside. Defaults to cover. See sharp documentation for details.

Development

First run npm i, then npm link to link the cli bin asset-resizer to the development directory.

Building

npm run build

Tests

  • npm run test will run Jest.
  • npm run test:cli will run command from package.json.

asset-resizer's People

Contributors

reiniiriarios avatar

Watchers

 avatar

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.