This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
index.js
66 lines (51 loc) · 2.04 KB
/
index.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import path from 'path';
import loaderUtils from 'loader-utils';
import validateOptions from 'schema-utils';
import schema from './options.json';
export default function loader(content) {
if (!this.emitFile) throw new Error('File Loader\n\nemitFile is required from module system');
const options = loaderUtils.getOptions(this) || {};
validateOptions(schema, options, 'File Loader');
const context = options.context || this.rootContext || (this.options && this.options.context);
let url = loaderUtils.interpolateName(this, options.name, {
context,
content,
regExp: options.regExp,
});
let outputPath = '';
if (options.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = (
typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath
);
}
if (options.useRelativePath) {
const filePath = this.resourcePath;
const issuerContext = (this._module && this._module.issuer
&& this._module.issuer.context) || context;
const relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join('/');
const relativePath = relativeUrl && `${path.dirname(relativeUrl)}/`;
// eslint-disable-next-line no-bitwise
if (~relativePath.indexOf('../')) {
outputPath = path.posix.join(outputPath, relativePath, url);
} else {
outputPath = relativePath + url;
}
url = relativePath + url;
} else {
outputPath += url;
}
let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`;
if (options.publicPath !== undefined) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof options.publicPath === 'function' ? options.publicPath(url) : options.publicPath + url,
);
}
if (options.emitFile === undefined || options.emitFile) {
this.emitFile(outputPath, content);
}
// TODO revert to ES2015 Module export, when new CSS Pipeline is in place
return `module.exports = ${publicPath};`;
}
export const raw = true;