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 all 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"
}
4 changes: 2 additions & 2 deletions common/config/rush/pnpm-lock.yaml

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

9 changes: 9 additions & 0 deletions libraries/rush-sdk/config/typescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Configures the TypeScript plugin for Heft. This plugin also manages linting.
*/
{
"$schema": "https://developer.microsoft.com/json-schemas/heft/typescript.schema.json",
"extends": "@rushstack/heft-node-rig/profiles/default/config/typescript.json",

"emitMjsExtensionForESModule": true
}
5 changes: 3 additions & 2 deletions libraries/rush-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"homepage": "https://rushjs.io",
"main": "lib/index.js",
"module": "lib/index.mjs",
"typings": "dist/rush-lib.d.ts",
"scripts": {
"build": "heft build --clean",
Expand All @@ -27,7 +28,7 @@
"@rushstack/heft": "workspace:*",
"@rushstack/heft-node-rig": "workspace:*",
"@types/heft-jest": "1.0.1",
"@types/node": "12.20.24",
"@types/semver": "7.3.5"
"@types/semver": "7.3.5",
"@types/webpack-env": "1.13.0"
}
}
24 changes: 19 additions & 5 deletions libraries/rush-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ declare const global: NodeJS.Global &
___rush___rushLibModuleFromInstallAndRunRush?: RushLibModuleType;
};

function _require<TResult>(moduleName: string): TResult {
if (typeof __non_webpack_require__ === 'function') {
// If this library has been bundled with Webpack, we need to call the real `require` function
// that doesn't get turned into a `__webpack_require__` statement.
// `__non_webpack_require__` is a Webpack macro that gets turned into a `require` statement
// during bundling.
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 +52,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 +135,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 +190,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
5 changes: 4 additions & 1 deletion libraries/rush-sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"extends": "./node_modules/@rushstack/heft-node-rig/profiles/default/tsconfig-base.json",
"compilerOptions": {
"types": ["heft-jest", "node"]
"types": [
"heft-jest",
"webpack-env" // Use webpack-env here instead of node so we have __non_webpack_require__
]
}
}