/
preset.ts
172 lines (158 loc) · 4.91 KB
/
preset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import fs from 'fs-extra';
import remarkSlug from 'remark-slug';
import remarkExternalLinks from 'remark-external-links';
import global from 'global';
import type { DocsOptions, IndexerOptions, Options, StoryIndexer } from '@storybook/core-common';
import { logger } from '@storybook/node-logger';
import { loadCsf } from '@storybook/csf-tools';
// for frameworks that are not working with react, we need to configure
// the jsx to transpile mdx, for now there will be a flag for that
// for more complex solutions we can find alone that we need to add '@babel/plugin-transform-react-jsx'
type BabelParams = {
babelOptions?: any;
mdxBabelOptions?: any;
configureJSX?: boolean;
};
function createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }: BabelParams) {
const babelPlugins = mdxBabelOptions?.plugins || babelOptions?.plugins || [];
const jsxPlugin = [
require.resolve('@babel/plugin-transform-react-jsx'),
{ pragma: 'React.createElement', pragmaFrag: 'React.Fragment' },
];
const plugins = configureJSX ? [...babelPlugins, jsxPlugin] : babelPlugins;
return {
// don't use the root babelrc by default (users can override this in mdxBabelOptions)
babelrc: false,
configFile: false,
...babelOptions,
...mdxBabelOptions,
plugins,
};
}
export async function webpack(
webpackConfig: any = {},
options: Options &
BabelParams & { sourceLoaderOptions: any; transcludeMarkdown: boolean } /* & Parameters<
typeof createCompiler
>[0] */
) {
const resolvedBabelLoader = require.resolve('babel-loader');
const { module = {} } = webpackConfig;
// it will reuse babel options that are already in use in storybook
// also, these babel options are chained with other presets.
const {
babelOptions,
mdxBabelOptions,
configureJSX = true,
sourceLoaderOptions = { injectStoryParameters: true },
transcludeMarkdown = false,
} = options;
const mdxLoaderOptions = {
// whether to skip storybook files, useful for docs only mdx or md files
skipCsf: true,
remarkPlugins: [remarkSlug, remarkExternalLinks],
};
const mdxVersion = global.FEATURES?.previewMdx2 ? 'MDX2' : 'MDX1';
logger.info(`Addon-docs: using ${mdxVersion}`);
const mdxLoader = global.FEATURES?.previewMdx2
? require.resolve('@storybook/mdx2-csf/loader')
: require.resolve('@storybook/mdx1-csf/loader');
// set `sourceLoaderOptions` to `null` to disable for manual configuration
const sourceLoader = sourceLoaderOptions
? [
{
test: /\.(stories|story)\.[tj]sx?$/,
loader: require.resolve('@storybook/source-loader'),
options: { ...sourceLoaderOptions, inspectLocalDependencies: true },
enforce: 'pre',
},
]
: [];
let rules = module.rules || [];
if (transcludeMarkdown) {
rules = [
...rules.filter((rule: any) => rule.test?.toString() !== '/\\.md$/'),
{
test: /\.md$/,
use: [
{
loader: resolvedBabelLoader,
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: mdxLoader,
options: mdxLoaderOptions,
},
],
},
];
}
const result = {
...webpackConfig,
module: {
...module,
rules: [
...rules,
{
test: /(stories|story)\.mdx$/,
use: [
{
loader: resolvedBabelLoader,
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: mdxLoader,
options: {
...mdxLoaderOptions,
skipCsf: false,
},
},
],
},
{
test: /\.mdx$/,
exclude: /(stories|story)\.mdx$/,
use: [
{
loader: resolvedBabelLoader,
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: mdxLoader,
options: mdxLoaderOptions,
},
],
},
...sourceLoader,
],
},
};
return result;
}
export const storyIndexers = async (indexers: StoryIndexer[] | null) => {
const mdxIndexer = async (fileName: string, opts: IndexerOptions) => {
let code = (await fs.readFile(fileName, 'utf-8')).toString();
// @ts-ignore
const { compile } = global.FEATURES?.previewMdx2
? await import('@storybook/mdx2-csf')
: await import('@storybook/mdx1-csf');
code = await compile(code, {});
return loadCsf(code, { ...opts, fileName }).parse();
};
return [
{
test: /(stories|story)\.mdx$/,
indexer: mdxIndexer,
addDocsTemplate: true,
},
...(indexers || []),
];
};
export const docs = (docsOptions: DocsOptions) => {
return {
...docsOptions,
enabled: true,
defaultName: 'Docs',
docsPage: true,
};
};