diff --git a/packages/react-dev-utils/ModuleScopePlugin.js b/packages/react-dev-utils/ModuleScopePlugin.js index f630a0279f5..3a10904d36b 100644 --- a/packages/react-dev-utils/ModuleScopePlugin.js +++ b/packages/react-dev-utils/ModuleScopePlugin.js @@ -13,8 +13,9 @@ const chalk = require('chalk'); const path = require('path'); class ModuleScopePlugin { - constructor(appSrc) { + constructor(appSrc, allowedFiles = []) { this.appSrc = appSrc; + this.allowedFiles = new Set(allowedFiles); } apply(resolver) { @@ -40,15 +41,16 @@ class ModuleScopePlugin { if (relative.startsWith('../') || relative.startsWith('..\\')) { return callback(); } - // Find path from src to the requested file - const requestRelative = path.relative( - appSrc, - path.resolve( - path.dirname(request.context.issuer), - request.__innerRequest_request - ) + const requestFullPath = path.resolve( + path.dirname(request.context.issuer), + request.__innerRequest_request ); + if (this.allowedFiles.has(requestFullPath)) { + return callback(); + } + // Find path from src to the requested file // Error if in a parent directory of src/ + const requestRelative = path.relative(appSrc, requestFullPath); if ( requestRelative.startsWith('../') || requestRelative.startsWith('..\\') diff --git a/packages/react-dev-utils/README.md b/packages/react-dev-utils/README.md index 9206f3e8c9f..e23f0a294aa 100644 --- a/packages/react-dev-utils/README.md +++ b/packages/react-dev-utils/README.md @@ -57,7 +57,7 @@ module.exports = { ``` -#### `new ModuleScopePlugin(appSrc: string)` +#### `new ModuleScopePlugin(appSrc: string, allowedFiles?: string[])` This Webpack plugin ensures that relative imports from app's source directory don't reach outside of it. @@ -71,7 +71,7 @@ module.exports = { resolve: { // ... plugins: [ - new ModuleScopePlugin(paths.appSrc), + new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]), // ... ], // ... diff --git a/packages/react-scripts/config/webpack.config.dev.js b/packages/react-scripts/config/webpack.config.dev.js index 253bc34f062..e9f985909da 100644 --- a/packages/react-scripts/config/webpack.config.dev.js +++ b/packages/react-scripts/config/webpack.config.dev.js @@ -117,7 +117,7 @@ module.exports = { // To fix this, we prevent you from importing files out of src/ -- if you'd like to, // please link the files into your node_modules/ and let module-resolution kick in. // Make sure your source files are compiled, as they will not be processed in any way. - new ModuleScopePlugin(paths.appSrc), + new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]), ], }, module: { diff --git a/packages/react-scripts/config/webpack.config.prod.js b/packages/react-scripts/config/webpack.config.prod.js index 0077c34a3f6..4c541e804b6 100644 --- a/packages/react-scripts/config/webpack.config.prod.js +++ b/packages/react-scripts/config/webpack.config.prod.js @@ -117,7 +117,7 @@ module.exports = { // To fix this, we prevent you from importing files out of src/ -- if you'd like to, // please link the files into your node_modules/ and let module-resolution kick in. // Make sure your source files are compiled, as they will not be processed in any way. - new ModuleScopePlugin(paths.appSrc), + new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]), ], }, module: {