Skip to content

Commit dec0409

Browse files
committed
Add a new resolve option resolver
- Refs #76
1 parent b837ade commit dec0409

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/options_resolvers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export { default as preprocessCss } from './preprocessCss';
1111
export { default as processCss } from './processCss';
1212
export { default as processorOpts } from './processorOpts';
1313
export { default as rootDir } from './rootDir';
14+
export { default as resolve } from './resolve';
1415
export { default as use } from './use';

src/options_resolvers/resolve.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { isAbsolute } from 'path';
2+
import { statSync } from 'fs';
3+
import { isBoolean, isPlainObject, isString } from '../utils';
4+
5+
/**
6+
* Resolves resolve option for css-modules-require-hook
7+
*
8+
* @param {*} value
9+
* @returns {Object}
10+
*/
11+
export default function resolve(value/* , currentConfig */) {
12+
if (!isPlainObject(value)) {
13+
throw new Error(`Configuration 'resolve' is not an object`);
14+
}
15+
16+
if (alias in value && !isPlainObject(value.alias)) {
17+
throw new Error(`Configuration 'resolve.alias' is not an object`);
18+
}
19+
20+
if (extensions in value) {
21+
if (!Array.isArray(value.extensions)) {
22+
throw new Error(`Configuration 'resolve.extensions' is not an array`);
23+
}
24+
25+
value.extensions.map((option, index) => {
26+
if (!isString(option)) {
27+
throw new Error(`Configuration 'resolve.extensions[${index}]' is not a string`);
28+
}
29+
});
30+
}
31+
32+
if (modules in value) {
33+
if (!Array.isArray(value.modules)) {
34+
throw new Error(`Configuration 'resolve.modules' is not an array`);
35+
}
36+
37+
value.modules.map((option, index) => {
38+
if (!isAbsolute(option) || !statSync(option).isDirectory()) {
39+
throw new Error(`Configuration 'resolve.modules[${index}]' is not containing a valid absolute path`);
40+
}
41+
});
42+
}
43+
44+
if (mainFile in value && !isString(value.mainFile)) {
45+
throw new Error(`Configuration 'resolve.mainFile' is not a string`);
46+
}
47+
48+
if (preserveSymlinks in value && !isBoolean(value.preserveSymlinks)) {
49+
throw new Error(`Configuration 'resolve.preserveSymlinks' is not a boolean`);
50+
}
51+
52+
return value;
53+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect } from 'chai';
2+
3+
import resolve from '../../src/options_resolvers/resolve';
4+
5+
describe('options_resolvers/resolve', () => {
6+
it('should throw if resolve is not an object', () => {
7+
expect(
8+
() => resolve([])
9+
).to.throw();
10+
});
11+
12+
it('should throw if resolve.alias is not an object', () => {
13+
expect(
14+
() => resolve({ alias: [] })
15+
).to.throw();
16+
});
17+
18+
it('should throw if resolve.extensions is not an array', () => {
19+
expect(
20+
() => resolve({ extensions: {} })
21+
).to.throw();
22+
});
23+
24+
it('should throw if resolve.modules is not an array', () => {
25+
expect(
26+
() => resolve({ modules: {} })
27+
).to.throw();
28+
});
29+
30+
it('should throw if resolve.modules.* is not an absolute directory or does not exist', () => {
31+
expect(
32+
() => resolve({ modules: ['/test/this/not/exists'] })
33+
).to.throw();
34+
35+
expect(
36+
() => resolve('./')
37+
).to.throw();
38+
});
39+
40+
it('should throw if resolve.mainFile is not a string', () => {
41+
expect(
42+
() => resolve({ mainFile: {} })
43+
).to.throw();
44+
});
45+
46+
it('should throw if resolve.preserveSymlinks is not a boolean', () => {
47+
expect(
48+
() => resolve({ preserveSymlinks: 1 })
49+
).to.throw();
50+
});
51+
});

0 commit comments

Comments
 (0)