Skip to content

Commit 1922f4d

Browse files
froyogandriijas
authored andcommitted
Allow ModuleScopePlugin accecpts an array as its appSrc (#4138)
* allow appSrc accepting an array * fixture of finding all appSrcs logic * update docs on ModuleScopePlugin accepts an array for appSrc * minor typo fix in docs: change directory to directories.
1 parent 058d03f commit 1922f4d

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

packages/react-dev-utils/ModuleScopePlugin.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const path = require('path');
1212

1313
class ModuleScopePlugin {
1414
constructor(appSrc, allowedFiles = []) {
15-
this.appSrc = appSrc;
15+
this.appSrcs = Array.isArray(appSrc) ? appSrc : [appSrc];
1616
this.allowedFiles = new Set(allowedFiles);
1717
}
1818

1919
apply(resolver) {
20-
const { appSrc } = this;
20+
const { appSrcs } = this;
2121
resolver.plugin('file', (request, callback) => {
2222
// Unknown issuer, probably webpack internals
2323
if (!request.context.issuer) {
@@ -34,9 +34,13 @@ class ModuleScopePlugin {
3434
}
3535
// Resolve the issuer from our appSrc and make sure it's one of our files
3636
// Maybe an indexOf === 0 would be better?
37-
const relative = path.relative(appSrc, request.context.issuer);
38-
// If it's not in src/ or a subdirectory, not our request!
39-
if (relative.startsWith('../') || relative.startsWith('..\\')) {
37+
if (
38+
appSrcs.every(appSrc => {
39+
const relative = path.relative(appSrc, request.context.issuer);
40+
// If it's not in one of our app src or a subdirectory, not our request!
41+
return relative.startsWith('../') || relative.startsWith('..\\');
42+
})
43+
) {
4044
return callback();
4145
}
4246
const requestFullPath = path.resolve(
@@ -47,11 +51,15 @@ class ModuleScopePlugin {
4751
return callback();
4852
}
4953
// Find path from src to the requested file
50-
// Error if in a parent directory of src/
51-
const requestRelative = path.relative(appSrc, requestFullPath);
54+
// Error if in a parent directory of all given appSrcs
5255
if (
53-
requestRelative.startsWith('../') ||
54-
requestRelative.startsWith('..\\')
56+
appSrcs.every(appSrc => {
57+
const requestRelative = path.relative(appSrc, requestFullPath);
58+
return (
59+
requestRelative.startsWith('../') ||
60+
requestRelative.startsWith('..\\')
61+
);
62+
})
5563
) {
5664
callback(
5765
new Error(

packages/react-dev-utils/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ module.exports = {
5757
```
5858

5959

60-
#### `new ModuleScopePlugin(appSrc: string, allowedFiles?: string[])`
60+
#### `new ModuleScopePlugin(appSrc: string | string[], allowedFiles?: string[])`
6161

62-
This Webpack plugin ensures that relative imports from app's source directory don't reach outside of it.
62+
This Webpack plugin ensures that relative imports from app's source directories don't reach outside of it.
6363

6464
```js
6565
var path = require('path');

0 commit comments

Comments
 (0)