@hamur/image 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,144 +1,143 @@
1
- import {
2
- Directive,
3
- ElementRef,
4
- Injector,
5
- Renderer2,
6
- DestroyRef,
7
- afterNextRender,
8
- booleanAttribute,
9
- computed,
10
- effect,
11
- inject,
12
- input,
13
- numberAttribute,
14
- runInInjectionContext,
15
- signal,
16
- type OnInit,
17
- } from "@angular/core"
18
- import type { ImageMeta } from "hamur"
19
- import { injectHead } from "@hamur/adapter-angular"
20
- import type { ImageFit, ImageFormat, ImageModifiers, HamurImagePreset, HamurImageProvider } from "../types.ts"
21
- import { imgAttributes } from "../runtime/index.ts"
22
- import { createImgEvents, imagePreloadLink, placeholderStyle } from "../runtime/img.ts"
23
-
24
- const optionalNumber = (value: unknown): number | undefined =>
25
- value === undefined ? undefined : numberAttribute(value)
26
-
1
+ import { Directive, ElementRef, Injector, Renderer2, DestroyRef, afterNextRender, booleanAttribute, computed, effect, inject, input, numberAttribute, runInInjectionContext, signal, } from "@angular/core";
2
+ import { injectHead } from "@hamur/adapter-angular";
3
+ import { imgAttributes } from "../runtime/index.ts";
4
+ import { createImgEvents, imagePreloadLink, placeholderStyle } from "../runtime/img.ts";
5
+ import * as i0 from "@angular/core";
6
+ const optionalNumber = (value) => value === undefined ? undefined : numberAttribute(value);
27
7
  /** An attribute binding takes a string, and `null` removes the attribute. */
28
- export const strings = (attributes: Record<string, unknown>): Record<string, string | null> =>
29
- Object.fromEntries(Object.entries(attributes).map(([name, value]) => [name, value == null ? null : String(value)]))
30
-
8
+ export const strings = (attributes) => Object.fromEntries(Object.entries(attributes).map(([name, value]) => [name, value == null ? null : String(value)]));
31
9
  /**
32
10
  * `<img hamurImg src="/hero.jpg" width="1200" alt="">`. The host bindings write what the other adapters' `Img`
33
11
  * writes on its `<img>`; every other attribute and listener stays the element's own. The props that are not attributes are
34
12
  * taken off the element. `src`, `srcset` and `sizes` are written only when they differ: the browser loads the image again on
35
13
  * every write, so a binding that rewrites the server's equal value during hydration fires `load` again.
36
14
  */
37
- @Directive({
38
- selector: "img[hamurImg]",
39
- host: {
40
- "[attr.hamurImg]": "null",
41
- "[attr.densities]": "null",
42
- "[attr.format]": "null",
43
- "[attr.quality]": "null",
44
- "[attr.fit]": "null",
45
- "[attr.modifiers]": "null",
46
- "[attr.provider]": "null",
47
- "[attr.preset]": "null",
48
- "[attr.preload]": "null",
49
- "[attr.placeholder]": "null",
50
- "[attr.placeholderClass]": "null",
51
- "[attr.width]": "attrs()['width']",
52
- "[attr.height]": "attrs()['height']",
53
- "[attr.alt]": "attrs()['alt']",
54
- "[attr.loading]": "attrs()['loading']",
55
- "[attr.decoding]": "attrs()['decoding']",
56
- "[attr.fetchpriority]": "attrs()['fetchpriority']",
57
- "[style]": "painted()",
58
- "[class]": "paintedClass()",
59
- },
60
- })
61
- export class Img implements OnInit {
62
- readonly src = input.required<string | ImageMeta>()
63
- readonly alt = input<string>()
64
- readonly width = input<number | string>()
65
- readonly height = input<number | string>()
66
- readonly sizes = input<string>()
67
- readonly densities = input<number[]>()
68
- readonly format = input<ImageFormat>()
69
- readonly quality = input<number | undefined, unknown>(undefined, { transform: optionalNumber })
70
- readonly fit = input<ImageFit>()
71
- readonly modifiers = input<ImageModifiers>()
72
- readonly provider = input<HamurImageProvider>()
73
- readonly preset = input<HamurImagePreset>()
74
- readonly loading = input<"lazy" | "eager">()
75
- readonly decoding = input<string>()
76
- readonly fetchpriority = input<"high" | "low" | "auto">()
77
- readonly preload = input(false, { transform: booleanAttribute })
78
- readonly placeholder = input<string>()
79
- readonly placeholderClass = input<string>()
80
-
81
- private readonly injector = inject(Injector)
82
- private readonly loaded = signal(false)
83
- private readonly events = createImgEvents(
84
- () => ({}),
85
- (value) => this.loaded.set(value),
86
- )
87
-
88
- private readonly resolved = computed(() =>
89
- imgAttributes({
90
- src: this.src(),
91
- alt: this.alt(),
92
- width: this.width(),
93
- height: this.height(),
94
- sizes: this.sizes(),
95
- densities: this.densities(),
96
- format: this.format(),
97
- quality: this.quality(),
98
- fit: this.fit(),
99
- modifiers: this.modifiers(),
100
- provider: this.provider(),
101
- preset: this.preset(),
102
- loading: this.loading(),
103
- decoding: this.decoding(),
104
- fetchpriority: this.fetchpriority(),
105
- preload: this.preload(),
106
- }),
107
- )
108
- protected readonly attrs = computed(() => strings(this.resolved()))
109
- protected readonly painted = computed(() => {
110
- const url = this.placeholder()
111
- return this.loaded() || !url ? {} : placeholderStyle(url)
112
- })
113
- protected readonly paintedClass = computed(() => (this.loaded() ? "" : (this.placeholderClass() ?? "")))
114
-
115
- constructor() {
116
- const element = inject<ElementRef<HTMLImageElement>>(ElementRef).nativeElement
117
- const renderer = inject(Renderer2)
118
- effect(() => {
119
- const attrs = this.attrs()
120
- for (const name of ["src", "srcset", "sizes"]) {
121
- const value = attrs[name] ?? null
122
- if (element.getAttribute(name) === value) continue
123
- if (value === null) renderer.removeAttribute(element, name)
124
- else renderer.setAttribute(element, name, value)
125
- }
126
- })
127
- // Read at hydration: a server image the browser already finished fired its load before any listener existed.
128
- const finished = element.isConnected && element.complete && element.getAttribute("src") !== null
129
- const offLoad = renderer.listen(element, "load", (event: Event) => this.events.load(event))
130
- const offError = renderer.listen(element, "error", (event: Event) => this.events.error(event))
131
- inject(DestroyRef).onDestroy(() => {
132
- offLoad()
133
- offError()
134
- })
135
- // Angular's event replay captures a load that fired before hydration and replays it to `(load)` bindings itself;
136
- // only the directive's own state is caught up here, so no second load is dispatched.
137
- if (finished) afterNextRender(() => this.loaded.set(element.naturalWidth > 0))
138
- }
139
-
140
- ngOnInit(): void {
141
- if (this.preload())
142
- runInInjectionContext(this.injector, () => injectHead(() => ({ link: [imagePreloadLink(this.resolved())] })))
143
- }
15
+ export class Img {
16
+ src = input.required(/* @ts-ignore */
17
+ ...(ngDevMode ? [{ debugName: "src" }] : /* istanbul ignore next */ []));
18
+ alt = input(/* @ts-ignore */
19
+ ...(ngDevMode ? [undefined, { debugName: "alt" }] : /* istanbul ignore next */ []));
20
+ width = input(/* @ts-ignore */
21
+ ...(ngDevMode ? [undefined, { debugName: "width" }] : /* istanbul ignore next */ []));
22
+ height = input(/* @ts-ignore */
23
+ ...(ngDevMode ? [undefined, { debugName: "height" }] : /* istanbul ignore next */ []));
24
+ sizes = input(/* @ts-ignore */
25
+ ...(ngDevMode ? [undefined, { debugName: "sizes" }] : /* istanbul ignore next */ []));
26
+ densities = input(/* @ts-ignore */
27
+ ...(ngDevMode ? [undefined, { debugName: "densities" }] : /* istanbul ignore next */ []));
28
+ format = input(/* @ts-ignore */
29
+ ...(ngDevMode ? [undefined, { debugName: "format" }] : /* istanbul ignore next */ []));
30
+ quality = input(undefined, { ...(ngDevMode ? { debugName: "quality" } : /* istanbul ignore next */ {}), transform: optionalNumber });
31
+ fit = input(/* @ts-ignore */
32
+ ...(ngDevMode ? [undefined, { debugName: "fit" }] : /* istanbul ignore next */ []));
33
+ modifiers = input(/* @ts-ignore */
34
+ ...(ngDevMode ? [undefined, { debugName: "modifiers" }] : /* istanbul ignore next */ []));
35
+ provider = input(/* @ts-ignore */
36
+ ...(ngDevMode ? [undefined, { debugName: "provider" }] : /* istanbul ignore next */ []));
37
+ preset = input(/* @ts-ignore */
38
+ ...(ngDevMode ? [undefined, { debugName: "preset" }] : /* istanbul ignore next */ []));
39
+ loading = input(/* @ts-ignore */
40
+ ...(ngDevMode ? [undefined, { debugName: "loading" }] : /* istanbul ignore next */ []));
41
+ decoding = input(/* @ts-ignore */
42
+ ...(ngDevMode ? [undefined, { debugName: "decoding" }] : /* istanbul ignore next */ []));
43
+ fetchpriority = input(/* @ts-ignore */
44
+ ...(ngDevMode ? [undefined, { debugName: "fetchpriority" }] : /* istanbul ignore next */ []));
45
+ preload = input(false, { ...(ngDevMode ? { debugName: "preload" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
46
+ placeholder = input(/* @ts-ignore */
47
+ ...(ngDevMode ? [undefined, { debugName: "placeholder" }] : /* istanbul ignore next */ []));
48
+ placeholderClass = input(/* @ts-ignore */
49
+ ...(ngDevMode ? [undefined, { debugName: "placeholderClass" }] : /* istanbul ignore next */ []));
50
+ injector = inject(Injector);
51
+ loaded = signal(false, /* @ts-ignore */
52
+ ...(ngDevMode ? [{ debugName: "loaded" }] : /* istanbul ignore next */ []));
53
+ events = createImgEvents(() => ({}), (value) => this.loaded.set(value));
54
+ resolved = computed(() => imgAttributes({
55
+ src: this.src(),
56
+ alt: this.alt(),
57
+ width: this.width(),
58
+ height: this.height(),
59
+ sizes: this.sizes(),
60
+ densities: this.densities(),
61
+ format: this.format(),
62
+ quality: this.quality(),
63
+ fit: this.fit(),
64
+ modifiers: this.modifiers(),
65
+ provider: this.provider(),
66
+ preset: this.preset(),
67
+ loading: this.loading(),
68
+ decoding: this.decoding(),
69
+ fetchpriority: this.fetchpriority(),
70
+ preload: this.preload(),
71
+ }), /* @ts-ignore */
72
+ ...(ngDevMode ? [{ debugName: "resolved" }] : /* istanbul ignore next */ []));
73
+ attrs = computed(() => strings(this.resolved()), /* @ts-ignore */
74
+ ...(ngDevMode ? [{ debugName: "attrs" }] : /* istanbul ignore next */ []));
75
+ painted = computed(() => {
76
+ const url = this.placeholder();
77
+ return this.loaded() || !url ? {} : placeholderStyle(url);
78
+ }, /* @ts-ignore */
79
+ ...(ngDevMode ? [{ debugName: "painted" }] : /* istanbul ignore next */ []));
80
+ paintedClass = computed(() => (this.loaded() ? "" : (this.placeholderClass() ?? "")), /* @ts-ignore */
81
+ ...(ngDevMode ? [{ debugName: "paintedClass" }] : /* istanbul ignore next */ []));
82
+ constructor() {
83
+ const element = inject(ElementRef).nativeElement;
84
+ const renderer = inject(Renderer2);
85
+ effect(() => {
86
+ const attrs = this.attrs();
87
+ for (const name of ["src", "srcset", "sizes"]) {
88
+ const value = attrs[name] ?? null;
89
+ if (element.getAttribute(name) === value)
90
+ continue;
91
+ if (value === null)
92
+ renderer.removeAttribute(element, name);
93
+ else
94
+ renderer.setAttribute(element, name, value);
95
+ }
96
+ });
97
+ // Read at hydration: a server image the browser already finished fired its load before any listener existed.
98
+ const finished = element.isConnected && element.complete && element.getAttribute("src") !== null;
99
+ const offLoad = renderer.listen(element, "load", (event) => this.events.load(event));
100
+ const offError = renderer.listen(element, "error", (event) => this.events.error(event));
101
+ inject(DestroyRef).onDestroy(() => {
102
+ offLoad();
103
+ offError();
104
+ });
105
+ // Angular's event replay captures a load that fired before hydration and replays it to `(load)` bindings itself;
106
+ // only the directive's own state is caught up here, so no second load is dispatched.
107
+ if (finished)
108
+ afterNextRender(() => this.loaded.set(element.naturalWidth > 0));
109
+ }
110
+ ngOnInit() {
111
+ if (this.preload())
112
+ runInInjectionContext(this.injector, () => injectHead(() => ({ link: [imagePreloadLink(this.resolved())] })));
113
+ }
114
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Img, deps: [], target: i0.ɵɵFactoryTarget.Directive });
115
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.3.2", type: Img, isStandalone: true, selector: "img[hamurImg]", inputs: { src: { classPropertyName: "src", publicName: "src", isSignal: true, isRequired: true, transformFunction: null }, alt: { classPropertyName: "alt", publicName: "alt", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null }, sizes: { classPropertyName: "sizes", publicName: "sizes", isSignal: true, isRequired: false, transformFunction: null }, densities: { classPropertyName: "densities", publicName: "densities", isSignal: true, isRequired: false, transformFunction: null }, format: { classPropertyName: "format", publicName: "format", isSignal: true, isRequired: false, transformFunction: null }, quality: { classPropertyName: "quality", publicName: "quality", isSignal: true, isRequired: false, transformFunction: null }, fit: { classPropertyName: "fit", publicName: "fit", isSignal: true, isRequired: false, transformFunction: null }, modifiers: { classPropertyName: "modifiers", publicName: "modifiers", isSignal: true, isRequired: false, transformFunction: null }, provider: { classPropertyName: "provider", publicName: "provider", isSignal: true, isRequired: false, transformFunction: null }, preset: { classPropertyName: "preset", publicName: "preset", isSignal: true, isRequired: false, transformFunction: null }, loading: { classPropertyName: "loading", publicName: "loading", isSignal: true, isRequired: false, transformFunction: null }, decoding: { classPropertyName: "decoding", publicName: "decoding", isSignal: true, isRequired: false, transformFunction: null }, fetchpriority: { classPropertyName: "fetchpriority", publicName: "fetchpriority", isSignal: true, isRequired: false, transformFunction: null }, preload: { classPropertyName: "preload", publicName: "preload", isSignal: true, isRequired: false, transformFunction: null }, placeholder: { classPropertyName: "placeholder", publicName: "placeholder", isSignal: true, isRequired: false, transformFunction: null }, placeholderClass: { classPropertyName: "placeholderClass", publicName: "placeholderClass", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "attr.hamurImg": "null", "attr.densities": "null", "attr.format": "null", "attr.quality": "null", "attr.fit": "null", "attr.modifiers": "null", "attr.provider": "null", "attr.preset": "null", "attr.preload": "null", "attr.placeholder": "null", "attr.placeholderClass": "null", "attr.width": "attrs()['width']", "attr.height": "attrs()['height']", "attr.alt": "attrs()['alt']", "attr.loading": "attrs()['loading']", "attr.decoding": "attrs()['decoding']", "attr.fetchpriority": "attrs()['fetchpriority']", "style": "painted()", "class": "paintedClass()" } }, ngImport: i0 });
144
116
  }
117
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Img, decorators: [{
118
+ type: Directive,
119
+ args: [{
120
+ selector: "img[hamurImg]",
121
+ host: {
122
+ "[attr.hamurImg]": "null",
123
+ "[attr.densities]": "null",
124
+ "[attr.format]": "null",
125
+ "[attr.quality]": "null",
126
+ "[attr.fit]": "null",
127
+ "[attr.modifiers]": "null",
128
+ "[attr.provider]": "null",
129
+ "[attr.preset]": "null",
130
+ "[attr.preload]": "null",
131
+ "[attr.placeholder]": "null",
132
+ "[attr.placeholderClass]": "null",
133
+ "[attr.width]": "attrs()['width']",
134
+ "[attr.height]": "attrs()['height']",
135
+ "[attr.alt]": "attrs()['alt']",
136
+ "[attr.loading]": "attrs()['loading']",
137
+ "[attr.decoding]": "attrs()['decoding']",
138
+ "[attr.fetchpriority]": "attrs()['fetchpriority']",
139
+ "[style]": "painted()",
140
+ "[class]": "paintedClass()",
141
+ },
142
+ }]
143
+ }], ctorParameters: () => [], propDecorators: { src: [{ type: i0.Input, args: [{ isSignal: true, alias: "src", required: true }] }], alt: [{ type: i0.Input, args: [{ isSignal: true, alias: "alt", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }], sizes: [{ type: i0.Input, args: [{ isSignal: true, alias: "sizes", required: false }] }], densities: [{ type: i0.Input, args: [{ isSignal: true, alias: "densities", required: false }] }], format: [{ type: i0.Input, args: [{ isSignal: true, alias: "format", required: false }] }], quality: [{ type: i0.Input, args: [{ isSignal: true, alias: "quality", required: false }] }], fit: [{ type: i0.Input, args: [{ isSignal: true, alias: "fit", required: false }] }], modifiers: [{ type: i0.Input, args: [{ isSignal: true, alias: "modifiers", required: false }] }], provider: [{ type: i0.Input, args: [{ isSignal: true, alias: "provider", required: false }] }], preset: [{ type: i0.Input, args: [{ isSignal: true, alias: "preset", required: false }] }], loading: [{ type: i0.Input, args: [{ isSignal: true, alias: "loading", required: false }] }], decoding: [{ type: i0.Input, args: [{ isSignal: true, alias: "decoding", required: false }] }], fetchpriority: [{ type: i0.Input, args: [{ isSignal: true, alias: "fetchpriority", required: false }] }], preload: [{ type: i0.Input, args: [{ isSignal: true, alias: "preload", required: false }] }], placeholder: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholder", required: false }] }], placeholderClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholderClass", required: false }] }] } });
@@ -1,37 +1,97 @@
1
- import {
2
- Component,
3
- ElementRef,
4
- Injector,
5
- Renderer2,
6
- afterRenderEffect,
7
- booleanAttribute,
8
- computed,
9
- inject,
10
- input,
11
- numberAttribute,
12
- runInInjectionContext,
13
- signal,
14
- viewChild,
15
- type OnInit,
16
- } from "@angular/core"
17
- import type { ImageMeta } from "hamur"
18
- import { injectHead } from "@hamur/adapter-angular"
19
- import type { ImageFit, ImageFormat, ImageModifiers, HamurImagePreset, HamurImageProvider } from "../types.ts"
20
- import { pictureAttributes } from "../runtime/index.ts"
21
- import { applyPlaceholder, createImgEvents } from "../runtime/img.ts"
22
- import { picturePreloadLink } from "../runtime/picture.ts"
23
- import { strings } from "./angular-img.ts"
24
-
25
- const optionalNumber = (value: unknown): number | undefined =>
26
- value === undefined ? undefined : numberAttribute(value)
27
-
1
+ import { Component, Injector, Renderer2, afterRenderEffect, booleanAttribute, computed, inject, input, numberAttribute, runInInjectionContext, signal, viewChild, } from "@angular/core";
2
+ import { injectHead } from "@hamur/adapter-angular";
3
+ import { pictureAttributes } from "../runtime/index.ts";
4
+ import { applyPlaceholder, createImgEvents } from "../runtime/img.ts";
5
+ import { picturePreloadLink } from "../runtime/picture.ts";
6
+ import { strings } from "./angular-img.ts";
7
+ import * as i0 from "@angular/core";
8
+ const optionalNumber = (value) => value === undefined ? undefined : numberAttribute(value);
28
9
  /**
29
10
  * `<hamur-picture src="/hero.jpg" format="webp" width="1200" alt="" />`, one `<source>` per format and the
30
11
  * `<img>` in the source's own format, or the `<img>` alone when the provider makes no other format.
31
12
  */
32
- @Component({
33
- selector: "hamur-picture",
34
- template: `@if (parts().sources.length > 0) {
13
+ export class Picture {
14
+ src = input.required(/* @ts-ignore */
15
+ ...(ngDevMode ? [{ debugName: "src" }] : /* istanbul ignore next */ []));
16
+ alt = input(/* @ts-ignore */
17
+ ...(ngDevMode ? [undefined, { debugName: "alt" }] : /* istanbul ignore next */ []));
18
+ width = input(/* @ts-ignore */
19
+ ...(ngDevMode ? [undefined, { debugName: "width" }] : /* istanbul ignore next */ []));
20
+ height = input(/* @ts-ignore */
21
+ ...(ngDevMode ? [undefined, { debugName: "height" }] : /* istanbul ignore next */ []));
22
+ sizes = input(/* @ts-ignore */
23
+ ...(ngDevMode ? [undefined, { debugName: "sizes" }] : /* istanbul ignore next */ []));
24
+ densities = input(/* @ts-ignore */
25
+ ...(ngDevMode ? [undefined, { debugName: "densities" }] : /* istanbul ignore next */ []));
26
+ format = input(/* @ts-ignore */
27
+ ...(ngDevMode ? [undefined, { debugName: "format" }] : /* istanbul ignore next */ []));
28
+ legacyFormat = input(/* @ts-ignore */
29
+ ...(ngDevMode ? [undefined, { debugName: "legacyFormat" }] : /* istanbul ignore next */ []));
30
+ quality = input(undefined, { ...(ngDevMode ? { debugName: "quality" } : /* istanbul ignore next */ {}), transform: optionalNumber });
31
+ fit = input(/* @ts-ignore */
32
+ ...(ngDevMode ? [undefined, { debugName: "fit" }] : /* istanbul ignore next */ []));
33
+ modifiers = input(/* @ts-ignore */
34
+ ...(ngDevMode ? [undefined, { debugName: "modifiers" }] : /* istanbul ignore next */ []));
35
+ provider = input(/* @ts-ignore */
36
+ ...(ngDevMode ? [undefined, { debugName: "provider" }] : /* istanbul ignore next */ []));
37
+ preset = input(/* @ts-ignore */
38
+ ...(ngDevMode ? [undefined, { debugName: "preset" }] : /* istanbul ignore next */ []));
39
+ loading = input(/* @ts-ignore */
40
+ ...(ngDevMode ? [undefined, { debugName: "loading" }] : /* istanbul ignore next */ []));
41
+ decoding = input(/* @ts-ignore */
42
+ ...(ngDevMode ? [undefined, { debugName: "decoding" }] : /* istanbul ignore next */ []));
43
+ fetchpriority = input(/* @ts-ignore */
44
+ ...(ngDevMode ? [undefined, { debugName: "fetchpriority" }] : /* istanbul ignore next */ []));
45
+ preload = input(false, { ...(ngDevMode ? { debugName: "preload" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
46
+ placeholder = input(/* @ts-ignore */
47
+ ...(ngDevMode ? [undefined, { debugName: "placeholder" }] : /* istanbul ignore next */ []));
48
+ placeholderClass = input(/* @ts-ignore */
49
+ ...(ngDevMode ? [undefined, { debugName: "placeholderClass" }] : /* istanbul ignore next */ []));
50
+ injector = inject(Injector);
51
+ loaded = signal(false, /* @ts-ignore */
52
+ ...(ngDevMode ? [{ debugName: "loaded" }] : /* istanbul ignore next */ []));
53
+ element = viewChild("img", /* @ts-ignore */
54
+ ...(ngDevMode ? [{ debugName: "element" }] : /* istanbul ignore next */ []));
55
+ events = createImgEvents(() => ({}), (value) => this.loaded.set(value));
56
+ parts = computed(() => pictureAttributes({
57
+ src: this.src(),
58
+ alt: this.alt(),
59
+ width: this.width(),
60
+ height: this.height(),
61
+ sizes: this.sizes(),
62
+ densities: this.densities(),
63
+ format: this.format(),
64
+ legacyFormat: this.legacyFormat(),
65
+ quality: this.quality(),
66
+ fit: this.fit(),
67
+ modifiers: this.modifiers(),
68
+ provider: this.provider(),
69
+ preset: this.preset(),
70
+ loading: this.loading(),
71
+ decoding: this.decoding(),
72
+ fetchpriority: this.fetchpriority(),
73
+ preload: this.preload(),
74
+ }), /* @ts-ignore */
75
+ ...(ngDevMode ? [{ debugName: "parts" }] : /* istanbul ignore next */ []));
76
+ shown = computed(() => strings(applyPlaceholder(this.parts().img, { placeholder: this.placeholder(), placeholderClass: this.placeholderClass() }, this.loaded())), /* @ts-ignore */
77
+ ...(ngDevMode ? [{ debugName: "shown" }] : /* istanbul ignore next */ []));
78
+ constructor() {
79
+ const renderer = inject(Renderer2);
80
+ afterRenderEffect((onCleanup) => {
81
+ const image = this.element()?.nativeElement;
82
+ if (!image)
83
+ return;
84
+ onCleanup(renderer.listen(image, "load", (event) => this.events.load(event)));
85
+ onCleanup(renderer.listen(image, "error", (event) => this.events.error(event)));
86
+ this.events.replay(image);
87
+ });
88
+ }
89
+ ngOnInit() {
90
+ if (this.preload())
91
+ runInInjectionContext(this.injector, () => injectHead(() => ({ link: [picturePreloadLink(this.parts())] })));
92
+ }
93
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Picture, deps: [], target: i0.ɵɵFactoryTarget.Component });
94
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.3.2", type: Picture, isStandalone: true, selector: "hamur-picture", inputs: { src: { classPropertyName: "src", publicName: "src", isSignal: true, isRequired: true, transformFunction: null }, alt: { classPropertyName: "alt", publicName: "alt", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null }, sizes: { classPropertyName: "sizes", publicName: "sizes", isSignal: true, isRequired: false, transformFunction: null }, densities: { classPropertyName: "densities", publicName: "densities", isSignal: true, isRequired: false, transformFunction: null }, format: { classPropertyName: "format", publicName: "format", isSignal: true, isRequired: false, transformFunction: null }, legacyFormat: { classPropertyName: "legacyFormat", publicName: "legacyFormat", isSignal: true, isRequired: false, transformFunction: null }, quality: { classPropertyName: "quality", publicName: "quality", isSignal: true, isRequired: false, transformFunction: null }, fit: { classPropertyName: "fit", publicName: "fit", isSignal: true, isRequired: false, transformFunction: null }, modifiers: { classPropertyName: "modifiers", publicName: "modifiers", isSignal: true, isRequired: false, transformFunction: null }, provider: { classPropertyName: "provider", publicName: "provider", isSignal: true, isRequired: false, transformFunction: null }, preset: { classPropertyName: "preset", publicName: "preset", isSignal: true, isRequired: false, transformFunction: null }, loading: { classPropertyName: "loading", publicName: "loading", isSignal: true, isRequired: false, transformFunction: null }, decoding: { classPropertyName: "decoding", publicName: "decoding", isSignal: true, isRequired: false, transformFunction: null }, fetchpriority: { classPropertyName: "fetchpriority", publicName: "fetchpriority", isSignal: true, isRequired: false, transformFunction: null }, preload: { classPropertyName: "preload", publicName: "preload", isSignal: true, isRequired: false, transformFunction: null }, placeholder: { classPropertyName: "placeholder", publicName: "placeholder", isSignal: true, isRequired: false, transformFunction: null }, placeholderClass: { classPropertyName: "placeholderClass", publicName: "placeholderClass", isSignal: true, isRequired: false, transformFunction: null } }, viewQueries: [{ propertyName: "element", first: true, predicate: ["img"], descendants: true, isSignal: true }], ngImport: i0, template: `@if (parts().sources.length > 0) {
35
95
  <picture>
36
96
  @for (source of parts().sources; track source.type) {
37
97
  <source [attr.type]="source.type" [attr.srcset]="source.srcset" [attr.sizes]="source.sizes" />
@@ -66,81 +126,47 @@ const optionalNumber = (value: unknown): number | undefined =>
66
126
  [attr.style]="shown()['style']"
67
127
  [attr.class]="shown()['class']"
68
128
  />
69
- }`,
70
- })
71
- export class Picture implements OnInit {
72
- readonly src = input.required<string | ImageMeta>()
73
- readonly alt = input<string>()
74
- readonly width = input<number | string>()
75
- readonly height = input<number | string>()
76
- readonly sizes = input<string>()
77
- readonly densities = input<number[]>()
78
- readonly format = input<ImageFormat | ImageFormat[]>()
79
- readonly legacyFormat = input<ImageFormat>()
80
- readonly quality = input<number | undefined, unknown>(undefined, { transform: optionalNumber })
81
- readonly fit = input<ImageFit>()
82
- readonly modifiers = input<ImageModifiers>()
83
- readonly provider = input<HamurImageProvider>()
84
- readonly preset = input<HamurImagePreset>()
85
- readonly loading = input<"lazy" | "eager">()
86
- readonly decoding = input<string>()
87
- readonly fetchpriority = input<"high" | "low" | "auto">()
88
- readonly preload = input(false, { transform: booleanAttribute })
89
- readonly placeholder = input<string>()
90
- readonly placeholderClass = input<string>()
91
-
92
- private readonly injector = inject(Injector)
93
- private readonly loaded = signal(false)
94
- private readonly element = viewChild<ElementRef<HTMLImageElement>>("img")
95
- private readonly events = createImgEvents(
96
- () => ({}),
97
- (value) => this.loaded.set(value),
98
- )
99
-
100
- protected readonly parts = computed(() =>
101
- pictureAttributes({
102
- src: this.src(),
103
- alt: this.alt(),
104
- width: this.width(),
105
- height: this.height(),
106
- sizes: this.sizes(),
107
- densities: this.densities(),
108
- format: this.format(),
109
- legacyFormat: this.legacyFormat(),
110
- quality: this.quality(),
111
- fit: this.fit(),
112
- modifiers: this.modifiers(),
113
- provider: this.provider(),
114
- preset: this.preset(),
115
- loading: this.loading(),
116
- decoding: this.decoding(),
117
- fetchpriority: this.fetchpriority(),
118
- preload: this.preload(),
119
- }),
120
- )
121
- protected readonly shown = computed(() =>
122
- strings(
123
- applyPlaceholder(
124
- this.parts().img,
125
- { placeholder: this.placeholder(), placeholderClass: this.placeholderClass() },
126
- this.loaded(),
127
- ),
128
- ),
129
- )
130
-
131
- constructor() {
132
- const renderer = inject(Renderer2)
133
- afterRenderEffect((onCleanup) => {
134
- const image = this.element()?.nativeElement
135
- if (!image) return
136
- onCleanup(renderer.listen(image, "load", (event: Event) => this.events.load(event)))
137
- onCleanup(renderer.listen(image, "error", (event: Event) => this.events.error(event)))
138
- this.events.replay(image)
139
- })
140
- }
141
-
142
- ngOnInit(): void {
143
- if (this.preload())
144
- runInInjectionContext(this.injector, () => injectHead(() => ({ link: [picturePreloadLink(this.parts())] })))
145
- }
129
+ }`, isInline: true });
146
130
  }
131
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Picture, decorators: [{
132
+ type: Component,
133
+ args: [{
134
+ selector: "hamur-picture",
135
+ template: `@if (parts().sources.length > 0) {
136
+ <picture>
137
+ @for (source of parts().sources; track source.type) {
138
+ <source [attr.type]="source.type" [attr.srcset]="source.srcset" [attr.sizes]="source.sizes" />
139
+ }
140
+ <img
141
+ #img
142
+ [attr.src]="shown()['src']"
143
+ [attr.srcset]="shown()['srcset']"
144
+ [attr.sizes]="shown()['sizes']"
145
+ [attr.width]="shown()['width']"
146
+ [attr.height]="shown()['height']"
147
+ [attr.alt]="shown()['alt']"
148
+ [attr.loading]="shown()['loading']"
149
+ [attr.decoding]="shown()['decoding']"
150
+ [attr.fetchpriority]="shown()['fetchpriority']"
151
+ [attr.style]="shown()['style']"
152
+ [attr.class]="shown()['class']"
153
+ />
154
+ </picture>
155
+ } @else {
156
+ <img
157
+ #img
158
+ [attr.src]="shown()['src']"
159
+ [attr.srcset]="shown()['srcset']"
160
+ [attr.sizes]="shown()['sizes']"
161
+ [attr.width]="shown()['width']"
162
+ [attr.height]="shown()['height']"
163
+ [attr.alt]="shown()['alt']"
164
+ [attr.loading]="shown()['loading']"
165
+ [attr.decoding]="shown()['decoding']"
166
+ [attr.fetchpriority]="shown()['fetchpriority']"
167
+ [attr.style]="shown()['style']"
168
+ [attr.class]="shown()['class']"
169
+ />
170
+ }`,
171
+ }]
172
+ }], ctorParameters: () => [], propDecorators: { src: [{ type: i0.Input, args: [{ isSignal: true, alias: "src", required: true }] }], alt: [{ type: i0.Input, args: [{ isSignal: true, alias: "alt", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }], sizes: [{ type: i0.Input, args: [{ isSignal: true, alias: "sizes", required: false }] }], densities: [{ type: i0.Input, args: [{ isSignal: true, alias: "densities", required: false }] }], format: [{ type: i0.Input, args: [{ isSignal: true, alias: "format", required: false }] }], legacyFormat: [{ type: i0.Input, args: [{ isSignal: true, alias: "legacyFormat", required: false }] }], quality: [{ type: i0.Input, args: [{ isSignal: true, alias: "quality", required: false }] }], fit: [{ type: i0.Input, args: [{ isSignal: true, alias: "fit", required: false }] }], modifiers: [{ type: i0.Input, args: [{ isSignal: true, alias: "modifiers", required: false }] }], provider: [{ type: i0.Input, args: [{ isSignal: true, alias: "provider", required: false }] }], preset: [{ type: i0.Input, args: [{ isSignal: true, alias: "preset", required: false }] }], loading: [{ type: i0.Input, args: [{ isSignal: true, alias: "loading", required: false }] }], decoding: [{ type: i0.Input, args: [{ isSignal: true, alias: "decoding", required: false }] }], fetchpriority: [{ type: i0.Input, args: [{ isSignal: true, alias: "fetchpriority", required: false }] }], preload: [{ type: i0.Input, args: [{ isSignal: true, alias: "preload", required: false }] }], placeholder: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholder", required: false }] }], placeholderClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholderClass", required: false }] }], element: [{ type: i0.ViewChild, args: ["img", { isSignal: true }] }] } });
@@ -1,9 +1,13 @@
1
- import { presetFeatures, type HamurContext } from "hamur/kit"
1
+ import { readdir } from "node:fs/promises"
2
+ import { join, sep } from "node:path"
3
+ import { DiagnosticError, presetFeatures, type HamurContext } from "hamur/kit"
4
+ import { accessRuleOf } from "hamur/server/internal"
2
5
  import { hostSource, parseDomain, pathSource } from "../domains.ts"
3
6
  import type { ImageBuildOptions, ImageFormat } from "../types.ts"
4
7
  import { providersUsed } from "./endpoint.ts"
5
8
 
6
9
  const MIME: Partial<Record<ImageFormat, string>> = { avif: "image/avif", webp: "image/webp" }
10
+ const IMAGE_FILE = /\.(?:png|jpe?g|gif|webp|avif|svg)$/i
7
11
  const MINIMUM_CACHE_TTL = 14400
8
12
  const DEFAULT_QUALITY = 75
9
13
 
@@ -96,4 +100,17 @@ export function setupPlatform(options: ImageBuildOptions, hamur: HamurContext):
96
100
  if (service === undefined || !providersUsed(options).has(service)) return
97
101
  const config = hamur.options[service].config
98
102
  config.images = mergeImages(config.images, service === "vercel" ? vercelImages(options) : netlifyImages(options))
103
+ hamur.hooks.hook("prerender:done", async () => {
104
+ const files = await readdir(join(hamur.options.outputDir, "public"), { recursive: true }).catch(() => [])
105
+ const ruleOf = accessRuleOf(hamur.routeRules)
106
+ for (const file of files.filter((name) => IMAGE_FILE.test(name)).toSorted()) {
107
+ const path = `/${file.split(sep).map(encodeURIComponent).join("/")}`
108
+ const found = ruleOf(path)
109
+ if (found)
110
+ throw new DiagnosticError(
111
+ "HAMUR_E6008",
112
+ `Route rule ${JSON.stringify(found.pattern)} ${found.key} does not protect ${path}, which the ${service} image service reads itself; move the image out of public/ or set image.provider to "none"`,
113
+ )
114
+ }
115
+ })
99
116
  }
@@ -6,6 +6,8 @@ import type { ImageBuildOptions, ImageFormat, ImageSources, ResolvedImageOptions
6
6
  import { providersUsed, transformsAtRunTime } from "./endpoint.ts"
7
7
  import { normalizedBase, readSources } from "./runtime.ts"
8
8
  import { DiagnosticError } from "hamur/kit"
9
+ import { accessRuleOf, hasPlatformFiles } from "hamur/server/internal"
10
+ import { isRefusal, parseImagePath } from "../server/parse.ts"
9
11
 
10
12
  const ATTRIBUTE = /\s(src|srcset|imagesrcset|href|style)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+))/gi
11
13
  const STYLE_URL = /url\(\s*(?:"([^"]*)"|'([^']*)'|([^)\s]*))\s*\)/gi
@@ -122,6 +124,21 @@ export function setupVariants(options: ImageBuildOptions, hamur: HamurContext):
122
124
  }
123
125
  if (!where.size) return
124
126
 
127
+ const ruleOf = accessRuleOf(hamur.routeRules)
128
+ const alone = hasPlatformFiles(hamur)
129
+ for (const url of where.keys()) {
130
+ const variant = new URL(url, "http://localhost").pathname
131
+ const parsed = parseImagePath(variant.slice(prefix.length))
132
+ if (isRefusal(parsed) || parsed.path === undefined) continue
133
+ const source = `/${parsed.path.split("/").map(encodeURIComponent).join("/")}`
134
+ const found = ruleOf(source, alone ? undefined : `/${variant.slice(baseURL.length)}`)
135
+ if (found)
136
+ throw new DiagnosticError(
137
+ "HAMUR_E6008",
138
+ `Route rule ${JSON.stringify(found.pattern)} ${found.key} does not protect the variants of ${source} (in ${where.get(url)}), which are public files; use the "none" provider for this image`,
139
+ )
140
+ }
141
+
125
142
  Bun.Image.backend = "bun"
126
143
  let created = 0
127
144
  const cache = fileCache(join(hamur.options.rootDir, "node_modules/.cache/hamur/image"), () => created++)