mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-04-27 00:14:57 +08:00
Add support for yarn and lerna monorepos. (#3741)
* Support for multiple source paths via package.json srcPaths entry. Initial support for yarn workspace. Support lerna workspace, fix for when to use template files. Remove support for specifying srcPaths in package.json. Re-enable transpilation caching. * Clean up, use file matching (similar to original) in webpack configs instead of matching function. * Remove package lock files. * Fix for eject. Note: monorepos won't work after eject. Can be fixed easily with JEST 22.0.?+ which has file pattern matches against realpaths. * Filter tests to run only tests in monorepo components included by the app. (Not sure this is desireable, might be cool to be able to easily run all tests in monorepo from one app.) * Fix conditions for when to use template. * Fix eject. * Remove code that is not needed w/ Jest 22. * Include all cra-comp tests in monorepo instead of trying to include only tests that are dependencies of app. (tests can be easily filtered via jest cli if desired, e.g. 'npm test -- myapp comp1') * Pin find-pkg version. * Hopefully fix jest test file matching on windows by removing first slash. * E2E tests for monorepo. * Run monorepo tests in CI. * Fix and test post-eject build. * Fix e2e test. * Fix test suite names in appveyor. * Include individual package dirs as srcPaths instead of top-level monorepo root. Fixes build/start after eject. * Fix running tests after eject. * Clean up test workspace, add some verifcations. * Cleanup. * Try to fix hang when running test on appveyor. * Don't write babel or lint config to package.json when ejecting. * Incorporate review comments. * Simply monorepo pkg finder * Only include monorepo pkgs if app itself is included in monorepo * Check for specific tests in e2e * Fixes for windows. * Fix for kitchensink mocha tests compiling. * Add lerna monorepo test. * Fix lerna bootstrap on windows. * Incorporate more review comments: * remove support for lerna w/o yarn workspace * add react and react-dom as devDeps to comp1 and comp2 * Add monorepo info to user guide.
This commit is contained in:
committed by
Dan Abramov
parent
5348d6eecf
commit
b43ad04b88
@@ -1,15 +1,18 @@
|
||||
// @remove-file-on-eject
|
||||
// @remove-on-eject-begin
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
// @remove-on-eject-end
|
||||
'use strict';
|
||||
|
||||
const babelJest = require('babel-jest');
|
||||
|
||||
module.exports = babelJest.createTransformer({
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
// @remove-on-eject-begin
|
||||
babelrc: false,
|
||||
// @remove-on-eject-end
|
||||
});
|
||||
|
||||
57
packages/react-scripts/config/paths.js
vendored
57
packages/react-scripts/config/paths.js
vendored
@@ -11,6 +11,8 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const url = require('url');
|
||||
const findPkg = require('find-pkg');
|
||||
const globby = require('globby');
|
||||
|
||||
// Make sure any symlinks in the project folder are resolved:
|
||||
// https://github.com/facebook/create-react-app/issues/637
|
||||
@@ -63,6 +65,8 @@ module.exports = {
|
||||
servedPath: getServedPath(resolveApp('package.json')),
|
||||
};
|
||||
|
||||
let checkForMonorepo = true;
|
||||
|
||||
// @remove-on-eject-begin
|
||||
const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
|
||||
|
||||
@@ -86,17 +90,13 @@ module.exports = {
|
||||
ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
|
||||
};
|
||||
|
||||
const ownPackageJson = require('../package.json');
|
||||
const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
|
||||
const reactScriptsLinked =
|
||||
fs.existsSync(reactScriptsPath) &&
|
||||
fs.lstatSync(reactScriptsPath).isSymbolicLink();
|
||||
// detect if template should be used, ie. when cwd is react-scripts itself
|
||||
const useTemplate =
|
||||
appDirectory === fs.realpathSync(path.join(__dirname, '..'));
|
||||
|
||||
// config before publish: we're in ./packages/react-scripts/config/
|
||||
if (
|
||||
!reactScriptsLinked &&
|
||||
__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
|
||||
) {
|
||||
checkForMonorepo = !useTemplate;
|
||||
|
||||
if (useTemplate) {
|
||||
module.exports = {
|
||||
dotenv: resolveOwn('template/.env'),
|
||||
appPath: resolveApp('.'),
|
||||
@@ -117,3 +117,40 @@ if (
|
||||
};
|
||||
}
|
||||
// @remove-on-eject-end
|
||||
|
||||
module.exports.srcPaths = [module.exports.appSrc];
|
||||
|
||||
const findPkgs = (rootPath, globPatterns) => {
|
||||
const globOpts = {
|
||||
cwd: rootPath,
|
||||
strict: true,
|
||||
absolute: true,
|
||||
};
|
||||
return globPatterns
|
||||
.reduce(
|
||||
(pkgs, pattern) =>
|
||||
pkgs.concat(globby.sync(path.join(pattern, 'package.json'), globOpts)),
|
||||
[]
|
||||
)
|
||||
.map(f => path.dirname(path.normalize(f)));
|
||||
};
|
||||
|
||||
const getMonorepoPkgPaths = () => {
|
||||
const monoPkgPath = findPkg.sync(path.resolve(appDirectory, '..'));
|
||||
if (monoPkgPath) {
|
||||
// get monorepo config from yarn workspace
|
||||
const pkgPatterns = require(monoPkgPath).workspaces;
|
||||
const pkgPaths = findPkgs(path.dirname(monoPkgPath), pkgPatterns);
|
||||
// only include monorepo pkgs if app itself is included in monorepo
|
||||
if (pkgPaths.indexOf(appDirectory) !== -1) {
|
||||
return pkgPaths.filter(f => fs.realpathSync(f) !== appDirectory);
|
||||
}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
if (checkForMonorepo) {
|
||||
// if app is in a monorepo (lerna or yarn workspace), treat other packages in
|
||||
// the monorepo as if they are app source
|
||||
Array.prototype.push.apply(module.exports.srcPaths, getMonorepoPkgPaths());
|
||||
}
|
||||
|
||||
@@ -145,10 +145,10 @@ module.exports = {
|
||||
options: {
|
||||
formatter: eslintFormatter,
|
||||
eslintPath: require.resolve('eslint'),
|
||||
// @remove-on-eject-begin
|
||||
baseConfig: {
|
||||
extends: [require.resolve('eslint-config-react-app')],
|
||||
},
|
||||
// @remove-on-eject-begin
|
||||
ignore: false,
|
||||
useEslintrc: false,
|
||||
// @remove-on-eject-end
|
||||
@@ -156,7 +156,8 @@ module.exports = {
|
||||
loader: require.resolve('eslint-loader'),
|
||||
},
|
||||
],
|
||||
include: paths.appSrc,
|
||||
include: paths.srcPaths,
|
||||
exclude: [/[/\\\\]node_modules[/\\\\]/],
|
||||
},
|
||||
{
|
||||
// "oneOf" will traverse all following loaders until one will
|
||||
@@ -178,7 +179,8 @@ module.exports = {
|
||||
// The preset includes JSX, Flow, and some ESnext features.
|
||||
{
|
||||
test: /\.(js|jsx|mjs)$/,
|
||||
include: paths.appSrc,
|
||||
include: paths.srcPaths,
|
||||
exclude: [/[/\\\\]node_modules[/\\\\]/],
|
||||
use: [
|
||||
// This loader parallelizes code compilation, it is optional but
|
||||
// improves compile time on larger projects
|
||||
@@ -188,8 +190,8 @@ module.exports = {
|
||||
options: {
|
||||
// @remove-on-eject-begin
|
||||
babelrc: false,
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
// @remove-on-eject-end
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
// This is a feature of `babel-loader` for webpack (not Babel itself).
|
||||
// It enables caching results in ./node_modules/.cache/babel-loader/
|
||||
// directory for faster rebuilds.
|
||||
@@ -275,8 +277,8 @@ module.exports = {
|
||||
options: {
|
||||
// @remove-on-eject-begin
|
||||
babelrc: false,
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
// @remove-on-eject-end
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
cacheDirectory: true,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -152,12 +152,12 @@ module.exports = {
|
||||
options: {
|
||||
formatter: eslintFormatter,
|
||||
eslintPath: require.resolve('eslint'),
|
||||
// @remove-on-eject-begin
|
||||
// TODO: consider separate config for production,
|
||||
// e.g. to enable no-console and no-debugger only in production.
|
||||
baseConfig: {
|
||||
extends: [require.resolve('eslint-config-react-app')],
|
||||
},
|
||||
// @remove-on-eject-begin
|
||||
ignore: false,
|
||||
useEslintrc: false,
|
||||
// @remove-on-eject-end
|
||||
@@ -165,7 +165,8 @@ module.exports = {
|
||||
loader: require.resolve('eslint-loader'),
|
||||
},
|
||||
],
|
||||
include: paths.appSrc,
|
||||
include: paths.srcPaths,
|
||||
exclude: [/[/\\\\]node_modules[/\\\\]/],
|
||||
},
|
||||
{
|
||||
// "oneOf" will traverse all following loaders until one will
|
||||
@@ -186,7 +187,8 @@ module.exports = {
|
||||
// The preset includes JSX, Flow, and some ESnext features.
|
||||
{
|
||||
test: /\.(js|jsx|mjs)$/,
|
||||
include: paths.appSrc,
|
||||
include: paths.srcPaths,
|
||||
exclude: [/[/\\\\]node_modules[/\\\\]/],
|
||||
use: [
|
||||
// This loader parallelizes code compilation, it is optional but
|
||||
// improves compile time on larger projects
|
||||
@@ -196,8 +198,8 @@ module.exports = {
|
||||
options: {
|
||||
// @remove-on-eject-begin
|
||||
babelrc: false,
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
// @remove-on-eject-end
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
compact: true,
|
||||
highlightCode: true,
|
||||
},
|
||||
@@ -317,8 +319,8 @@ module.exports = {
|
||||
options: {
|
||||
// @remove-on-eject-begin
|
||||
babelrc: false,
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
// @remove-on-eject-end
|
||||
presets: [require.resolve('babel-preset-react-app')],
|
||||
cacheDirectory: true,
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user