-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10813 from storybookjs/mrmckeb/issue10790
Core: Zero-config TypeScript loading
- Loading branch information
Showing
49 changed files
with
1,120 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 16 additions & 56 deletions
72
app/react/src/server/framework-preset-react-docgen.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,36 @@ | ||
import { TransformOptions } from '@babel/core'; | ||
import * as preset from './framework-preset-react-docgen'; | ||
|
||
describe('framework-preset-react-docgen', () => { | ||
const babelPluginReactDocgenPath = require.resolve('babel-plugin-react-docgen'); | ||
|
||
it('should return the config with the extra plugins when `plugins` is an array.', () => { | ||
it('should return the config with the extra plugin', () => { | ||
const babelConfig = { | ||
babelrc: false, | ||
presets: ['env', 'foo-preset'], | ||
plugins: ['foo-plugin'], | ||
}; | ||
|
||
const config = preset.babel(babelConfig); | ||
|
||
expect(config).toEqual({ | ||
babelrc: false, | ||
plugins: [ | ||
'foo-plugin', | ||
[ | ||
babelPluginReactDocgenPath, | ||
{ | ||
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES', | ||
}, | ||
], | ||
], | ||
presets: ['env', 'foo-preset'], | ||
const config = preset.babel(babelConfig, { | ||
typescriptOptions: { check: false, reactDocgen: 'react-docgen' }, | ||
}); | ||
}); | ||
|
||
it('should return the config with the extra plugins when `plugins` is not an array.', () => { | ||
const babelConfig: TransformOptions = { | ||
babelrc: false, | ||
presets: ['env', 'foo-preset'], | ||
plugins: ['bar-plugin'], | ||
}; | ||
|
||
const config = preset.babel(babelConfig); | ||
|
||
expect(config).toEqual({ | ||
babelrc: false, | ||
plugins: [ | ||
'bar-plugin', | ||
[ | ||
babelPluginReactDocgenPath, | ||
{ | ||
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES', | ||
}, | ||
], | ||
], | ||
presets: ['env', 'foo-preset'], | ||
}); | ||
}); | ||
|
||
it('should return the config only with the extra plugins when `plugins` is not present.', () => { | ||
const babelConfig = { | ||
babelrc: false, | ||
plugins: ['foo-plugin'], | ||
presets: ['env', 'foo-preset'], | ||
}; | ||
|
||
const config = preset.babel(babelConfig); | ||
|
||
expect(config).toEqual({ | ||
babelrc: false, | ||
plugins: [ | ||
[ | ||
babelPluginReactDocgenPath, | ||
{ | ||
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES', | ||
}, | ||
], | ||
overrides: [ | ||
{ | ||
test: /\.(mjs|tsx?|jsx?)$/, | ||
plugins: [ | ||
[ | ||
babelPluginReactDocgenPath, | ||
{ | ||
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES', | ||
}, | ||
], | ||
], | ||
}, | ||
], | ||
presets: ['env', 'foo-preset'], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,50 @@ | ||
import { TransformOptions } from '@babel/core'; | ||
import type { TransformOptions } from '@babel/core'; | ||
import type { Configuration } from 'webpack'; | ||
import type { StorybookOptions } from '@storybook/core/types'; | ||
|
||
export function babel(config: TransformOptions) { | ||
// Ensure plugins are defined or fallback to an array to avoid empty values. | ||
const babelConfigPlugins = config.plugins || []; | ||
|
||
const extraPlugins = [ | ||
[ | ||
require.resolve('babel-plugin-react-docgen'), | ||
export function babel(config: TransformOptions, { typescriptOptions }: StorybookOptions) { | ||
const { reactDocgen } = typescriptOptions; | ||
if (!reactDocgen) { | ||
return config; | ||
} | ||
return { | ||
...config, | ||
overrides: [ | ||
{ | ||
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES', | ||
test: reactDocgen === 'react-docgen' ? /\.(mjs|tsx?|jsx?)$/ : /\.(mjs|jsx?)$/, | ||
plugins: [ | ||
[ | ||
require.resolve('babel-plugin-react-docgen'), | ||
{ | ||
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES', | ||
}, | ||
], | ||
], | ||
}, | ||
], | ||
]; | ||
}; | ||
} | ||
|
||
// If `babelConfigPlugins` is not an `Array`, calling `concat` will inject it | ||
// as a single value, if it is an `Array` it will spread. | ||
export function webpackFinal(config: Configuration, { typescriptOptions }: StorybookOptions) { | ||
const { reactDocgen, reactDocgenTypescriptOptions } = typescriptOptions; | ||
if (reactDocgen !== 'react-docgen-typescript') return config; | ||
return { | ||
...config, | ||
plugins: [].concat(babelConfigPlugins, extraPlugins), | ||
module: { | ||
...config.module, | ||
rules: [ | ||
...config.module.rules, | ||
{ | ||
test: /\.tsx?$/, | ||
// include: path.resolve(__dirname, "../src"), | ||
use: [ | ||
{ | ||
loader: require.resolve('react-docgen-typescript-loader'), | ||
options: reactDocgenTypescriptOptions, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.