-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
543 lines (392 loc) · 12.6 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
const fs = require( 'fs' );
const glob = require( 'glob' );
const murmurhash = require( './murmur' );
const normalizePath = require( 'normalize-path' );
const parser = require( '@babel/parser' );
const path = require( 'path' );
const traverse = require( '@babel/traverse' ).default;
const { watch } = require( 'chokidar' );
const { runQuery } = require( './index' );
const { reporter } = require( './utils' );
const { groqDirectories } = require( './index' );
const { GROQ_DIR, ROOT } = groqDirectories;
/**
* Here's where we extract and run all initial queries.
* Also sets up a watcher to re-run queries during dev when files change.fcache
*
* Runs in right after schema creation and before createPages.
*/
exports.resolvableExtensions = async ( { graphql, actions, cache, getNodes, traceId, store }, plugin ) => {
if( !! fs.existsSync( GROQ_DIR ) ) {
fs.rmdirSync( GROQ_DIR, { recursive: true } );
}
fs.mkdirSync( GROQ_DIR );
// Cache options (because we need them on frontend)
// Probably a more sophisticated Gatsby way of doing this.
if( !! plugin ) {
fs.writeFileSync( `${GROQ_DIR}/options.json`, JSON.stringify( plugin ), err => {
if( err ) {
throw new Error( err );
}
} );
}
// Cache fragments.
const fragmentsDir = !! plugin.fragmentsDir ? path.join( ROOT, plugin.fragmentsDir ) : null;
if( !! fragmentsDir ) {
cacheFragments( fragmentsDir, cache );
}
// Extract initial queries.
const intitialNodes = getNodes();
extractQueries( { nodes: intitialNodes, traceId, cache } );
// For now watching all files to re-extract queries.
// Right now there doesn't seem to be a way to watch for build updates using Gatsby's public node apis.
// Created a ticket in github to explore option here.
const watcher = watch( `${ROOT}/src/**/*.js` );
watcher.on( 'change', async ( filePath ) => {
// Recache if this was a change within fragments directory.
if( !! fragmentsDir && filePath.includes( fragmentsDir ) ) {
await cacheFragments( fragmentsDir, cache );
//TODO need to figure out a way to refresh page with new data.
reporter.info( `DON'T FORGET TO RESAVE FILES WITH UPDATED FRAGMENT!` );
}
// Get info for file that was changed.
const fileContents = fs.readFileSync( filePath, 'utf8' );
// Check if file has either page or static queries.
const pageQueryMatch = /export const groqQuery = /.exec( fileContents );
const staticQueryMatch = /useGroqQuery/.exec( fileContents );
if( ! pageQueryMatch && ! staticQueryMatch ) {
return;
}
reporter.info( 'Re-processing groq queries...' );
// Get updated nodes to query against.
const nodes = getNodes();
// Run page queries.
if( pageQueryMatch ) {
const { deletePage, createPage } = actions;
const { pages } = store.getState();
// First we need to reprocess the page query.
const processedPageQuery = await processFilePageQuery( filePath, nodes, cache );
if( !! processedPageQuery ) {
const { fileHash: newHash, query: newQuery } = processedPageQuery;
const queryFile = `${GROQ_DIR}/${newHash}.json`;
await cacheQueryResults( newHash, newQuery );
// Update all paths using this page component.
// Is this performant or should we try to leverage custom cache?
for( let [ path, page ] of pages ) {
if( page.component !== filePath ) {
continue;
}
reporter.info( `Updating path: ${page.path}` );
// Run query and inject into page context.
pageQueryToContext( {
actions,
cache,
file: queryFile,
nodes,
page
} );
}
}
}
// Static queries.
if( ! staticQueryMatch ) {
return reporter.success( 'Finished re-processing page queries' );
}
// Run query and save to cache.
// Files using the static query will be automatically refreshed.
const staticQueries = await processFileStaticQueries( filePath, nodes, cache );
if( !! ( staticQueries || [] ).length ) {
for( let { hash, json } of staticQueries ) {
if( ! hash || ! json ) {
return reporter.warn( 'There was a problem processing one of your static queries' );
}
cacheQueryResults( hash, json );
}
}
return reporter.success( 'Finished re-processing queries' );
} );
};
/**
* Inject page query results its page.
*/
exports.onCreatePage = async ( { actions, cache, getNodes, page, traceId } ) => {
// Check for hashed page queries for this component.
const componentPath = page.component;
const hash = murmurhash( componentPath );
const queryFile = `${GROQ_DIR}/${hash}.json`;
if( ! fs.existsSync( queryFile) ) {
return;
}
// Run query and write to page context.
pageQueryToContext( {
actions,
cache,
file: queryFile,
getNodes,
page,
} );
}
/**
* Extract page and static queries from all files.
* Process and cache results.
*
* @param {Object} $0 Gatsby Node Helpers.
*/
async function extractQueries( { nodes, traceId, cache } ) {
reporter.info( 'Getting files for groq extraction...' );
const filesRegex = `*.+(t|j)s?(x)`
const pathRegex = `/{${filesRegex},!(node_modules)/**/${filesRegex}}`;
let hasErrors = false;
let files = [
path.join( ROOT, 'src' ),
].reduce( ( merged, folderPath ) => {
merged.push(
...glob.sync( path.join( folderPath, pathRegex ), {
nodir: true,
} )
);
return merged;
}, [] );
files = files.filter( d => ! d.match( /\.d\.ts$/ ) );
files = files.map( normalizePath );
// Loop through files and look for queries to extract.
for( let file of files ) {
const pageQuery = await processFilePageQuery( file, nodes, cache );
const staticQueries = await processFileStaticQueries( file, nodes, cache );
// Cache page query.
// This will only contain a json file of unprocessed query.
if( !! pageQuery ) {
const { fileHash, query } = pageQuery;
cacheQueryResults( fileHash, query );
}
// Cache static queries.
// These will contain actual results of the query.
if( !! ( staticQueries || [] ).length ) {
for( let { hash, json } of staticQueries ) {
if( ! hash || ! json ) {
return reporter.warn( 'There was an error processing static query' );
}
cacheQueryResults( hash, json, 'static', );
}
}
}
reporter.success( 'Finished getting files for query extraction' );
}
/**
* Cache fragments.
*
* @param {string} fragmentsDir
* @param {Object} cache
* @return {bool} if succesfully cached.
*/
async function cacheFragments( fragmentsDir, cache ) {
const index = path.join( fragmentsDir, 'index.js' );
if( !! fs.readFileSync( index ) ) {
delete require.cache[ require.resolve( index ) ];
fragments = require( index );
await cache.set( 'groq-fragments', fragments );
reporter.info( 'Cached fragments' );
return true;
}
return false;
}
/**
* Run page query and update the related page via createPage.
*
* @param {Object} $0 Gatsby Node Helpers.
*/
async function pageQueryToContext( { actions, cache, file, getNodes, nodes, page, } ) {
const { createPage, deletePage, setPageData } = actions;
// Get query content.
const content = fs.readFileSync( file, 'utf8' );
let { unprocessed: query } = JSON.parse( content );
// Replace any variables within query with context values.
if( page.context ){
for( let [ key, value ] of Object.entries( page.context ) ) {
const search = `\\$${key}`;
const pattern = new RegExp( search, 'g' );
query = query.replace( pattern, `"${value}"` );
}
}
// Do the thing.
try {
const allNodes = nodes || getNodes();
const fragments = await cache.get( 'groq-fragments' );
const { result } = await runQuery( query, allNodes, { file, fragments } );
page.context.data = result;
deletePage( page );
createPage( {
...page,
} );
}
catch( err ) {
console.error( page.component );
reporter.error( `${err}` );
reporter.error( query );
}
}
/**
* Custom webpack.
*/
exports.onCreateWebpackConfig = async ( { actions, cache, plugins, store } ) => {
// Make sure we have have access to GROQ_DIR for useGroqQuery().
actions.setWebpackConfig( {
plugins: [
plugins.define( {
'process.env.GROQ_DIR': JSON.stringify( GROQ_DIR ),
} )
]
} );
}
/**
* Extracts page query from file and returns its hash and unprocessed string.
*
* @param {string} file
* @param {map} nodes
* @param {Object} cache
* @return {Object} fileHash and query
*/
async function processFilePageQuery( file, nodes, cache ) {
const contents = fs.readFileSync( file, 'utf8' );
const match = /export const groqQuery = /.exec( contents );
if( ! match ) {
return;
}
const ast = parse( file, contents );
if( ! ast ) {
return null;
}
let pageQuery = null;
let queryStart = null;
let queryEnd = null;
traverse( ast, {
ExportNamedDeclaration: function( path ) {
const declarator = path.node.declaration.declarations[0];
if( declarator.id.name === 'groqQuery' ) {
queryStart = declarator.init.start;
queryEnd = declarator.init.end;
pageQuery = contents.substring( queryStart, queryEnd );
}
}
} );
if( ! pageQuery ) {
return;
}
const hash = hashQuery( file );
return {
fileHash: hash,
query: JSON.stringify( { unprocessed: pageQuery } ),
}
}
/**
* Extracts static query from file and returns its hash and result.
*
* @param {string} file
* @param {map} nodes
* @param {Object} cache
* @return {array}
*/
async function processFileStaticQueries( file, nodes, cache ) {
const contents = fs.readFileSync( file, 'utf8' );
const match = /useGroqQuery/.exec( contents );
if( ! match ) {
return;
}
const ast = parse( file, contents );
if( ! ast ) {
return null;
}
const staticQueries = [];
let queryStart = null;
let queryEnd = null;
traverse( ast, {
CallExpression: function( path ) {
if( !! path.node.callee && path.node.callee.name === 'useGroqQuery' ) {
queryStart = path.node.arguments[0].start + 1;
queryEnd = path.node.arguments[0].end - 1;
staticQueries.push( contents.substring( queryStart, queryEnd ) );
}
}
} );
if( ! staticQueries.length ) {
return null;
}
const fragments = await cache.get( 'groq-fragments' );
const results = [];
for( let staticQuery of staticQueries ) {
const { result, finalQuery, queryToHash } = await runQuery( staticQuery, nodes, { file, fragments } );
if( result instanceof Error ) {
results.push( result );
}
const hash = hashQuery( queryToHash );
const json = JSON.stringify( result );
results.push( { hash, json } );
}
return results;
}
/**
* Cache result from query extraction.
* For page queries this caches the query itself.
* For static queries this caches the results of the query.
*
* @param {number} hash Hash to the json file.
* @param {Object|string} data Data we are caching.
* @param {string} type Page or static query. Optional. Default 'page'
*/
async function cacheQueryResults( hash, data, type = 'page' ) {
reporter.info( `Caching ${type} query: ${hash}` );
const json = typeof data !== 'string' ? JSON.stringify( data ) : data;
if( process.env.NODE_ENV === 'development' ) {
// Probably a more sophisticated Gatsby way of doing this.
fs.writeFileSync( `${GROQ_DIR}/${hash}.json`, json, err => {
if( err ) {
throw new Error( err );
}
} );
}
else {
fs.writeFileSync( `${GROQ_DIR}/${hash}.json`, json, err => {
if( err ) {
throw new Error( err );
}
} );
}
}
/**
* Parse.
*
* @param {string} filePath
* @param {string} content
* @param {Object} options
* @return {ast}
*/
function parse( filePath, content, options = {} ) {
const { plugins: additionalPlugins, ...additionalOptions } = options;
let plugins = [ 'jsx' ];
if( !! additionalPlugins ) {
plugins = [ ...plugins, ...additionalPlugins ];
}
try {
const ast = parser.parse( content, {
errorRecovery: true,
plugins,
sourceType: 'module',
...additionalOptions,
} );
return ast;
}
catch( err ) {
console.warn( `Error parsing file: ${filePath}` );
console.log( 'Check below for more info' );
return null;
}
}
/**
* Generate a hash based on the query.
*
* @param {string} query
* @return {number}
*/
function hashQuery( query ) {
return murmurhash( query );
}