Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rush] Add support for rush-sdk to be webpacked. #3208

Merged
merged 6 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Add support for rush-sdk to be bundled with Webpack.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
22 changes: 17 additions & 5 deletions libraries/rush-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ declare const global: NodeJS.Global &
___rush___rushLibModuleFromInstallAndRunRush?: RushLibModuleType;
};

// eslint-disable-next-line @typescript-eslint/naming-convention,@typescript-eslint/no-explicit-any
declare const __non_webpack_require__: ((moduleName: string) => any) | undefined;
iclanton marked this conversation as resolved.
Show resolved Hide resolved
function _require<TResult>(moduleName: string): TResult {
if (typeof __non_webpack_require__ === 'function') {
return __non_webpack_require__(moduleName);
} else {
return require(moduleName);
}
}

// SCENARIO 1: Rush's PluginManager has initialized "rush-sdk" with Rush's own instance of rush-lib.
// The Rush host process will assign "global.___rush___rushLibModule" before loading the plugin.
let rushLibModule: RushLibModuleType | undefined =
Expand All @@ -40,13 +50,13 @@ let errorMessage: string = '';
// SCENARIO 2: The project importing "rush-sdk" has installed its own instance of "rush-lib"
// as a package.json dependency. For example, this is used by the Jest tests for Rush plugins.
if (rushLibModule === undefined) {
const importingPath: string | undefined = module?.parent?.filename;
if (importingPath !== undefined) {
const importingPath: string | null | undefined = module?.parent?.filename;
if (importingPath) {
const callerPackageFolder: string | undefined =
PackageJsonLookup.instance.tryGetPackageFolderFor(importingPath);

if (callerPackageFolder !== undefined) {
const callerPackageJson: IPackageJson = require(path.join(callerPackageFolder, 'package.json'));
const callerPackageJson: IPackageJson = _require(path.join(callerPackageFolder, 'package.json'));

// Does the caller properly declare a dependency on rush-lib?
if (
Expand Down Expand Up @@ -123,7 +133,9 @@ if (rushLibModule === undefined) {
}

// Retry to load "rush-lib" after install-run-rush run
terminal.writeVerboseLine(`Trying to load ${RUSH_LIB_NAME} installed by install-run-rush a second time`);
terminal.writeVerboseLine(
`Trying to load ${RUSH_LIB_NAME} installed by install-run-rush a second time`
);
rushLibModule = requireRushLibUnderFolderPath(installRunNodeModuleFolder);
} catch (e) {
console.error(`${installAndRunRushStderrContent}`);
Expand Down Expand Up @@ -176,7 +188,7 @@ function requireRushLibUnderFolderPath(folderPath: string): RushLibModuleType {
baseFolderPath: folderPath
});

return require(rushLibModulePath);
return _require(rushLibModulePath);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions libraries/rush-sdk/src/test/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ describe('used in script', () => {
[
'-e',
`
const { Import } = require("${coreLibPath}");
const { Import } = require(${JSON.stringify(coreLibPath)});
iclanton marked this conversation as resolved.
Show resolved Hide resolved
const originalResolveModule = Import.resolveModule;
const mockResolveModule = (options) => {
if (options.baseFolderPath.includes('install-run') && options.modulePath === '@microsoft/rush-lib') {
return "${mockRushLibPath}";
return ${JSON.stringify(mockRushLibPath)};
}
return originalResolveModule(options);
}
Import.resolveModule = mockResolveModule;
console.log(require("${rushSdkPath}"));
console.log(require(${JSON.stringify(rushSdkPath)}));
`
],
{
Expand Down