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

feature: allow for custom error messaging in ModuleScopePlugin #11978

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
refactor: eslint and formatting
Aghassi authored Jan 25, 2022
commit 3bea88c4db5f5f5c80d9e34729ffacbc3832bedc
45 changes: 26 additions & 19 deletions packages/react-dev-utils/ModuleScopePlugin.js
Original file line number Diff line number Diff line change
@@ -12,10 +12,12 @@ const path = require('path');
const os = require('os');

class ModuleScopePlugin {
constructor(appSrc, allowedFiles = [], errorMessage) {
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;
}

@@ -55,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
@@ -71,20 +75,23 @@ class ModuleScopePlugin {
);
})
) {
const message = !!this.errorMessage ? 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 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,