-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support storybook 6.2.x-beta (#165)
* chore: upgrade storybook * up snaps * prepare storybook v6.2 integration tests * fix in semver usage * first working version with storybook next * up snap * remove some non needed things * fix tests * try fixing node meomry max * try fixing node meomry max 2nd try
- Loading branch information
1 parent
eb3910e
commit 06a8f89
Showing
83 changed files
with
3,787 additions
and
1,039 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
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
25 changes: 25 additions & 0 deletions
25
packages/cli/src/storybook/getProjectWebpackConfig/index.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable camelcase */ | ||
import webpack from 'webpack'; | ||
import * as semver from 'semver'; | ||
import { getStorybookFramework, getStorybookVersion } from '../storybookUtils'; | ||
import { getV_6_1_X_StorybookProjectWebpackConfig } from './v6.1.x'; | ||
import { getV_6_2_X_StorybookProjectWebpackConfig } from './v6.2.x'; | ||
import debuggers, { DebugNamespaces } from '../../debug'; | ||
|
||
const debug = debuggers[DebugNamespaces.STORYBOOK]; | ||
|
||
export async function getStorybookProjectWebpackConfig(): Promise<webpack.Configuration | void> { | ||
const framework = getStorybookFramework(); | ||
if (framework) { | ||
debug(`storybook framework detected: ${framework}`); | ||
const version = getStorybookVersion(framework); | ||
debug(`storybook version: ${version}`); | ||
if (semver.gte(semver.coerce(version), '6.2.0')) { | ||
debug('version greater than 6.2.0, using V_6_2_X configuration'); | ||
return getV_6_2_X_StorybookProjectWebpackConfig(framework); | ||
} | ||
debug('version below 6.2.0, using V_6_1_X configuration'); | ||
return getV_6_1_X_StorybookProjectWebpackConfig(framework); | ||
} | ||
return undefined; | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/cli/src/storybook/getProjectWebpackConfig/replaceLoaders.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import webpack from 'webpack'; | ||
|
||
export function replaceDefaultMediaLoader(rules: webpack.RuleSetRule[]): webpack.RuleSetRule[] { | ||
const defaultAssertLoaderIndex = rules.findIndex( | ||
rule => | ||
rule.loader?.toString().includes('file-loader') && | ||
rule.test?.toString().includes('svg|ico|jpg|jpeg|png|apng|gif') | ||
); | ||
|
||
// if we find storybooks default asset loader we make sure to use the url loader instead | ||
if (defaultAssertLoaderIndex >= 0) { | ||
const ruleCopy = { ...rules[defaultAssertLoaderIndex] }; | ||
|
||
const newRules = [...rules]; | ||
newRules.splice(defaultAssertLoaderIndex, 1, { | ||
...ruleCopy, | ||
loader: 'url-loader', // we bundle all small assets into the js | ||
options: { limit: 10000, name: 'static/media/[name].[hash:8].[ext]', esModule: false }, | ||
}); | ||
return newRules; | ||
} | ||
return rules; | ||
} |
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
74 changes: 74 additions & 0 deletions
74
packages/cli/src/storybook/getProjectWebpackConfig/v6.2.x.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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as path from 'path'; | ||
import { getStorybookFrameworkLoadOptions } from '../storybookUtils'; | ||
import { replaceWebpackRules } from '../../utils/replaceWebpackRules'; | ||
import { getSbOption, getSbCliOptions } from '../getSbOption'; | ||
import { getPackageFolder } from '../../utils/getPackageFolder'; | ||
import { replaceDefaultMediaLoader } from './replaceLoaders'; | ||
import { StorybookFramework } from '../types'; | ||
|
||
import webpack = require('webpack'); | ||
|
||
async function getWebpackConfig(loadOptions) { | ||
// we have to load all those libs dynamically as they are all optional | ||
const { | ||
getPreviewBuilder, | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
} = require('@storybook/core-server/dist/cjs/utils/get-preview-builder'); | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const { loadAllPresets } = require('@storybook/core-common'); | ||
const cliOptions = getSbCliOptions(); | ||
const configDir = getSbOption('configDir', './.storybook'); | ||
|
||
const previewBuilder = await getPreviewBuilder(configDir); | ||
|
||
const coreOptions = { | ||
...loadOptions, | ||
...cliOptions, | ||
configType: 'PRODUCTION', | ||
outputDir: getOutputDir(getSbOption('outputDir', './storybook-static')), | ||
configDir, | ||
ignorePreview: !!cliOptions.previewUrl, | ||
docsMode: !!cliOptions.docs, | ||
}; | ||
|
||
const presets = await loadAllPresets({ | ||
...coreOptions, | ||
corePresets: [ | ||
require.resolve('@storybook/core-server/dist/cjs/presets/common-preset'), | ||
require.resolve('@storybook/core-server/dist/cjs/presets/manager-preset'), | ||
...previewBuilder.corePresets, | ||
require.resolve('@storybook/core-server/dist/cjs/presets/babel-cache-preset'), | ||
], | ||
overridePresets: previewBuilder.overridePresets, | ||
}); | ||
|
||
const fullOptions = { | ||
...coreOptions, | ||
presets, | ||
}; | ||
|
||
const webpackConfig = await previewBuilder.getConfig(fullOptions); | ||
|
||
// Add node_modules of SB core in case resolvers are placed there by npm/yarn | ||
webpackConfig.resolveLoader = webpackConfig.resolveLoader || {}; | ||
const corePackagePath = getPackageFolder('@storybook/core-server'); | ||
webpackConfig.resolveLoader.modules = webpackConfig.resolveLoader?.modules || ['node_modules']; | ||
webpackConfig.resolveLoader.modules.push(path.join(corePackagePath, 'node_modules')); | ||
|
||
return replaceWebpackRules(webpackConfig, replaceDefaultMediaLoader); | ||
} | ||
|
||
// eslint-disable-next-line camelcase | ||
export async function getV_6_2_X_StorybookProjectWebpackConfig( | ||
framework: StorybookFramework | ||
): Promise<webpack.Configuration | void> { | ||
const loadConfig = getStorybookFrameworkLoadOptions(framework); | ||
|
||
return getWebpackConfig(loadConfig); | ||
} | ||
|
||
function getOutputDir(givenOutputDir) { | ||
return path.isAbsolute(givenOutputDir) | ||
? givenOutputDir | ||
: path.join(process.cwd(), givenOutputDir); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as path from 'path'; | ||
import { softRequireResolve } from './requireUtils'; | ||
|
||
test('return the path if file exists', () => { | ||
expect(softRequireResolve('./requireUtils')).toEqual(path.resolve(__dirname, 'requireUtils.ts')); | ||
}); | ||
|
||
test('return undefined otherwise', () => { | ||
expect(softRequireResolve('./unknown')).toEqual(undefined); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import debuggers, { DebugNamespaces } from '../debug'; | ||
|
||
const debug = debuggers[DebugNamespaces.REQUIRE]; | ||
|
||
export function softRequireResolve(pathName) { | ||
try { | ||
return require.resolve(pathName); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
export function requireFromPaths<T>(paths: Array<string>): T { | ||
for (let i = 0; i < paths.length; i += 1) { | ||
try { | ||
return require(paths[i]); | ||
} catch (e) { | ||
debug(`not found under ${paths[i]}`); | ||
} | ||
} | ||
throw new Error(`module not found - ${paths.join(',')}`); | ||
} | ||
|
||
export function resolveFromPaths(paths: Array<string>): string { | ||
for (let i = 0; i < paths.length; i += 1) { | ||
try { | ||
return require.resolve(paths[i]); | ||
} catch (e) { | ||
debug(`not found under ${paths[i]}`); | ||
} | ||
} | ||
throw new Error(`module not found - ${paths.join(',')}`); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.