Skip to content

Commit

Permalink
Always use base64 for inlined SVG [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Feb 29, 2024
1 parent 4d3a553 commit 966b26b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ jobs:
runs-on: ubuntu-latest
if: ${{ contains(github.event.head_commit.message, '[publish]') }}
steps:
- uses: actions/checkout@v3
- uses: xhyrom/setup-bun@v1
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.25 # https://github.com/oven-sh/bun/issues/8759
- run: bun install
- run: bun ci
- uses: ArnaudBarre/npm-publish@v1
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.7.4

- Always use base64 for inlined SVG to avoid issues when used inside `url()`

## 0.7.3

- Fix multiple issues with assets handling:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@arnaud-barre/rds",
"private": true,
"type": "module",
"version": "0.7.3",
"version": "0.7.4",
"license": "MIT",
"scripts": {
"dev": "scripts/bundle.ts --dev",
Expand Down
7 changes: 7 additions & 0 deletions src/server/assets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { readFileSync } from "node:fs";
import { cache } from "./cache.ts";
import { type Extension, mimeTypes } from "./mimeTypes.ts";
import { getExtension } from "./utils.ts";

export const assetsCache = cache("assets", (url) => readFileSync(url));

export const toBase64Url = (resolveUrl: string, content: Buffer) => {
const mimeType = mimeTypes[getExtension(resolveUrl) as Extension];
return `data:${mimeType};base64,${content.toString("base64")}`;
};
15 changes: 11 additions & 4 deletions src/server/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { join } from "node:path";
import { logEsbuildErrors } from "@arnaud-barre/config-loader";
import { downwind } from "@arnaud-barre/downwind/esbuild";
import { build, type BuildResult, type Metafile } from "esbuild";
import { toBase64Url } from "./assets.ts";
import { colors } from "./colors.ts";
import { commandWrapper } from "./commandWrapper.ts";
import { ENTRY_POINT } from "./consts.ts";
import { esbuildFilesLoaders } from "./mimeTypes.ts";
import { stopProfiler } from "./stopProfiler.ts";
import { svgToJS } from "./svgToJS.ts";
import { isCSS, readFile } from "./utils.ts";
import { isCSS } from "./utils.ts";

export const main = commandWrapper(async (config) => {
if (config.build.emptyOutDir) {
Expand Down Expand Up @@ -52,15 +53,21 @@ export const main = commandWrapper(async (config) => {
};
});
pluginBuild.onLoad({ filter: /\.svg$/u }, (args) => {
const contents = readFile(args.path);
const contents = readFileSync(args.path);
if (args.namespace === "inline") {
return { loader: "dataurl", contents };
return {
loader: "js",
contents: `export default "${toBase64Url(
args.path,
contents,
)}"`,
};
}
if (args.namespace === "url") return { loader: "file", contents };
return {
loader: "js",
contents: svgToJS(
contents,
contents.toString(),
'import { jsx } from "react/jsx-runtime";',
"jsx",
'import { forwardRef } from "react";',
Expand Down
17 changes: 3 additions & 14 deletions src/server/importsTransform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assetsCache } from "./assets.ts";
import { assetsCache, toBase64Url } from "./assets.ts";
import { cache } from "./cache.ts";
import {
DEPENDENCY_PREFIX,
Expand All @@ -10,17 +10,9 @@ import { dependenciesCache, getDependencyMetadata } from "./dependencies.ts";
import type { Downwind } from "./downwind.ts";
import { RDSError } from "./errors.ts";
import { jsonCache } from "./json.ts";
import { type Extension, mimeTypes } from "./mimeTypes.ts";
import type { Scanner } from "./scanner.ts";
import { svgCache } from "./svg.ts";
import {
getExtension,
getHashedUrl,
isInnerNode,
isJSON,
isSVG,
run,
} from "./utils.ts";
import { getHashedUrl, isInnerNode, isJSON, isSVG, run } from "./utils.ts";

export type ImportsTransform = ReturnType<typeof initImportsTransform>;

Expand Down Expand Up @@ -128,10 +120,7 @@ export const initImportsTransform = ({

const getAssetUrl = (raw: string, resolveUrl: string) => {
const content = assetsCache.get(resolveUrl);
if (raw.endsWith("?inline")) {
const mimeType = mimeTypes[getExtension(resolveUrl) as Extension];
return `data:${mimeType};base64,${content.toString("base64")}`;
}
if (raw.endsWith("?inline")) return toBase64Url(resolveUrl, content);
const hashed = getHashedUrl(`${FS_PREFIX}/${resolveUrl}`, content);
if (isSVG(resolveUrl) || isJSON(resolveUrl)) return `${hashed}&url`;
return hashed;
Expand Down

0 comments on commit 966b26b

Please sign in to comment.