1 | const path = require('path')
|
2 | const loaderUtils = require('loader-utils')
|
3 | const getMainCompilation = require('./utils/get-main-compilation')
|
4 | const toPosix = require('./utils/to-posix')
|
5 |
|
6 | module.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 | warn: (err) => {
|
30 | this.emitWarning(err)
|
31 | }
|
32 | }).outputPath
|
33 | }
|
34 |
|
35 | if (options.outputPath) {
|
36 | if (typeof options.outputPath === 'function') {
|
37 | outputPath = options.outputPath(outputPath, this.resourcePath, context)
|
38 | } else {
|
39 | outputPath = toPosix(path.join(options.outputPath, outputPath))
|
40 | }
|
41 | }
|
42 |
|
43 | let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`
|
44 |
|
45 | if (options.publicPath) {
|
46 | if (typeof options.publicPath === 'function') {
|
47 | publicPath = options.publicPath(outputPath, this.resourcePath, context)
|
48 | } else {
|
49 | publicPath = `${
|
50 | options.publicPath.endsWith('/')
|
51 | ? options.publicPath
|
52 | : `${options.publicPath}/`
|
53 | }${outputPath}`
|
54 | }
|
55 | publicPath = JSON.stringify(publicPath)
|
56 | }
|
57 |
|
58 | this.emitFile(outputPath, content)
|
59 |
|
60 |
|
61 | return `module.exports = ${publicPath};`
|
62 | }
|
63 |
|
64 | module.exports.raw = true
|