diff --git a/packages/react-dev-utils/ModuleScopePlugin.js b/packages/react-dev-utils/ModuleScopePlugin.js index e2c16ffd819..2cd20313f0d 100644 --- a/packages/react-dev-utils/ModuleScopePlugin.js +++ b/packages/react-dev-utils/ModuleScopePlugin.js @@ -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) { @@ -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 @@ -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,