Skip to content
Open
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
49 changes: 28 additions & 21 deletions packages/react-dev-utils/ModuleScopePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ const path = require('path');
const os = require('os');

class ModuleScopePlugin {
constructor(appSrc, allowedFiles = []) {
constructor(appSrc, allowedFiles = [], errorMessage = '') {
this.appSrcs = Array.isArray(appSrc) ? appSrc : [appSrc];
this.allowedFiles = new Set(allowedFiles);
this.allowedPaths = [...allowedFiles].map(path.dirname).filter(p => path.relative(p, process.cwd()) !== '');
this.allowedPaths = [...allowedFiles]
.map(path.dirname)
.filter(p => path.relative(p, process.cwd()) !== '');
this.errorMessage = errorMessage;
}

apply(resolver) {
Expand Down Expand Up @@ -54,9 +57,11 @@ class ModuleScopePlugin {
if (this.allowedFiles.has(requestFullPath)) {
return callback();
}
if (this.allowedPaths.some((allowedFile) => {
return requestFullPath.startsWith(allowedFile);
})) {
if (
this.allowedPaths.some(allowedFile => {
return requestFullPath.startsWith(allowedFile);
})
) {
return callback();
}
// Find path from src to the requested file
Expand All @@ -70,22 +75,24 @@ class ModuleScopePlugin {
);
})
) {
const scopeError = new Error(
`You attempted to import ${chalk.cyan(
request.__innerRequest_request
)} which falls outside of the project ${chalk.cyan(
'src/'
)} directory. ` +
`Relative imports outside of ${chalk.cyan(
'src/'
)} are not supported.` +
os.EOL +
`You can either move it inside ${chalk.cyan(
'src/'
)}, or add a symlink to it from project's ${chalk.cyan(
'node_modules/'
)}.`
);
const message =
this.errorMessage.length > 0
? this.errorMessage
: `You attempted to import ${chalk.cyan(
request.__innerRequest_request
)} which falls outside of the project ${chalk.cyan(
'src/'
)} directory. ` +
`Relative imports outside of ${chalk.cyan(
'src/'
)} are not supported.` +
os.EOL +
`You can either move it inside ${chalk.cyan(
'src/'
)}, or add a symlink to it from project's ${chalk.cyan(
'node_modules/'
)}.`;
const scopeError = new Error(message);
Object.defineProperty(scopeError, '__module_scope_plugin', {
value: true,
writable: false,
Expand Down