Skip to content

Commit

Permalink
Replace require_context.js with babel-plugin-require-context-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-dv committed Jun 12, 2018
1 parent 86f37eb commit 2a992ba
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 147 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"presets": ["env", "stage-0", "react"],
"env": {
"test": {
"plugins": ["require-context-hook"]
},
"plugins": [
"emotion",
"babel-plugin-macros",
Expand Down
4 changes: 0 additions & 4 deletions addons/storyshots/storyshots-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
},
"dependencies": {
"@storybook/addons": "4.0.0-alpha.9",
"@storybook/core": "4.0.0-alpha.9",
"babel-runtime": "^6.26.0",
"glob": "^7.1.2",
"global": "^4.3.2",
Expand All @@ -31,8 +30,5 @@
"@storybook/react": "4.0.0-alpha.9",
"enzyme-to-json": "^3.3.4",
"react": "^16.4.0"
},
"peerDependencies": {
"babel-core": "^6.26.0 || ^7.0.0-0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ function load(options) {
setupAngularJestPreset();

const { configPath, config } = options;
const frameworkOptions = '@storybook/angular/options';
const storybook = require.requireActual('@storybook/angular');

configure({ configPath, config, frameworkOptions, storybook });
configure({ configPath, config, storybook });

return {
framework: 'angular',
Expand Down
43 changes: 0 additions & 43 deletions addons/storyshots/storyshots-core/src/frameworks/config-loader.js

This file was deleted.

25 changes: 15 additions & 10 deletions addons/storyshots/storyshots-core/src/frameworks/configure.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import loadConfig from './config-loader';
import runWithRequireContext from './require_context';
import fs from 'fs';
import path from 'path';

function getConfigPathParts(configPath) {
const resolvedConfigPath = path.resolve(configPath);

if (fs.lstatSync(resolvedConfigPath).isDirectory()) {
return path.join(resolvedConfigPath, 'config.js');
}

return resolvedConfigPath;
}

function configure(options) {
const { configPath = '.storybook', config, frameworkOptions, storybook } = options;
const { configPath = '.storybook', config, storybook } = options;

if (config && typeof config === 'function') {
config(storybook);
return;
}

const appOptions = require.requireActual(frameworkOptions).default;

const { content, contextOpts } = loadConfig({
configPath,
appOptions,
});
const resolvedConfigPath = getConfigPathParts(configPath);

runWithRequireContext(content, contextOpts);
require.requireActual(resolvedConfigPath);
}

export default configure;
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ function load(options) {
global.STORYBOOK_ENV = 'html';

const { configPath, config } = options;
const frameworkOptions = '@storybook/html/options';
const storybook = require.requireActual('@storybook/html');

configure({ configPath, config, frameworkOptions, storybook });
configure({ configPath, config, storybook });

return {
framework: 'html',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ function test(options) {

function load(options) {
const { configPath, config } = options;
const frameworkOptions = '@storybook/react/options';
const storybook = require.requireActual('@storybook/react');

configure({ configPath, config, frameworkOptions, storybook });
configure({ configPath, config, storybook });

return {
framework: 'react',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ function load(options) {
mockVueToIncludeCompiler();

const { configPath, config } = options;
const frameworkOptions = '@storybook/vue/options';
const storybook = require.requireActual('@storybook/vue');

configure({ configPath, config, frameworkOptions, storybook });
configure({ configPath, config, storybook });

return {
framework: 'vue',
Expand Down
3 changes: 2 additions & 1 deletion examples/vue-kitchen-sink/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
],
"env": {
"test": {
"presets": ["env"]
"presets": ["env"],
"plugins": ["require-context-hook"]
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"babel-eslint": "^8.2.3",
"babel-plugin-emotion": "^9.1.2",
"babel-plugin-macros": "^2.2.2",
"babel-plugin-require-context-hook": "^1.0.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
Expand Down
4 changes: 4 additions & 0 deletions scripts/jest.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import Adapter from 'enzyme-adapter-react-16';
import * as emotion from 'emotion';
import { createSerializer } from 'jest-emotion';

import registerRequireContextHook from 'babel-plugin-require-context-hook/register';

registerRequireContextHook();

expect.addSnapshotSerializer(createSerializer(emotion));
// mock console.info calls for cleaner test execution
global.console.info = jest.fn().mockImplementation(() => {});
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,10 @@ babel-plugin-react-transform@^3.0.0:
dependencies:
lodash "^4.6.1"

babel-plugin-require-context-hook@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/babel-plugin-require-context-hook/-/babel-plugin-require-context-hook-1.0.0.tgz#3f0e7cce87c338f53639b948632fd4e73834632d"

babel-plugin-syntax-async-functions@^6.13.0, babel-plugin-syntax-async-functions@^6.5.0, babel-plugin-syntax-async-functions@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"
Expand Down

0 comments on commit 2a992ba

Please sign in to comment.