Skip to content

Commit

Permalink
v1.1.2 (#20)
Browse files Browse the repository at this point in the history
* Update index.ts

* v1.1.1

* Update README.md

* Update package.json

* v1.1.2
  • Loading branch information
JakiChen authored Nov 20, 2024
1 parent 6c06754 commit 09e77dd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-svgs",
"version": "1.1.1",
"version": "1.1.2",
"description": "A compact solution for SVG sprites in Astro projects. It automates symbol ID management, supports hot reloading, and generates optimized SVG sprites with minimal setup—ideal for seamless SVG icon integration.",
"type": "module",
"main": "./dist/index.mjs",
Expand Down Expand Up @@ -57,7 +57,7 @@
},
"homepage": "https://github.com/ACP-CODE/astro-svgs#readme",
"devDependencies": {
"astro": "^4.16.10",
"astro": "^4.16.13",
"prettier": "^3.3.3",
"unbuild": "^2.0.0",
"@astrojs/check": "^0.9.4",
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ export interface SVGsOptions {
*/
input?: string | string[];
/**
* @default "high"
* @example Dynamic setup based on environment
* ```ts
* compress: isDev ? 'beautify' : 'high',
* ```
* Compression level of `sprite.svg` file.
* @default
* isDev ? 'beautify' : 'high',
*/
compress?: Precision;
}
Expand Down
31 changes: 30 additions & 1 deletion src/modules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { InjectedType } from "astro";
import type { AstroConfig, InjectedType } from "astro";
import fs from "fs/promises";
import path from "path";
import { type SVGsOptions, name } from ".";
import { compose } from "./core";

Expand All @@ -16,3 +18,30 @@ export async function virtual(opts: SVGsOptions): Promise<InjectedType> {

return { filename, content };
}

export async function genTypeFile(
file: string,
opts: SVGsOptions,
cfg: AstroConfig,
): Promise<boolean> {
const inputs = Array.isArray(opts.input) ? opts.input : [opts.input];

if (
!inputs.some((input) => file.includes(input!)) ||
!file.endsWith(".svg")
) {
return false; // No treatment
}

const { filename, content } = await virtual(opts);
const typeFile = new URL(`.astro/integrations/${name}/${filename}`, cfg.root);

try {
await fs.mkdir(path.dirname(typeFile.pathname), { recursive: true });
await fs.writeFile(typeFile, content);
} catch (err) {
console.error(err);
}

return true;
}
53 changes: 28 additions & 25 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import type { AstroConfig } from "astro";
import type { Plugin } from "vite";
import fs from "fs/promises";
import path from "path";
import { type SVGsOptions, name } from ".";
import { compose } from "./core";
import { virtual } from "./modules";
import { genTypeFile } from "./modules";

export function create(options: SVGsOptions, config: AstroConfig): Plugin {
const virtualModuleId = `virtual:${name}`;
const resolvedVirtualModuleId = "\0" + virtualModuleId;

const base = "/@svgs/sprite.svg";
let fileId: string, data: string, hash: string, filePath: string;
let fileId: string, data: string, hash: string, filePath: string, typeFileUpdated: boolean;;
const inputs = Array.isArray(options.input) ? options.input : [options.input];

return {
name,
Expand All @@ -27,6 +26,14 @@ export function create(options: SVGsOptions, config: AstroConfig): Plugin {
next();
}
});

server.watcher.on("unlink", async (file) => {
typeFileUpdated = await genTypeFile(file, options, config);
});

server.watcher.on("add", async (file) => {
typeFileUpdated = await genTypeFile(file, options, config);
});
},

async buildStart() {
Expand Down Expand Up @@ -56,29 +63,25 @@ export function create(options: SVGsOptions, config: AstroConfig): Plugin {
},

async handleHotUpdate({ file, server }) {
const filePathDir = path.dirname(file);

if (
Array.isArray(options.input) &&
options.input.some((input) => filePathDir.includes(input))
) {
({ hash, data } = await compose(options)); // 更新 hash 和 data
filePath = `${base}?v=${hash}`; // 更新 filePath

const { filename, content } = await virtual(options);
const typeFile = new URL(
`.astro/integrations/${name}/${filename}`,
config.root,
);
await fs.writeFile(typeFile, content);

const mod = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
if (mod) {
server.moduleGraph.invalidateModule(mod);
const wathFile = inputs.some((input) => file.includes(input!)) && file.endsWith(".svg");

if (wathFile || typeFileUpdated) {
const { data: Data, hash: Hash } = await compose(options);

if (Hash !== hash) {
hash = Hash;
data = Data;
filePath = `${base}?v=${hash}`;

const mod = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
if (mod) {
server.moduleGraph.invalidateModule(mod);
}

server.ws.send({ type: "full-reload" });
}

server.ws.send({ type: "full-reload" });
return [];
return []; // Returns null to avoid further processing
}
},
};
Expand Down

0 comments on commit 09e77dd

Please sign in to comment.