From bb71938d1d84abd77040fd0ebecdcaf5fd35886d Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Fri, 4 Feb 2022 18:58:14 -0800 Subject: [PATCH 1/6] Add support for rush-sdk to be webpacked. --- libraries/rush-sdk/src/index.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/libraries/rush-sdk/src/index.ts b/libraries/rush-sdk/src/index.ts index 54501826052..5c7b1f79706 100644 --- a/libraries/rush-sdk/src/index.ts +++ b/libraries/rush-sdk/src/index.ts @@ -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; +function _require(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 = @@ -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 ( @@ -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}`); @@ -176,7 +188,7 @@ function requireRushLibUnderFolderPath(folderPath: string): RushLibModuleType { baseFolderPath: folderPath }); - return require(rushLibModulePath); + return _require(rushLibModulePath); } /** From 3cdb73a70978b80fa3bf2c58a252156defb7a3d6 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Fri, 4 Feb 2022 19:02:24 -0800 Subject: [PATCH 2/6] Fix a test on Windows. --- libraries/rush-sdk/src/test/script.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/rush-sdk/src/test/script.test.ts b/libraries/rush-sdk/src/test/script.test.ts index a367dce25c4..e795a0abedc 100644 --- a/libraries/rush-sdk/src/test/script.test.ts +++ b/libraries/rush-sdk/src/test/script.test.ts @@ -15,16 +15,16 @@ describe('used in script', () => { [ '-e', ` -const { Import } = require("${coreLibPath}"); +const { Import } = require(${JSON.stringify(coreLibPath)}); 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)})); ` ], { From 31085f2b0a2caedfd4011da9115b4847151c13b8 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Fri, 4 Feb 2022 19:03:12 -0800 Subject: [PATCH 3/6] Rush change. --- .../rush/rush-sdk-webpack_2022-02-05-03-03.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@microsoft/rush/rush-sdk-webpack_2022-02-05-03-03.json diff --git a/common/changes/@microsoft/rush/rush-sdk-webpack_2022-02-05-03-03.json b/common/changes/@microsoft/rush/rush-sdk-webpack_2022-02-05-03-03.json new file mode 100644 index 00000000000..ded1596f677 --- /dev/null +++ b/common/changes/@microsoft/rush/rush-sdk-webpack_2022-02-05-03-03.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/rush", + "comment": "Add support for rush-sdk to be bundled with Webpack.", + "type": "none" + } + ], + "packageName": "@microsoft/rush" +} \ No newline at end of file From b583b0f760e474c9ef7b37f5412c246e9f058fa6 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Sat, 5 Feb 2022 22:35:45 -0800 Subject: [PATCH 4/6] Use webpack-env typings instead of node typings in rush-sdk --- common/config/rush/pnpm-lock.yaml | 4 ++-- libraries/rush-sdk/package.json | 4 ++-- libraries/rush-sdk/src/index.ts | 4 ++-- libraries/rush-sdk/tsconfig.json | 5 ++++- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index 464eba84b9e..7a8ced6712c 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -1634,9 +1634,9 @@ importers: '@rushstack/heft-node-rig': workspace:* '@rushstack/node-core-library': workspace:* '@types/heft-jest': 1.0.1 - '@types/node': 12.20.24 '@types/node-fetch': 1.6.9 '@types/semver': 7.3.5 + '@types/webpack-env': 1.13.0 tapable: 2.2.1 dependencies: '@rushstack/node-core-library': link:../node-core-library @@ -1648,8 +1648,8 @@ importers: '@rushstack/heft': link:../../apps/heft '@rushstack/heft-node-rig': link:../../rigs/heft-node-rig '@types/heft-jest': 1.0.1 - '@types/node': 12.20.24 '@types/semver': 7.3.5 + '@types/webpack-env': 1.13.0 ../../libraries/rushell: specifiers: diff --git a/libraries/rush-sdk/package.json b/libraries/rush-sdk/package.json index 42826ce4100..c04cdc0f156 100644 --- a/libraries/rush-sdk/package.json +++ b/libraries/rush-sdk/package.json @@ -27,7 +27,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" } } diff --git a/libraries/rush-sdk/src/index.ts b/libraries/rush-sdk/src/index.ts index 5c7b1f79706..fc972e91910 100644 --- a/libraries/rush-sdk/src/index.ts +++ b/libraries/rush-sdk/src/index.ts @@ -31,10 +31,10 @@ 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; function _require(moduleName: string): TResult { if (typeof __non_webpack_require__ === 'function') { + // If this library has been webpacked, we need to call the real `require` function + // that doesn't get turned into a __webpack_require__ statement return __non_webpack_require__(moduleName); } else { return require(moduleName); diff --git a/libraries/rush-sdk/tsconfig.json b/libraries/rush-sdk/tsconfig.json index 22f94ca28b5..ab74a420295 100644 --- a/libraries/rush-sdk/tsconfig.json +++ b/libraries/rush-sdk/tsconfig.json @@ -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__ + ] } } From 7427e0b41b5ec67b6ebf16642c7a9aa02580cf89 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Sat, 5 Feb 2022 22:39:29 -0800 Subject: [PATCH 5/6] Improve documentation. --- libraries/rush-sdk/src/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libraries/rush-sdk/src/index.ts b/libraries/rush-sdk/src/index.ts index fc972e91910..e619eca098d 100644 --- a/libraries/rush-sdk/src/index.ts +++ b/libraries/rush-sdk/src/index.ts @@ -33,8 +33,10 @@ declare const global: NodeJS.Global & function _require(moduleName: string): TResult { if (typeof __non_webpack_require__ === 'function') { - // If this library has been webpacked, we need to call the real `require` function - // that doesn't get turned into a __webpack_require__ statement + // 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); From 6c95b4244426dbbc7e10d38198e3bde73ac9cb56 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Sat, 5 Feb 2022 22:57:10 -0800 Subject: [PATCH 6/6] Include .mjs files. --- libraries/rush-sdk/config/typescript.json | 9 +++++++++ libraries/rush-sdk/package.json | 1 + 2 files changed, 10 insertions(+) create mode 100644 libraries/rush-sdk/config/typescript.json diff --git a/libraries/rush-sdk/config/typescript.json b/libraries/rush-sdk/config/typescript.json new file mode 100644 index 00000000000..9da485f84fb --- /dev/null +++ b/libraries/rush-sdk/config/typescript.json @@ -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 +} diff --git a/libraries/rush-sdk/package.json b/libraries/rush-sdk/package.json index c04cdc0f156..f6631d5841a 100644 --- a/libraries/rush-sdk/package.json +++ b/libraries/rush-sdk/package.json @@ -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",