UNPKG

@storybook/preset-react-webpack

Version:

Storybook for React: Develop React Component in isolation with Hot Reloading

77 lines (71 loc) 3.24 kB
import CJS_COMPAT_NODE_URL_yhv20ho0s6p from 'node:url'; import CJS_COMPAT_NODE_PATH_yhv20ho0s6p from 'node:path'; import CJS_COMPAT_NODE_MODULE_yhv20ho0s6p from "node:module"; var __filename = CJS_COMPAT_NODE_URL_yhv20ho0s6p.fileURLToPath(import.meta.url); var __dirname = CJS_COMPAT_NODE_PATH_yhv20ho0s6p.dirname(__filename); var require = CJS_COMPAT_NODE_MODULE_yhv20ho0s6p.createRequire(import.meta.url); // ------------------------------------------------------------ // end of CJS compatibility banner, injected by Storybook's esbuild configuration // ------------------------------------------------------------ // src/framework-preset-cra.ts import { logger as logger2 } from "storybook/internal/node-logger"; // src/cra-config.ts import { existsSync, readFileSync, realpathSync } from "node:fs"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; import { logger } from "storybook/internal/node-logger"; import semver from "semver"; var appDirectory = realpathSync(process.cwd()), reactScriptsPath; function getReactScriptsPath({ noCache } = {}) { if (reactScriptsPath && !noCache) return reactScriptsPath; let reactScriptsScriptPath = realpathSync(join(appDirectory, "/node_modules/.bin/react-scripts")); try { if (/node_modules[\\/]\.bin[\\/]react-scripts/i.test( reactScriptsScriptPath )) { let packagePathMatch = readFileSync(reactScriptsScriptPath, "utf8").match( /"\$basedir[\\/]([^\s]+?[\\/]bin[\\/]react-scripts\.js")/i ); packagePathMatch && packagePathMatch.length > 1 && (reactScriptsScriptPath = join(appDirectory, "/node_modules/.bin/", packagePathMatch[1])); } } catch (e) { logger.warn(`Error occurred during react-scripts package path resolving: ${e}`); } reactScriptsPath = join(reactScriptsScriptPath, "../.."); let scriptsPkgJson = join(reactScriptsPath, "package.json"); return existsSync(scriptsPkgJson) || (reactScriptsPath = "react-scripts"), reactScriptsPath; } async function isReactScriptsInstalled(minimumVersion = "2.0.0") { try { let { default: reactScriptsJson } = await import(pathToFileURL(join(getReactScriptsPath(), "package.json")).href, { with: { type: "json" } }); return !semver.gtr(minimumVersion, reactScriptsJson.version); } catch { return !1; } } // src/framework-preset-cra.ts var checkForNewPreset = (presetsList) => { presetsList.some((preset) => { let presetName = typeof preset == "string" ? preset : preset.name; return /@storybook(\/|\\)preset-create-react-app/.test(presetName); }) || (logger2.warn("Storybook support for Create React App is now a separate preset."), logger2.warn( "To use the new preset, install `@storybook/preset-create-react-app` and add it to the list of `addons` in your `.storybook/main.js` config file." ), logger2.warn("The built-in preset has been disabled in Storybook 6.0.")); }, webpackFinal = async (config, { presetsList }) => (await isReactScriptsInstalled() && presetsList && checkForNewPreset(presetsList), config.module?.rules?.push( { test: /\.m?js$/, type: "javascript/auto" }, { test: /\.m?js$/, resolve: { fullySpecified: !1 } } ), config); export { webpackFinal };