Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": [["@babel/preset-env", { "targets": { "node": "12" } }]],
"presets": [["@babel/preset-env", { "targets": { "node": "22" } }]],
"plugins": [
[
"@babel/plugin-transform-runtime",
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CI
on:
push:
branches: [main]
pull_request:
workflow_call:

permissions:
contents: read

jobs:
check:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node: [22, 24]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: ${{ matrix.node }}
package-manager-cache: false
- run: npm install --global --force yarn@1.22.22
- run: yarn install --frozen-lockfile
- run: yarn validate
44 changes: 30 additions & 14 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
name: npm publish
on:
push:
branches:
- main
release:
types: [published]

permissions:
contents: read

jobs:
build:
check:
uses: ./.github/workflows/ci.yml
publish:
needs: check
if: ${{ !github.event.release.prerelease }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
node-version: '15.x'
registry-url: 'https://registry.npmjs.org'
# Defaults to the user or organization that owns the workflow file
scope: '@octocat'
- run: yarn
- run: yarn test
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: '24'
package-manager-cache: false
- name: Verify release tag matches package version
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
run: node -e 'if (process.env.RELEASE_TAG !== `v${require("./package.json").version}`) process.exit(1)'
- run: npm install --global --force yarn@1.22.22
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn publish dist/ --no-git-tag-version
- name: Configure npm publishing
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: '24'
registry-url: 'https://registry.npmjs.org'
package-manager-cache: false
- run: npm publish ./dist
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@

Generate \<picture /> srcset with AVIF support.

## Requirements

Node.js 22 or newer; Node.js 24 LTS is recommended. Older Node.js versions are no longer supported.

## Development

Use Yarn Classic 1.22.22 (`npm install --global yarn@1.22.22`), then run:

```bash
yarn install --frozen-lockfile
yarn validate
```

`yarn validate` lints the source, runs the existing unit tests, builds the CLI, and checks real JPEG, PNG and AVIF output. CI validates Node.js 22 and 24 on pushes and pull requests.

Publishing only runs after a non-prerelease GitHub release is published and validation passes. The release tag must be `v` followed by the package version (for example, `v0.4.1`). Keep the CLI version in `src/index.js` in sync when bumping the package version. Publishing requires the existing `NPM_TOKEN` repository secret. Pushing to `main` does not publish a package.

## Cli arguments

- **-i --input:** input file
- **-o --output:** output path (optional, defaults to current path)
- **-b --breakpoints:** image widths that gen-srcset should generate
- **-n --noAvif:** skip AVIF output
- **-h --help:** displays this section

## Example usage
Expand Down
17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
"start": "node dist/index.js",
"dev": "nodemon --exec babel-node --extensions '.js,.ts' src/index.js",
"build": "yarn run babel --extensions '.js,.ts' --ignore '**/__tests__' src -d dist --copy-files",
"postbuild": "cp package.json dist && cp README.md dist",
"postbuild": "node scripts/prepare-package.cjs",
"pretest": "yarn run deliquus",
"test": "jest"
"test": "jest",
"test:smoke": "node scripts/smoke.cjs",
"validate": "yarn lint && yarn test --runInBand && yarn build && yarn test:smoke",
"lint": "eslint src"
},
"dependencies": {
"@babel/runtime": "^7.15.4",
"@babel/runtime": "^7.29.7",
"commander": "^8.2.0",
"sharp": "^0.29.1",
"sharp": "^0.35.5",
"threads": "^1.7.0"
},
"devDependencies": {
Expand Down Expand Up @@ -52,5 +55,9 @@
"responsive image",
"generator",
"cli"
]
],
"engines": {
"node": ">=22"
},
"packageManager": "yarn@1.22.22"
}
18 changes: 18 additions & 0 deletions scripts/prepare-package.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('node:fs');
const path = require('node:path');
const root = path.resolve(__dirname, '..');
const manifest = require('../package.json');

// The published package is the contents of dist, not the source checkout.
const published = {
...manifest,
main: 'index.js',
files: ['index.js', 'helpers/', 'services/', 'README.md', 'LICENSE'],
};
delete published.scripts;
delete published.devDependencies;
delete published.packageManager;
fs.writeFileSync(path.join(root, 'dist/package.json'), `${JSON.stringify(published, null, 2)}\n`);
for (const file of ['README.md', 'LICENSE']) {
fs.copyFileSync(path.join(root, file), path.join(root, 'dist', file));
}
78 changes: 78 additions & 0 deletions scripts/smoke.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const assert = require('node:assert/strict');
const { execFileSync } = require('node:child_process');
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const sharp = require('sharp');

const packageRoot = path.resolve(process.argv[2] || path.join(__dirname, '../dist'));
const published = require(path.join(packageRoot, 'package.json'));
const cli = path.resolve(packageRoot, published.bin['gen-srcset']);
assert.equal(published.main, 'index.js');
assert.equal(published.scripts, undefined);
assert.equal(published.devDependencies, undefined);
const run = (args, cwd) =>
execFileSync(process.execPath, [cli, ...args], {
cwd,
timeout: 60000,
stdio: 'pipe',
});

async function main() {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'gen-srcset smoke-'));
try {
assert.match(run(['--help'], root).toString(), /--breakpoints/);
assert.equal(run(['--version'], root).toString().trim(), require('../package.json').version);
assert.throws(
() => run([], root),
(error) => error.status === 1 && /input argument is required/.test(error.stderr.toString()),
);
assert.throws(
() => run(['--input', 'missing.png'], root),
(error) =>
error.status === 1 && /breakpoints argument is required/.test(error.stderr.toString()),
);
for (const extension of ['jpg', 'png']) {
const input = path.join(root, `sample.${extension}`);
const output = path.join(root, extension);
await fs.mkdir(output);
await sharp({ create: { width: 40, height: 20, channels: 3, background: '#336699' } }).toFile(
input,
);
run(['--input', input, '--breakpoints', '10, 20', '--output', output], root);
assert.deepEqual(
(await fs.readdir(output)).sort(),
[
`sample_10.avif`,
`sample_10.${extension}`,
`sample_20.avif`,
`sample_20.${extension}`,
].sort(),
);
for (const width of [10, 20]) {
for (const format of [extension, 'avif']) {
const metadata = await sharp(path.join(output, `sample_${width}.${format}`)).metadata();
assert.equal(metadata.width, width);
assert.equal(metadata.height, width / 2);
assert.equal(
metadata.format,
format === 'avif' ? 'heif' : format === 'jpg' ? 'jpeg' : 'png',
);
}
}
const defaultOutput = path.join(root, `${extension}-no-avif`);
await fs.mkdir(defaultOutput);
run(['-i', input, '-b', '10', '-n'], defaultOutput);
assert.deepEqual(await fs.readdir(defaultOutput), [`sample_10.${extension}`]);
}
console.log(
'CLI smoke tests passed: JPEG, PNG, AVIF, dimensions, filenames, default output, --noAvif, help and version',
);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Loading
Loading