Skip to content

Commit

Permalink
[FIX] Resolve properly package.json dependency aliases (#608)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
d3xter666 committed May 4, 2023
1 parent 78eb482 commit f8753e5
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/graph/providers/NodePackageDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NodePackageDependencies {
}
return Promise.all(node._dependencies.map(async ({name, optional}) => {
const modulePath = await this._resolveModulePath(node.path, name, workspace);
return this._getNode(modulePath, optional);
return this._getNode(modulePath, optional, name);
}));
}

Expand Down Expand Up @@ -99,15 +99,25 @@ class NodePackageDependencies {
}
}

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

return {
id: packageJson.name,
id: nameAlias || packageJson.name,
version: packageJson.version,
path: modulePath,
optional,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/fixtures/application.a.aliases/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "application.a.aliases",
"version": "1.0.0",
"description": "Simple SAPUI5 based application",
"main": "index.html",
"dependencies": {
"extension.a.esm.alias": "file:../extension.a.esm"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
23 changes: 23 additions & 0 deletions test/fixtures/application.a.aliases/ui5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
specVersion: "3.0"
type: application
metadata:
name: application.a.aliases

--- # Everything below this line could also be put into the ui5.yaml of a standalone extension module
specVersion: "3.0"
kind: extension
type: project-shim
metadata:
name: my.application.thirdparty
shims:
configurations:
extension.a.esm.alias: # name as defined in package.json
specVersion: "3.0"
type: module # Use module type
metadata:
name: extension.a.esm.alias
resources:
configuration:
paths:
/resources/my/application/thirdparty/: "" # map root directory of lodash module
9 changes: 9 additions & 0 deletions test/fixtures/application.a.aliases/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Application A</title>
</head>
<body>

</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/application.a.aliases/webapp/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"_version": "1.1.0",
"sap.app": {
"_version": "1.1.0",
"id": "id1",
"type": "application",
"applicationVersion": {
"version": "1.2.2"
},
"embeds": ["embedded"],
"title": "{{title}}"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/application.a.aliases/webapp/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function test(paramA) {
var variableA = paramA;
console.log(variableA);
}
test();
18 changes: 18 additions & 0 deletions test/lib/graph/providers/NodePackageDependencies.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sinonGlobal from "sinon";
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const applicationAPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.a");
const applicationAAliasesPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.a.aliases");
const applicationCPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.c");
const applicationC2Path = path.join(__dirname, "..", "..", "..", "fixtures", "application.c2");
const applicationC3Path = path.join(__dirname, "..", "..", "..", "fixtures", "application.c3");
Expand Down Expand Up @@ -68,6 +69,23 @@ test("AppA: project with collection dependency", async (t) => {
]);
});

test("AppA: project with an alias dependency", async (t) => {
const workspace = {
getName: () => "workspace name",
getModuleByNodeId: t.context.sinon.stub().resolves(undefined).onFirstCall().resolves({
getPath: () => path.join(applicationAAliasesPath, "node_modules", "extension.a.esm.alias"),
getVersion: () => "1.0.0",
})
};
const npmProvider = new NodePackageDependenciesProvider({
cwd: applicationAAliasesPath
});
await testGraphCreationDfs(t, npmProvider, [
"extension.a.esm.alias",
"application.a.aliases",
], workspace);
});

test("AppA: project with workspace overrides", async (t) => {
const workspace = {
getName: () => "workspace name",
Expand Down

0 comments on commit f8753e5

Please sign in to comment.