Skip to content

Commit

Permalink
Extract custom config logic to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ailrun committed Jun 27, 2018
1 parent d67c587 commit 59acb45
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 85 deletions.
86 changes: 1 addition & 85 deletions lib/core/src/server/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/* eslint-disable global-require, import/no-dynamic-require */
import fs from 'fs';
import path from 'path';
import findCacheDir from 'find-cache-dir';
import interpret from 'interpret';
import { logger } from '@storybook/node-logger';
import { createDefaultWebpackConfig } from './config/defaults/webpack.config';
import devBabelConfig from './config/babel';
import loadBabelConfig from './babel_config';
import loadCustomConfig from './loadCustomWebpackConfig';

const noopWrapper = config => config;

Expand Down Expand Up @@ -59,87 +56,6 @@ function informAboutCustomConfig(defaultConfigName) {
logger.info(`=> Using default webpack setup based on "${defaultConfigName}".`);
}

// Copied and modified from
// https://github.com/webpack/webpack-cli/blob/ca504de8c7c0ea66278021b72fa6a953e3ffa43c/bin/convert-argv.js#L102-L119
function registerCompiler(moduleDescriptor) {
if (!moduleDescriptor) {
return 0;
}

if (typeof moduleDescriptor === 'string') {
require(moduleDescriptor);
return 1;
}

if (!Array.isArray(moduleDescriptor)) {
moduleDescriptor.register(require(moduleDescriptor.module));
return 1;
}

let registered = 0;

for (let i = 0; i < moduleDescriptor.length; i += 1) {
try {
registered += registerCompiler(moduleDescriptor[i]);
break;
} catch (e) {
// do nothing
}
}

return registered;
}

// Copied and modified from
// https://github.com/webpack/webpack-cli/blob/ca504de8c7c0ea66278021b72fa6a953e3ffa43c/bin/convert-argv.js#L121-L137
function requireConfig(configPath) {
const config = require(configPath);

const isES6DefaultExported =
typeof config === 'object' && config !== null && typeof config.default !== 'undefined';

return isES6DefaultExported ? config.default : config;
}

// Copied and modified from
// https://github.com/webpack/webpack-cli/blob/ca504de8c7c0ea66278021b72fa6a953e3ffa43c/bin/convert-argv.js#L45-L143
function loadCustomConfig(configDir) {
const extensions = Object.keys(interpret.extensions).sort((a, b) => {
if (a === '.js') {
return -1;
}
if (b === '.js') {
return 1;
}
return a.length - b.length;
});
const customConfigCandidates = ['webpack.config', 'webpackfile']
.map(filename =>
extensions.map(ext => ({
path: path.resolve(configDir, filename + ext),
ext,
}))
)
.reduce((a, i) => a.concat(i), []);

for (let i = 0; i < customConfigCandidates.length; i += 1) {
const customConfigPath = customConfigCandidates[i].path;
if (fs.existsSync(customConfigPath)) {
if (registerCompiler(interpret.extensions[customConfigCandidates[i].ext]) === 0) {
logger.warn(`=> Custom configuration file ${customConfigPath} is detected`);
logger.warn(` but impossible to import loader for ${customConfigCandidates[i].ext}`);
}
try {
return requireConfig(customConfigPath);
} catch (e) {
return null;
}
}
}

return null;
}

// `baseConfig` is a webpack configuration bundled with storybook.
// Storybook will look in the `configDir` directory
// (inside working directory) if a config path is not provided.
Expand Down
86 changes: 86 additions & 0 deletions lib/core/src/server/loadCustomWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable global-require, import/no-dynamic-require */
import fs from 'fs';
import path from 'path';
import interpret from 'interpret';
import { logger } from '@storybook/node-logger';

// Copied and modified from
// https://github.com/webpack/webpack-cli/blob/ca504de8c7c0ea66278021b72fa6a953e3ffa43c/bin/convert-argv.js#L102-L119
function registerCompiler(moduleDescriptor) {
if (!moduleDescriptor) {
return 0;
}

if (typeof moduleDescriptor === 'string') {
require(moduleDescriptor);
return 1;
}

if (!Array.isArray(moduleDescriptor)) {
moduleDescriptor.register(require(moduleDescriptor.module));
return 1;
}

let registered = 0;

for (let i = 0; i < moduleDescriptor.length; i += 1) {
try {
registered += registerCompiler(moduleDescriptor[i]);
break;
} catch (e) {
// do nothing
}
}

return registered;
}

// Copied and modified from
// https://github.com/webpack/webpack-cli/blob/ca504de8c7c0ea66278021b72fa6a953e3ffa43c/bin/convert-argv.js#L121-L137
function requireConfig(configPath) {
const config = require(configPath);

const isES6DefaultExported =
typeof config === 'object' && config !== null && typeof config.default !== 'undefined';

return isES6DefaultExported ? config.default : config;
}

// Copied and modified from
// https://github.com/webpack/webpack-cli/blob/ca504de8c7c0ea66278021b72fa6a953e3ffa43c/bin/convert-argv.js#L45-L143
export default configDir => {
const extensions = Object.keys(interpret.extensions).sort((a, b) => {
if (a === '.js') {
return -1;
}
if (b === '.js') {
return 1;
}
return a.length - b.length;
});
const customConfigCandidates = ['webpack.config', 'webpackfile']
.map(filename =>
extensions.map(ext => ({
path: path.resolve(configDir, filename + ext),
ext,
}))
)
.reduce((a, i) => a.concat(i), []);

for (let i = 0; i < customConfigCandidates.length; i += 1) {
const customConfigPath = customConfigCandidates[i].path;
if (fs.existsSync(customConfigPath)) {
if (registerCompiler(interpret.extensions[customConfigCandidates[i].ext]) === 0) {
logger.warn(`=> Custom configuration file ${customConfigPath} is detected`);
logger.warn(` but impossible to import loader for ${customConfigCandidates[i].ext}`);
}
try {
return requireConfig(customConfigPath);
} catch (e) {
// do nothing
}
}
}

return null;
};

0 comments on commit 59acb45

Please sign in to comment.