Skip to content

Commit

Permalink
refactor(core): improve typings of server/common
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanmaisse committed Nov 3, 2020
1 parent 784c321 commit 3239d25
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/core/src/server/common/babel-cache-preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolvePathInStorybookCache } from '../utils/resolve-path-in-sb-cache';

// FIXME: babelConfig is maybe a TransformOptions?
const extend = (babelConfig: any) => ({
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables a cache directory for faster-rebuilds
Expand Down
4 changes: 3 additions & 1 deletion lib/core/src/server/common/babel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TransformOptions } from '@babel/core';

export const plugins = [
require.resolve('@babel/plugin-transform-shorthand-properties'),
require.resolve('@babel/plugin-transform-block-scoping'),
Expand Down Expand Up @@ -39,7 +41,7 @@ export const presets = [
require.resolve('@babel/preset-typescript'),
];

export default () => {
export const babelConfig: () => TransformOptions = () => {
return {
sourceType: 'unambiguous',
presets: [...presets],
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/server/common/common-preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import loadCustomBabelConfig from '../utils/load-custom-babel-config';

import babelConfig from './babel';
import { babelConfig } from './babel';

export const babel = async (_: unknown, options: { configDir: string; presets: any }) => {
const { configDir, presets } = options;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/server/common/custom-presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import { serverRequire, serverResolve } from '../utils/server-require';
import validateConfigurationFiles from '../utils/validate-configuration-files';

export default function loadCustomPresets({ configDir }: { configDir: string }) {
export default function loadCustomPresets({ configDir }: { configDir: string }): string[] {
validateConfigurationFiles(configDir);

const presets = serverRequire(path.resolve(configDir, 'presets'));
Expand Down
5 changes: 2 additions & 3 deletions lib/core/src/server/common/es6Transpiler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RuleSetRule } from 'webpack';
import { plugins } from './babel';

const es6Transpiler = () => {
export const es6Transpiler: () => RuleSetRule = () => {
// TODO: generate regexp using are-you-es5

const include = /[\\/]node_modules[\\/](@storybook\/node-logger|are-you-es5|better-opn|boxen|chalk|commander|find-cache-dir|find-up|fs-extra|json5|node-fetch|pkg-dir|resolve-from|semver)/;
Expand All @@ -25,5 +26,3 @@ const es6Transpiler = () => {
include,
};
};

export default es6Transpiler;
3 changes: 2 additions & 1 deletion lib/core/src/server/manager/babel-loader-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RuleSetRule } from 'webpack';
import { includePaths } from '../config/utils';
import { plugins, presets } from '../common/babel';

export default () => ({
export const babelLoader: () => RuleSetRule = () => ({
test: /\.(mjs|tsx?|jsx?)$/,
use: [
{
Expand Down
10 changes: 6 additions & 4 deletions lib/core/src/server/manager/manager-webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fse from 'fs-extra';
import { DefinePlugin } from 'webpack';
import { DefinePlugin, Configuration } from 'webpack';
import Dotenv from 'dotenv-webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
Expand All @@ -13,9 +13,9 @@ import uiPaths from '@storybook/ui/paths';
import { getManagerHeadHtml } from '../utils/template';
import { loadEnv } from '../config/utils';

import babelLoader from './babel-loader-manager';
import { babelLoader } from './babel-loader-manager';
import { resolvePathInStorybookCache } from '../utils/resolve-path-in-sb-cache';
import es6Transpiler from '../common/es6Transpiler';
import { es6Transpiler } from '../common/es6Transpiler';

const { version } = require('../../../package.json');

Expand All @@ -31,7 +31,7 @@ export default async ({
versionCheck,
releaseNotesData,
presets,
}: any) => {
}: any): Promise<Configuration> => {
const { raw, stringified } = loadEnv();
const logLevel = await presets.apply('logLevel', undefined);
const isProd = configType === 'PRODUCTION';
Expand All @@ -43,6 +43,8 @@ export default async ({
name: 'manager',
mode: isProd ? 'production' : 'development',
bail: isProd,
// FIXME: `none` is not a valid option for devtool
// @ts-ignore
devtool: 'none',
entry: entries,
output: {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/server/preview/iframe-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import resolveFrom from 'resolve-from';
import themingPaths from '@storybook/theming/paths';

import { createBabelLoader } from './babel-loader-preview';
import es6Transpiler from '../common/es6Transpiler';
import { es6Transpiler } from '../common/es6Transpiler';

import { nodeModulesPaths, loadEnv } from '../config/utils';
import { getPreviewHeadHtml, getPreviewBodyHtml } from '../utils/template';
Expand Down

0 comments on commit 3239d25

Please sign in to comment.