-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add util function to generate module paths (#763)
* feat: add util function to generate module paths * test: move getModulePaths test to __tests__ * test: move getModulePaths test to __tests__ * feat: make getModulePaths accept array of deps * test: simplify getModulePaths tests * test: simplify getModulePaths tests * chore: add getModulePaths to nodeModulesLoadingRules
- Loading branch information
1 parent
b041344
commit e433584
Showing
5 changed files
with
122 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@callstack/repack": minor | ||
--- | ||
|
||
Add getModulePaths utility to generate include and exclude paths for modules in the bundler config |
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
47 changes: 47 additions & 0 deletions
47
packages/repack/src/utils/__tests__/getModulePaths.test.ts
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,47 @@ | ||
import { getModulePaths } from '../getModulePaths'; | ||
|
||
describe('getModulePaths', () => { | ||
const packages = [ | ||
'react-native', | ||
'react-native-windows', | ||
'react-native-macos', | ||
'react-native-tvos', | ||
'@callstack/react-native-visionos', | ||
'@react-native/core', | ||
'@babel/core', | ||
'@types/react', | ||
'@expo/vector-icons', | ||
'socket.io', | ||
]; | ||
|
||
it.each(packages)('should generate classic path - %s', (packageName) => { | ||
const [classicPath] = getModulePaths([packageName]); | ||
|
||
const classicTest = `node_modules/${packageName}/`; | ||
expect(classicPath.test(classicTest)).toBe(true); | ||
}); | ||
|
||
it.each(packages)('should generate exotic path - %s', (packageName) => { | ||
const [_, exoticPath] = getModulePaths([packageName]); | ||
const exoticPackageName = packageName.replace(/[/\\]/g, '+'); | ||
|
||
const exoticTestAtSymbol = `node_modules/.pnpm/${exoticPackageName}@`; | ||
const exoticTestPlusSymbol = `node_modules/.pnpm/${exoticPackageName}+`; | ||
|
||
expect(exoticPath.test(exoticTestAtSymbol)).toBe(true); | ||
expect(exoticPath.test(exoticTestPlusSymbol)).toBe(true); | ||
}); | ||
|
||
it.each(packages)('should handle backslashes - %s', (packageName) => { | ||
const [classicPath, exoticPath] = getModulePaths([packageName]); | ||
const exoticPackageName = packageName.replace(/[/\\]/g, '+'); | ||
|
||
const classicTestBackslash = `node_modules\\${packageName}\\`; | ||
const exoticTestAtSymbolBackslash = `node_modules\\.pnpm\\${exoticPackageName}@`; | ||
const exoticTestPlusSymbolBackslash = `node_modules\\.pnpm\\${exoticPackageName}+`; | ||
|
||
expect(classicPath.test(classicTestBackslash)).toBe(true); | ||
expect(exoticPath.test(exoticTestAtSymbolBackslash)).toBe(true); | ||
expect(exoticPath.test(exoticTestPlusSymbolBackslash)).toBe(true); | ||
}); | ||
}); |
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,51 @@ | ||
/** | ||
* Generates regular expressions for matching module paths in both classic (npm, yarn) and exotic (pnpm) formats. | ||
* | ||
* In classic paths, the module names are escaped to match both forward slashes and backslashes. | ||
* In exotic paths, the module names' forward or backward slashes are replaced with `+`. | ||
* | ||
* @param moduleNames The name of the modules to generate paths for. | ||
* @returns An array of RegExp objects. | ||
* | ||
* @category Webpack util | ||
* | ||
* @example Usage in Webpack config: | ||
* ```ts | ||
* import * as Repack from '@callstack/repack'; | ||
* | ||
* return { | ||
* ..., | ||
* module: { | ||
* rules: [ | ||
* ..., | ||
* include: [ | ||
* ...Repack.getModulePaths([ | ||
* 'react-native', | ||
* '@react-native', | ||
* 'react-native-macos', | ||
* 'react-native-windows', | ||
* 'react-native-tvos', | ||
* '@callstack/react-native-visionos', | ||
* ... | ||
* ]), | ||
* ], | ||
* ] | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export function getModulePaths(moduleNames: string[]): RegExp[] { | ||
return moduleNames.flatMap((moduleName) => { | ||
const escapedClassic = moduleName.replace(/[/\\]/g, '[/\\\\]'); | ||
const escapedExotic = moduleName.replace(/[/\\]/g, '\\+'); | ||
|
||
const classicPath = new RegExp( | ||
`node_modules([/\\\\])+${escapedClassic}[/\\\\]` | ||
); | ||
const exoticPath = new RegExp( | ||
`node_modules(.*[/\\\\])+${escapedExotic}[@\\+]` | ||
); | ||
|
||
return [classicPath, exoticPath]; | ||
}); | ||
} |