| 1 | /* This loader renders the template with underscore if no other loader was found */
|
|---|
| 2 | // @ts-nocheck
|
|---|
| 3 | "use strict";
|
|---|
| 4 | const _template = require("lodash/template");
|
|---|
| 5 |
|
|---|
| 6 | module.exports = function (source) {
|
|---|
| 7 | // Get templating options
|
|---|
| 8 | const options = this.getOptions();
|
|---|
| 9 | const force = options.force || false;
|
|---|
| 10 |
|
|---|
| 11 | const allLoadersButThisOne = this.loaders.filter(
|
|---|
| 12 | (loader) => loader.normal !== module.exports,
|
|---|
| 13 | );
|
|---|
| 14 |
|
|---|
| 15 | // This loader shouldn't kick in if there is any other loader (unless it's explicitly enforced)
|
|---|
| 16 | if (allLoadersButThisOne.length > 0 && !force) {
|
|---|
| 17 | return source;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | // Allow only one html-webpack-plugin loader to allow loader options in the webpack config
|
|---|
| 21 | const htmlWebpackPluginLoaders = this.loaders.filter(
|
|---|
| 22 | (loader) => loader.normal === module.exports,
|
|---|
| 23 | );
|
|---|
| 24 | const lastHtmlWebpackPluginLoader =
|
|---|
| 25 | htmlWebpackPluginLoaders[htmlWebpackPluginLoaders.length - 1];
|
|---|
| 26 | if (this.loaders[this.loaderIndex] !== lastHtmlWebpackPluginLoader) {
|
|---|
| 27 | return source;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | // Skip .js files (unless it's explicitly enforced)
|
|---|
| 31 | if (/\.(c|m)?js$/.test(this.resourcePath) && !force) {
|
|---|
| 32 | return source;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | // The following part renders the template with lodash as a minimalistic loader
|
|---|
| 36 | //
|
|---|
| 37 | const template = _template(source, {
|
|---|
| 38 | interpolate: /<%=([\s\S]+?)%>/g,
|
|---|
| 39 | variable: "data",
|
|---|
| 40 | ...options,
|
|---|
| 41 | });
|
|---|
| 42 | // Use `eval("require")("lodash")` to enforce using the native nodejs require
|
|---|
| 43 | // during template execution
|
|---|
| 44 | return (
|
|---|
| 45 | 'var _ = eval("require")(' +
|
|---|
| 46 | JSON.stringify(require.resolve("lodash")) +
|
|---|
| 47 | ");" +
|
|---|
| 48 | "module.exports = function (templateParams) { with(templateParams) {" +
|
|---|
| 49 | // Execute the lodash template
|
|---|
| 50 | "return (" +
|
|---|
| 51 | template.source +
|
|---|
| 52 | ")();" +
|
|---|
| 53 | "}}"
|
|---|
| 54 | );
|
|---|
| 55 | };
|
|---|