This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
56 lines (56 loc) · 1.93 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSchemaCustomization = exports.createResolvers = void 0;
const tslib_1 = require("tslib");
const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
const mini_svg_data_uri_1 = (0, tslib_1.__importDefault)(require("mini-svg-data-uri"));
const svgo_1 = require("svgo");
async function parseSVG(path) {
try {
const svg = await fs_1.default.promises.readFile(path, "utf8");
const result = (0, svgo_1.optimize)(svg, { multipass: true });
const encoded = (0, mini_svg_data_uri_1.default)(result.data);
return { content: result.data, encoded, path };
}
catch (ex) {
console.error("error in parse svg", path, ex);
throw ex;
}
}
function createResolvers(args) {
const { createResolvers, reporter } = args;
reporter.verbose("Creating resolvers for svg");
const resolvers = {
File: {
svg: {
type: "ExtractedSvg",
async resolve(parent) {
const name = parent.absolutePath.toLowerCase();
reporter.verbose(`Looking at ${name}`);
if (name.endsWith(".svg") || name.endsWith(".svg.xml")) {
return await parseSVG(parent.absolutePath);
}
else {
return null;
}
},
},
},
};
createResolvers(resolvers);
}
exports.createResolvers = createResolvers;
function createSchemaCustomization(args) {
const { reporter, actions: { createTypes }, } = args;
reporter.verbose("Create SVG customization");
const typeDefs = `
type ExtractedSvg implements Node {
content: String!
encoded: String!
path: String!
}
`;
createTypes(typeDefs);
}
exports.createSchemaCustomization = createSchemaCustomization;
//# sourceMappingURL=gatsby-node.js.map