-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Renamed API tools and added
enableWorkspacesResolution
and `a…
…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
1 parent
ab0dd91
commit ba3ff8a
Showing
6 changed files
with
265 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.