This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
125 lines (111 loc) · 3.62 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
require('dotenv').config();
require('source-map-support').install();
const { optimize, extendDefaultPlugins } = require('svgo');
const sites = require('./build-utils/sites');
const SANITY_PROJECT_ID = '8ux9tyb9';
const SANITY_DATASET = process.env.DATASET;
require('ts-node').register({
compilerOptions: {
module: 'commonjs',
target: 'es2017',
},
});
const pageCreator = require('./build-utils/create-pages');
const createPagesForSite = async (site, onlyPublicPages, { graphql, actions }) => {
await pageCreator.createFrontpage(
site,
{ graphql, actions },
onlyPublicPages,
'./src/templates/frontpagePageTemplate.tsx'
);
await pageCreator.createPages(
'allSanityYtelsePage',
site,
onlyPublicPages,
{ graphql, actions },
'./src/templates/ytelsePageTemplate.tsx'
);
await pageCreator.createPages(
'allSanityCustomPage',
site,
onlyPublicPages,
{ graphql, actions },
'./src/templates/customPageTemplate.tsx'
);
await pageCreator.createPages(
'allSanitySectionPage',
site,
onlyPublicPages,
{ graphql, actions },
'./src/templates/sectionPageTemplate.tsx'
);
};
exports.createPages = async (tools) => {
const onlyPublicPages = process.env.ENV !== 'dev';
console.log('Use only public pages:', onlyPublicPages);
await createPagesForSite(sites.privatperson, onlyPublicPages, tools);
await createPagesForSite(sites.arbeidsgiver, onlyPublicPages, tools);
await createPagesForSite(sites.samarbeid, onlyPublicPages, tools);
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
externals: {
canvas: 'commonjs canvas',
},
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes, createFieldExtension } = actions;
const typeDefs = `
type pdfLink implements Node @childOf(types: ["SanityPdf"]) {
id: ID!
}
type optimizedSvg implements Node @childOf(types: ["SanityIllustration"]) {
id: ID!
}
`;
createTypes(typeDefs);
};
const getFileUrlFromRef = (ref) => {
const [_file, id, extension] = ref.split('-');
return `https://cdn.sanity.io/files/${SANITY_PROJECT_ID}/${SANITY_DATASET}/${id}.${extension}`;
};
async function onCreateNode({ node, actions, createNodeId, createContentDigest }) {
if (node.internal.type === 'SanityPdf' && node.file) {
const pdfNode = {
id: createNodeId(`pdf-${node.id}`),
url: getFileUrlFromRef(node.file.asset._ref),
children: [],
parent: node.id,
internal: {
contentDigest: createContentDigest({}),
type: 'pdfLink',
},
};
const { createNode, createParentChildLink } = actions;
createNode(pdfNode);
createParentChildLink({ parent: node, child: pdfNode });
return;
}
if (node.internal.type !== 'SanityIllustration') {
return;
}
if (node.svg) {
const optimizedSvg = await optimize(node.svg);
const svgNode = {
id: createNodeId(`svgo-${node.id}`),
svg: optimizedSvg.data,
children: [],
parent: node.id,
internal: {
contentDigest: createContentDigest({}),
type: 'optimizedSvg',
},
};
const { createNode, createParentChildLink } = actions;
createNode(svgNode);
createParentChildLink({ parent: node, child: svgNode });
}
}
exports.onCreateNode = onCreateNode;