Skip to content

Commit

Permalink
Support custom dir for svg sprite file
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzasse committed Feb 21, 2022
1 parent de7d1d6 commit 1d04ea3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
1 change: 1 addition & 0 deletions example/assets/icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"icons": {
"filetype": "svg",
"sprite": {
"writeIds": true
"writeIds": true,
"spriteDir": "assets"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/tokens/icons-ids.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* eslint-disable */
export const ids = ["featherAperture","featherArrowDownLeft","featherArrowLeftCircle","featherCamera","featherCheckCircle","featherCloudLightning","featherEye","featherHeart",];
export const ids = ["featherAperture","featherArrowDownLeft","featherArrowLeftCircle","featherCamera","featherCheckCircle","featherCloudLightning","featherEye","featherHeart",] as const;
29 changes: 0 additions & 29 deletions example/tokens/icons.svg

This file was deleted.

33 changes: 20 additions & 13 deletions src/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,22 @@ export default class Codegen {
const rgx = /<svg.*?>([\s\S]*)<\/svg>/i; // remove wrapping svg tag
const svgs = tokens.map(([k, v]) => [k, v.match(rgx)[1]]);

let spriteDir = outDir;
let writeIds = false;

if (typeof config.sprite === "object") {
writeIds = Boolean(config.sprite.writeIds);

if (typeof config.sprite.spriteDir === "string") {
spriteDir = config.sprite.spriteDir;
}
}

fs.writeFileSync(
`${outDir}/${filename}.svg`,
`${spriteDir}/${filename}.svg`,
spriteCompiled({ svgs })
);

const writeIds =
typeof config.sprite === "object" ? config.sprite.writeIds : false;

// Write ids so that we can more easily reference them in TS
if (writeIds) {
const idsCompiled = template(SVG_SPRITE_IDS_TEMPLATE, {});
Expand Down Expand Up @@ -134,15 +142,14 @@ const TOKEN_TEMPLATE =
"export const <%= x[0] %> = <%= JSON.stringify(x[1], null, 2) %>;\n" +
"<% }); %>";

const SVG_SPRITE_TEMPLATE = `/* eslint-disable */
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs><% svgs.forEach(function(x) { %>
<symbol viewBox="0 0 24 24" id="<%= x[0] %>">
<%= x[1] %>
</symbol><% }); %>
</defs>
</svg>
`;
const SVG_SPRITE_TEMPLATE =
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
"<defs><% svgs.forEach(function(x) { %>" +
'<symbol viewBox="0 0 24 24" id="<%= x[0] %>">' +
"<%= x[1] %>" +
"</symbol><% }); %>" +
"</defs>" +
"</svg>";

const SVG_SPRITE_IDS_TEMPLATE =
"/* eslint-disable */\n" +
Expand Down

0 comments on commit 1d04ea3

Please sign in to comment.