diff --git a/src/options_resolvers/index.js b/src/options_resolvers/index.js index 46456c3..033aadc 100644 --- a/src/options_resolvers/index.js +++ b/src/options_resolvers/index.js @@ -11,4 +11,5 @@ export { default as preprocessCss } from './preprocessCss'; export { default as processCss } from './processCss'; export { default as processorOpts } from './processorOpts'; export { default as rootDir } from './rootDir'; +export { default as resolve } from './resolve'; export { default as use } from './use'; diff --git a/src/options_resolvers/processorOpts.js b/src/options_resolvers/processorOpts.js index c4188a7..450129c 100644 --- a/src/options_resolvers/processorOpts.js +++ b/src/options_resolvers/processorOpts.js @@ -1,7 +1,7 @@ import { isModulePath, isPlainObject, requireLocalFileOrNodeModule } from '../utils'; /** - * Resolves processOpts option for css-modules-require-hook + * Resolves processorOpts option for css-modules-require-hook * * @param {String|Function} value * diff --git a/src/options_resolvers/resolve.js b/src/options_resolvers/resolve.js new file mode 100644 index 0000000..102f99a --- /dev/null +++ b/src/options_resolvers/resolve.js @@ -0,0 +1,53 @@ +import { isAbsolute } from 'path'; +import { statSync } from 'fs'; +import { isBoolean, isPlainObject, isString } from '../utils'; + +/** + * Resolves resolve option for css-modules-require-hook + * + * @param {*} value + * @returns {Object} + */ +export default function resolve(value/* , currentConfig */) { + if (!isPlainObject(value)) { + throw new Error(`Configuration 'resolve' is not an object`); + } + + if (alias in value && !isPlainObject(value.alias)) { + throw new Error(`Configuration 'resolve.alias' is not an object`); + } + + if (extensions in value) { + if (!Array.isArray(value.extensions)) { + throw new Error(`Configuration 'resolve.extensions' is not an array`); + } + + value.extensions.map((option, index) => { + if (!isString(option)) { + throw new Error(`Configuration 'resolve.extensions[${index}]' is not a string`); + } + }); + } + + if (modules in value) { + if (!Array.isArray(value.modules)) { + throw new Error(`Configuration 'resolve.modules' is not an array`); + } + + value.modules.map((option, index) => { + if (!isAbsolute(option) || !statSync(option).isDirectory()) { + throw new Error(`Configuration 'resolve.modules[${index}]' is not containing a valid absolute path`); + } + }); + } + + if (mainFile in value && !isString(value.mainFile)) { + throw new Error(`Configuration 'resolve.mainFile' is not a string`); + } + + if (preserveSymlinks in value && !isBoolean(value.preserveSymlinks)) { + throw new Error(`Configuration 'resolve.preserveSymlinks' is not a boolean`); + } + + return value; +} diff --git a/src/options_resolvers/rootDir.js b/src/options_resolvers/rootDir.js index 65d0b06..117ca7e 100644 --- a/src/options_resolvers/rootDir.js +++ b/src/options_resolvers/rootDir.js @@ -14,7 +14,7 @@ export default function rootDir(value/* , currentConfig */) { } if (!isAbsolute(value) || !statSync(value).isDirectory()) { - throw new Error(`Configuration 'rootDir' is not containg a valid absolute path`); + throw new Error(`Configuration 'rootDir' is not containing a valid absolute path`); } return value; diff --git a/test/options_resolvers/resolve.spec.js b/test/options_resolvers/resolve.spec.js new file mode 100644 index 0000000..34cf0aa --- /dev/null +++ b/test/options_resolvers/resolve.spec.js @@ -0,0 +1,51 @@ +import { expect } from 'chai'; + +import resolve from '../../src/options_resolvers/resolve'; + +describe('options_resolvers/resolve', () => { + it('should throw if resolve is not an object', () => { + expect( + () => resolve([]) + ).to.throw(); + }); + + it('should throw if resolve.alias is not an object', () => { + expect( + () => resolve({ alias: [] }) + ).to.throw(); + }); + + it('should throw if resolve.extensions is not an array', () => { + expect( + () => resolve({ extensions: {} }) + ).to.throw(); + }); + + it('should throw if resolve.modules is not an array', () => { + expect( + () => resolve({ modules: {} }) + ).to.throw(); + }); + + it('should throw if resolve.modules.* is not an absolute directory or does not exist', () => { + expect( + () => resolve({ modules: ['/test/this/not/exists'] }) + ).to.throw(); + + expect( + () => resolve('./') + ).to.throw(); + }); + + it('should throw if resolve.mainFile is not a string', () => { + expect( + () => resolve({ mainFile: {} }) + ).to.throw(); + }); + + it('should throw if resolve.preserveSymlinks is not a boolean', () => { + expect( + () => resolve({ preserveSymlinks: 1 }) + ).to.throw(); + }); +});