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 5 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
26 changes: 15 additions & 11 deletions packages/react-dev-utils/ModuleScopePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,28 @@ class ModuleScopePlugin {
// Maybe an indexOf === 0 would be better?
const relative = path.relative(appSrc, request.context.issuer);
// If it's not in src/ or a subdirectory, not our request!
if (relative.startsWith('../') || relative.startsWith('..\\')) {
return callback();
}
const requestFullPath = path.resolve(
path.dirname(request.context.issuer),
request.__innerRequest_request
);
const requestRelativeToRoot = path.relative(
request.descriptionFileRoot,
requestFullPath
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gaearon you are totally right. My previous code wasn't working . I'm not sure what I did there, but this is what I meant to do - to check this is the package.json that's in the root. I didn't want to assume package.json always lives in ../src in case the src is more than one level deep like true/src

if (
relative.startsWith('../') ||
relative.startsWith('..\\')
requestRelativeToRoot === 'package.json' ||
requestRelativeToRoot === 'package'
) {
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 requestRelative = path.relative(appSrc, requestFullPath);
// Error if in a parent directory of src/
if (
requestRelative.startsWith('../') ||
requestRelative.startsWith('..\\')
requestRelative.startsWith('../') || requestRelative.startsWith('..\\')
) {
callback(
new Error(
Expand Down
2 changes: 2 additions & 0 deletions packages/react-scripts/template/src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { version, name } from '../../package.json';

class App extends Component {
render() {
Expand All @@ -9,6 +10,7 @@ class App extends Component {
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
<p>{`${name} v${version}`}</p>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
Expand Down