@@ -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.
3648let 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.
4254if ( 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/**
0 commit comments