Skip to content

Commit

Permalink
Merge pull request #3208 from iclanton/rush-sdk-webpack
Browse files Browse the repository at this point in the history
[rush] Add support for rush-sdk to be webpacked.
  • Loading branch information
iclanton authored Feb 6, 2022
2 parents f9fb221 + 6c95b42 commit a5c7c3c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
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
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__
]
}
}

0 comments on commit a5c7c3c

Please sign in to comment.