Skip to content

Commit

Permalink
Load swc preset options for swc loader
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Nov 10, 2023
1 parent ae0ff86 commit c714f97
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export default async (
},
},
builderOptions.useSWC
? createSWCLoader(Object.keys(virtualModuleMapping))
? await createSWCLoader(Object.keys(virtualModuleMapping), options)
: createBabelLoader(babelOptions, typescriptOptions, Object.keys(virtualModuleMapping)),
{
test: /\.md$/,
Expand Down
19 changes: 15 additions & 4 deletions code/builders/builder-webpack5/src/preview/loaders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getProjectRoot } from '@storybook/core-common';
import type { Options } from '@swc/core';
import type { Options as SwcOptions } from '@swc/core';
import { dedent } from 'ts-dedent';
import { logger } from '@storybook/node-logger';
import type { Options } from '@storybook/types';
import type { TypescriptOptions } from '../types';

export const createBabelLoader = (
Expand All @@ -22,22 +23,32 @@ export const createBabelLoader = (
};
};

export const createSWCLoader = (excludes: string[] = []) => {
export const createSWCLoader = async (excludes: string[] = [], options: Options) => {
logger.warn(dedent`
The SWC loader is an experimental feature and may change or even be removed at any time.
`);

const config: Options = {
const swc = await options.presets.apply('swc', {}, options);
const typescriptOptions = await options.presets.apply<{ skipCompiler?: boolean }>(
'typescript',
{},
options
);

const config: SwcOptions = {
...swc,
jsc: {
...(swc.jsc ?? {}),
parser: {
...(swc.jsc?.parser ?? {}),
syntax: 'typescript',
tsx: true,
dynamicImport: true,
},
},
};
return {
test: /\.(mjs|cjs|tsx?|jsx?)$/,
test: typescriptOptions.skipCompiler ? /\.(mjs|cjs|jsx?)$/ : /\.(mjs|cjs|tsx?|jsx?)$/,
use: [
{
loader: require.resolve('swc-loader'),
Expand Down

0 comments on commit c714f97

Please sign in to comment.