1 |
|
2 | 'use strict';
|
3 |
|
4 | const _ = require('lodash');
|
5 | const loaderUtils = require('loader-utils');
|
6 |
|
7 | module.exports = function (source) {
|
8 | if (this.cacheable) {
|
9 | this.cacheable();
|
10 | }
|
11 | const allLoadersButThisOne = this.loaders.filter(function (loader) {
|
12 |
|
13 | return (loader.module || loader.normal) !== module.exports;
|
14 | });
|
15 |
|
16 | if (allLoadersButThisOne.length > 0) {
|
17 | return source;
|
18 | }
|
19 |
|
20 | if (/\.js$/.test(this.resourcePath)) {
|
21 | return source;
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const options = this.query !== '' ? loaderUtils.parseQuery(this.query) : {};
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const template = _.template(source, _.defaults(options, { variable: 'data' }));
|
33 |
|
34 |
|
35 | const templateVariables = [
|
36 | 'compilation',
|
37 | 'webpack',
|
38 | 'webpackConfig',
|
39 | 'htmlWebpackPlugin'
|
40 | ];
|
41 | return 'var _ = require(' + loaderUtils.stringifyRequest(this, require.resolve('lodash')) + ');' +
|
42 | 'module.exports = function (templateParams) {' +
|
43 |
|
44 |
|
45 | templateVariables.map(function (variableName) {
|
46 | return 'var ' + variableName + ' = templateParams.' + variableName;
|
47 | }).join(';') + ';' +
|
48 |
|
49 | 'return (' + template.source + ')();' +
|
50 | '}';
|
51 | };
|