Skip to content

Commit 3583f8d

Browse files
committed
Ignore the assetSizeLimit for spritesheets
1 parent 55810ea commit 3583f8d

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-icons-spritesheet",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Vite plugin that generates a spritesheet and types out of your icons folder.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",
@@ -14,7 +14,6 @@
1414
"react",
1515
"angular",
1616
"vue",
17-
"nextjs",
1817
"remix"
1918
],
2019
"exports": {

src/index.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ async function generateSvgSprite({
173173
svg.removeAttribute("width");
174174
svg.removeAttribute("height");
175175
return svg.toString().trim();
176-
})
176+
}),
177177
);
178178
const output = [
179179
'<?xml version="1.0" encoding="UTF-8"?>',
@@ -188,7 +188,7 @@ async function generateSvgSprite({
188188
return writeIfChanged(
189189
outputPath,
190190
formattedOutput,
191-
`🖼️ Generated SVG spritesheet in ${chalk.green(outputDirRelative)}`
191+
`🖼️ Generated SVG spritesheet in ${chalk.green(outputDirRelative)}`,
192192
);
193193
}
194194

@@ -212,7 +212,7 @@ async function lintFileContent(
212212
fileContent: string,
213213
formatter: Formatter | undefined,
214214
pathToFormatterConfig: string | undefined,
215-
typeOfFile: "ts" | "svg"
215+
typeOfFile: "ts" | "svg",
216216
) {
217217
if (!formatter) {
218218
return fileContent;
@@ -260,7 +260,7 @@ async function generateTypes({
260260
const file = await writeIfChanged(
261261
outputPath,
262262
formattedOutput,
263-
`${chalk.blueBright("TS")} Generated icon types in ${chalk.green(outputPath)}`
263+
`${chalk.blueBright("TS")} Generated icon types in ${chalk.green(outputPath)}`,
264264
);
265265
return file;
266266
}
@@ -286,7 +286,7 @@ async function writeIfChanged(filepath: string, newContent: string, message: str
286286
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
287287
export const iconsSpritesheet: (args: PluginProps | PluginProps[]) => any = (maybeConfigs) => {
288288
const configs = Array.isArray(maybeConfigs) ? maybeConfigs : [maybeConfigs];
289-
289+
const allSpriteSheetNames = configs.map((config) => config.fileName ?? "sprite.svg");
290290
return configs.map((config, i) => {
291291
const {
292292
withTypes,
@@ -310,24 +310,53 @@ export const iconsSpritesheet: (args: PluginProps | PluginProps[]) => any = (may
310310
formatter,
311311
pathToFormatterConfig,
312312
});
313+
const workDir = cwd ?? process.cwd();
313314
return {
314315
name: `icon-spritesheet-generator${i > 0 ? i.toString() : ""}`,
315316
async buildStart() {
316317
await iconGenerator();
317318
},
318319
async watchChange(file, type) {
319-
const inputPath = normalizePath(path.join(cwd ?? process.cwd(), inputDir));
320+
const inputPath = normalizePath(path.join(workDir, inputDir));
320321
if (file.includes(inputPath) && file.endsWith(".svg") && ["create", "delete"].includes(type.event)) {
321322
await iconGenerator();
322323
}
323324
},
324325
async handleHotUpdate({ file }) {
325-
const inputPath = normalizePath(path.join(cwd ?? process.cwd(), inputDir));
326+
const inputPath = normalizePath(path.join(workDir, inputDir));
326327
if (file.includes(inputPath) && file.endsWith(".svg")) {
327328
await iconGenerator();
328329
}
329330
},
330-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
331-
} satisfies Plugin<any>;
331+
// Augment the config with our own assetsInlineLimit function, we want to do this only once
332+
async configResolved(config) {
333+
if (i === 0) {
334+
const subFunc =
335+
typeof config.build.assetsInlineLimit === "function" ? config.build.assetsInlineLimit : undefined;
336+
const limit = typeof config.build.assetsInlineLimit === "number" ? config.build.assetsInlineLimit : undefined;
337+
338+
config.build.assetsInlineLimit = (name, content) => {
339+
const isSpriteSheet = allSpriteSheetNames.some((spriteSheetName) => {
340+
return name === normalizePath(`${workDir}/${outputDir}/${spriteSheetName}`);
341+
});
342+
// Our spritesheet? Early return
343+
if (isSpriteSheet) {
344+
return false;
345+
}
346+
// User defined limit? Check if it's smaller than the limit
347+
if (limit) {
348+
const size = content.byteLength;
349+
return size <= limit;
350+
}
351+
// User defined function? Run it
352+
if (typeof subFunc === "function") {
353+
return subFunc(name, content);
354+
}
355+
356+
return undefined;
357+
};
358+
}
359+
},
360+
} satisfies Plugin<unknown>;
332361
});
333362
};

test-apps/remix-vite/vite.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ export default defineConfig({
1515
inputDir: "icons",
1616
outputDir: "./app/icons",
1717
formatter: "prettier",
18-
// pathToFormatterConfig: "./biome.json",
18+
// pathToFormatterConfig: "./biome.json",
1919
},
2020
{
2121
withTypes: true,
2222
inputDir: "icons",
2323
outputDir: "./public/icons",
2424
formatter: "biome",
25-
// pathToFormatterConfig: "./biome.json",
26-
}
25+
// pathToFormatterConfig: "./biome.json",
26+
},
2727
]),
2828
],
2929
build: {
30-
assetsInlineLimit: 0,
30+
assetsInlineLimit: 1024 * 1024 * 10,
3131
},
3232
});

0 commit comments

Comments
 (0)