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

React - use babel presets/plugins based on CRA. #4836

Merged
merged 18 commits into from
Nov 24, 2018
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
2 changes: 1 addition & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
],
env: {
test: {
plugins: ['babel-plugin-require-context-hook'],
plugins: ['babel-plugin-require-context-hook', 'babel-plugin-dynamic-import-node'],
},
},
overrides: [
Expand Down
4 changes: 4 additions & 0 deletions app/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
"prepare": "node ../../scripts/prepare.js"
},
"dependencies": {
"@babel/plugin-transform-react-constant-elements": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/runtime": "^7.1.2",
"@emotion/styled": "^0.10.6",
"@storybook/core": "4.1.0-alpha.7",
"@storybook/node-logger": "4.1.0-alpha.7",
"@svgr/webpack": "^4.0.3",
"babel-plugin-named-asset-import": "^0.2.3",
"babel-plugin-react-docgen": "^2.0.0",
"babel-preset-react-app": "^6.1.0",
"common-tags": "^1.8.0",
"global": "^4.3.2",
"lodash": "^4.17.11",
Expand Down
36 changes: 28 additions & 8 deletions app/react/src/server/cra-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,30 @@ const cssModuleExtensions = ['.module.css', '.module.scss', '.module.sass'];
const typeScriptExtensions = ['.ts', '.tsx'];

let reactScriptsPath;

export function getReactScriptsPath({ noCache } = {}) {
if (reactScriptsPath && !noCache) return reactScriptsPath;

const appDirectory = fs.realpathSync(process.cwd());
const reactScriptsScriptPath = fs.realpathSync(
path.join(appDirectory, '/node_modules/.bin/react-scripts')
);

reactScriptsPath = path.join(reactScriptsScriptPath, '../..');
const scriptsPkgJson = path.join(reactScriptsPath, 'package.json');

if (!fs.existsSync(scriptsPkgJson)) {
reactScriptsPath = 'react-scripts';
}

return reactScriptsPath;
}

export function isReactScriptsInstalled(requiredVersion = '2.0.0') {
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
// eslint-disable-next-line import/no-dynamic-require,global-require
const reactScriptsJson = require(path.join(getReactScriptsPath(), 'package.json'));
if (semver.lt(reactScriptsJson.version, requiredVersion)) return false;
return true;
return !semver.lt(reactScriptsJson.version, requiredVersion);
} catch (e) {
return false;
}
Expand Down Expand Up @@ -57,13 +65,25 @@ const getStyleRules = getRules(cssExtensions.concat(cssModuleExtensions));
const getTypeScriptRules = getRules(typeScriptExtensions);

export function getCraWebpackConfig(mode) {
if (mode === 'production') {
// eslint-disable-next-line global-require, import/no-dynamic-require
return require(path.join(getReactScriptsPath(), 'config/webpack.config.prod'));
const pathToReactScripts = getReactScriptsPath();

const craWebpackConfig =
mode === 'production' ? 'config/webpack.config.prod' : 'config/webpack.config.dev';
igor-dv marked this conversation as resolved.
Show resolved Hide resolved

let pathToWebpackConfig = require.resolve(path.join(pathToReactScripts, craWebpackConfig));

if (!fs.existsSync(pathToWebpackConfig)) {
pathToWebpackConfig = path.join(pathToReactScripts, 'config/webpack.config');
}

// eslint-disable-next-line import/no-dynamic-require,global-require
const webpackConfig = require(pathToWebpackConfig);

if (typeof webpackConfig === 'function') {
return webpackConfig(mode);
}

// eslint-disable-next-line global-require, import/no-dynamic-require
return require(path.join(getReactScriptsPath(), 'config/webpack.config.dev'));
return webpackConfig;
}

export function applyCRAWebpackConfig(baseConfig) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fs from 'fs';
import { getReactScriptsPath } from '../cra-config';
import { getReactScriptsPath } from './cra-config';

jest.mock('fs', () => ({
realpathSync: jest.fn(),
existsSync: () => true,
}));
jest.mock('mini-css-extract-plugin', () => {});

Expand Down
13 changes: 0 additions & 13 deletions app/react/src/server/framework-preset-cra-rules.js

This file was deleted.

36 changes: 36 additions & 0 deletions app/react/src/server/framework-preset-cra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { logger } from '@storybook/node-logger';
import { applyCRAWebpackConfig, isReactScriptsInstalled } from './cra-config';

export function webpackFinal(config) {
if (!isReactScriptsInstalled()) {
logger.info('=> Using base config because react-scripts is not installed.');
return config;
}

logger.info('=> Loading create-react-app config.');

return applyCRAWebpackConfig(config);
}

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

return {
...config,
presets: [require.resolve('babel-preset-react-app')],
plugins: [
[
require.resolve('babel-plugin-named-asset-import'),
{
loaderMap: {
svg: {
ReactComponent: '@svgr/webpack?-prettier,-svgo![path]',
},
},
},
],
],
};
}
2 changes: 1 addition & 1 deletion app/react/src/server/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
defaultConfigName: 'create-react-app',
frameworkPresets: [
require.resolve('./framework-preset-react.js'),
require.resolve('./framework-preset-cra.js'),
require.resolve('./framework-preset-react-docgen.js'),
require.resolve('./framework-preset-cra-rules.js'),
],
};
30 changes: 16 additions & 14 deletions examples/cra-kitchen-sink/.storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { configure } from '@storybook/react';
import { setOptions } from '@storybook/addon-options';
import { configure, addDecorator } from '@storybook/react';
import { withOptions } from '@storybook/addon-options';

setOptions({
name: 'CRA Kitchen Sink',
url: 'https://github.com/storybooks/storybook/tree/master/examples/cra-kitchen-sink',
goFullScreen: false,
showAddonsPanel: true,
showSearchBox: false,
addonPanelInRight: true,
sortStoriesByKind: false,
hierarchySeparator: /\./,
hierarchyRootSeparator: /\|/,
enableShortcuts: true,
});
addDecorator(
withOptions({
name: 'CRA Kitchen Sink',
url: 'https://github.com/storybooks/storybook/tree/master/examples/cra-kitchen-sink',
goFullScreen: false,
showAddonsPanel: true,
showSearchBox: false,
addonPanelInRight: true,
sortStoriesByKind: false,
hierarchySeparator: /\./,
hierarchyRootSeparator: /\|/,
enableShortcuts: true,
})
);

function loadStories() {
// put welcome screen at the top of the list so it's the first one displayed
Expand Down
10 changes: 10 additions & 0 deletions examples/cra-kitchen-sink/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@ const config = require('../../jest.config');
module.exports = {
...config,
roots: [__dirname],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
'\\.(css|scss)$': '<rootDir>/__mocks__/styleMock.js',
'\\.(md)$': '<rootDir>/__mocks__/htmlMock.js',
},
transform: {
...config.transform,
'^.+\\.svg$': '<rootDir>/node_modules/react-scripts/config/jest/fileTransform.js',
},
moduleDirectories: ['<rootDir>/node_modules', 'src'],
};
7 changes: 7 additions & 0 deletions examples/cra-kitchen-sink/src/stories/App.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';
import { storiesOf } from '@storybook/react';

// eslint-disable-next-line import/extensions,import/no-unresolved
import App from 'App';

storiesOf('App', module).add('full app', () => <App />);
5 changes: 5 additions & 0 deletions examples/cra-kitchen-sink/src/stories/Lifecycle.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import LifecycleLogger from '../components/LifecycleLogger';

storiesOf('Lifecycle', module).add('logging', () => <LifecycleLogger />);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots App full app 1`] = `
<div
class="App"
>
<div
class="App-header"
>
<img
alt="logo"
class="App-logo"
src="logo.svg"
/>
<h2>
Welcome to React
</h2>
</div>
<p
class="App-intro"
>
To get started, edit
<code>
src/App.js
</code>
and save to reload.
</p>
</div>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Lifecycle logging 1`] = `
<div>
Lifecycle methods are logged to the console
</div>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots CRA Dynamic import 1`] = `
<div>
Waiting for Dynamic Import
</div>
`;

exports[`Storyshots CRA Svgr 1`] = `
<svg>
logo.svg
</svg>
`;
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots App full app 1`] = `
<div
class="App"
>
<div
class="App-header"
>
<img
alt="logo"
class="App-logo"
src="file-stub"
/>
<h2>
Welcome to React
</h2>
</div>
<p
class="App-intro"
>
To get started, edit
<code>
src/App.js
</code>
and save to reload.
</p>
</div>
`;

exports[`Storyshots Button addons composition 1`] = `
.emotion-4 {
display: -webkit-box;
Expand Down Expand Up @@ -168,13 +140,6 @@ exports[`Storyshots Button addons composition 1`] = `
&gt;
</span>
</div>
<div
style="padding-left:33px;padding-right:3px"
>
<span
style="color:#444"
/>
</div>
<div
style="padding-left:33px;padding-right:3px"
>
Expand Down Expand Up @@ -445,13 +410,6 @@ exports[`Storyshots Button with new info 1`] = `
&gt;
</span>
</div>
<div
style="padding-left:33px;padding-right:3px"
>
<span
style="color:#444"
/>
</div>
<div
style="padding-left:33px;padding-right:3px"
>
Expand Down Expand Up @@ -768,26 +726,3 @@ exports[`Storyshots Button with text 1`] = `
Hello Button
</button>
`;

exports[`Storyshots Lifecycle logging 1`] = `
<div>
Lifecycle methods are logged to the console
</div>
`;

exports[`Storyshots Some really long story kind description with text 1`] = `
<div
style="position:fixed;top:0;left:0;bottom:0;right:0;display:flex;align-items:center;overflow:auto"
>
<div
style="margin:auto;max-height:100%"
>
<button
style="border:1px solid #eee;border-radius:3px;background-color:#FFFFFF;cursor:pointer;font-size:15px;padding:3px 10px;margin:10px"
type="button"
>
Hello Button
</button>
</div>
</div>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Some really long story kind description with text 1`] = `
<div
style="position:fixed;top:0;left:0;bottom:0;right:0;display:flex;align-items:center;overflow:auto"
>
<div
style="margin:auto;max-height:100%"
>
<button
style="border:1px solid #eee;border-radius:3px;background-color:#FFFFFF;cursor:pointer;font-size:15px;padding:3px 10px;margin:10px"
type="button"
>
Hello Button
</button>
</div>
</div>
`;