Skip to content

Commit

Permalink
Apply caching to webpack loader
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Sep 24, 2024
1 parent 2c5ed95 commit d068941
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 21 deletions.
13 changes: 6 additions & 7 deletions packages/build-utils/src/resize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ImageType } from '@responsive-image/core';
import type { ImageConfig } from 'imagetools-core';
import { readFile, writeFile } from 'node:fs/promises';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import type { Metadata } from 'sharp';
import type {
ImageLoaderChainedResult,
Expand Down Expand Up @@ -33,12 +33,11 @@ export async function generateResizedImage(
let cacheFile: string | undefined = undefined;

if (options.cache && input.hash) {
cacheFile = getCacheFilename(
input,
config,
format,
options.cacheDir ?? './node_modules/.cache',
);
const cacheDir = options.cacheDir ?? './node_modules/.cache';

cacheFile = getCacheFilename(input, config, format, cacheDir);

await mkdir(cacheDir, { recursive: true });

try {
const buffer = await readFile(cacheFile);
Expand Down
9 changes: 0 additions & 9 deletions packages/vite-plugin/src/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
generateResizedImage,
getImagetoolsConfigs,
} from '@responsive-image/build-utils';
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import type { Plugin } from 'vite';
import type { Options } from './types';
Expand All @@ -24,14 +23,6 @@ export default function resizePlugin(
cacheDir = join(config.cacheDir, '.responsive-image');
},

async buildStart() {
if (!cacheDir) {
throw new Error('Expected cacheDir to exist.');
}

await mkdir(cacheDir, { recursive: true });
},

async transform(code, id) {
const input = getInput(this, id);

Expand Down
7 changes: 6 additions & 1 deletion packages/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import {
type ImageLoaderChainedResult,
normalizeInput,
} from '@responsive-image/build-utils';
import { getWebpackOptions } from './utils';
import { LoaderContext } from 'webpack';
import { Options } from './types';

export default function loader(
this: LoaderContext<Partial<Options>>,
input: Buffer | ImageLoaderChainedResult,
): ImageLoaderChainedResult {
const data = normalizeInput(input);
const options = getWebpackOptions(this);
const data = normalizeInput(input, { generateHash: options.cache });

return data;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function process(
);

const images = await Promise.all(
configs.map((config) => generateResizedImage(sharp, config)),
configs.map((config) => generateResizedImage(data, config, options)),
);

return {
Expand Down
1 change: 1 addition & 0 deletions packages/webpack/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface WebpackLoaderOptions {
webPath?: string;
outputPath: string;
lqip?: LqipOptions;
cache: boolean;
}

export type Options = WebpackLoaderOptions & ImageOptions;
1 change: 1 addition & 0 deletions packages/webpack/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
export const defaultWebpackOptions = {
name: '[name]-[width]w-[hash].[ext]',
outputPath: 'images',
cache: true,
};

export const webpackOptionKeys = [
Expand Down
6 changes: 3 additions & 3 deletions packages/webpack/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test('custom loader options are supported', async () => {
const output = stats.modules?.[0]?.modules?.[0]?.source;
expect(sanitizeOutput(output)).toMatchSnapshot();

const imageAssets = stats.modules![0]!.assets!;
const imageAssets = stats.modules![0]!.assets!.toSorted();
expect(imageAssets).toEqual(['images/test-100.png', 'images/test-200.png']);

for (const image of imageAssets) {
Expand All @@ -106,7 +106,7 @@ test('custom query params are supported', async () => {
const output = stats.modules?.[0]?.modules?.[0]?.source;
expect(sanitizeOutput(output)).toMatchSnapshot();

const imageAssets = stats.modules![0]!.assets!;
const imageAssets = stats.modules![0]!.assets!.toSorted();
expect(imageAssets).toEqual([
'images/image-100w.png',
'images/image-200w.png',
Expand All @@ -131,7 +131,7 @@ test('imagetools params are supported', async () => {
const output = stats.modules?.[0]?.modules?.[0]?.source;
expect(sanitizeOutput(output)).toMatchSnapshot();

const imageAssets = stats.modules![0]!.assets!;
const imageAssets = stats.modules![0]!.assets!.toSorted();
expect(imageAssets).toEqual([
'images/image-100w.png',
'images/image-200w.png',
Expand Down

0 comments on commit d068941

Please sign in to comment.