Commit 24b18aed authored by iamdoron's avatar iamdoron Committed by Joe Haddad
Browse files

Allow importing package.json (#2468)

* Allow importing package.json

* Remove package.json import from App.js template

* fix importing package.json

* Rename variable to reflect path is relative to root

* Check for both package & package.json in ModuleScopePlugin

* Use regex to check relative path to package.json

* Strictly enforce package.json extension on scope plugin

* Add allowedPaths to ModuleScopePlugin ctor and use it to allow app package.json

* Remove package.json import from App.js template

* Add package.json to react-scripts/template, show package version and name in the template

* Remove import package.json from template

* Remove template/package.json and its references in code

* Update ModuleScopePlugin.js

* Update README.md
parent 664b7651
3 merge requests!12191Lim.Pisey.168:/Identified - We are currently investigating reports of missing build logs. The issue has been identified and a resolution is in progress. We will provide a further update when available.Mar 21, 09:02 UTC,!12853brikk,!5717Automatically extract project file structure from build bundle file
Showing with 14 additions and 12 deletions
+14 -12
...@@ -13,8 +13,9 @@ const chalk = require('chalk'); ...@@ -13,8 +13,9 @@ const chalk = require('chalk');
const path = require('path'); const path = require('path');
class ModuleScopePlugin { class ModuleScopePlugin {
constructor(appSrc) { constructor(appSrc, allowedFiles = []) {
this.appSrc = appSrc; this.appSrc = appSrc;
this.allowedFiles = new Set(allowedFiles);
} }
apply(resolver) { apply(resolver) {
...@@ -40,15 +41,16 @@ class ModuleScopePlugin { ...@@ -40,15 +41,16 @@ class ModuleScopePlugin {
if (relative.startsWith('../') || relative.startsWith('..\\')) { if (relative.startsWith('../') || relative.startsWith('..\\')) {
return callback(); return callback();
} }
// Find path from src to the requested file const requestFullPath = path.resolve(
const requestRelative = path.relative( path.dirname(request.context.issuer),
appSrc, request.__innerRequest_request
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/ // Error if in a parent directory of src/
const requestRelative = path.relative(appSrc, requestFullPath);
if ( if (
requestRelative.startsWith('../') || requestRelative.startsWith('../') ||
requestRelative.startsWith('..\\') requestRelative.startsWith('..\\')
......
...@@ -57,7 +57,7 @@ module.exports = { ...@@ -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. This Webpack plugin ensures that relative imports from app's source directory don't reach outside of it.
...@@ -71,7 +71,7 @@ module.exports = { ...@@ -71,7 +71,7 @@ module.exports = {
resolve: { resolve: {
// ... // ...
plugins: [ plugins: [
new ModuleScopePlugin(paths.appSrc), new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
// ... // ...
], ],
// ... // ...
......
...@@ -117,7 +117,7 @@ module.exports = { ...@@ -117,7 +117,7 @@ module.exports = {
// To fix this, we prevent you from importing files out of src/ -- if you'd like to, // 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. // 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. // 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: { module: {
......
...@@ -117,7 +117,7 @@ module.exports = { ...@@ -117,7 +117,7 @@ module.exports = {
// To fix this, we prevent you from importing files out of src/ -- if you'd like to, // 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. // 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. // 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: { module: {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment