-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·38 lines (34 loc) · 1.02 KB
/
index.js
File metadata and controls
executable file
·38 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const path = require('path');
const os = require('os');
const fs = require('fs');
const loaderUtils = require('loader-utils');
const defaultOptions = {
tryFilename: false,
files: ['styles.css', 'style.css', 'main.css'],
};
const toArray = (option) => Array.isArray(option) ? option : [option];
module.exports = function(content) {
this.cacheable && this.cacheable();
const options = Object.assign(
{},
defaultOptions,
loaderUtils.getOptions(this)
);
if (options.tryFilename === true) {
options.tryFilename = ['.css'];
}
const resource = path.parse(this.resourcePath);
let files = toArray(options.files);
if (options.tryFilename) {
files = toArray(options.tryFilename).map(ext => resource.name + ext).concat(files);
}
let stylePath = files.map(file => path.join(resource.dir, file)).find(fs.existsSync);
if (stylePath) {
this.addDependency(stylePath);
if (os.platform().startsWith('win')) {
stylePath = stylePath.split(path.sep).join('/');
}
return `require('${stylePath}');\n${content}`;
}
return content;
}