-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
83 lines (72 loc) · 2.32 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
83
"use strict";
const fs = require("fs");
const path = require("path");
const loaderUtils = require("loader-utils");
let loaderOptions = null;
function ui5Loader(source, map)
{
this.cacheable();
if (!source)
{
return this.callback(null, source, map);
}
loaderOptions = loaderUtils.parseQuery(this.query);
if (!loaderOptions.sourceRoot)
{
loaderOptions.sourceRoot = "./";
}
const webpackRemainingChain = loaderUtils.getRemainingRequest(this).split('!');
const filename = webpackRemainingChain[webpackRemainingChain.length - 1];
let groups = source.match(/sap\.ui\.define\(["'].+]/);
if (groups && groups.length > 0)
{
const definePart = groups[0];
if (!definePart.endsWith("[]"))
{
groups = definePart.match(/\[(.+)\]/);
if (groups && groups.length > 0)
{
const group = groups[1];
let dependencies = group.split(", ").map(d => d.replace(/["']/g, ""));
const requires = [];
dependencies = dependencies.map(d => {
const absPath = resolveModule(d, filename);
if (absPath !== null)
{
requires.push(`require("${absPath}");`);
}
});
source = requires.join("") + source;
}
}
}
this.callback(null, source, map);
};
function resolveModule(modulePath, sourceFilename)
{
const sourceRoot = path.resolve(loaderOptions.sourceRoot ? loaderOptions.sourceRoot : process.cwd());
let absPath = path.resolve(sourceRoot, "./" + modulePath);
if (!absPath.endsWith(".js"))
{
absPath += ".js";
}
if (fs.existsSync(absPath))
{
let relativePath = path.relative(path.dirname(sourceFilename), absPath);
if (!relativePath.startsWith("."))
{
relativePath = "./" + relativePath;
}
relativePath = relativePath.replace(/\\/g, "/");
return relativePath;
}
else
{
if (!modulePath.startsWith("sap/ui/base"))
{
console.warn(`WARN: Dependency "${modulePath}" of "${path.relative(sourceRoot, sourceFilename)}" is not found in ${sourceRoot}.`);
}
return null;
}
}
module.exports = ui5Loader;