This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Rebundle when routes/templates change
- Loading branch information
Showing
9 changed files
with
267 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
const glob = require('glob'); | ||
const chokidar = require('chokidar'); | ||
const create_routes = require('./utils/create_routes.js'); | ||
const { src } = require('./config.js'); | ||
const { src, dev } = require('./config.js'); | ||
|
||
const route_manager = { | ||
routes: create_routes( | ||
glob.sync('**/*.+(html|js|mjs)', { cwd: src }) | ||
), | ||
const callbacks = []; | ||
|
||
onchange(fn) { | ||
// TODO in dev mode, keep this updated, and allow | ||
// webpack compiler etc to hook into it | ||
} | ||
exports.onchange = fn => { | ||
callbacks.push(fn); | ||
}; | ||
|
||
module.exports = route_manager; | ||
function update() { | ||
exports.routes = create_routes( | ||
glob.sync('**/*.+(html|js|mjs)', { cwd: src }) | ||
); | ||
|
||
callbacks.forEach(fn => fn()); | ||
} | ||
|
||
update(); | ||
|
||
if (dev) { | ||
const watcher = chokidar.watch(`${src}/**/*.+(html|js|mjs)`, { | ||
ignoreInitial: true | ||
}); | ||
|
||
watcher.on('add', update); | ||
watcher.on('change', update); | ||
watcher.on('unlink', update); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,56 @@ | ||
const fs = require('fs'); | ||
const glob = require('glob'); | ||
const chokidar = require('chokidar'); | ||
const { dev } = require('./config.js'); | ||
|
||
const templates = glob.sync('*.html', { cwd: 'templates' }) | ||
.map(file => { | ||
const template = fs.readFileSync(`templates/${file}`, 'utf-8'); | ||
const status = file.replace('.html', '').toLowerCase(); | ||
|
||
if (!/^[0-9x]{3}$/.test(status)) { | ||
throw new Error(`Bad template — should be a valid status code like 404.html, or a wildcard like 2xx.html`); | ||
} | ||
|
||
const specificity = ( | ||
(status[0] === 'x' ? 0 : 4) + | ||
(status[1] === 'x' ? 0 : 2) + | ||
(status[2] === 'x' ? 0 : 1) | ||
); | ||
|
||
const pattern = new RegExp(`^${status.split('').map(d => d === 'x' ? '\\d' : d).join('')}$`); | ||
|
||
return { | ||
test: status => pattern.test(status), | ||
specificity, | ||
render(data) { | ||
return template.replace(/%sapper\.(\w+)%/g, (match, key) => { | ||
return key in data ? data[key] : ''; | ||
}); | ||
let templates; | ||
|
||
function create_templates() { | ||
templates = glob.sync('*.html', { cwd: 'templates' }) | ||
.map(file => { | ||
const template = fs.readFileSync(`templates/${file}`, 'utf-8'); | ||
const status = file.replace('.html', '').toLowerCase(); | ||
|
||
if (!/^[0-9x]{3}$/.test(status)) { | ||
throw new Error(`Bad template — should be a valid status code like 404.html, or a wildcard like 2xx.html`); | ||
} | ||
|
||
const specificity = ( | ||
(status[0] === 'x' ? 0 : 4) + | ||
(status[1] === 'x' ? 0 : 2) + | ||
(status[2] === 'x' ? 0 : 1) | ||
); | ||
|
||
const pattern = new RegExp(`^${status.split('').map(d => d === 'x' ? '\\d' : d).join('')}$`); | ||
|
||
return { | ||
test: status => pattern.test(status), | ||
specificity, | ||
render(data) { | ||
return template.replace(/%sapper\.(\w+)%/g, (match, key) => { | ||
return key in data ? data[key] : ''; | ||
}); | ||
} | ||
} | ||
} | ||
}) | ||
.sort((a, b) => b.specificity - a.specificity); | ||
}) | ||
.sort((a, b) => b.specificity - a.specificity); | ||
} | ||
|
||
create_templates(); | ||
|
||
if (dev) { | ||
const watcher = chokidar.watch('templates/**.html', { | ||
ignoreInitial: true | ||
}); | ||
|
||
watcher.on('add', create_templates); | ||
watcher.on('change', create_templates); | ||
watcher.on('unlink', create_templates); | ||
} | ||
|
||
exports.render = (status, data) => { | ||
const template = templates.find(template => template.test(status)); | ||
if (template) return template.render(data); | ||
|
||
return `Missing template for status code ${status}`; | ||
}; | ||
|
||
exports.onchange = fn => { | ||
// TODO in dev mode, keep this updated, and allow | ||
// webpack compiler etc to hook into it | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.