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

Use babel-loader from react-scripts #5308

Merged
merged 1 commit into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/react/src/server/__snapshots__/cra-config.test.js.snap
Expand Up @@ -47,6 +47,12 @@ Object {
".jsx",
],
},
"resolveLoader": Object {
"modules": Array [
"node_modules",
"/app/react/src/server/__mocks__/react-scripts-2-0-0/node_modules",
],
},
}
`;

Expand Down Expand Up @@ -110,5 +116,11 @@ Object {
".tsx",
],
},
"resolveLoader": Object {
"modules": Array [
"node_modules",
"/app/react/src/server/__mocks__/react-scripts-2-1-0/node_modules",
],
},
}
`;
5 changes: 4 additions & 1 deletion app/react/src/server/cra-config.js
Expand Up @@ -60,7 +60,7 @@ export function isReactScriptsInstalled(requiredVersion = '2.0.0') {
try {
// eslint-disable-next-line import/no-dynamic-require,global-require
const reactScriptsJson = require(path.join(getReactScriptsPath(), 'package.json'));
return !semver.lt(reactScriptsJson.version, requiredVersion);
return !semver.gtr(requiredVersion, reactScriptsJson.version);
} catch (e) {
return false;
}
Expand Down Expand Up @@ -176,5 +176,8 @@ export function applyCRAWebpackConfig(baseConfig, configDir) {
...baseConfig.resolve,
extensions: [...baseConfig.resolve.extensions, ...tsExtensions],
},
resolveLoader: {
modules: ['node_modules', path.join(getReactScriptsPath(), 'node_modules')],
},
};
}
14 changes: 11 additions & 3 deletions app/react/src/server/cra-config.test.js
Expand Up @@ -13,6 +13,8 @@ jest.mock('mini-css-extract-plugin', () => {});

const SCRIPT_PATH = '.bin/react-scripts';

const stripCwd = loaderPath => loaderPath.replace(process.cwd(), '');

describe('cra-config', () => {
beforeEach(() => {
fs.realpathSync.mockReset();
Expand Down Expand Up @@ -64,7 +66,7 @@ esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../custom-react-scripts/bin/react-scripts.js" "$@"
ret=$?
else
else
node "$basedir/../custom-react-scripts/bin/react-scripts.js" "$@"
ret=$?
fi
Expand Down Expand Up @@ -103,7 +105,10 @@ exit $ret`
});

it('should apply styling webpack rules', () => {
expect(applyCRAWebpackConfig(mockConfig, '/test-project')).toMatchSnapshot();
const webpackConfig = applyCRAWebpackConfig(mockConfig, '/test-project');
// We don't want full paths in snapshots.
webpackConfig.resolveLoader.modules = webpackConfig.resolveLoader.modules.map(stripCwd);
expect(webpackConfig).toMatchSnapshot();
});
});

Expand All @@ -116,7 +121,10 @@ exit $ret`
});

it('should apply Babel, styling rules and merge plugins', () => {
expect(applyCRAWebpackConfig(mockConfig, '/test-project')).toMatchSnapshot();
const webpackConfig = applyCRAWebpackConfig(mockConfig, '/test-project');
// We don't want full paths in snapshots.
webpackConfig.resolveLoader.modules = webpackConfig.resolveLoader.modules.map(stripCwd);
expect(webpackConfig).toMatchSnapshot();
});
});
});
16 changes: 15 additions & 1 deletion app/react/src/server/framework-preset-cra.js
@@ -1,5 +1,6 @@
import path from 'path';
import { logger } from '@storybook/node-logger';
import { applyCRAWebpackConfig, isReactScriptsInstalled } from './cra-config';
import { applyCRAWebpackConfig, getReactScriptsPath, isReactScriptsInstalled } from './cra-config';

export function webpackFinal(config, { configDir }) {
if (!isReactScriptsInstalled()) {
Expand All @@ -12,6 +13,19 @@ export function webpackFinal(config, { configDir }) {
return applyCRAWebpackConfig(config, configDir);
}

export function managerWebpack(config) {
if (!isReactScriptsInstalled()) {
return config;
}

return {
...config,
resolveLoader: {
modules: ['node_modules', path.join(getReactScriptsPath(), 'node_modules')],
},
};
}

export function babelDefault(config) {
if (!isReactScriptsInstalled()) {
return config;
Expand Down
6 changes: 5 additions & 1 deletion lib/cli/generators/REACT_SCRIPTS/index.js
@@ -1,6 +1,7 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import fs from 'fs';
import semver from 'semver';
import { getVersions, getPackageJson, writePackageJson, installBabel } from '../../lib/helpers';

export default async npmOptions => {
Expand All @@ -24,7 +25,10 @@ export default async npmOptions => {
packageJson.devDependencies['@storybook/addon-links'] = linksVersion;
packageJson.devDependencies['@storybook/addons'] = addonsVersion;

await installBabel(npmOptions, packageJson);
// When working with `create-react-app@>=2.0.0`, we know `babel-loader` is installed.
if (semver.gtr('2.0.0', packageJson.dependencies['react-scripts'])) {
await installBabel(npmOptions, packageJson);
}

packageJson.scripts.storybook = 'start-storybook -p 9009';
packageJson.scripts['build-storybook'] = 'build-storybook';
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/server/build-static.js
Expand Up @@ -122,6 +122,7 @@ async function buildManager(configType, outputDir, configDir, options) {
outputDir,
configDir,
corePresets: [require.resolve('./manager/manager-preset.js')],
frameworkPresets: options.frameworkPresets,
});

if (options.debugWebpack) {
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/server/manager/manager-config.js
Expand Up @@ -9,11 +9,12 @@ async function getManagerWebpackConfig(options, presets) {
}

export default async options => {
const { corePresets = [], overridePresets = [], ...restOptions } = options;
const { corePresets = [], frameworkPresets = [], overridePresets = [], ...restOptions } = options;

const presetsConfig = [
...corePresets,
require.resolve('../common/babel-cache-preset.js'),
...frameworkPresets,
...loadCustomPresets(options),
...overridePresets,
];
Expand Down
14 changes: 9 additions & 5 deletions lib/core/src/server/utils/load-custom-babel-config.js
Expand Up @@ -47,11 +47,15 @@ function loadFromPath(babelConfigPath) {
}

function isBabelLoader8() {
// eslint-disable-next-line import/no-dynamic-require,global-require
const babelLoaderPkg = require(resolveSync('babel-loader/package.json', {
basedir: process.cwd(),
}));
return satisfies(babelLoaderPkg.version, '>=8.0.0-0');
try {
// eslint-disable-next-line import/no-dynamic-require,global-require
const babelLoaderPkg = require(resolveSync('babel-loader/package.json', {
basedir: process.cwd(),
}));
return satisfies(babelLoaderPkg.version, '>=8.0.0-0');
} catch (e) {
return false;
Copy link
Member

Choose a reason for hiding this comment

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

In what case there will be an error here ? do we want to hide it ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question @igor-dv. This happens when babel-loader isn't in the root of node_modules. When testing, this was sufficient to handle the error and still load successfully.

}
}

export default async function(configDir, getDefaultConfig) {
Expand Down