src/loader.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 27
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 5
c 1
b 0
f 0
nc 1
mnd 3
bc 5
fnc 1
dl 0
loc 27
rs 10
bpm 5
cpm 5
noi 0
1
import JsYaml from 'js-yaml';
2
import * as util from './util';
3
4
export default function (source) {
5
    this.cacheable && this.cacheable();
6
    
7
    try {
8
        const yamlStr = util.readFile(this.resourcePath).toString();
9
        const yamlJSON = JsYaml.safeLoad(yamlStr);
10
        let yamlURL = '';
11
12
        if (this.loaders.length > 1) {
13
            const parentLoader = this.loaders[this.loaderIndex + 1];
14
            if (parentLoader && parentLoader.path.split('/').indexOf('file-loader') >= 0) {
15
                yamlURL = `/${source.split('"')[1]}`;
16
            }
17
        }
18
19
        return 'module.exports = ' + JSON.stringify({
20
            url: yamlURL,
21
            text: yamlStr,
22
            json: yamlJSON
23
        });
24
    } catch(error) {
25
        throw new Error(error);
26
    }
27
}
28