Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function prepareFilesStructure(outputDir, defaultFavIcon) {
// clear the output dir
logger.info('clean outputDir..');
shelljs.rm('-rf', outputDir);
// create output directory if not exists
shelljs.mkdir('-p', outputDir);
shelljs.mkdir('-p', path.join(outputDir, 'sb_dll'));
shelljs.cp(defaultFavIcon, outputDir);
}
// Check whether user has a custom webpack config file and
// return the (extended) base configuration if it's not available.
const customConfig = loadCustomWebpackConfig(configDir);
if (customConfig === null) {
logger.info('=> Using default Webpack setup.');
return createFinalDefaultConfig(presets, config, options);
}
if (typeof customConfig === 'function') {
logger.info('=> Loading custom Webpack config (full-control mode).');
const finalDefaultConfig = await createFinalDefaultConfig(presets, config, options);
return customConfig({ config: finalDefaultConfig, mode: configType });
}
logger.info('=> Loading custom webpack config (extending mode).');
// Restore 4.x behavior, but deprecate this mode of extending webpack
const finalConfig = await presets.apply('webpackFinal', config, options);
return deprecate(
() => mergeConfigs(finalConfig, customConfig),
stripIndents`
Extend-mode configuration is deprecated, please use full-control mode instead.
See https://storybook.js.org/docs/configurations/custom-webpack-config/#full-control-mode
`
)();
}
function loadCustomAddons({ configDir }) {
const storybookCustomAddonsPath = getInterpretedFile(path.resolve(configDir, 'addons'));
const storybookCustomManagerPath = getInterpretedFile(path.resolve(configDir, 'manager'));
if (storybookCustomAddonsPath || storybookCustomManagerPath) {
logger.info('=> Loading custom manager config.');
}
if (storybookCustomAddonsPath && storybookCustomManagerPath) {
throw new Error(dedent`
You have both a "addons.js" and a "manager.js", remove the "addons.js" file from your configDir (${path.resolve(
configDir,
'addons'
)})`);
}
return [...(toArray(storybookCustomManagerPath) || toArray(storybookCustomAddonsPath) || [])];
}
function resolveTsConfig(tsConfigPath) {
if (!fs.existsSync(tsConfigPath)) {
return null;
}
logger.info('=> Found custom tsconfig.json');
return tsConfigPath;
}
export function webpackFinal(config: Configuration) {
const cwd = process.cwd();
const cliWebpackConfigOptions = getAngularCliWebpackConfigOptions(cwd as Path);
if (cliWebpackConfigOptions) {
logger.info('=> Loading angular-cli config.');
}
return applyAngularCliWebpackConfig(config, cliWebpackConfigOptions);
}
async function buildManager(configType, outputDir, configDir, options) {
logger.info('=> Building manager..');
const managerStartTime = process.hrtime();
logger.info('=> Loading manager config..');
const managerConfig = await loadManagerConfig({
configType,
outputDir,
configDir,
corePresets: [require.resolve('./manager/manager-preset.js')],
frameworkPresets: options.frameworkPresets,
docsMode: options.docsMode,
previewUrl: options.previewUrl,
});
if (options.debugWebpack) {
logConfig('Manager webpack config', managerConfig);
}
return compileManager(managerConfig, managerStartTime);
}
async function outputStats(previewStats, managerStats) {
if (previewStats) {
await writeStats('preview', previewStats);
}
await writeStats('manager', managerStats);
logger.info(`stats written to => ${chalk.cyan(path.join(cacheDir, '[name].json'))}`);
}
async function compileManager(managerConfig, managerStartTime) {
logger.info('=> Compiling manager..');
return new Promise((resolve, reject) => {
webpack(managerConfig).run((error, stats) => {
if (error || !stats || stats.hasErrors()) {
logger.error('=> Failed to build the manager');
if (error) {
logger.error(error.message);
}
if (stats && (stats.hasErrors() || stats.hasWarnings())) {
const { warnings, errors } = stats.toJson(managerConfig.stats);
errors.forEach(e => logger.error(e));
warnings.forEach(e => logger.error(e));
}
export function webpackFinal(config, { configDir }) {
if (!isReactScriptsInstalled()) {
logger.info('=> Using base config because react-scripts is not installed.');
return config;
}
logger.info('=> Loading create-react-app config.');
return applyCRAWebpackConfig(config, configDir);
}