Skip to content

Add a new resolve option resolver #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/options_resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 1 addition & 1 deletion src/options_resolvers/processorOpts.js
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down
53 changes: 53 additions & 0 deletions src/options_resolvers/resolve.js
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/options_resolvers/rootDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 51 additions & 0 deletions test/options_resolvers/resolve.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});