Skip to content

Commit

Permalink
Support for multiple source paths via package.json srcPaths entry.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Brad Lemley authored and bradfordlemley committed Jan 22, 2018
1 parent 726e5f9 commit bf2d814
Show file tree
Hide file tree
Showing 9 changed files with 10,227 additions and 18 deletions.
713 changes: 713 additions & 0 deletions packages/create-react-app/yarn.lock

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions packages/react-scripts/config/jest/babelTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const babelJest = require('babel-jest');
const paths = require('../paths');

module.exports = babelJest.createTransformer({
const babelTransformer = babelJest.createTransformer({
presets: [require.resolve('babel-preset-react-app')],
babelrc: false,
});

// choose files to transpile
// jest < 22.0.x transform patterns don't resolve symlinks, ie. don't match against realpaths
// jest >= 22.0.04 matches against realpaths, but this method might be preferable anyway
const babelProcess = babelTransformer.process;
babelTransformer.process = (src, filename, config, transformOptions) => {
if (paths.shouldTranspile(filename)) {
return babelProcess(src, filename, config, transformOptions);
}
return src;
};

module.exports = babelTransformer;
54 changes: 46 additions & 8 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const path = require('path');
const fs = require('fs');
const url = require('url');
const findPkg = require('find-pkg');

// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
Expand Down Expand Up @@ -86,17 +87,12 @@ 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();

// config before publish: we're in ./packages/react-scripts/config/
// if appDirectory (cwd) is the create-react-app repo root, use template files.
if (
!reactScriptsLinked &&
__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
fs.existsSync(path.join(appDirectory, 'packages', 'react-scripts', 'config'))
) {
const appPackageJson = require(resolveOwn('package.json'));
module.exports = {
dotenv: resolveOwn('template/.env'),
appPath: resolveApp('.'),
Expand All @@ -117,3 +113,45 @@ if (
};
}
// @remove-on-eject-end

const isInPath = (fpath, searchPaths) => {
for (let i = 0; i < searchPaths.length; i++) {
if (fpath.startsWith(searchPaths[i] + path.sep)) {
return true;
}
}
return false;
};

const hasNodeModules = /[/\\\\]node_modules[/\\\\]/;

// treat as source if realpath is in a srcPath, but not in a node_modules dir
const isSrcFile = srcFile => {
const realSrc = fs.realpathSync(srcFile);
return (
!realSrc.match(hasNodeModules) && isInPath(realSrc, module.exports.srcPaths)
);
};

module.exports.srcPaths = [module.exports.appSrc];

// if app is in a monorepo (lerna or yarn workspace), allow any module inside
// the monorepo to be linted, transpiled, and tested as if it came from app's
// src.
const monoPkgPath = findPkg.sync(resolveApp('..'));
if (monoPkgPath) {
const monoRoot = path.dirname(monoPkgPath);
const monoPkgJson = require(monoPkgPath);
if (monoPkgJson.workspaces) {
module.exports.yarnWorkspaceRoot = monoRoot;
module.exports.srcPaths.push(monoRoot);
} else if (
fs.existsSync(path.resolve(path.dirname(monoPkgPath), 'lerna.json'))
) {
module.exports.lernaRoot = monoRoot;
module.exports.srcPaths.push(monoRoot);
}
}

module.exports.shouldLint = modPath => isSrcFile(modPath);
module.exports.shouldTranspile = modPath => isSrcFile(modPath);
4 changes: 2 additions & 2 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ module.exports = {
loader: require.resolve('eslint-loader'),
},
],
include: paths.appSrc,
include: paths.shouldLint,
},
{
// "oneOf" will traverse all following loaders until one will
Expand All @@ -178,7 +178,7 @@ module.exports = {
// The preset includes JSX, Flow, and some ESnext features.
{
test: /\.(js|jsx|mjs)$/,
include: paths.appSrc,
include: paths.shouldTranspile,
use: [
// This loader parallelizes code compilation, it is optional but
// improves compile time on larger projects
Expand Down
4 changes: 2 additions & 2 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ module.exports = {
loader: require.resolve('eslint-loader'),
},
],
include: paths.appSrc,
include: paths.shouldLint,
},
{
// "oneOf" will traverse all following loaders until one will
Expand All @@ -186,7 +186,7 @@ module.exports = {
// The preset includes JSX, Flow, and some ESnext features.
{
test: /\.(js|jsx|mjs)$/,
include: paths.appSrc,
include: paths.shouldTranspile,
use: [
// This loader parallelizes code compilation, it is optional but
// improves compile time on larger projects
Expand Down
Loading

0 comments on commit bf2d814

Please sign in to comment.