-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtojson-file-loader.js
43 lines (34 loc) · 1.09 KB
/
tojson-file-loader.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
var loaderUtils = require('loader-utils')
module.exports = function (content) {
this.cacheable(false)
if (!this.emitFile) throw new Error('emitFile is required from module system')
var options = loaderUtils.getOptions(this)
var config = {
publicPath: false,
name: '[hash].[ext]'
}
// options takes precedence over config
Object.keys(options).forEach(function (attr) {
config[attr] = options[attr]
})
var url = loaderUtils.interpolateName(this, config.name, {
context: config.context,
content: content,
regExp: config.regExp
})
var publicPath = '__webpack_public_path__ + ' + JSON.stringify(url)
if (config.publicPath) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof config.publicPath === 'function'
? config.publicPath(url)
: config.publicPath + url,
null,
2
)
}
if (config.emitFile === undefined || config.emitFile) {
this.emitFile(url, JSON.stringify(this.exec(content, this.resourcePath)))
}
return 'module.exports = ' + publicPath + ';'
}