Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ModuleScopePlugin accecpts an array as its appSrc #4138

Merged
merged 4 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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