coralite-scripts 0.42.0 → 0.43.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/libs/server.js +91 -79
  2. package/package.json +2 -2
package/libs/server.js CHANGED
@@ -135,6 +135,7 @@ async function server (config, options, runMode = 'dev') {
135
135
  const clients = new Set()
136
136
  const pageCache = new Map()
137
137
  const memoryPageSource = new Map()
138
+ let compilePromise = Promise.resolve()
138
139
 
139
140
  // start coralite
140
141
  let coralite
@@ -377,100 +378,111 @@ async function server (config, options, runMode = 'dev') {
377
378
  return
378
379
  }
379
380
 
380
- try {
381
- const start = process.hrtime()
382
- let duration, dash = colours.gray(' ─ ')
383
-
384
- let rebuildScript = '</body>\n'
385
-
386
- if (runMode !== 'test') {
387
- rebuildScript = '\n<script>\n'
388
- rebuildScript += " const eventSource = new EventSource('/_/rebuild');\n"
389
- rebuildScript += ' eventSource.onmessage = function(event) {\n'
390
- rebuildScript += " if (event.data === 'connected') return;\n"
391
- rebuildScript += ' // Reload page when file changes\n'
392
- rebuildScript += ' location.reload()\n'
393
- rebuildScript += ' }\n'
394
- rebuildScript += ' </script>\n'
395
- rebuildScript += '</body>\n'
396
- }
381
+ await new Promise((resolve) => {
382
+ compilePromise = compilePromise.then(async () => {
383
+ try {
384
+ if (pageCache.has(cacheKey)) {
385
+ res.send(pageCache.get(cacheKey))
386
+ return
387
+ }
397
388
 
398
- // Only set item if it's not already in the collection (virtual pages are pre-registered)
399
- const item = coralite.pages.getItem(pathname)
389
+ const start = process.hrtime()
390
+ let duration, dash = colours.gray(' ─ ')
391
+
392
+ let rebuildScript = '</body>\n'
393
+
394
+ if (runMode !== 'test') {
395
+ rebuildScript = '\n<script>\n'
396
+ rebuildScript += " const eventSource = new EventSource('/_/rebuild');\n"
397
+ rebuildScript += ' eventSource.onmessage = function(event) {\n'
398
+ rebuildScript += " if (event.data === 'connected') return;\n"
399
+ rebuildScript += ' // Reload page when file changes\n'
400
+ rebuildScript += ' location.reload()\n'
401
+ rebuildScript += ' }\n'
402
+ rebuildScript += ' </script>\n'
403
+ rebuildScript += '</body>\n'
404
+ }
400
405
 
401
- if (!item || item.virtual !== true) {
402
- await coralite.pages.setItem(pathname)
403
- }
406
+ // Only set item if it's not already in the collection (virtual pages are pre-registered)
407
+ const item = coralite.pages.getItem(pathname)
404
408
 
405
- // build the HTML for this page using the built-in compiler.
406
- const documents = await coralite.build(pathname, async (result) => {
407
- // inject a script to enable live reload via Server-Sent Events
408
- const injectedHtml = result.content.replace(/<\/body>/i, rebuildScript)
409
+ if (!item || item.virtual !== true) {
410
+ await coralite.pages.setItem(pathname)
411
+ }
409
412
 
410
- const relPath = relative(config.pages, result.path.pathname)
411
- const normalizedKey = relPath.split(sep).join('/')
413
+ // build the HTML for this page using the built-in compiler.
414
+ const documents = await coralite.build(pathname, async (result) => {
415
+ // inject a script to enable live reload via Server-Sent Events
416
+ const injectedHtml = result.content.replace(/<\/body>/i, rebuildScript)
412
417
 
413
- // map in memory page to source
414
- if (normalizedKey !== pathname) {
415
- memoryPageSource.set(normalizedKey, pathname)
416
- }
418
+ const relPath = relative(config.pages, result.path.pathname)
419
+ const normalizedKey = relPath.split(sep).join('/')
417
420
 
418
- // only cache pages that were out of scope of the initial page request
419
- if (normalizedKey !== cacheKey) {
420
- pageCache.set(normalizedKey, injectedHtml)
421
- }
421
+ // map in memory page to source
422
+ if (normalizedKey !== pathname) {
423
+ memoryPageSource.set(normalizedKey, pathname)
424
+ }
422
425
 
423
- return {
424
- path: result.path,
425
- content: injectedHtml,
426
- duration: result.duration
427
- }
428
- })
426
+ // only cache pages that were out of scope of the initial page request
427
+ if (normalizedKey !== cacheKey) {
428
+ pageCache.set(normalizedKey, injectedHtml)
429
+ }
429
430
 
430
- // Write ESM script assets generated during the build phase
431
- if (coralite.outputFiles) {
432
- const assetsDir = join(config.output, 'assets', 'js')
431
+ return {
432
+ path: result.path,
433
+ content: injectedHtml,
434
+ duration: result.duration
435
+ }
436
+ })
433
437
 
434
- if (!existsSync(assetsDir)) {
435
- await mkdir(assetsDir, { recursive: true })
436
- }
438
+ // Write ESM script assets generated during the build phase
439
+ if (coralite.outputFiles) {
440
+ const assetsDir = join(config.output, 'assets', 'js')
437
441
 
438
- const assetWrites = Object.values(coralite.outputFiles).map(async (file) => {
439
- const outFile = join(assetsDir, file.hashedPath)
440
- await writeFile(outFile, file.text)
441
- })
442
+ if (!existsSync(assetsDir)) {
443
+ await mkdir(assetsDir, { recursive: true })
444
+ }
442
445
 
443
- await Promise.all(assetWrites)
444
- }
446
+ const assetWrites = Object.values(coralite.outputFiles).map(async (file) => {
447
+ const outFile = join(assetsDir, file.hashedPath)
448
+ await writeFile(outFile, file.text)
449
+ })
445
450
 
446
- // prints time and path to the file that has been changed or added.
447
- duration = process.hrtime(start)
448
- process.stdout.write(toTime() + colours.bgGreen(' Compiled HTML ') + dash + toMS(duration) + dash + '/' + cacheKey + '\n')
451
+ await Promise.all(assetWrites)
452
+ }
449
453
 
450
- // find the document that matches the request path
451
- const doc = documents.find(doc => {
452
- if (!doc) {
453
- return false
454
+ // prints time and path to the file that has been changed or added.
455
+ duration = process.hrtime(start)
456
+ process.stdout.write(toTime() + colours.bgGreen(' Compiled HTML ') + dash + toMS(duration) + dash + '/' + cacheKey + '\n')
457
+
458
+ // find the document that matches the request path
459
+ const doc = documents.find(doc => {
460
+ if (!doc) {
461
+ return false
462
+ }
463
+ const relPath = relative(config.pages, doc.path.pathname)
464
+ const normalizedKey = relPath.split(sep).join('/')
465
+ return normalizedKey === cacheKey
466
+ })
467
+
468
+ if (doc) {
469
+ pageCache.set(cacheKey, doc.content)
470
+ res.send(doc.content)
471
+ } else {
472
+ res.sendStatus(404)
473
+ }
474
+ } catch (error) {
475
+ // If headers haven't been sent, send 500
476
+ if (!res.headersSent) {
477
+ res.status(500).send(error.message)
478
+ }
479
+ displayError('Request processing failed', error)
480
+ } finally {
481
+ resolve()
454
482
  }
455
- const relPath = relative(config.pages, doc.path.pathname)
456
- const normalizedKey = relPath.split(sep).join('/')
457
- return normalizedKey === cacheKey
458
483
  })
459
-
460
- if (doc) {
461
- res.send(doc.content)
462
- } else {
463
- res.sendStatus(404)
464
- }
465
- } catch (error) {
466
- // If headers haven't been sent, send 500
467
- if (!res.headersSent) {
468
- res.status(500).send(error.message)
469
- }
470
- displayError('Request processing failed', error)
471
- }
472
- }
473
- )
484
+ })
485
+ })
474
486
 
475
487
  if (runMode !== 'test') {
476
488
  // watch for file changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coralite-scripts",
3
- "version": "0.42.0",
3
+ "version": "0.43.1",
4
4
  "description": "Configuration and scripts for Create Coralite.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -61,7 +61,7 @@
61
61
  "portfinder": "^1.0.38",
62
62
  "postcss": "^8.5.15",
63
63
  "sass": "^1.101.0",
64
- "coralite": "0.42.0"
64
+ "coralite": "0.43.1"
65
65
  },
66
66
  "scripts": {
67
67
  "build": "premove dist && pnpm build:types",