Skip to content

Commit d1d4546

Browse files
Use craSourceWorkspaces from app's package.json to opt packages in as source.
1 parent a98c3df commit d1d4546

30 files changed

+262
-9
lines changed

packages/react-dev-utils/workspaceUtils.js

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

3232
const findMonorepo = appDir => {
33+
const appPkg = require(path.resolve(appDir, 'package.json'));
3334
const monoPkgPath = findPkg.sync(path.resolve(appDir, '..'));
3435
const monoPkg = monoPkgPath && require(monoPkgPath);
3536
const workspaces = monoPkg && monoPkg.workspaces;
3637
const patterns = (workspaces && workspaces.packages) || workspaces;
3738
const isYarnWs = Boolean(patterns);
39+
const srcPatterns = appPkg && appPkg.craSourceWorkspaces;
3840
const allPkgs = patterns && findPkgs(path.dirname(monoPkgPath), patterns);
41+
const allSrcPkgs =
42+
srcPatterns && findPkgs(path.dirname(monoPkgPath), srcPatterns);
3943
const isIncluded = dir => allPkgs && allPkgs.indexOf(dir) !== -1;
4044
const isAppIncluded = isIncluded(appDir);
41-
const pkgs = allPkgs
42-
? allPkgs.filter(f => fs.realpathSync(f) !== appDir)
45+
const srcPkgPaths = allSrcPkgs
46+
? allSrcPkgs.filter(f => fs.realpathSync(f) !== appDir)
4347
: [];
4448

4549
return {
4650
isAppIncluded,
4751
isYarnWs,
48-
pkgs,
52+
srcPkgPaths,
4953
};
5054
};
5155

packages/react-scripts/config/paths.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ if (checkForMonorepo) {
125125
// the monorepo as if they are app source
126126
const mono = findMonorepo(appDirectory);
127127
if (mono.isAppIncluded) {
128-
Array.prototype.push.apply(module.exports.srcPaths, mono.pkgs);
128+
Array.prototype.push.apply(module.exports.srcPaths, mono.srcPkgPaths);
129129
}
130130
module.exports.useYarn = module.exports.useYarn || mono.isYarnWs;
131131
}

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+
"craSourceWorkspaces": [
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,10 @@ monorepo/
18841884
},
18851885
"devDependencies": {
18861886
"react-scripts": "2.0.0"
1887-
}
1887+
},
1888+
"craSourceWorkspaces" : [
1889+
"comp*"
1890+
]
18881891
src/
18891892
app.js: import comp1 from '@myorg/comp1';
18901893
app2/
@@ -1895,7 +1898,10 @@ monorepo/
18951898
},
18961899
"devDependencies": {
18971900
"react-scripts": "2.0.0"
1898-
}
1901+
},
1902+
"craSourceWorkspaces" : [
1903+
"comp*"
1904+
]
18991905
src/
19001906
app.js: import comp1 from '@myorg/comp1';
19011907
comp1/
@@ -1916,7 +1922,8 @@ monorepo/
19161922
index.js: import comp1 from '@myorg/comp1'
19171923
```
19181924
* Monorepo tools work on a package level, the same level as an npm package.
1919-
* The "workspaces" in the top-level package.json is an array of glob patterns specifying where shared packages are located in the monorepo.
1925+
* The "workspaces" entry in the top-level package.json is an array of glob patterns specifying where shared packages are located in the monorepo.
1926+
* The "craSourceWorkspaces" 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.
19201927
* 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.
19211928
* Using a package in the monorepo is accomplished in the same manner as a published npm package, by specifying the shared package as dependency.
19221929
* 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
@@ -108,7 +108,6 @@ function verifyBuild {
108108
pushd "$temp_app_path"
109109
cp -r "$root_path/packages/react-scripts/fixtures/monorepos/yarn-ws" .
110110
cd "yarn-ws"
111-
cp -r "$root_path/packages/react-scripts/fixtures/monorepos/packages" .
112111
yarn
113112

114113
# Test cra-app1

0 commit comments

Comments
 (0)