Skip to content

Commit 13336d6

Browse files
Require packages to be opted in via glob, facebook#4570
1 parent a7066cd commit 13336d6

File tree

30 files changed

+276
-11
lines changed

30 files changed

+276
-11
lines changed

packages/react-dev-utils/workspaceUtils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,29 @@ const findPkgs = (rootPath, globPatterns) => {
3030
};
3131

3232
const findMonorepo = appDir => {
33+
const appPkg = JSON.parse(
34+
fs.readFileSync(path.resolve(appDir, 'package.json'))
35+
);
3336
const monoPkgPath = findPkg.sync(path.resolve(appDir, '..'));
3437
const monoRootPath = monoPkgPath && path.dirname(monoPkgPath);
3538
const monoPkg = monoPkgPath && require(monoPkgPath);
3639
const patterns = monoPkg && monoPkg.workspaces;
3740
const isYarnWs = Boolean(patterns);
41+
const srcPatterns = appPkg && appPkg.sourceWorkspaces;
3842
const allPkgs = patterns && findPkgs(monoRootPath, patterns);
43+
const allSrcPkgs =
44+
srcPatterns && findPkgs(path.dirname(monoPkgPath), srcPatterns);
3945
const isIncluded = dir => allPkgs && allPkgs.indexOf(dir) !== -1;
4046
const isAppIncluded = isIncluded(appDir);
41-
const pkgs = allPkgs
42-
? allPkgs.filter(f => fs.realpathSync(f) !== appDir)
47+
const srcPkgPaths = allSrcPkgs
48+
? allSrcPkgs.filter(f => fs.realpathSync(f) !== appDir)
4349
: [];
4450

4551
return {
4652
isAppIncluded,
4753
isYarnWs,
48-
pkgs,
4954
rootPath: monoRootPath,
55+
srcPkgPaths,
5056
};
5157
};
5258

packages/react-scripts/config/paths.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ if (checkForMonorepo) {
128128
// the monorepo as if they are app source
129129
const mono = findMonorepo(appDirectory);
130130
if (mono.isAppIncluded) {
131-
Array.prototype.push.apply(module.exports.srcPaths, mono.pkgs);
131+
Array.prototype.push.apply(module.exports.srcPaths, mono.srcPkgPaths);
132132
module.exports.isMonorepo = true;
133133
module.exports.monorepoRoot = mono.rootPath;
134134
}

packages/react-scripts/fixtures/monorepos/packages/cra-app1/package.json renamed to packages/react-scripts/fixtures/monorepos/yarn-ws/packages/cra-app1/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"comp2": "^1.0.0",
7+
"nwbcomp": ">0.0.0",
78
"react": "^16.2.0",
89
"react-dom": "^16.2.0"
910
},
@@ -28,5 +29,8 @@
2829
"Firefox ESR",
2930
"not ie < 11"
3031
]
31-
}
32+
},
33+
"sourceWorkspaces": [
34+
"packages/comp*"
35+
]
3236
}

packages/react-scripts/fixtures/monorepos/packages/cra-app1/src/App.js renamed to packages/react-scripts/fixtures/monorepos/yarn-ws/packages/cra-app1/src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import logo from './logo.svg';
33
import './App.css';
44

55
import Comp2 from 'comp2';
6+
import NwbComp from 'nwbcomp';
67

78
class App extends Component {
89
render() {
@@ -16,6 +17,7 @@ class App extends Component {
1617
To get started, edit <code>src/App.js</code> and save to reload.
1718
</p>
1819
<Comp2 />
20+
<NwbComp />
1921
</div>
2022
);
2123
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/coverage
2+
/demo/dist
3+
/node_modules
4+
/umd
5+
npm-debug.log*
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sudo: false
2+
3+
language: node_js
4+
node_js:
5+
- 8
6+
7+
before_install:
8+
- npm install codecov.io coveralls
9+
10+
after_success:
11+
- cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js
12+
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
13+
14+
branches:
15+
only:
16+
- master
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function _classCallCheck(instance, Constructor) {
2+
if (!(instance instanceof Constructor)) {
3+
throw new TypeError('Cannot call a class as a function');
4+
}
5+
}
6+
7+
function _possibleConstructorReturn(self, call) {
8+
if (!self) {
9+
throw new ReferenceError(
10+
"this hasn't been initialised - super() hasn't been called"
11+
);
12+
}
13+
return call && (typeof call === 'object' || typeof call === 'function')
14+
? call
15+
: self;
16+
}
17+
18+
function _inherits(subClass, superClass) {
19+
if (typeof superClass !== 'function' && superClass !== null) {
20+
throw new TypeError(
21+
'Super expression must either be null or a function, not ' +
22+
typeof superClass
23+
);
24+
}
25+
subClass.prototype = Object.create(superClass && superClass.prototype, {
26+
constructor: {
27+
value: subClass,
28+
enumerable: false,
29+
writable: true,
30+
configurable: true,
31+
},
32+
});
33+
if (superClass)
34+
Object.setPrototypeOf
35+
? Object.setPrototypeOf(subClass, superClass)
36+
: (subClass.__proto__ = superClass);
37+
}
38+
39+
import React, { Component } from 'react';
40+
41+
var _default = (function(_Component) {
42+
_inherits(_default, _Component);
43+
44+
function _default() {
45+
_classCallCheck(this, _default);
46+
47+
return _possibleConstructorReturn(this, _Component.apply(this, arguments));
48+
}
49+
50+
_default.prototype.render = function render() {
51+
return React.createElement(
52+
'div',
53+
null,
54+
React.createElement('h2', null, 'Welcome to React components')
55+
);
56+
};
57+
58+
return _default;
59+
})(Component);
60+
61+
export { _default as default };
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
exports.__esModule = true;
4+
exports.default = undefined;
5+
6+
var _react = require('react');
7+
8+
var _react2 = _interopRequireDefault(_react);
9+
10+
function _interopRequireDefault(obj) {
11+
return obj && obj.__esModule ? obj : { default: obj };
12+
}
13+
14+
function _classCallCheck(instance, Constructor) {
15+
if (!(instance instanceof Constructor)) {
16+
throw new TypeError('Cannot call a class as a function');
17+
}
18+
}
19+
20+
function _possibleConstructorReturn(self, call) {
21+
if (!self) {
22+
throw new ReferenceError(
23+
"this hasn't been initialised - super() hasn't been called"
24+
);
25+
}
26+
return call && (typeof call === 'object' || typeof call === 'function')
27+
? call
28+
: self;
29+
}
30+
31+
function _inherits(subClass, superClass) {
32+
if (typeof superClass !== 'function' && superClass !== null) {
33+
throw new TypeError(
34+
'Super expression must either be null or a function, not ' +
35+
typeof superClass
36+
);
37+
}
38+
subClass.prototype = Object.create(superClass && superClass.prototype, {
39+
constructor: {
40+
value: subClass,
41+
enumerable: false,
42+
writable: true,
43+
configurable: true,
44+
},
45+
});
46+
if (superClass)
47+
Object.setPrototypeOf
48+
? Object.setPrototypeOf(subClass, superClass)
49+
: (subClass.__proto__ = superClass);
50+
}
51+
52+
var _default = (function(_Component) {
53+
_inherits(_default, _Component);
54+
55+
function _default() {
56+
_classCallCheck(this, _default);
57+
58+
return _possibleConstructorReturn(this, _Component.apply(this, arguments));
59+
}
60+
61+
_default.prototype.render = function render() {
62+
return _react2.default.createElement(
63+
'div',
64+
null,
65+
_react2.default.createElement('h2', null, 'Welcome to React components')
66+
);
67+
};
68+
69+
return _default;
70+
})(_react.Component);
71+
72+
exports.default = _default;
73+
module.exports = exports['default'];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
type: 'react-component',
3+
npm: {
4+
esModules: true,
5+
umd: false,
6+
},
7+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "nwbcomp",
3+
"version": "0.1.0",
4+
"description": "nwb React component",
5+
"main": "lib/index.js",
6+
"module": "es/index.js",
7+
"files": [
8+
"css",
9+
"es",
10+
"lib",
11+
"umd"
12+
],
13+
"scripts": {
14+
"build": "nwb build-react-component",
15+
"clean": "nwb clean-module && nwb clean-demo",
16+
"start": "nwb serve-react-demo",
17+
"test": "nwb test-react",
18+
"test:coverage": "nwb test-react --coverage",
19+
"test:watch": "nwb test-react --server"
20+
},
21+
"dependencies": {},
22+
"peerDependencies": {
23+
"react": "16.x"
24+
},
25+
"devDependencies": {
26+
"react": "^16.4.0",
27+
"react-dom": "^16.4.0"
28+
},
29+
"author": "",
30+
"homepage": "",
31+
"license": "MIT",
32+
"repository": "",
33+
"keywords": [
34+
"react-component"
35+
]
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React, { Component } from 'react';
2+
3+
export default class extends Component {
4+
render() {
5+
return (
6+
<div>
7+
<h2>Welcome to React components</h2>
8+
</div>
9+
);
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"env": {
3+
"mocha": true
4+
}
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import expect from 'expect';
2+
import React from 'react';
3+
import { render, unmountComponentAtNode } from 'react-dom';
4+
5+
import Component from 'src/';
6+
7+
describe('Component', () => {
8+
let node;
9+
10+
beforeEach(() => {
11+
node = document.createElement('div');
12+
});
13+
14+
afterEach(() => {
15+
unmountComponentAtNode(node);
16+
});
17+
18+
it('displays a welcome message', () => {
19+
render(<Component />, node, () => {
20+
expect(node.innerHTML).toContain('Welcome to React components');
21+
});
22+
});
23+
});

packages/react-scripts/template/README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,14 +1831,30 @@ monorepo/
18311831
"private": true
18321832
app1/
18331833
package.json:
1834-
"dependencies": ["@myorg/comp1": ">=0.0.0", "react": "^16.2.0"],
1835-
"devDependencies": ["react-scripts": "2.0.0"]
1834+
"dependencies": {
1835+
"@myorg/comp1": ">=0.0.0",
1836+
"react": "^16.2.0"
1837+
},
1838+
"devDependencies": {
1839+
"react-scripts": "2.0.0"
1840+
},
1841+
"sourceWorkspaces" : [
1842+
"comp*"
1843+
]
18361844
src/
18371845
app.js: import comp1 from '@myorg/comp1';
18381846
app2/
18391847
package.json:
1840-
"dependencies": ["@myorg/comp1": ">=0.0.0", "react": "^16.2.0"],
1841-
"devDependencies": ["react-scripts": "2.0.0"]
1848+
"dependencies": {
1849+
"@myorg/comp1": ">=0.0.0",
1850+
"react": "^16.2.0"
1851+
},
1852+
"devDependencies": {
1853+
"react-scripts": "2.0.0"
1854+
},
1855+
"sourceWorkspaces" : [
1856+
"comp*"
1857+
]
18421858
src/
18431859
app.js: import comp1 from '@myorg/comp1';
18441860
comp1/
@@ -1856,7 +1872,8 @@ monorepo/
18561872
```
18571873

18581874
- Monorepo tools work on a package level, the same level as an npm package.
1859-
- The "workspaces" in the top-level package.json is an array of glob patterns specifying where shared packages are located in the monorepo.
1875+
- The "workspaces" entry in the top-level package.json is an array of glob patterns specifying where shared packages are located in the monorepo.
1876+
- The "sourceWorkspaces" entry in an app's package.json is array of glob patterns similar to "workspaces", but specifying which packages in the monorepo should be treated as source.
18601877
- The scoping prefixes, e.g. @myorg/, are not required, but are recommended, allowing you to differentiate your packages from others of the same name. See [scoped packages ](https://docs.npmjs.com/misc/scope) for more info.
18611878
- Using a package in the monorepo is accomplished in the same manner as a published npm package, by specifying the shared package as dependency.
18621879
- In order to pick up the monorepo version of a package, the specified dependency version must semantically match the package version in the monorepo. See [semver](https://docs.npmjs.com/misc/semver) for info on semantic version matching.

tasks/e2e-monorepos.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ function verifyBuild {
109109
pushd "$temp_app_path"
110110
cp -r "$root_path/packages/react-scripts/fixtures/monorepos/yarn-ws" .
111111
cd "yarn-ws"
112-
cp -r "$root_path/packages/react-scripts/fixtures/monorepos/packages" .
113112
yarn
114113

115114
# Test cra-app1

0 commit comments

Comments
 (0)