UNPKG

2.49 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4var loaderUtils = require('loader-utils');
5
6module.exports = function (content) {
7 this.cacheable && this.cacheable();
8 if (!this.emitFile) throw new Error('emitFile is required from module system');
9
10 var query = loaderUtils.parseQuery(this.query) || {};
11
12 var configKey = query.config || 'fileLoader';
13
14 var options = this.options[configKey] || {};
15
16 var config = {
17 publicPath: false,
18 useRelativePath: false,
19 name: '[hash].[ext]',
20 publicPathStrigify: true
21 };
22
23 // options takes precedence over config
24 Object.keys(options).forEach(function (attr) {
25 config[attr] = options[attr];
26 });
27
28 // query takes precedence over config and options
29 Object.keys(query).forEach(function (attr) {
30 config[attr] = query[attr];
31 });
32
33 var context = config.context || this.options.context;
34 var url = loaderUtils.interpolateName(this, config.name, {
35 context: context,
36 content: content,
37 regExp: config.regExp
38 });
39
40 var outputPath = '';
41
42 var filePath = this.resourcePath;
43 if (config.useRelativePath) {
44 var issuerContext = this._module && this._module.issuer && this._module.issuer.context || context;
45 var relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join('/');
46 var relativePath = relativeUrl && path.dirname(relativeUrl) + '/';
47 if (~relativePath.indexOf('../')) {
48 outputPath = path.posix.join(outputPath, relativePath, url);
49 } else {
50 outputPath = relativePath + url;
51 }
52 url = relativePath + url;
53 } else if (config.outputPath) {
54 // support functions as outputPath to generate them dynamically
55 outputPath = typeof config.outputPath === 'function' ? config.outputPath(url) : config.outputPath + url;
56 url = outputPath;
57 } else {
58 outputPath = url;
59 }
60
61 var publicPath = '__webpack_public_path__ + ' + JSON.stringify(url);
62 if (config.publicPath !== false) {
63 // support functions as publicPath to generate them dynamically
64 if (config.publicPathStringify) {
65 publicPath = JSON.Stringify(typeof config.publicPath === 'function' ? config.publicPath(url) : config.publicPath + url);
66 } else {
67 publicPath = typeof config.publicPath === 'function' ? config.publicPath(url) : config.publicPath + url;
68 }
69 }
70
71 if (query.emitFile === undefined || query.emitFile) {
72 this.emitFile(outputPath, content);
73 }
74
75 return 'module.exports = ' + publicPath + ';';
76};
77
78module.exports.raw = true;
\No newline at end of file