Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Siilwyn committed Jul 17, 2024
0 parents commit c940700
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# https://editorconfig.org/

root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
36 changes: 36 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Publish Package

on:
push:
tags:
- 'v**'

jobs:
publish:
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
id-token: write
steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'

- name: Use dependencies cache
uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-

- name: Install dependencies
run: npm ci --ignore-scripts --no-audit --no-fund

- name: Publish package
run: npm publish --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict = true
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
19 changes: 19 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Contributing
To setup, follow these steps:

```sh
git clone https://github.com/voorhoede/Image.git
cd Image
npm ci
```

## General Prerequisites
Node.js, [latest LTS is recommended](https://nodejs.org/en/about/releases/).

### Tips

## Publishing
1. Switch to the main branch and ensure it is up-to-date with remote.
1. Update `changelog.md` with relevant changes and stage with `git add`.
1. Run `npm version --force` with the appropiate version bump to include the changelog changes in the same commit.
1. This should automatically push the commit and new version tag to trigger publishing from CI.
52 changes: 52 additions & 0 deletions core/generate-src-set.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { ImageLoader } from './types.mts';

const deviceSizes = [640, 750, 828, 1080, 1200, 1920, 2048, 3840];
const imageSizes = [16, 32, 48, 64, 96, 128, 256, 384];
const allSizes = [...deviceSizes, ...imageSizes].sort((a, b) => a - b);

export function generateSrcSet({
loader,
src,
width,
quality,
sizes,
}: {
loader: ImageLoader;
src: string;
width: number;
quality: number;
sizes?: string;
}) {
const { widths, kind } = getWidths(width, sizes);

return widths
.map(
(width, index) =>
`${loader({ src, width, quality })} ${
kind === 'w' ? width : index + 1
}${kind}`
)
.join(', ');
}

function getWidths(width: number, sizes: string | undefined) {
if (sizes) {
// Find all the viewport percentage lengths
const percentSizes = [...sizes.matchAll(/(^|\s)(1?\d?\d)vw/g)].map(
(captureGroups) =>
parseInt(captureGroups[2])
);
if (percentSizes.length) {
const smallestRatio = Math.min(...percentSizes) * 0.01;
return {
widths: allSizes.filter(
(size) => size >= deviceSizes[0] * smallestRatio
),
kind: 'w',
};
}
return { widths: allSizes, kind: 'w' };
}

return { widths: [width, width * 2], kind: 'x' };
}
5 changes: 5 additions & 0 deletions core/types.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type ImageLoader = (resolverProps: {
src: string;
width: number;
quality?: number;
}) => string;
15 changes: 15 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c)

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Image",
"workspaces": [
],
"engines": {
"node": ">=20"
}
}
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Image component
Image component to fetch optimized images from a CDN.

## Niceness
- No client-side JavaScript
- Generate `srcset` from native `sizes` attribute

0 comments on commit c940700

Please sign in to comment.