Skip to content

Commit

Permalink
feat(ui5-tooling-modules): new module resolution (#1087)
Browse files Browse the repository at this point in the history
* fix(ui5-tooling-modules): properly resolve and match modules in the bundle

* feat(ui5-tooling-modules): support browser object for dependencies in package.json

* fix(ui5-tooling-modules): avoid lookup when nod node_modules found

* feat(ui5-tooling-modules): new module resolution

* feat(ui5-tooling-modules): consider package.json mappings

* fix(ui5-tooling-modules): fix existsSyncWithCase on Windows

* fix(ui5-tooling-modules): performance improvements

* fix(ui5-tooling-modules): windows compatibility of caches

* fix(ui5-tooling-modules): support linked projects for require.resolve

* fix(ui5-tooling-modules): only include depPaths of linked projects

---

The new module resolution utilizes the Node.js resolution algorithm. Especially, the handling of the entry points: https://nodejs.org/api/packages.html#package-entry-points and the mapping defined in the browser field in the `package.json` ensures a consistent bundling experience. 

The module resolution has been centralized in the `resolveModule` function. This is the central place which ensures a consistent module resolution for all places in the `ui5-tooling-modules` script.

In addition, the feature implements several caches to avoid unnecessary expensive lookups.
  • Loading branch information
petermuessig authored Oct 5, 2024
1 parent 40dc6ef commit 2e6c50c
Show file tree
Hide file tree
Showing 27 changed files with 63,142 additions and 29,543 deletions.
21 changes: 4 additions & 17 deletions packages/ui5-tooling-modules/lib/rollup-plugin-pnpm-resolve.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
const path = require("path");
const fs = require("fs");

const existsAndIsFile = function (file) {
return fs.existsSync(file) && fs.statSync(file).isFile();
};

module.exports = function ({ resolveModule } = {}) {
return {
name: "pnpm-resolve",
resolveId: function (importee, importer) {
let module = importee;
if (path.isAbsolute(importee)) {
// ignore absolute paths
return null;
} else if (importee.startsWith("./") || importee.startsWith("../")) {
// resolve relative paths
const file = path.resolve(path.dirname(importer), importee);
if (existsAndIsFile(file)) {
return file;
} else if (existsAndIsFile(`${file}.js`)) {
return `${file}.js`;
} else if (existsAndIsFile(`${file}.cjs`)) {
return `${file}.cjs`;
} else if (existsAndIsFile(`${file}.mjs`)) {
return `${file}.mjs`;
}
module = path.resolve(path.dirname(importer), importee);
}
// needs to be in sync with nodeResolve
const resolvedModule = resolveModule(importee);
// try to resolve the node module using the provided function
const resolvedModule = resolveModule(module);
//console.log(`Resolved ${importee} to ${resolvedModule}`);
return resolvedModule;
},
Expand Down
10 changes: 8 additions & 2 deletions packages/ui5-tooling-modules/lib/rollup-plugin-webcomponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { lt, gte } = require("semver");
// TODO:
// - enabled - disabled mapping
// - Externalize UI5 Web Components specific code
module.exports = function ({ log, resolveModule, framework, options } = {}) {
module.exports = function ({ log, resolveModule, getPackageJson, framework, options } = {}) {
// derive the configuration from the provided options
let { skip, scoping, scopeSuffix, enrichBusyIndicator } = Object.assign({ skip: false, scoping: true, enrichBusyIndicator: false }, options);

Expand Down Expand Up @@ -50,7 +50,13 @@ module.exports = function ({ log, resolveModule, framework, options } = {}) {
if (!registryEntry) {
const packageJsonPath = resolveModule(`${npmPackage}/package.json`);
if (packageJsonPath) {
const packageJson = require(packageJsonPath);
let packageJson;
try {
packageJson = getPackageJson(packageJsonPath);
} catch (err) {
log.error(`Failed to parse package.json of ${npmPackage}`, err);
return undefined;
}
const npmPackagePath = dirname(packageJsonPath);
// check if the custom elements metadata file exists (fallback to custom-elements-internal.json for @ui5/webcomponents)
let metadataPath;
Expand Down
Loading

0 comments on commit 2e6c50c

Please sign in to comment.