Skip to content

Commit a5c7c3c

Browse files
authored
Merge pull request #3208 from iclanton/rush-sdk-webpack
[rush] Add support for rush-sdk to be webpacked.
2 parents f9fb221 + 6c95b42 commit a5c7c3c

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Add support for rush-sdk to be bundled with Webpack.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}

common/config/rush/pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Configures the TypeScript plugin for Heft. This plugin also manages linting.
3+
*/
4+
{
5+
"$schema": "https://developer.microsoft.com/json-schemas/heft/typescript.schema.json",
6+
"extends": "@rushstack/heft-node-rig/profiles/default/config/typescript.json",
7+
8+
"emitMjsExtensionForESModule": true
9+
}

libraries/rush-sdk/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"homepage": "https://rushjs.io",
1111
"main": "lib/index.js",
12+
"module": "lib/index.mjs",
1213
"typings": "dist/rush-lib.d.ts",
1314
"scripts": {
1415
"build": "heft build --clean",
@@ -27,7 +28,7 @@
2728
"@rushstack/heft": "workspace:*",
2829
"@rushstack/heft-node-rig": "workspace:*",
2930
"@types/heft-jest": "1.0.1",
30-
"@types/node": "12.20.24",
31-
"@types/semver": "7.3.5"
31+
"@types/semver": "7.3.5",
32+
"@types/webpack-env": "1.13.0"
3233
}
3334
}

libraries/rush-sdk/src/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ declare const global: NodeJS.Global &
3131
___rush___rushLibModuleFromInstallAndRunRush?: RushLibModuleType;
3232
};
3333

34+
function _require<TResult>(moduleName: string): TResult {
35+
if (typeof __non_webpack_require__ === 'function') {
36+
// If this library has been bundled with Webpack, we need to call the real `require` function
37+
// that doesn't get turned into a `__webpack_require__` statement.
38+
// `__non_webpack_require__` is a Webpack macro that gets turned into a `require` statement
39+
// during bundling.
40+
return __non_webpack_require__(moduleName);
41+
} else {
42+
return require(moduleName);
43+
}
44+
}
45+
3446
// SCENARIO 1: Rush's PluginManager has initialized "rush-sdk" with Rush's own instance of rush-lib.
3547
// The Rush host process will assign "global.___rush___rushLibModule" before loading the plugin.
3648
let rushLibModule: RushLibModuleType | undefined =
@@ -40,13 +52,13 @@ let errorMessage: string = '';
4052
// SCENARIO 2: The project importing "rush-sdk" has installed its own instance of "rush-lib"
4153
// as a package.json dependency. For example, this is used by the Jest tests for Rush plugins.
4254
if (rushLibModule === undefined) {
43-
const importingPath: string | undefined = module?.parent?.filename;
44-
if (importingPath !== undefined) {
55+
const importingPath: string | null | undefined = module?.parent?.filename;
56+
if (importingPath) {
4557
const callerPackageFolder: string | undefined =
4658
PackageJsonLookup.instance.tryGetPackageFolderFor(importingPath);
4759

4860
if (callerPackageFolder !== undefined) {
49-
const callerPackageJson: IPackageJson = require(path.join(callerPackageFolder, 'package.json'));
61+
const callerPackageJson: IPackageJson = _require(path.join(callerPackageFolder, 'package.json'));
5062

5163
// Does the caller properly declare a dependency on rush-lib?
5264
if (
@@ -123,7 +135,9 @@ if (rushLibModule === undefined) {
123135
}
124136

125137
// Retry to load "rush-lib" after install-run-rush run
126-
terminal.writeVerboseLine(`Trying to load ${RUSH_LIB_NAME} installed by install-run-rush a second time`);
138+
terminal.writeVerboseLine(
139+
`Trying to load ${RUSH_LIB_NAME} installed by install-run-rush a second time`
140+
);
127141
rushLibModule = requireRushLibUnderFolderPath(installRunNodeModuleFolder);
128142
} catch (e) {
129143
console.error(`${installAndRunRushStderrContent}`);
@@ -176,7 +190,7 @@ function requireRushLibUnderFolderPath(folderPath: string): RushLibModuleType {
176190
baseFolderPath: folderPath
177191
});
178192

179-
return require(rushLibModulePath);
193+
return _require(rushLibModulePath);
180194
}
181195

182196
/**

libraries/rush-sdk/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"extends": "./node_modules/@rushstack/heft-node-rig/profiles/default/tsconfig-base.json",
33
"compilerOptions": {
4-
"types": ["heft-jest", "node"]
4+
"types": [
5+
"heft-jest",
6+
"webpack-env" // Use webpack-env here instead of node so we have __non_webpack_require__
7+
]
58
}
69
}

0 commit comments

Comments
 (0)