Commit b051fe6a authored by Aral Balkan's avatar Aral Balkan
Browse files

Add support for .cjs and .mjs files

parent b0489092
......@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.1.0] - 2021-04-23
### Added
- Support for .cjs and .mjs files.
## [3.0.1] - 2020-09-02
### Fixed
......
......@@ -7,7 +7,13 @@ const routes = getRoutes(path.join(__dirname, '.routes'))
routes.forEach(route => {
console.log(` • Adding route: ${route.path}`)
app.get(route.path, require(route.callback))
// Note that while .mjs and .cjs files are supported,
// this module cannot defy the laws of ECMAScript. So the
// same rules defining mixing of CommonJS and ESM apply here too.
if (!route.callback.endsWith('.mjs')) {
app.get(route.path, require(route.callback))
}
})
app.listen(8080, () => console.log('\nServer running on http://localhost:8080'))
......@@ -25,7 +25,7 @@ function routes (directory, directoryRoot = null) {
}
// Recurse.
_routes = _routes.concat(routes(path.join(directory, file.name), directoryRoot))
} else if (file.isFile() && file.name.endsWith('.js')) {
} else if (file.isFile() && (file.name.endsWith('.js') || file.name.endsWith('.cjs') || file.name.endsWith('.mjs'))) {
//
// File.
//
......@@ -36,7 +36,11 @@ function routes (directory, directoryRoot = null) {
// Note: the regexp is written so that it will strip the leading slash properly
// ===== on both Linux-style and Windows environments.
routeUrlPath = routeUrlPath.replace(/\/?\\?index(.*?)\.js$/, '$1')
routeUrlPath = routeUrlPath.replace(/\/?\\?index(.*?)\.cjs$/, '$1')
routeUrlPath = routeUrlPath.replace(/\/?\\?index(.*?)\.mjs$/, '$1')
routeUrlPath = routeUrlPath.replace('.js', '')
routeUrlPath = routeUrlPath.replace('.cjs', '')
routeUrlPath = routeUrlPath.replace('.mjs', '')
routeUrlPath = routeUrlPath.replace(/\/$/, '')
// Handle parameter formatting:
......
This diff is collapsed.
{
"name": "@small-tech/web-routes-from-files",
"version": "3.0.1",
"version": "3.1.0",
"description": "Recursively traverses a given directory structure and uses convention to create a list of web route objects that map url paths to JavaScript callback files.",
"keywords": [
"files",
......
......@@ -28,12 +28,12 @@ Given the following directory structure:
.routes
├─── index.js
├─── my-folder
│ ├── index.js
│ ├── index.cjs
│ └── other.js
├─── person
│ └── index_personId__book_bookId.js
├─── rabbit_rabbitName.js
└─── neat.js
└─── neat.mjs
```
And the following invocation:
......@@ -52,7 +52,7 @@ You will get the following data structure:
{ path: '/my-folder/other', callback: '.routes/my-folder/other.js' },
{ path: '/person/:personId/book/:bookId', callback: '.routes/person/index_personId__book_bookId.js' },
{ path: '/rabbit/:rabbitName', callback: '.routes/rabbit_rabbitName.js' },
{ path: '/neat', callback: '.routes/neat.js' }
{ path: '/neat', callback: '.routes/neat.mjs' }
]
```
......@@ -67,10 +67,12 @@ const getRoutes = require ('..')
const app = express()
const routes = getRoutes(path.join(__dirname, '.routes'))
routes.forEach(route => {
console.log(` • Adding route: ${route.path}`)
// Note that while .mjs and .cjs files are supported,
// this module cannot defy the laws of ECMAScript. So the
// same rules defining mixing of CommonJS and ESM apply here too.
if (!route.callback.endsWith('.mjs')) {
app.get(route.path, require(route.callback))
})
}
app.listen(8080, () => console.log('\nServer running on http://localhost:8080'))
```
......
......@@ -15,7 +15,7 @@ test('routes', t => {
},
{
path: '/my-folder',
callback: path.join('my-folder', 'index.js')
callback: path.join('my-folder', 'index.cjs')
},
{
path: '/my-folder/other',
......@@ -23,7 +23,7 @@ test('routes', t => {
},
{
path: '/neat',
callback: 'neat.js'
callback: 'neat.mjs'
},
{
path: '/person/:personId/book/:bookId',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment