Skip to content

Commit

Permalink
feat: Renamed API tools and added enableWorkspacesResolution and `a…
Browse files Browse the repository at this point in the history
…ddNohoistAliases`

- Renamed `getMetroConfig` to `getMetroTools` (`getMetroConfig` is still supported but deprecated).
- Renamed `getWebpackConfig` to `getWebpackTools` (`getWebpackConfig` is still supported but deprecated).
- `getWebpackTools` returns a new `enableWorkspacesResolution(webpackConfig)` tool: Updates a webpack config to allow importing from external workspaces.
- `getWebpackTools` returns a new `addNohoistAliases(webpackConfig)` tool: Updates a webpack config to ensure nohoisted libraries are resolved from this workspace.
  • Loading branch information
mmazzarolo authored Sep 21, 2021
1 parent ab0dd91 commit ba3ff8a
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 209 deletions.
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ yarn add -D react-native-monorepo-tools

## Tools for making React Native codebases compatible with Yarn Workspaces

### `getMetroConfig(params: Object): Object`
### `getMetroTools(params: Object): Object`

Return a (partial) [Metro](https://facebook.github.io/metro/) configuration to make it compatible with Yarn workspaces.
Return [Metro](https://facebook.github.io/metro/) tools to make it compatible with Yarn workspaces.

#### Usage:

```js
const { getMetroConfig } = require("react-native-monorepo-tools");
const { getMetroTools } = require("react-native-monorepo-tools");

const metroConfig = getMetroConfig();
const metroTools = getMetroTools();

console.log(metroConfig.watchFolders);
console.log(metroTools.watchFolders);
// ↪ [
// "/Users/me/my-monorepo/node_modules/"
// "/Users/me/my-monorepo/packages/workspace-a/",
// "/Users/me/my-monorepo/packages/workspace-b",
// ]

console.log(metroConfig.blockList);
console.log(metroTools.blockList);
// ↪ [
// /^((?!workspace-a).)*\/node_modules\/react\/.*$/,
// /^((?!workspace-a).)*\/node_modules\/react-native\/.*$/,
// ]

console.log(metroConfig.extraNodeModules);
console.log(metroTools.extraNodeModules);
// ↪ {
// "react": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react",
// "react-native": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react-native"
Expand All @@ -61,21 +61,26 @@ console.log(metroConfig.extraNodeModules);
- `blockList`: List of regexes resolving to paths ignored by metro.
- `extraNodeModules`: List of required modules outside of the workspace directory.

### `getWebpackConfig(params: Object): Object`
### `getWebpackTools(params: Object): Object`

Return a (partial) Webpack configuration compatible with Yarn workspaces.
Return Webpack tools to make it compatible with Yarn workspaces.

#### Usage:

```js
const { getWebpackConfig } = require("react-native-monorepo-tools");
const { getWebpackTools } = require("react-native-monorepo-tools");
const webpackConfig = require('./webpack.config.js')

const webpackConfig = getWebpackConfig();
const webpackTools = getWebpackTools();

console.log(webpackConfig.alias);
// ↪ {
console.log(webpackTools.addNohoistAliases(webpackConfig));
// ↪ alias: {
// "react": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react"
// "react-native": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react-native-web"
// }

console.log(webpackTools.enableWorkspacesResolution(webpackConfig));
// ↪ adds to babel-loader each workspace
```

#### Available params:
Expand All @@ -84,7 +89,8 @@ console.log(webpackConfig.alias);

#### Output:

- `alias`: React Native web alias mapping (`react-native``react-native-web`).
- `enableWorkspacesResolution`: Allow importing from external workspaces.
- `addNohoistAliases`: Ensure nohoisted libraries are resolved from this workspace.

### `getMetroAndroidAssetsResolutionFix(params: Object): Object`

Expand Down
2 changes: 1 addition & 1 deletion src/get-metro-config.js → src/get-metro-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getWorkspaces = require("./get-workspaces");
*/

/**
* Return a (partial) Metro configuration to make it compatible with Yarn workspaces.
* Return Metro tools to make it compatible with Yarn workspaces.
* @param {object} params - Input parameters
* @param {string} [params.cwd] - Current working dir (defaults to process.cwd()).
* @param {string} [params.reactNativeAlias] - Alias for the react-native lib (e.g., react-native-macos).
Expand Down
30 changes: 0 additions & 30 deletions src/get-webpack-config.js

This file was deleted.

59 changes: 59 additions & 0 deletions src/get-webpack-tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require("path");
const getNohoist = require("./get-nohoist");
const getWorkspaces = require("./get-workspaces");

/**
* @typedef {Object} MonorepoWebpackConfig
* @prop {Function} addNohoistAliases - Updates a webpack config to ensure nohoisted libraries are resolved correctly.
* @prop {Function} enableWorkspacesResolution - Updates a webpack config allowing to import from external workspaces.
*/

/**
* Return Webpack tools to make it compatible with Yarn workspaces.
* @param {object} params - Input parameters
* @param {string} [params.cwd] - Current working dir (defaults to process.cwd()).
* @returns {MonorepoWebpackConfig} Webpack config for this monorepo.
*/
module.exports = function getWebpackTools(params = {}) {
const { cwd = process.cwd() } = params;

// Ensure nohoisted libraries are resolved from the current workspace
const nohoistLibNames = getNohoist({
cwd,
currentWorkspaceOnly: true,
libNamesOnly: true,
});
const nohoistAlias = {};
nohoistLibNames.forEach((nohoistLibName) => {
nohoistAlias[nohoistLibName] =
nohoistLibName === "react-native"
? path.resolve(cwd, "./node_modules/react-native-web")
: path.resolve(cwd, `./node_modules/${nohoistLibName}`);
});
function addNohoistAliases(webpackConfig) {
webpackConfig.resolve.alias = {
...webpackConfig.resolve.alias,
...nohoistAlias,
};
}

// Allow importing from external workspaces.
const workspaces = getWorkspaces({ cwd });
function enableWorkspacesResolution(webpackConfig) {
const babelLoader = webpackConfig.module.rules[1].oneOf.find((rule) =>
rule.loader.includes("babel-loader")
);
babelLoader.include = Array.isArray(babelLoader.include)
? babelLoader.include
: [babelLoader.include];
for (const workspace of workspaces) {
babelLoader.include.push(workspace);
}
}

return {
addNohoistAliases,
enableWorkspacesResolution,
alias: nohoistAlias // Deprecated
};
};
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const getMonorepoRoot = require("./get-monorepo-root");
const getWorkspaces = require("./get-workspaces");
const getNohoist = require("./get-nohoist");
const getMetroConfig = require("./get-metro-config");
const getWebpackConfig = require("./get-webpack-config");
const getMetroTools = require("./get-metro-tools");
const getWebpackTools = require("./get-webpack-tools");
const getMetroAndroidAssetsResolutionFix = require("./get-metro-android-assets-resolution-fix");

module.exports = {
getMonorepoRoot,
getWorkspaces,
getNohoist,
getMetroConfig,
getWebpackConfig,
getMetroTools,
getWebpackTools,
getMetroConfig: getMetroTools, // Deprecated
getWebpackConfig: getWebpackTools, // Deprecated
getMetroAndroidAssetsResolutionFix,
};
Loading

0 comments on commit ba3ff8a

Please sign in to comment.