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

TestBuild: Implement builder options for test build #24826

Merged
merged 10 commits into from
Nov 13, 2023
9 changes: 8 additions & 1 deletion code/builders/builder-vite/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ export async function build(options: Options) {
build: {
outDir: options.outputDir,
emptyOutDir: false, // do not clean before running Vite build - Storybook has already added assets in there!
sourcemap: !options.test,
rollupOptions: {
// Do not try to bundle the storybook runtime, it is copied into the output dir after the build.
external: ['./sb-preview/runtime.js'],
},
...(options.test
? {
reportCompressedSize: false,
sourcemap: !options.build?.test?.disableSourcemaps,
target: 'esnext',
treeshake: !options.build?.test?.disableTreeShaking,
}
: {}),
},
}).build;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import TerserWebpackPlugin from 'terser-webpack-plugin';
import VirtualModulePlugin from 'webpack-virtual-modules';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import slash from 'slash';

import type { TransformOptions as EsbuildOptions } from 'esbuild';
import type { JsMinifyOptions as SwcOptions } from '@swc/core';
import type { Options, CoreConfig, DocsOptions, PreviewAnnotation } from '@storybook/types';
import { globalsNameReferenceMap } from '@storybook/preview/globals';
import {
Expand Down Expand Up @@ -228,7 +229,7 @@ export default async (
name: 'preview',
mode: isProd ? 'production' : 'development',
bail: isProd,
devtool: options.test ? false : 'cheap-module-source-map',
devtool: options.build?.test?.disableSourcemaps ? false : 'cheap-module-source-map',
entry: entries,
output: {
path: resolve(process.cwd(), outputDir),
Expand Down Expand Up @@ -323,7 +324,7 @@ export default async (
fullySpecified: false,
},
},
builderOptions.useSWC
builderOptions.useSWC || options.build?.test?.optimizeCompilation
? await createSWCLoader(Object.keys(virtualModuleMapping), options)
: createBabelLoader(babelOptions, typescriptOptions, Object.keys(virtualModuleMapping)),
{
Expand Down Expand Up @@ -356,17 +357,29 @@ export default async (
},
runtimeChunk: true,
sideEffects: true,
usedExports: isProd,
usedExports: options.build?.test?.disableTreeShaking ? false : isProd,
moduleIds: 'named',
...(isProd
? {
minimize: true,
minimizer: builderOptions.useSWC
// eslint-disable-next-line no-nested-ternary
minimizer: options.build?.test?.optimizeCompilation
? [
new TerserWebpackPlugin({
new TerserWebpackPlugin<EsbuildOptions>({
parallel: true,
minify: TerserWebpackPlugin.esbuildMinify,
terserOptions: {
sourcemap: !options.build?.test?.disableSourcemaps,
treeShaking: !options.build?.test?.disableTreeShaking,
},
}),
]
: builderOptions.useSWC
? [
new TerserWebpackPlugin<SwcOptions>({
minify: TerserWebpackPlugin.swcMinify,
terserOptions: {
sourceMap: true,
sourceMap: !options.build?.test?.disableSourcemaps,
mangle: false,
keep_fnames: true,
},
Expand All @@ -376,7 +389,7 @@ export default async (
new TerserWebpackPlugin({
parallel: true,
terserOptions: {
sourceMap: true,
sourceMap: !options.build?.test?.disableSourcemaps,
mangle: false,
keep_fnames: true,
},
Expand Down
9 changes: 2 additions & 7 deletions code/lib/core-server/src/build-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { copy, emptyDir, ensureDir } from 'fs-extra';
import { dirname, isAbsolute, join, resolve } from 'path';
import { global } from '@storybook/global';
import { deprecate, logger } from '@storybook/node-logger';
import { telemetry, getPrecedingUpgrade } from '@storybook/telemetry';
import { getPrecedingUpgrade, telemetry } from '@storybook/telemetry';
import type {
BuilderOptions,
CLIOptions,
Expand All @@ -30,12 +30,11 @@ import {
copyAllStaticFilesRelativeToMain,
} from './utils/copy-all-static-files';
import { getBuilders } from './utils/get-builders';
import { extractStoriesJson, convertToIndexV3 } from './utils/stories-json';
import { convertToIndexV3, extractStoriesJson } from './utils/stories-json';
import { extractStorybookMetadata } from './utils/metadata';
import { StoryIndexGenerator } from './utils/StoryIndexGenerator';
import { summarizeIndex } from './utils/summarizeIndex';
import { defaultStaticDirs } from './utils/constants';
import { warnOnIncompatibleAddons } from './utils/warnOnIncompatibleAddons';

export type BuildStaticStandaloneOptions = CLIOptions &
LoadOptions &
Expand Down Expand Up @@ -77,10 +76,6 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption
logger.warn(`you have not specified a framework in your ${options.configDir}/main.js`);
}

if (options.test) {
await warnOnIncompatibleAddons(config);
}

logger.info('=> Loading presets');
let presets = await loadAllPresets({
corePresets: [
Expand Down
3 changes: 3 additions & 0 deletions code/lib/core-server/src/presets/common-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ const testBuildFeatures = (value: boolean): Required<TestBuildFlags> => ({
removeMDXEntries: value,
removeAutoDocs: value,
disableDocgen: value,
disableSourcemaps: value,
disableTreeShaking: value,
optimizeCompilation: value,
});

export const features = async (
Expand Down
12 changes: 12 additions & 0 deletions code/lib/types/src/modules/core-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ export interface TestBuildFlags {
* Override docgen to be disabled.
*/
disableDocgen?: boolean;
/**
* Override sourcemaps generation to be disabled.
*/
disableSourcemaps?: boolean;
/**
* Override tree-shaking (dead code elimination) to be disabled.
*/
disableTreeShaking?: boolean;
/**
* Compile/Optimize with SWC.
*/
optimizeCompilation?: boolean;
}

export interface TestBuildConfig {
Expand Down