Skip to content
Open
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
6 changes: 4 additions & 2 deletions docs/asset-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ A relative base would produce document-relative URLs, which break on nested rout
- emits a content-hashed copy next to each one, keeping the directory layout (`assets/images/logo.svg` → `assets/images/logo-<16 hex>.svg`);
- maps each logical name to its hashed copy (`enabled: true`).

This applies to every `vite build`, test builds (`vite build --mode development`) included, so tests should get expected asset URLs from the `asset-map` service rather than hardcode stable paths. The stable copies stay in the output, so CSS `url()`s, favicons and other direct references keep working. Assets Vite already fingerprints from module imports (`import url from './logo.svg'`) are not in the map; the importing module already has their URL. A public file whose extension isn't listed resolves to its stable path and warns as a missing entry in development-mode builds.
This applies to every `vite build`, test builds (`vite build --mode development`) included, so tests should get expected asset URLs from the `asset-map` service rather than hardcode stable paths. The stable copies stay in the output, but a direct reference to them resolves against the page's host rather than `base`, so OSS components resolve their own images through the `asset-map` service. Assets Vite already fingerprints from module imports (`import url from './logo.svg'`) are not in the map; the importing module already has their URL. A public file whose extension isn't listed resolves to its stable path and warns as a missing entry in development-mode builds.

**OSS stylesheets.** The OSS Less builds its font and image URLs from `@oss-assets-root`, which defaults to the stable path (`/@upfluence/oss-components/assets`). Classic builds compile the same URLs as before, and broccoli-asset-rev keeps rewriting them. In `vite build`, the plugin points the variable at this package's files through Less `modifyVars`, so Vite resolves, hashes and serves them from `base` like any imported asset; without the plugin, Vite leaves the stable paths unresolved and warns. The dev server keeps the stable paths, which `@embroider/vite` serves.

**Failures.** The Vite plugin shares the classic wrapper's HTML checks, in the dev server and in `vite build`: an `ember-cli-ifa:assetMap` meta (including one injected through `content-for`) fails, and every HTML entry must contain the placeholder exactly once.

Expand Down Expand Up @@ -111,7 +113,7 @@ pnpm typecheck
pnpm build && pnpm --filter test-app test:ember --filter='asset-map' --launch=Chrome
```

Build tests exercise real broccoli-asset-rev output with app-owned and namespaced addon-owned assets, CDN prepend, hashed/default/custom manifests, disabled fingerprinting, repeated builds, and error paths. Vite tests run real Vite 8 builds and dev servers: app `public/` and addon public assets (emitted the way `@embroider/vite` does), Vite-imported assets left out, CDN `base`, the `extensions` option, rebuilds, and the same error paths plus relative bases. Ember tests cover the service, independent instances, helper output, and reactive configuration.
Build tests exercise real broccoli-asset-rev output with app-owned and namespaced addon-owned assets, CDN prepend, hashed/default/custom manifests, disabled fingerprinting, repeated builds, and error paths. Vite tests run real Vite 8 builds and dev servers: app `public/` and addon public assets (emitted the way `@embroider/vite` does), Vite-imported assets left out, CDN `base`, the `extensions` option, OSS stylesheet assets, rebuilds, and the same error paths plus relative bases. Ember tests cover the service, independent instances, helper output, and reactive configuration.

Before accepting the plugin-web pilot, run its development and production builds with the local/new OSS package, then check dynamically selected login images, branding logos, locked-state illustrations, profile empty-state images, and OSS upload/social badges. Verify CDN URLs point to actual emitted files.

Expand Down
12 changes: 11 additions & 1 deletion packages/oss-components/build/vite-asset-map.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { createHash } from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import assetMapHTML from './asset-map-html.cjs';

const { PREFIX, injectManifest } = assetMapHTML;

const DEFAULT_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'avif', 'ico'];
const PUBLIC_ASSETS = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../public/assets');

function isAbsoluteURL(url) {
return /^(?:[a-z][a-z\d+.-]*:)?\/\//i.test(url);
Expand Down Expand Up @@ -39,7 +41,8 @@ function publicFiles(directory, relative = '') {
* - `vite` (dev server): an empty, disabled map rooted at Vite's `base`.
* - `vite build`: app `public/` files and addon public assets matching `extensions` get a content-hashed copy
* next to their stable one, and the map points each logical name to its hashed copy. An absolute `base`
* (CDN) becomes the manifest's `prepend`.
* (CDN) becomes the manifest's `prepend`. The OSS styles' `@oss-assets-root` points at this package's files, so
* Vite hashes the fonts and images they reference and serves them from `base`.
*
* @param {{ extensions?: string[] }} [options]
* @returns {import('vite').Plugin}
Expand Down Expand Up @@ -70,6 +73,13 @@ export function assetMap({ extensions = DEFAULT_EXTENSIONS } = {}) {
return {
name: 'upfluence-asset-map',

config(_config, { command }) {
if (command !== 'build') return;
return {
css: { preprocessorOptions: { less: { modifyVars: { 'oss-assets-root': `'${PUBLIC_ASSETS}'` } } } }
};
},

configResolved(resolved) {
config = resolved;
if (config.command === 'build') prefixFor(config.base);
Expand Down
14 changes: 11 additions & 3 deletions packages/oss-components/src/components/o-s-s/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import type AssetMapService from '../../services/asset-map';
import type { OSSBadgeArgs } from './badge';

type SizeDefType = { [key in SizeType]: string };
Expand Down Expand Up @@ -57,18 +59,23 @@ export interface OSSAvatarSignature {
export const DEFAULT_IMAGE_URL: string = '/@upfluence/oss-components/assets/images/avatar-placeholder.svg';

export default class OSSAvatar extends Component<OSSAvatarSignature> {
@service('asset-map') declare assetMap: AssetMapService;
@tracked hasError: boolean = false;

get displayInitials(): boolean {
return (!this.args.image || this.hasError) && !!this.args.initials;
}

get defaultImage(): string {
return this.assetMap.resolve(DEFAULT_IMAGE_URL);
}

get image(): string | undefined {
return this.args.image ?? DEFAULT_IMAGE_URL;
return this.args.image ?? this.defaultImage;
}

get imageClass(): string {
return this.image === DEFAULT_IMAGE_URL || this.hasError ? 'default-img-placeholder' : '';
return this.image === this.defaultImage || this.hasError ? 'default-img-placeholder' : '';
}

get computedClass(): string {
Expand Down Expand Up @@ -98,7 +105,8 @@ export default class OSSAvatar extends Component<OSSAvatarSignature> {

@action
imageLoadError(event: Event): void {
(<HTMLImageElement>event.target).src = DEFAULT_IMAGE_URL;
if (this.isDestroying || this.isDestroyed) return;
(<HTMLImageElement>event.target).src = this.defaultImage;
this.hasError = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
{{yield to="empty-state"}}
{{else}}
<OSS::Illustration
@src="/@upfluence/oss-components/assets/images/no-records.svg"
@src={{asset-map "@upfluence/oss-components/assets/images/no-records.svg"}}
data-control-name="infinite-select-empty-state-illustration"
/>
<div class="margin-top-xx-sm text-color-default upf-align--center">
Expand Down
4 changes: 2 additions & 2 deletions packages/oss-components/src/components/o-s-s/smart/blob.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div class={{this.computedClass}} ...attributes>
<div class="smart-blob">
<img
src="/@upfluence/oss-components/assets/images/smart-blob-loading.gif"
src={{asset-map "@upfluence/oss-components/assets/images/smart-blob-loading.gif"}}
alt="smart-blob-loading"
class={{this.loadingClass}}
/>
<img
src="/@upfluence/oss-components/assets/images/smart-blob-inactive.gif"
src={{asset-map "@upfluence/oss-components/assets/images/smart-blob-inactive.gif"}}
alt="smart-blob-inactive"
class={{this.inactiveClass}}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import type Owner from '@ember/owner';
import { service } from '@ember/service';
import { isBlank } from '@ember/utils';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import type AssetMapService from '../../../../services/asset-map';
import { runSmartGradientAnimation } from '../../../../utils/run-smart-gradient-animation';

const FALLBACK_SVG_URL = '/@upfluence/oss-components/assets/images/picture-frame.svg';
Expand Down Expand Up @@ -47,8 +49,8 @@ export interface OSSSmartImmersiveLogoSignature {
const BASE_COMPONENT_CLASS = 'oss-smart__immersive-icon-container';

export default class OSSSmartImmersiveLogoComponent extends Component<OSSSmartImmersiveLogoSignature> {
@service('asset-map') declare assetMap: AssetMapService;
@tracked declare element: HTMLElement;
fallbackSvgUrl = FALLBACK_SVG_URL;

constructor(owner: Owner, args: OSSSmartImmersiveLogoArgs) {
super(owner, args);
Expand All @@ -68,6 +70,10 @@ export default class OSSSmartImmersiveLogoComponent extends Component<OSSSmartIm
}
}

get fallbackSvgUrl(): string {
return this.assetMap.resolve(FALLBACK_SVG_URL);
}

get size(): size {
return this.args.size ?? 'md';
}
Expand Down Expand Up @@ -114,6 +120,7 @@ export default class OSSSmartImmersiveLogoComponent extends Component<OSSSmartIm

@action
onError(event: Event): void {
(<HTMLImageElement>event.target).src = FALLBACK_SVG_URL;
if (this.isDestroying || this.isDestroyed) return;
(<HTMLImageElement>event.target).src = this.fallbackSvgUrl;
}
}
6 changes: 5 additions & 1 deletion packages/oss-components/src/components/upf-image.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Component from '@ember/component';
import { service } from '@ember/service';

export default Component.extend({
assetMap: service('asset-map'),

src: null,

classNames: ['upf-image'],

didInsertElement() {
this._super();
this.element.style.backgroundImage = `url("${this.src}"), url("/@upfluence/oss-components/assets/images/no-image.svg")`;
const fallback = this.assetMap.resolve('@upfluence/oss-components/assets/images/no-image.svg');
this.element.style.backgroundImage = `url("${this.src}"), url("${fallback}")`;
}
});
8 changes: 4 additions & 4 deletions packages/oss-components/styles/core/_font-faces.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
font-style: normal;
font-display: swap;
font-weight: 400 700;
src: url('/@upfluence/oss-components/assets/fonts/inter-variable.woff2') format('woff2-variations');
src: url('@{oss-assets-root}/fonts/inter-variable.woff2') format('woff2-variations');
}

@font-face {
font-family: 'Roboto Mono';
font-style: normal;
font-display: swap;
font-weight: 400 700;
src: url('/@upfluence/oss-components/assets/fonts/roboto-mono-variable.woff2') format('woff2-variations');
src: url('@{oss-assets-root}/fonts/roboto-mono-variable.woff2') format('woff2-variations');
}

@font-face {
font-family: 'Reddit Sans';
font-style: normal;
font-display: swap;
font-weight: 400 600;
src: url('/@upfluence/oss-components/assets/fonts/reddit-sans-variable.woff2') format('woff2-variations');
src: url('@{oss-assets-root}/fonts/reddit-sans-variable.woff2') format('woff2-variations');
}

@font-face {
font-family: 'Open Sans';
font-style: normal;
font-display: swap;
font-weight: 400 700;
src: url('/@upfluence/oss-components/assets/fonts/open-sans-variable.woff2') format('woff2-variations');
src: url('@{oss-assets-root}/fonts/open-sans-variable.woff2') format('woff2-variations');
}
3 changes: 3 additions & 0 deletions packages/oss-components/styles/core/_variables.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Vite builds point this at the package's files through the assetMap plugin.
@oss-assets-root: '/@upfluence/oss-components/assets';

:root {
--box-shadow-xs: 0px 1px 2px rgba(0, 0, 0, 0.04), 0px 1px 3px rgba(0, 0, 0, 0.06);
--box-shadow-sm: 0px 4px 8px -2px rgba(27, 30, 33, 0.1), 0px 2px 4px -2px rgba(27, 30, 33, 0.06);
Expand Down
2 changes: 1 addition & 1 deletion packages/oss-components/styles/flags.less
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ For complete information visit: www.freakflagsprite.com
******************************************************/

.fflag {
background-image: url('/@upfluence/oss-components/assets/images/flagSprite42.png');
background-image: url('@{oss-assets-root}/images/flagSprite42.png');
background-repeat: no-repeat;
background-size: 100% 49494%;
display: inline-block;
Expand Down
4 changes: 2 additions & 2 deletions packages/oss-components/styles/molecules/upload-area.less
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
&-end {
&:before {
content: '';
background-image: url('/@upfluence/oss-components/assets/images/upload-area/disabled-md.svg');
background-image: url('@{oss-assets-root}/images/upload-area/disabled-md.svg');
background-size: cover;
position: absolute;
display: block;
Expand Down Expand Up @@ -94,7 +94,7 @@
&:before,
&:after {
content: '';
background-image: url('/@upfluence/oss-components/assets/images/upload-area/disabled-lg.svg');
background-image: url('@{oss-assets-root}/images/upload-area/disabled-lg.svg');
background-size: cover;
position: absolute;
display: block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
width: 270px;
height: 150px;
position: absolute;
background-image: url('/@upfluence/oss-components/assets/components/marketing-banner/grid.svg');
background-image: url('@{oss-assets-root}/components/marketing-banner/grid.svg');
}
}

Expand Down
23 changes: 23 additions & 0 deletions packages/oss-components/tests/build/vite-asset-map-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { test } from 'node:test';
import { fileURLToPath } from 'node:url';
import { build, createServer } from 'vite';

import { assetMap } from '../../build/vite-asset-map.mjs';
Expand Down Expand Up @@ -140,6 +141,28 @@ test('an absolute base becomes the CDN prepend', async (t) => {
}
});

test('build serves the OSS stylesheet assets as hashed files from the base', async (t) => {
const variables = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../styles/core/_variables.less');
const root = fixture(t, {
'main.js': 'import "./styles.less";\n',
'styles.less': `@import '${variables}';\n.flag { background-image: url('@{oss-assets-root}/images/flagSprite42.png'); }\n`
});
const outDir = await viteBuild(root, { base: 'https://cdn.example.com/amb/' });
const css = fs
.readdirSync(path.join(outDir, 'assets'))
.filter((file) => file.endsWith('.css'))
.map((file) => fs.readFileSync(path.join(outDir, 'assets', file), 'utf8'))
.join('');
const url = css.match(/url\(([^)]*flagSprite42[^)]*)\)/)[1];

assert.match(url, /^https:\/\/cdn\.example\.com\/amb\/assets\/flagSprite42-[\w-]+\.png$/);
assert.ok(fs.existsSync(path.join(outDir, url.replace('https://cdn.example.com/amb/', ''))));
});

test('dev server leaves the OSS stylesheet asset root alone', () => {
assert.equal(assetMap().config({}, { command: 'serve', mode: 'development' }), undefined);
});

test('extensions narrows the fingerprinted files', async (t) => {
const outDir = await viteBuild(fixture(t, { 'public/assets/images/photo.png': 'png' }), {
plugin: assetMap({ extensions: ['.PNG'] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { setupRenderingTest } from 'ember-qunit';
import { module, test } from 'qunit';

import { SizeDefinition, AvatarSkins, DEFAULT_IMAGE_URL } from '@upfluence/oss-components/components/o-s-s/avatar';
import type AssetMapService from '@upfluence/oss-components/services/asset-map';

import { renderExpectingError } from 'test-app/tests/helpers/render-expecting-error';

Expand Down Expand Up @@ -33,9 +34,10 @@ module('Integration | Component | o-s-s/avatar', function (hooks) {
test('it displays the default image when initials and image are not provided', async function (assert) {
await render(hbs`<OSS::Avatar />`);

const assetMap = this.owner.lookup('service:asset-map') as AssetMapService;
assert.dom('.upf-avatar').exists();
assert.dom('.upf-avatar img').exists();
assert.dom('.upf-avatar img').hasAttribute('src', DEFAULT_IMAGE_URL);
assert.dom('.upf-avatar img').hasAttribute('src', assetMap.resolve(DEFAULT_IMAGE_URL));
});

test('it displays the image when image and initials are provided', async function (assert) {
Expand Down Expand Up @@ -195,14 +197,15 @@ module('Integration | Component | o-s-s/avatar', function (hooks) {
});

test('it displays the placeholder image when the image provided in parameters fails to load', async function (assert) {
const defaultImage = (this.owner.lookup('service:asset-map') as AssetMapService).resolve(DEFAULT_IMAGE_URL);
await render(hbs`<OSS::Avatar @image="/@upfluence/oss-components/assets/images/fail.svg" />`);
await waitUntil(function () {
return find('.upf-avatar img')?.getAttribute('src') === DEFAULT_IMAGE_URL;
return find('.upf-avatar img')?.getAttribute('src') === defaultImage;
});

assert.dom('.upf-avatar').exists();
assert.dom('.upf-avatar img').exists();
assert.dom('.upf-avatar img').hasAttribute('src', DEFAULT_IMAGE_URL);
assert.dom('.upf-avatar img').hasAttribute('src', defaultImage);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module('Integration | Component | o-s-s/smart/blob', function (hooks) {

const images = this.element.querySelectorAll('img');
const inactiveImg = Array.from(images).find((img: Element) =>
img.getAttribute('src')?.includes('smart-blob-inactive.gif')
img.getAttribute('src')?.includes('smart-blob-inactive')
) as HTMLImageElement;

assert.ok(inactiveImg, 'Found the inactive GIF');
Expand All @@ -32,7 +32,7 @@ module('Integration | Component | o-s-s/smart/blob', function (hooks) {

const images = this.element.querySelectorAll('img');
const active = Array.from(images).find((img: Element) =>
img.getAttribute('src')?.includes('smart-blob-loading.gif')
img.getAttribute('src')?.includes('smart-blob-loading')
) as HTMLImageElement;

assert.ok(active, 'Found the active GIF');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { setupRenderingTest } from 'ember-qunit';
import { module, test } from 'qunit';
import sinon from 'sinon';

import type AssetMapService from '@upfluence/oss-components/services/asset-map';
import { LOGO_COLORS, LOGO_ICONS } from '@upfluence/oss-components/utils/logo-config';

import { renderExpectingError } from 'test-app/tests/helpers/render-expecting-error';
Expand Down Expand Up @@ -190,8 +191,11 @@ module('Integration | Component | o-s-s/smart/immersive/logo', function (hooks)
@onEdit={{this.onEdit}}
/>`);

const assetMap = this.owner.lookup('service:asset-map') as AssetMapService;
assert.dom('.smart-logo-image').exists('Fallback image container is rendered');
assert.dom('.smart-logo-image').hasAttribute('src', '/@upfluence/oss-components/assets/images/picture-frame.svg');
assert
.dom('.smart-logo-image')
.hasAttribute('src', assetMap.resolve('/@upfluence/oss-components/assets/images/picture-frame.svg'));
});

module('@size', () => {
Expand Down
Loading