Skip to content

Commit e0ddefb

Browse files
iamdoronJohnNilsson
authored andcommitted
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
1 parent 052d2fa commit e0ddefb

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

packages/react-dev-utils/ModuleScopePlugin.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ const chalk = require('chalk');
1313
const path = require('path');
1414

1515
class ModuleScopePlugin {
16-
constructor(appSrc) {
16+
constructor(appSrc, allowedFiles = []) {
1717
this.appSrc = appSrc;
18+
this.allowedFiles = new Set(allowedFiles);
1819
}
1920

2021
apply(resolver) {
@@ -40,15 +41,16 @@ class ModuleScopePlugin {
4041
if (relative.startsWith('../') || relative.startsWith('..\\')) {
4142
return callback();
4243
}
43-
// Find path from src to the requested file
44-
const requestRelative = path.relative(
45-
appSrc,
46-
path.resolve(
47-
path.dirname(request.context.issuer),
48-
request.__innerRequest_request
49-
)
44+
const requestFullPath = path.resolve(
45+
path.dirname(request.context.issuer),
46+
request.__innerRequest_request
5047
);
48+
if (this.allowedFiles.has(requestFullPath)) {
49+
return callback();
50+
}
51+
// Find path from src to the requested file
5152
// Error if in a parent directory of src/
53+
const requestRelative = path.relative(appSrc, requestFullPath);
5254
if (
5355
requestRelative.startsWith('../') ||
5456
requestRelative.startsWith('..\\')

packages/react-dev-utils/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = {
5757
```
5858

5959

60-
#### `new ModuleScopePlugin(appSrc: string)`
60+
#### `new ModuleScopePlugin(appSrc: string, allowedFiles?: string[])`
6161

6262
This Webpack plugin ensures that relative imports from app's source directory don't reach outside of it.
6363

@@ -71,7 +71,7 @@ module.exports = {
7171
resolve: {
7272
// ...
7373
plugins: [
74-
new ModuleScopePlugin(paths.appSrc),
74+
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
7575
// ...
7676
],
7777
// ...

packages/react-scripts/config/webpack.config.dev.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module.exports = {
126126
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
127127
// please link the files into your node_modules/ and let module-resolution kick in.
128128
// Make sure your source files are compiled, as they will not be processed in any way.
129-
new ModuleScopePlugin(paths.appSrc),
129+
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
130130
],
131131
},
132132
module: {

packages/react-scripts/config/webpack.config.prod.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module.exports = {
126126
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
127127
// please link the files into your node_modules/ and let module-resolution kick in.
128128
// Make sure your source files are compiled, as they will not be processed in any way.
129-
new ModuleScopePlugin(paths.appSrc),
129+
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
130130
],
131131
},
132132
module: {

0 commit comments

Comments
 (0)