diff --git a/docs/asset-map.md b/docs/asset-map.md index 6aa7ff4ae..5ef90010a 100644 --- a/docs/asset-map.md +++ b/docs/asset-map.md @@ -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. @@ -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. diff --git a/packages/oss-components/build/vite-asset-map.mjs b/packages/oss-components/build/vite-asset-map.mjs index a691bf02b..549e1245e 100644 --- a/packages/oss-components/build/vite-asset-map.mjs +++ b/packages/oss-components/build/vite-asset-map.mjs @@ -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); @@ -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} @@ -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); diff --git a/packages/oss-components/src/components/o-s-s/avatar.ts b/packages/oss-components/src/components/o-s-s/avatar.ts index f77f4e657..aaa2c5c1b 100644 --- a/packages/oss-components/src/components/o-s-s/avatar.ts +++ b/packages/oss-components/src/components/o-s-s/avatar.ts @@ -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 }; @@ -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 { + @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 { @@ -98,7 +105,8 @@ export default class OSSAvatar extends Component { @action imageLoadError(event: Event): void { - (event.target).src = DEFAULT_IMAGE_URL; + if (this.isDestroying || this.isDestroyed) return; + (event.target).src = this.defaultImage; this.hasError = true; } } diff --git a/packages/oss-components/src/components/o-s-s/infinite-select.hbs b/packages/oss-components/src/components/o-s-s/infinite-select.hbs index 144108c54..d63194197 100644 --- a/packages/oss-components/src/components/o-s-s/infinite-select.hbs +++ b/packages/oss-components/src/components/o-s-s/infinite-select.hbs @@ -63,7 +63,7 @@ {{yield to="empty-state"}} {{else}}
diff --git a/packages/oss-components/src/components/o-s-s/smart/blob.hbs b/packages/oss-components/src/components/o-s-s/smart/blob.hbs index 2c1a0e027..d3a7c5968 100644 --- a/packages/oss-components/src/components/o-s-s/smart/blob.hbs +++ b/packages/oss-components/src/components/o-s-s/smart/blob.hbs @@ -1,12 +1,12 @@
smart-blob-loading smart-blob-inactive diff --git a/packages/oss-components/src/components/o-s-s/smart/immersive/logo.ts b/packages/oss-components/src/components/o-s-s/smart/immersive/logo.ts index 3e368b15f..fbdb8b668 100644 --- a/packages/oss-components/src/components/o-s-s/smart/immersive/logo.ts +++ b/packages/oss-components/src/components/o-s-s/smart/immersive/logo.ts @@ -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'; @@ -47,8 +49,8 @@ export interface OSSSmartImmersiveLogoSignature { const BASE_COMPONENT_CLASS = 'oss-smart__immersive-icon-container'; export default class OSSSmartImmersiveLogoComponent extends Component { + @service('asset-map') declare assetMap: AssetMapService; @tracked declare element: HTMLElement; - fallbackSvgUrl = FALLBACK_SVG_URL; constructor(owner: Owner, args: OSSSmartImmersiveLogoArgs) { super(owner, args); @@ -68,6 +70,10 @@ export default class OSSSmartImmersiveLogoComponent extends Componentevent.target).src = FALLBACK_SVG_URL; + if (this.isDestroying || this.isDestroyed) return; + (event.target).src = this.fallbackSvgUrl; } } diff --git a/packages/oss-components/src/components/upf-image.js b/packages/oss-components/src/components/upf-image.js index 632d77b2f..8a0b3e5d4 100644 --- a/packages/oss-components/src/components/upf-image.js +++ b/packages/oss-components/src/components/upf-image.js @@ -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}")`; } }); diff --git a/packages/oss-components/styles/core/_font-faces.less b/packages/oss-components/styles/core/_font-faces.less index 2b869d16b..84188ef9d 100644 --- a/packages/oss-components/styles/core/_font-faces.less +++ b/packages/oss-components/styles/core/_font-faces.less @@ -3,7 +3,7 @@ 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 { @@ -11,7 +11,7 @@ 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 { @@ -19,7 +19,7 @@ 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 { @@ -27,5 +27,5 @@ 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'); } diff --git a/packages/oss-components/styles/core/_variables.less b/packages/oss-components/styles/core/_variables.less index c1b987f08..9279032c8 100644 --- a/packages/oss-components/styles/core/_variables.less +++ b/packages/oss-components/styles/core/_variables.less @@ -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); diff --git a/packages/oss-components/styles/flags.less b/packages/oss-components/styles/flags.less index 308f76b99..c17bac8f5 100644 --- a/packages/oss-components/styles/flags.less +++ b/packages/oss-components/styles/flags.less @@ -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; diff --git a/packages/oss-components/styles/molecules/upload-area.less b/packages/oss-components/styles/molecules/upload-area.less index f660bbd4b..4c51b9598 100644 --- a/packages/oss-components/styles/molecules/upload-area.less +++ b/packages/oss-components/styles/molecules/upload-area.less @@ -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; @@ -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; diff --git a/packages/oss-components/styles/organisms/marketing-banner.less b/packages/oss-components/styles/organisms/marketing-banner.less index 8461590fc..60b4df14c 100644 --- a/packages/oss-components/styles/organisms/marketing-banner.less +++ b/packages/oss-components/styles/organisms/marketing-banner.less @@ -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'); } } diff --git a/packages/oss-components/tests/build/vite-asset-map-test.mjs b/packages/oss-components/tests/build/vite-asset-map-test.mjs index 7035ada7b..da95c5c91 100644 --- a/packages/oss-components/tests/build/vite-asset-map-test.mjs +++ b/packages/oss-components/tests/build/vite-asset-map-test.mjs @@ -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'; @@ -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'] }) diff --git a/packages/test-app/tests/integration/components/o-s-s/avatar-test.ts b/packages/test-app/tests/integration/components/o-s-s/avatar-test.ts index cc2c1b238..c2ee2e872 100644 --- a/packages/test-app/tests/integration/components/o-s-s/avatar-test.ts +++ b/packages/test-app/tests/integration/components/o-s-s/avatar-test.ts @@ -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'; @@ -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``); + 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) { @@ -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``); 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); }); }); }); diff --git a/packages/test-app/tests/integration/components/o-s-s/smart/blob-test.ts b/packages/test-app/tests/integration/components/o-s-s/smart/blob-test.ts index c16b895dc..b01fc58bf 100644 --- a/packages/test-app/tests/integration/components/o-s-s/smart/blob-test.ts +++ b/packages/test-app/tests/integration/components/o-s-s/smart/blob-test.ts @@ -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'); @@ -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'); diff --git a/packages/test-app/tests/integration/components/o-s-s/smart/immersive/logo-test.ts b/packages/test-app/tests/integration/components/o-s-s/smart/immersive/logo-test.ts index 33265f119..ec6f88aa0 100644 --- a/packages/test-app/tests/integration/components/o-s-s/smart/immersive/logo-test.ts +++ b/packages/test-app/tests/integration/components/o-s-s/smart/immersive/logo-test.ts @@ -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'; @@ -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', () => {