-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
preset.ts
171 lines (157 loc) · 5.08 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
import path from 'path';
import remarkSlug from 'remark-slug';
import remarkExternalLinks from 'remark-external-links';
import { DllReferencePlugin } from 'webpack';
// @ts-ignore
import createCompiler from '../../mdx/mdx-compiler-plugin';
const coreDirName = path.dirname(require.resolve('@storybook/core/package.json'));
// TODO: improve node_modules detection
const context = coreDirName.includes('node_modules')
? path.join(coreDirName, '../../') // Real life case, already in node_modules
: path.join(coreDirName, '../../node_modules'); // SB Monorepo
// 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 const webpackDlls = (dlls: string[], options: any) => {
return options.dll ? [...dlls, './sb_dll/storybook_docs_dll.js'] : [];
};
export function webpack(webpackConfig: any = {}, options: any = {}) {
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 = {
remarkPlugins: [remarkSlug, remarkExternalLinks],
};
// 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: require.resolve('babel-loader'),
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: require.resolve('@mdx-js/loader'),
options: mdxLoaderOptions,
},
],
},
];
}
const result = {
...webpackConfig,
module: {
...module,
rules: [
...rules,
{
test: /\.js$/,
include: new RegExp(`node_modules\\${path.sep}acorn-jsx`),
use: [
{
loader: require.resolve('babel-loader'),
options: {
presets: [[require.resolve('@babel/preset-env'), { modules: 'commonjs' }]],
},
},
],
},
{
test: /\.(stories|story).mdx$/,
use: [
{
loader: require.resolve('babel-loader'),
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: require.resolve('@mdx-js/loader'),
options: {
compilers: [createCompiler(options)],
...mdxLoaderOptions,
},
},
],
},
{
test: /\.mdx$/,
exclude: /\.(stories|story).mdx$/,
use: [
{
loader: require.resolve('babel-loader'),
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: require.resolve('@mdx-js/loader'),
options: mdxLoaderOptions,
},
],
},
...sourceLoader,
],
},
};
if (options.dll) {
result.plugins.push(
new DllReferencePlugin({
context,
manifest: require.resolve('@storybook/core/dll/storybook_docs-manifest.json'),
})
);
}
return result;
}
export function managerEntries(entry: any[] = [], options: any) {
return [...entry, require.resolve('../../register')];
}
export function config(entry: any[] = [], options: any = {}) {
const { framework } = options;
const docsConfig = [require.resolve('./config')];
try {
docsConfig.push(require.resolve(`../${framework}/config`));
} catch (err) {
// there is no custom config for the user's framework, do nothing
}
return [...docsConfig, ...entry];
}