Skip to content

Commit

Permalink
Merge pull request #14 from beforesemicolon/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ECorreia45 authored Jul 6, 2021
2 parents d6f8e97 + 30d02ab commit 6c89b2d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 92 deletions.
12 changes: 8 additions & 4 deletions core/engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ const engine = (app, pagesDirectoryPath, opt = defaultOptions) => {
} else {
filePath = filePath.replace(pagesDirectoryPath, '');
const template = `${filePath.replace('.html', '')}`.slice(1);
let tempPath = '';

if (filePath.endsWith('index.html')) {
pagesRoutes[filePath.replace('index.html', '')] = template;
tempPath = filePath.replace('/index.html', '');
pagesRoutes[tempPath || '/'] = template;
pagesRoutes[`${tempPath}/`] = template;
pagesRoutes[`${tempPath}/index.html`] = template;
} else {
pagesRoutes[filePath.replace('.html', '')] = template;
tempPath = filePath.replace('.html', '');
pagesRoutes[tempPath] = template;
pagesRoutes[`${tempPath}/`] = template;
}

pagesRoutes[filePath.replace(/\/$/, '')] = template;
}
}

Expand Down
136 changes: 69 additions & 67 deletions core/engine/page-and-resources-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,82 +19,84 @@ const cache = {};

function pageAndResourcesMiddleware(pagesRoutes, pagesDirectoryPath, {env, onPageRequest}) {
return async (req, res, next) => {
const ext = path.extname(req.path);

if (ext && sourcesExtensions.has(ext)) {
let content = '';
let contentType = 'text/css';
let resourcePath = '';
let file = null;

if (/node_modules/.test(req.path)) {
resourcePath = path.join(process.cwd(), req.path);
} else {
resourcePath = path.join(pagesDirectoryPath, req.path);
}

if (env === 'production' && cache[resourcePath]) {
res.setHeader('Content-Type', cache[resourcePath].contentType);
return res.send(cache[resourcePath].content);
}
if (req.method === 'GET') {
const ext = path.extname(req.path);

file = new File(resourcePath, pagesDirectoryPath);

try {
switch (ext) {
case '.scss':
case '.sass':
content = await transformResource.sass({file});
content = (await transformResource.css(content, {file, env})).content;
break;
case '.less':
content = await transformResource.less({file});
content = (await transformResource.css(content, {file, env})).content;
break;
case '.styl':
content = await transformResource.stylus({file});
content = (await transformResource.css(content, {file, env})).content;
break;
case '.css':
content = (await transformResource.css({file, env})).content;
break;
case '.js':
case '.jsx':
case '.ts':
case '.tsx':
case '.mjs':
case '.cjs':
const result = await transformResource.js({file, env});
content = result.content;
contentType = 'application/javascript';
break;
if (ext && sourcesExtensions.has(ext)) {
let content = '';
let contentType = 'text/css';
let resourcePath = '';
let file = null;

if (/node_modules/.test(req.path)) {
resourcePath = path.join(process.cwd(), req.path);
} else {
resourcePath = path.join(pagesDirectoryPath, req.path);
}

if (env === 'production') {
cache[resourcePath] = {content, contentType}

if (env === 'production' && cache[resourcePath]) {
res.setHeader('Content-Type', cache[resourcePath].contentType);
return res.send(cache[resourcePath].content);
}

res.setHeader('Content-Type', contentType);

return res.send(content);
} catch(e) {
console.error(`Failed to load style/script content "${req.path}"`, e);
return res.sendStatus(404);
}
} else if(!ext || ext === '.html') {
const template = pagesRoutes[req.path] ?? pagesRoutes[`${req.path}/`];

file = new File(resourcePath, pagesDirectoryPath);

try {
switch (ext) {
case '.scss':
case '.sass':
content = await transformResource.sass({file});
content = (await transformResource.css(content, {file, env})).content;
break;
case '.less':
content = await transformResource.less({file});
content = (await transformResource.css(content, {file, env})).content;
break;
case '.styl':
content = await transformResource.stylus({file});
content = (await transformResource.css(content, {file, env})).content;
break;
case '.css':
content = (await transformResource.css({file, env})).content;
break;
case '.js':
case '.jsx':
case '.ts':
case '.tsx':
case '.mjs':
case '.cjs':
const result = await transformResource.js({file, env});
content = result.content;
contentType = 'application/javascript';
break;
}

if (env === 'production') {
cache[resourcePath] = {content, contentType}
}

res.setHeader('Content-Type', contentType);

if (template) {
return res.render(template, onPageRequest(req) || {})
return res.send(content);
} catch(e) {
console.error(`Failed to load style/script content "${req.path}"`, e);
return res.sendStatus(404);
}
} else if(!ext || ext === '.html') {

if (req.path.startsWith('/404')) {
const template = pagesRoutes[req.path] ?? pagesRoutes[`${req.path}/`];

if (template) {
return res.render(template, onPageRequest(req) || {})
} else if(!ext || ext === '.html') {

if (req.path.startsWith('/404')) {
return pagesRoutes['/404']
? res.render(pagesRoutes['/404'])
: res.send('<h1>404 - Page Not Found</h1>')
}

return res.redirect('/404')
}

return res.redirect('/404')
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beforesemicolon/html-plus",
"version": "0.4.2",
"version": "0.4.3",
"description": "HTML Template Engine/Language",
"main": "dist/index.js",
"files": [
Expand Down
15 changes: 3 additions & 12 deletions website/pages/_partials/docs/_engine-function.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ <h3>Asynchronous function</h3>
const app = express();

(async () => {
// setup all custom dynamic routes here
// to override the site routes
// when you await on the engine
// setup all middlewares and any route overrides before the engine
await engine(app, path.resolve(__dirname, './pages'), {
staticData: {
site: {
Expand All @@ -31,7 +29,8 @@ <h3>Asynchronous function</h3>
})();
</code-snippet>
<p>If you await on the engine and have some <a href="/documentation/routes/route-pages">dynamic routes</a>
you may need to set them up before otherwise any matching static route will override.</p>
you may need to set them up before otherwise any matching static route will override them. Prefer the engine
call as the last thing before you call the <strong>listen</strong> method for the server.</p>
<p>If you are not awaiting the engine, you can just set the routes after. The effect is the same but not
awaiting on the engine may throw an error if you get to the page before the engine is done processing your
pages directory.</p>
Expand Down Expand Up @@ -89,10 +88,6 @@ <h3>Return</h3>
<p>The engine returns undefined.</p>
<h3>Usage Examples</h3>
<code-snippet language="js">
const {engine} = require('@beforesemicolon/html-plus');

const app = express();

engine(app, path.resolve(__dirname, './pages'), {
staticData: {
site: {
Expand All @@ -103,10 +98,6 @@ <h3>Usage Examples</h3>
});
</code-snippet>
<code-snippet language="js">
const {engine} = require('@beforesemicolon/html-plus');

const app = express();

engine(app, path.resolve(__dirname, './pages'), {
onPageRequest: (req) => {
return {
Expand Down
8 changes: 5 additions & 3 deletions website/pages/_partials/docs/_getting-started.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ <h3>Server Setup</h3>

const app = express();

engine(app, path.resolve(__dirname, './pages'));
(async () => {
await engine(app, path.resolve(__dirname, './pages'));

const server = http.createServer(app);
const server = http.createServer(app);

server.listen(3000)
server.listen(3000);
})()
</code-snippet>
<p><strong>HTML+ engine</strong> will setup your <a href="/documentation/routes">Routes</a> for your pages
and all handlers for CSS(including preprocessors), Javascript and Typescript files.</p>
Expand Down
14 changes: 9 additions & 5 deletions website/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ <h3>{page.quick_start.title}</h3>

const app = express();

engine(app, path.resolve(__dirname, './pages'));
(async () => {
// wait for engine to finish setting upt the routes
// and all files
await engine(app, path.resolve(__dirname, './pages'));

const server = http.createServer(app);
const server = http.createServer(app);

server.listen(3000, () => {
console.log('server live on port 3000')
})
server.listen(3000, () => {
console.log('server live on port 3000')
})
})()
</code-snippet>
<a href="/documentation/getting-started" class="link-button cta">{page.quick_start.link.title}</a>
</section>
Expand Down

0 comments on commit 6c89b2d

Please sign in to comment.