Skip to content

Commit

Permalink
Allow ModuleScopePlugin accecpts an array as its appSrc (#4138)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
froyog authored and andriijas committed Mar 23, 2018
1 parent 058d03f commit 1922f4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
26 changes: 17 additions & 9 deletions packages/react-dev-utils/ModuleScopePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const path = require('path');

class ModuleScopePlugin {
constructor(appSrc, allowedFiles = []) {
this.appSrc = appSrc;
this.appSrcs = Array.isArray(appSrc) ? appSrc : [appSrc];
this.allowedFiles = new Set(allowedFiles);
}

apply(resolver) {
const { appSrc } = this;
const { appSrcs } = this;
resolver.plugin('file', (request, callback) => {
// Unknown issuer, probably webpack internals
if (!request.context.issuer) {
Expand All @@ -34,9 +34,13 @@ class ModuleScopePlugin {
}
// Resolve the issuer from our appSrc and make sure it's one of our files
// Maybe an indexOf === 0 would be better?
const relative = path.relative(appSrc, request.context.issuer);
// If it's not in src/ or a subdirectory, not our request!
if (relative.startsWith('../') || relative.startsWith('..\\')) {
if (
appSrcs.every(appSrc => {
const relative = path.relative(appSrc, request.context.issuer);
// If it's not in one of our app src or a subdirectory, not our request!
return relative.startsWith('../') || relative.startsWith('..\\');
})
) {
return callback();
}
const requestFullPath = path.resolve(
Expand All @@ -47,11 +51,15 @@ class ModuleScopePlugin {
return callback();
}
// Find path from src to the requested file
// Error if in a parent directory of src/
const requestRelative = path.relative(appSrc, requestFullPath);
// Error if in a parent directory of all given appSrcs
if (
requestRelative.startsWith('../') ||
requestRelative.startsWith('..\\')
appSrcs.every(appSrc => {
const requestRelative = path.relative(appSrc, requestFullPath);
return (
requestRelative.startsWith('../') ||
requestRelative.startsWith('..\\')
);
})
) {
callback(
new Error(
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dev-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ module.exports = {
```


#### `new ModuleScopePlugin(appSrc: string, allowedFiles?: string[])`
#### `new ModuleScopePlugin(appSrc: string | string[], allowedFiles?: string[])`

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

```js
var path = require('path');
Expand Down

0 comments on commit 1922f4d

Please sign in to comment.