Jarid Margolin
8 years ago
7 changed files with 107 additions and 27 deletions
@ -0,0 +1,36 @@ |
|||
/* eslint-disable global-require*/ |
|||
const { join } = require('path'); |
|||
|
|||
const castToArray = val => (Array.isArray(val) ? val : [val]); |
|||
|
|||
function requirePath(path, middleware) { |
|||
try { |
|||
return require(path); |
|||
} catch (exception) { |
|||
if (!/Cannot find module/.test(exception.message)) { |
|||
exception.message = `Neutrino was unable to load the module '${middleware}'. ` + |
|||
`Ensure this module exports a function and is free from errors.\n${exception.message}`; |
|||
throw exception; |
|||
} |
|||
|
|||
return undefined; |
|||
} |
|||
} |
|||
|
|||
module.exports = function requireMiddleware(middleware, options = {}) { |
|||
const root = options.root || process.cwd(); |
|||
|
|||
return castToArray(middleware).map((middleware) => { |
|||
const path = [ |
|||
join(root, middleware), |
|||
join(root, 'node_modules', middleware) |
|||
].find(path => requirePath(path)); |
|||
|
|||
if (!path) { |
|||
throw new Error(`Neutrino cannot find a module with the name or path '${middleware}'. ` + |
|||
'Ensure this module can be found relative to the root of the project.'); |
|||
} |
|||
|
|||
return require(path); |
|||
}); |
|||
}; |
@ -0,0 +1,3 @@ |
|||
module.exports = (api) => api.config.module |
|||
.rule('compile') |
|||
.test(/\.js$/); |
@ -0,0 +1,45 @@ |
|||
import { join } from 'path'; |
|||
import { outputFile as fsOutputFile, remove as fsRemove } from 'fs-extra'; |
|||
import pify from 'pify'; |
|||
import test from 'ava'; |
|||
import requireMiddleware from '../src/requireMiddleware'; |
|||
|
|||
const cwd = process.cwd(); |
|||
const outputFile = pify(fsOutputFile); |
|||
const remove = pify(fsRemove); |
|||
|
|||
const rootPath = join(__dirname, 'test-module'); |
|||
const rootMiddlewarePath = join(rootPath, 'middleware.js'); |
|||
const errorMiddlewarePath = join(rootPath, 'errorMiddleware.js'); |
|||
const modulePath = join(rootPath, 'node_modules', 'mymodule'); |
|||
const moduleMiddlewarePath = join(modulePath, 'index.js'); |
|||
|
|||
test.before(async (t) => { |
|||
await Promise.all([ |
|||
outputFile(rootMiddlewarePath, 'module.exports = "root"'), |
|||
outputFile(errorMiddlewarePath, '[;'), |
|||
outputFile(moduleMiddlewarePath, 'module.exports = "mymodule"') |
|||
]) |
|||
process.chdir(rootPath); |
|||
}); |
|||
|
|||
test.after.always(async (t) => { |
|||
await remove(rootPath); |
|||
process.chdir(cwd); |
|||
}); |
|||
|
|||
test('requires middleware relative to root', t => { |
|||
t.is(requireMiddleware('middleware')[0], 'root'); |
|||
}); |
|||
|
|||
test('requires middleware from root/node_modules', t => { |
|||
t.is(requireMiddleware('mymodule')[0], 'mymodule'); |
|||
}); |
|||
|
|||
test('throws if middleware contains error', t => { |
|||
t.throws(() => requireMiddleware('errorMiddleware')); |
|||
}); |
|||
|
|||
test('throws if middleware cannot be found', t => { |
|||
t.throws(() => requireMiddleware('notExistent')); |
|||
}); |
Loading…
Reference in new issue