From ed55ded0b26a51eb7932ba6c532f4e9b1e1c60ba Mon Sep 17 00:00:00 2001 From: igor-dv Date: Thu, 13 Sep 2018 11:04:10 +0300 Subject: [PATCH] Make presets contain only an `apply` method. --- lib/core/src/server/config.js | 11 +++++- lib/core/src/server/core-preset-dev.js | 5 ++- lib/core/src/server/core-preset-prod.js | 5 ++- lib/core/src/server/presets.js | 6 +-- lib/core/src/server/presets.test.js | 51 ++++++++++++------------- 5 files changed, 43 insertions(+), 35 deletions(-) diff --git a/lib/core/src/server/config.js b/lib/core/src/server/config.js index 084c958249ba..bf1f975ca7cb 100644 --- a/lib/core/src/server/config.js +++ b/lib/core/src/server/config.js @@ -3,6 +3,15 @@ import { logger } from '@storybook/node-logger'; import loadPresets from './presets'; import serverRequire from './serverRequire'; +function wrapCorePresets(presets) { + return { + babel: (config, args) => presets.apply('babel', config, args), + webpack: (config, args) => presets.apply('webpack', config, args), + preview: (config, args) => presets.apply('preview', config, args), + manager: (config, args) => presets.apply('manager', config, args), + }; +} + function customPreset({ configDir }) { const presets = serverRequire(path.resolve(configDir, 'presets')); @@ -41,5 +50,5 @@ export default options => { const presets = loadPresets(presetsConfig); - return getWebpackConfig({ ...restOptions, presets }, presets); + return getWebpackConfig({ ...restOptions, presets }, wrapCorePresets(presets)); }; diff --git a/lib/core/src/server/core-preset-dev.js b/lib/core/src/server/core-preset-dev.js index ec52deba4e0d..87521813b379 100644 --- a/lib/core/src/server/core-preset-dev.js +++ b/lib/core/src/server/core-preset-dev.js @@ -9,7 +9,10 @@ export function webpack(_, options) { export function babel(_, options) { const { configDir, presets } = options; - return loadCustomBabelConfig(configDir, () => presets.babelDefault(defaultBabelConfig, options)); + + return loadCustomBabelConfig(configDir, () => + presets.apply('babelDefault', defaultBabelConfig, options) + ); } export function manager(_, options) { diff --git a/lib/core/src/server/core-preset-prod.js b/lib/core/src/server/core-preset-prod.js index bf48e431ae35..2138b9816e12 100644 --- a/lib/core/src/server/core-preset-prod.js +++ b/lib/core/src/server/core-preset-prod.js @@ -9,7 +9,10 @@ export function webpack(_, options) { export function babel(_, options) { const { configDir, presets } = options; - return loadCustomBabelConfig(configDir, () => presets.babelDefault(defaultBabelConfig, options)); + + return loadCustomBabelConfig(configDir, () => + presets.apply('babelDefault', defaultBabelConfig, options) + ); } export function manager(_, options) { diff --git a/lib/core/src/server/presets.js b/lib/core/src/server/presets.js index 4af75f05ce11..47c8707e82c4 100644 --- a/lib/core/src/server/presets.js +++ b/lib/core/src/server/presets.js @@ -66,11 +66,7 @@ function getPresets(presets) { const loadedPresets = loadPresets(presets); return { - babel: (config, args) => applyPresets(loadedPresets, config, args, 'babel'), - babelDefault: (config, args) => applyPresets(loadedPresets, config, args, 'babelDefault'), - webpack: (config, args) => applyPresets(loadedPresets, config, args, 'webpack'), - preview: (config, args) => applyPresets(loadedPresets, config, args, 'preview'), - manager: (config, args) => applyPresets(loadedPresets, config, args, 'manager'), + apply: (extension, config, args) => applyPresets(loadedPresets, config, args, extension), }; } diff --git a/lib/core/src/server/presets.test.js b/lib/core/src/server/presets.test.js index 0177792f824f..ecafa8a33820 100644 --- a/lib/core/src/server/presets.test.js +++ b/lib/core/src/server/presets.test.js @@ -1,3 +1,10 @@ +function wrapPreset(basePresets) { + return { + babel: (config, args) => basePresets.apply('babel', config, args), + webpack: (config, args) => basePresets.apply('webpack', config, args), + }; +} + function mockPreset(name, mockPresetObject) { jest.mock(name, () => mockPresetObject, { virtual: true }); } @@ -8,11 +15,9 @@ describe('presets', () => { let presets; expect(() => { - presets = loadPresets(); + presets = wrapPreset(loadPresets()); presets.webpack(); presets.babel(); - presets.preview(); - presets.manager(); }).not.toThrow(); expect(presets).toBeDefined(); @@ -20,24 +25,20 @@ describe('presets', () => { it('does not throw when presets are empty', () => { const loadPresets = require.requireActual('./presets').default; - const presets = loadPresets([]); + const presets = wrapPreset(loadPresets([])); expect(() => { presets.webpack(); presets.babel(); - presets.preview(); - presets.manager(); }).not.toThrow(); }); it('does not throw when preset can not be loaded', () => { const loadPresets = require.requireActual('./presets').default; - const presets = loadPresets(['preset-foo']); + const presets = wrapPreset(loadPresets(['preset-foo'])); expect(() => { presets.webpack({}); - presets.preview(); - presets.manager(); presets.babel(); }).not.toThrow(); }); @@ -55,12 +56,10 @@ describe('presets', () => { }); const loadPresets = require.requireActual('./presets').default; - const presets = loadPresets(['preset-foo', 'preset-bar']); + const presets = wrapPreset(loadPresets(['preset-foo', 'preset-bar'])); expect(() => { presets.webpack(); - presets.preview(); - presets.manager(); presets.babel(); }).not.toThrow(); @@ -81,12 +80,10 @@ describe('presets', () => { }); const loadPresets = require.requireActual('./presets').default; - const presets = loadPresets([{ name: 'preset-foo' }, { name: 'preset-bar' }]); + const presets = wrapPreset(loadPresets([{ name: 'preset-foo' }, { name: 'preset-bar' }])); expect(() => { presets.webpack(); - presets.preview(); - presets.manager(); presets.babel(); }).not.toThrow(); @@ -107,15 +104,15 @@ describe('presets', () => { }); const loadPresets = require.requireActual('./presets').default; - const presets = loadPresets([ - { name: 'preset-foo', options: { foo: 1 } }, - { name: 'preset-bar', options: { bar: 'a' } }, - ]); + const presets = wrapPreset( + loadPresets([ + { name: 'preset-foo', options: { foo: 1 } }, + { name: 'preset-bar', options: { bar: 'a' } }, + ]) + ); expect(() => { presets.webpack({}); - presets.preview(); - presets.manager(); presets.babel({}); }).not.toThrow(); @@ -136,12 +133,12 @@ describe('presets', () => { }); const loadPresets = require.requireActual('./presets').default; - const presets = loadPresets(['preset-foo', { name: 'preset-bar', options: { bar: 'a' } }]); + const presets = wrapPreset( + loadPresets(['preset-foo', { name: 'preset-bar', options: { bar: 'a' } }]) + ); expect(() => { presets.webpack({}); - presets.preview(); - presets.manager(); presets.babel({}); }).not.toThrow(); @@ -162,12 +159,12 @@ describe('presets', () => { }); const loadPresets = require.requireActual('./presets').default; - const presets = loadPresets(['preset-foo', { name: 'preset-bar', options: { bar: 'a' } }]); + const presets = wrapPreset( + loadPresets(['preset-foo', { name: 'preset-bar', options: { bar: 'a' } }]) + ); expect(() => { presets.webpack({}); - presets.preview(); - presets.manager(); presets.babel(); }).not.toThrow();