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

Presets - API generalization #4173

Merged
merged 4 commits into from
Sep 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/core/src/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down Expand Up @@ -41,5 +50,5 @@ export default options => {

const presets = loadPresets(presetsConfig);

return getWebpackConfig({ ...restOptions, presets }, presets);
return getWebpackConfig({ ...restOptions, presets }, wrapCorePresets(presets));
};
5 changes: 4 additions & 1 deletion lib/core/src/server/core-preset-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion lib/core/src/server/core-preset-prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions lib/core/src/server/presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}

Expand Down
51 changes: 24 additions & 27 deletions lib/core/src/server/presets.test.js
Original file line number Diff line number Diff line change
@@ -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 });
}
Expand All @@ -8,36 +15,30 @@ describe('presets', () => {
let presets;

expect(() => {
presets = loadPresets();
presets = wrapPreset(loadPresets());
presets.webpack();
presets.babel();
presets.preview();
presets.manager();
}).not.toThrow();

expect(presets).toBeDefined();
});

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();
});
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down