Skip to content

Commit f8753e5

Browse files
authored
[FIX] Resolve properly package.json dependency aliases (#608)
Resolves: SAP/ui5-tooling#809 When shimmed packages are defined as dependency aliases in the package.json, they are being excluded from the bundlig as the packager eventually finds the real package and its path and as it's not defined in the shim, but its alias, it gets ignored. We need to provide more robust discovery in order to handle those cases: - npm/cli#3 - https://github.com/npm/rfcs/blob/main/implemented/0001-package-aliases.md
1 parent 78eb482 commit f8753e5

File tree

9 files changed

+100
-3
lines changed

9 files changed

+100
-3
lines changed

lib/graph/providers/NodePackageDependencies.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class NodePackageDependencies {
6464
}
6565
return Promise.all(node._dependencies.map(async ({name, optional}) => {
6666
const modulePath = await this._resolveModulePath(node.path, name, workspace);
67-
return this._getNode(modulePath, optional);
67+
return this._getNode(modulePath, optional, name);
6868
}));
6969
}
7070

@@ -99,15 +99,25 @@ class NodePackageDependencies {
9999
}
100100
}
101101

102-
async _getNode(modulePath, optional) {
102+
/**
103+
* Resolves a Node module by reading its package.json
104+
*
105+
* @param {string} modulePath Path to the module.
106+
* @param {boolean} optional Whether this dependency is optional.
107+
* @param {string} [nameAlias] The name of the module. It's usually the same as the name definfed
108+
* in package.json and could easily be skipped. Useful when defining dependency as an alias:
109+
* {@link https://github.com/npm/rfcs/blob/main/implemented/0001-package-aliases.md}
110+
* @returns {Promise<object>}
111+
*/
112+
async _getNode(modulePath, optional, nameAlias) {
103113
log.verbose(`Reading package.json in directory ${modulePath}...`);
104114
const packageJson = await readPackage({
105115
cwd: modulePath,
106116
normalize: false
107117
});
108118

109119
return {
110-
id: packageJson.name,
120+
id: nameAlias || packageJson.name,
111121
version: packageJson.version,
112122
path: modulePath,
113123
optional,

test/fixtures/application.a.aliases/node_modules/extension.a.esm.alias/lib/extensionModule.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/application.a.aliases/node_modules/extension.a.esm.alias/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "application.a.aliases",
3+
"version": "1.0.0",
4+
"description": "Simple SAPUI5 based application",
5+
"main": "index.html",
6+
"dependencies": {
7+
"extension.a.esm.alias": "file:../extension.a.esm"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
}
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
specVersion: "3.0"
3+
type: application
4+
metadata:
5+
name: application.a.aliases
6+
7+
--- # Everything below this line could also be put into the ui5.yaml of a standalone extension module
8+
specVersion: "3.0"
9+
kind: extension
10+
type: project-shim
11+
metadata:
12+
name: my.application.thirdparty
13+
shims:
14+
configurations:
15+
extension.a.esm.alias: # name as defined in package.json
16+
specVersion: "3.0"
17+
type: module # Use module type
18+
metadata:
19+
name: extension.a.esm.alias
20+
resources:
21+
configuration:
22+
paths:
23+
/resources/my/application/thirdparty/: "" # map root directory of lodash module
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Application A</title>
5+
</head>
6+
<body>
7+
8+
</body>
9+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"_version": "1.1.0",
3+
"sap.app": {
4+
"_version": "1.1.0",
5+
"id": "id1",
6+
"type": "application",
7+
"applicationVersion": {
8+
"version": "1.2.2"
9+
},
10+
"embeds": ["embedded"],
11+
"title": "{{title}}"
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function test(paramA) {
2+
var variableA = paramA;
3+
console.log(variableA);
4+
}
5+
test();

test/lib/graph/providers/NodePackageDependencies.integration.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import sinonGlobal from "sinon";
66
const __dirname = path.dirname(fileURLToPath(import.meta.url));
77

88
const applicationAPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.a");
9+
const applicationAAliasesPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.a.aliases");
910
const applicationCPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.c");
1011
const applicationC2Path = path.join(__dirname, "..", "..", "..", "fixtures", "application.c2");
1112
const applicationC3Path = path.join(__dirname, "..", "..", "..", "fixtures", "application.c3");
@@ -68,6 +69,23 @@ test("AppA: project with collection dependency", async (t) => {
6869
]);
6970
});
7071

72+
test("AppA: project with an alias dependency", async (t) => {
73+
const workspace = {
74+
getName: () => "workspace name",
75+
getModuleByNodeId: t.context.sinon.stub().resolves(undefined).onFirstCall().resolves({
76+
getPath: () => path.join(applicationAAliasesPath, "node_modules", "extension.a.esm.alias"),
77+
getVersion: () => "1.0.0",
78+
})
79+
};
80+
const npmProvider = new NodePackageDependenciesProvider({
81+
cwd: applicationAAliasesPath
82+
});
83+
await testGraphCreationDfs(t, npmProvider, [
84+
"extension.a.esm.alias",
85+
"application.a.aliases",
86+
], workspace);
87+
});
88+
7189
test("AppA: project with workspace overrides", async (t) => {
7290
const workspace = {
7391
getName: () => "workspace name",

0 commit comments

Comments
 (0)