UNPKG

1.72 kBJavaScriptView Raw
1const path = require('path')
2const loaderUtils = require('loader-utils')
3const getMainCompilation = require('./utils/get-main-compilation')
4const toPosix = require('./utils/to-posix')
5
6module.exports = function loader (content) {
7 const options = loaderUtils.getOptions(this) || {}
8 const context = options.context || this.rootContext
9 const mainCompilation = getMainCompilation(this._compilation)
10 const mpx = mainCompilation.__mpx__
11
12 let url = loaderUtils.interpolateName(this, options.name, {
13 context,
14 content,
15 regExp: options.regExp
16 })
17
18 let outputPath
19
20 if (options.publicPath) {
21 outputPath = url
22 } else {
23 outputPath = mpx.getPackageInfo(this.resource, {
24 outputPath: url,
25 isStatic: true,
26 error: (err) => {
27 this.emitError(err)
28 }
29 }).outputPath
30 }
31
32 if (options.outputPath) {
33 if (typeof options.outputPath === 'function') {
34 outputPath = options.outputPath(outputPath, this.resourcePath, context)
35 } else {
36 outputPath = toPosix(path.join(options.outputPath, outputPath))
37 }
38 }
39
40 let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`
41
42 if (options.publicPath) {
43 if (typeof options.publicPath === 'function') {
44 publicPath = options.publicPath(outputPath, this.resourcePath, context)
45 } else {
46 publicPath = `${
47 options.publicPath.endsWith('/')
48 ? options.publicPath
49 : `${options.publicPath}/`
50 }${outputPath}`
51 }
52 publicPath = JSON.stringify(publicPath)
53 }
54
55 this.emitFile(outputPath, content)
56
57 // TODO revert to ES2015 Module export, when new CSS Pipeline is in place
58 return `module.exports = ${publicPath};`
59}
60
61module.exports.raw = true