This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (68 loc) · 2.17 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const path = require("path");
const Promise = require("bluebird");
const fs = Promise.promisifyAll(require("fs"));
const { getOptions } = require("loader-utils");
const validateOptions = require("schema-utils");
const projectRoot = path.resolve(__dirname, "../..");
const projectPublicRoot = path.resolve(projectRoot, "view/layout");
const kmtRoot = path.resolve(projectRoot, "../..");
const kmtPublicRoot = path.resolve(kmtRoot, "public_html");
const replace = p => p.replace(projectPublicRoot, kmtPublicRoot);
const optionsSchema = {
type: "object",
properties: {
baseUrl: {
type: "string",
format: "uri"
}
},
required: ["baseUrl"]
};
module.exports = function applyLoader(content, map) {
const options = getOptions(this);
validateOptions(optionsSchema, options, "ResolveKmtUrlLoader");
if (this.cacheable) {
this.cacheable();
}
const cb = this.async();
content = content
// replace /download with abs /download
.replace(/\/download/gim, `${options.baseUrl}/download`)
// replace /<known> with abs /<known>
.replace(/\/.*?\/layout\/((img|css)\/)/gim, `${options.baseUrl}/$1`)
// remove any possible cache busters
// clean up any image.kmt123.png
.replace(/\.kmt[\w]+\./gim, ".")
// clean up any ?r=123
.replace(/\?r=[\d]+/gim, "");
// now try local vs base files
// i.e.
// custom/X/view/layout/img/test.png
// (if not exists) would, according to the framework, be
// public_html/img/test.png
//
// This mirrors the framework expectation
//
const matches = content.match(new RegExp(`${projectRoot}[^'")]+`, "mig"));
if (matches && matches.length) {
Promise.map(
matches,
uri =>
new Promise((resolve /* , reject */) => {
fs.stat(uri, (err /* , stats */) => resolve({ uri, replace: err ? replace(uri) : false }));
})
).then(results => {
results.forEach(r => {
if (!r.replace) {
return;
}
// eslint-disable-next-line
console.log(r.uri, ' -> ', r.replace);
content = content.replace(r.uri, r.replace);
});
cb(null, content, map);
});
return;
}
cb(null, content, map);
};