Skip to content

Commit

Permalink
Support other type of webpack configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ailrun committed Jun 22, 2018
1 parent 2d7985f commit d67c587
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"find-cache-dir": "^1.0.0",
"global": "^4.3.2",
"html-webpack-plugin": "^3.2.0",
"interpret": "^1.1.0",
"json5": "^1.0.1",
"postcss-flexbugs-fixes": "^3.3.1",
"postcss-loader": "^2.1.5",
Expand Down
88 changes: 84 additions & 4 deletions lib/core/src/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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';
Expand Down Expand Up @@ -58,6 +59,87 @@ 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 Expand Up @@ -86,15 +168,13 @@ export default options => {

// Check whether user has a custom webpack config file and
// return the (extended) base configuration if it's not available.
const customConfigPath = path.resolve(configDir, 'webpack.config.js');
const customConfig = loadCustomConfig(configDir);

if (!fs.existsSync(customConfigPath)) {
if (customConfig === null) {
informAboutCustomConfig(defaultConfigName);
return defaultConfig;
}

const customConfig = require(customConfigPath);

if (typeof customConfig === 'function') {
logger.info('=> Loading custom webpack config (full-control mode).');
return customConfig(wrapBasicConfig(config), configType, defaultConfig);
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8744,7 +8744,7 @@ internal-ip@1.2.0:
dependencies:
meow "^3.3.0"

interpret@^1.0.0:
interpret@^1.0.0, interpret@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614"

Expand Down

0 comments on commit d67c587

Please sign in to comment.