From 2a4373f0ece66a1a2e29ebdd3b098553d24df74d Mon Sep 17 00:00:00 2001 From: Pascal Duez Date: Sun, 25 Feb 2018 10:00:42 +0100 Subject: [PATCH 1/3] Fix typo in `rootDir` option resolver --- src/options_resolvers/rootDir.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From b837ade2e0d6f30b315f6f221407589ef6f22870 Mon Sep 17 00:00:00 2001 From: Pascal Duez Date: Sun, 25 Feb 2018 10:02:52 +0100 Subject: [PATCH 2/3] Fix typo in `processorOpts` option resolver --- src/options_resolvers/processorOpts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 * From dec0409a71d4ea22a1a59fdd308d0b7b153a16cd Mon Sep 17 00:00:00 2001 From: Pascal Duez Date: Sun, 25 Feb 2018 11:34:53 +0100 Subject: [PATCH 3/3] Add a new `resolve` option resolver - Refs #76 --- src/options_resolvers/index.js | 1 + src/options_resolvers/resolve.js | 53 ++++++++++++++++++++++++++ test/options_resolvers/resolve.spec.js | 51 +++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/options_resolvers/resolve.js create mode 100644 test/options_resolvers/resolve.spec.js 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/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/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(); + }); +});