-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ui5-tooling-modules): improved packaging of npm packages
The packaging of npm packages resolves the browser modules also from the exports section of the package.json. This provides a better coverage to detect the browser modules inside the npm packages. In addition, this fix also allows to add polyfill overrides in the current working directory. Fixes #927 Fixes #915
- Loading branch information
1 parent
d4520e3
commit 97dcf94
Showing
27 changed files
with
83,930 additions
and
57,422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 17 additions & 4 deletions
21
packages/ui5-tooling-modules/lib/rollup-plugin-pnpm-resolve.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
module.exports = function ({ resolveModule } = {}) { | ||
return { | ||
name: "pnpm-resolve", | ||
resolveId(source) { | ||
// ignore absolute paths | ||
if (path.isAbsolute(source)) { | ||
resolveId: function (importee, importer) { | ||
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 (fs.existsSync(file) && fs.statSync(file).isFile()) { | ||
return file; | ||
} else if (fs.existsSync(`${file}.js`)) { | ||
return `${file}.js`; | ||
} else if (fs.existsSync(`${file}.cjs`)) { | ||
return `${file}.cjs`; | ||
} else if (fs.existsSync(`${file}.mjs`)) { | ||
return `${file}.mjs`; | ||
} | ||
} | ||
// needs to be in sync with nodeResolve | ||
return resolveModule(source); | ||
return resolveModule(importee); | ||
}, | ||
}; | ||
}; |
28 changes: 0 additions & 28 deletions
28
packages/ui5-tooling-modules/lib/rollup-plugin-polyfill-node-ignore.js
This file was deleted.
Oops, something went wrong.
85 changes: 60 additions & 25 deletions
85
packages/ui5-tooling-modules/lib/rollup-plugin-polyfill-node-override.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,72 @@ | ||
/* eslint-disable no-unused-vars */ | ||
const { existsSync, readFileSync } = require("fs"); | ||
const { join } = require("path"); | ||
|
||
// reuse logic from polyfill-node plugin | ||
const nodePolyfills = require("rollup-plugin-polyfill-node"); | ||
module.exports = function ({ log } = {}) { | ||
const { resolveId } = nodePolyfills(); | ||
const PREFIX = `\0polyfill-node.`; | ||
const PREFIX_LENGTH = PREFIX.length; | ||
const DIRNAME_PATH = "\0node-polyfills:dirname"; | ||
const FILENAME_PATH = "\0node-polyfills:filename"; | ||
const inject = require("@rollup/plugin-inject"); | ||
|
||
const isBuiltInModule = function isBuiltInModule(module) { | ||
try { | ||
if (!require("path").isAbsolute(module) && require.resolve(module) === module) { | ||
return true; | ||
} | ||
} catch (ex) { | ||
/* */ | ||
} | ||
return false; | ||
}; | ||
|
||
module.exports = function nodePolyfillsOverride({ log, cwd } = {}) { | ||
const { resolveId, load, transform } = nodePolyfills(); | ||
const overridesDir = join(cwd, "_polyfill-overrides_"); | ||
return { | ||
name: "polyfill-node-override", | ||
resolveId: function (importee, importer, options) { | ||
if (importee === "http2") { | ||
return { id: "http2?node-polyfill-override", moduleSideEffects: false }; | ||
} | ||
if (importee === "async_hooks") { | ||
return { id: "async_hooks?node-polyfill-override", moduleSideEffects: false }; | ||
} | ||
if (importee.startsWith("node:")) { | ||
return resolveId.call(this, importee.substr("node:".length), importer, options); | ||
if (isBuiltInModule(importee)) { | ||
const builtInModule = importee.startsWith("node:") ? importee.substr("node:".length) : importee; | ||
if (existsSync(join(overridesDir, `${builtInModule}.js`))) { | ||
return { id: `${PREFIX}${builtInModule}.js`, moduleSideEffects: false }; | ||
} | ||
const resolvedId = resolveId.call(this, builtInModule, importer, options); | ||
if (resolvedId) { | ||
return resolvedId; | ||
} else { | ||
return { id: `${builtInModule}?polyfill-node-ignore`, moduleSideEffects: false }; | ||
} | ||
} | ||
return null; | ||
}, | ||
load: function (source) { | ||
if (source === "http2?node-polyfill-override") { | ||
return `export const constants = { | ||
HTTP2_HEADER_AUTHORITY: "authority", | ||
HTTP2_HEADER_METHOD: "method", | ||
HTTP2_HEADER_PATH: "path", | ||
HTTP2_HEADER_SCHEME: "scheme", | ||
HTTP2_HEADER_CONTENT_LENGTH: "content-length", | ||
HTTP2_HEADER_EXPECT: "expect", | ||
HTTP2_HEADER_STATUS: "status" | ||
};`; | ||
} | ||
if (source === "async_hooks?node-polyfill-override") { | ||
return `export class AsyncResource {};`; | ||
load: function (importee) { | ||
if (importee.startsWith(PREFIX)) { | ||
const builtInModule = importee.substr(PREFIX_LENGTH); | ||
let content; | ||
if (existsSync(join(overridesDir, `${builtInModule}`))) { | ||
content = readFileSync(join(overridesDir, `${builtInModule}`), { encoding: "utf8" }); | ||
} else { | ||
content = load.apply(this, arguments); | ||
} | ||
return content; | ||
} else if (importee.endsWith("?polyfill-node-ignore")) { | ||
return ""; | ||
} | ||
return null; | ||
}, | ||
}; | ||
}; | ||
|
||
// prevent the renaming of global, process, Buffer in any module | ||
// => needs to be used as a standalone rollup plugin in build | ||
// !!!! KEEP IN SYNC WITH >> rollup-plugin-polyfill-node << !!!! | ||
module.exports.inject = function nodePolyfillsOverrideInject() { | ||
return inject({ | ||
process: PREFIX + "process", | ||
Buffer: [PREFIX + "buffer", "Buffer"], | ||
global: PREFIX + "global", | ||
__filename: FILENAME_PATH, | ||
__dirname: DIRNAME_PATH, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.