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

Allow importing package.json #2468

Merged
merged 15 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from 13 commits
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
18 changes: 10 additions & 8 deletions packages/react-dev-utils/ModuleScopePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const chalk = require('chalk');
const path = require('path');

class ModuleScopePlugin {
constructor(appSrc) {
constructor(appSrc, allowedPaths = []) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit, how about allowedFiles? Paths makes it sound like you can give it a directory.

this.appSrc = appSrc;
this.allowedPaths = new Set(allowedPaths);
Copy link
Contributor

@Timer Timer Jun 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is safe when allowedPaths is undefined? We could add a default above to be explicit:

constructor(appSrc, allowedPaths = []) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is safe, new Set(undefined) is an empty set
but I can add it 👍

}

apply(resolver) {
Expand All @@ -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.allowedPaths.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('..\\')
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dev-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
```


#### `new ModuleScopePlugin(appSrc: string)`
#### `new ModuleScopePlugin(appSrc: string, allowedPaths: [string])`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, allowedFiles. And we should probably let them know it's optional allowedFiles?: string[].


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

Expand All @@ -71,7 +71,7 @@ module.exports = {
resolve: {
// ...
plugins: [
new ModuleScopePlugin(paths.appSrc),
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
// ...
],
// ...
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down