Best JavaScript code snippet using storybook-root
codegen-modern-iframe-script.js
Source:codegen-modern-iframe-script.js
1const { loadPreviewOrConfigFile } = require('@storybook/core-common');2const { normalizePath } = require('vite');3module.exports.generateModernIframeScriptCode =4 async function generateModernIframeScriptCode(options, { storiesFilename }) {5 const { presets, configDir } = options;6 const previewOrConfigFile = loadPreviewOrConfigFile({ configDir });7 const presetEntries = await presets.apply('config', [], options);8 const configEntries = [...presetEntries, previewOrConfigFile].filter(9 Boolean10 ).map(configEntry => `/@fs/${normalizePath(configEntry)}`);11 /**12 * This code is largely taken from https://github.com/storybookjs/storybook/blob/d1195cbd0c61687f1720fefdb772e2f490a46584/lib/builder-webpack4/src/preview/virtualModuleModernEntry.js.handlebars13 * Some small tweaks were made to `getProjectAnnotations` (since `import()` needs to be resolved asynchronously)14 * and the HMR implementation has been tweaked to work with Vite.15 */16 const code = `17 import fetch from 'unfetch';18 import global from 'global';19 20 import { composeConfigs, PreviewWeb } from '@storybook/preview-web'; 21 import { ClientApi } from '@storybook/client-api'; 22 import { addons } from '@storybook/addons';23 import createPostMessageChannel from '@storybook/channel-postmessage';24 import createWebSocketChannel from '@storybook/channel-websocket';25 26 import { importFn } from '${storiesFilename}';27 28 const { SERVER_CHANNEL_URL } = global;29 30 const getProjectAnnotations = async () =>31 composeConfigs(await Promise.all([${configEntries.map(configEntry => `import('${configEntry}')`).join(',\n')}]));32 33 const channel = createPostMessageChannel({ page: 'preview' });34 addons.setChannel(channel);35 36 if (SERVER_CHANNEL_URL) {37 const serverChannel = createWebSocketChannel({ url: SERVER_CHANNEL_URL, });38 addons.setServerChannel(serverChannel);39 window.__STORYBOOK_SERVER_CHANNEL__ = serverChannel;40 }41 42 const preview = new PreviewWeb();43 44 window.__STORYBOOK_PREVIEW__ = preview;45 window.__STORYBOOK_STORY_STORE__ = preview.storyStore;46 window.__STORYBOOK_ADDONS_CHANNEL__ = channel;47 window.__STORYBOOK_CLIENT_API__ = new ClientApi({ storyStore: preview.storyStore });48 49 preview.initialize({ importFn, getProjectAnnotations });50 51 if (import.meta.hot) {52 import.meta.hot.accept('${storiesFilename}', (newModule) => {53 // importFn has changed so we need to patch the new one in54 preview.onStoriesChanged({ importFn: newModule.importFn });55 });56 57 import.meta.hot.accept(${JSON.stringify(configEntries)}, ([...newConfigEntries]) => {58 const newGetProjectAnnotations = () => composeConfigs(newConfigEntries);59 // getProjectAnnotations has changed so we need to patch the new one in60 preview.onGetProjectAnnotationsChanged({ getProjectAnnotations: newGetProjectAnnotations });61 });62 }63 `.trim();64 return code;...
create-react-component.js
Source:create-react-component.js
1import { mkdir, writeFile } from "fs/promises";2//import { argv } from "process";3import path from "path";4import process from "node:process";5const jsxTemplate = (componentName) => `import React from "react";6const ${componentName} = ({children, ...rest}) => {7 return <div {...rest}>{children}</div>8}9export default ${componentName};10`;11const readmeTemplate = (componentName) => `# ${componentName}12## How to use this component13`;14const storiesTemplate = (componentName) => `import React from "react";15import ${componentName} from "./";16export default {17 title: "${componentName}",18 component: ${componentName},19};20export const Default = () => <${componentName}>${componentName}</${componentName}>;21`;22const testTemplate = (componentName) => `23/**24 * @jest-environment jsdom25 */26import "@testing-library/jest-dom";27import { render, screen } from "@testing-library/react";28import userEvent from "@testing-library/user-event";29import React from "react";30import ${componentName} from "./";31describe("${componentName} component", () => {32 it("should render", () => {33 const text = "Click me";34 render(<${componentName}>{text}</${componentName}>);35 expect(screen.getByText(text)).toBeInTheDocument();36 });37});38`;39// node create-react-component.js40// npx create-react-app41// API42// node create-react-component.js directory componentName43// node create-react-component.js atoms Button44// ->45// * out/atoms/Button/index.jsx46// * out/atoms/Button/README.md47const outFolder = "out";48const createReactComponent = async (directoryName, componentName) => {49 const directory = path.join(outFolder, directoryName, componentName);50 const jsxFilename = path.join(directory, "index.jsx");51 const readmeFilename = path.join(directory, "README.md");52 const storiesFilename = path.join(53 directory,54 `${componentName}.stories.jsx`55 );56 const testFilename = path.join(directory, `${componentName}.test.jsx`);57 // we can use await since this is an async function58 // CLI `mkdir -p`59 await mkdir(directory, { recursive: true });60 writeFile(jsxFilename, jsxTemplate(componentName))61 .then(() => {62 console.log(`created file: ${jsxFilename}`);63 })64 .catch((error_) => {65 console.error(error_);66 });67 writeFile(readmeFilename, readmeTemplate(componentName))68 .then(() => {69 console.log(`created file: ${readmeFilename}`);70 })71 .catch((error_) => {72 console.error(error_);73 });74 writeFile(storiesFilename, storiesTemplate(componentName))75 .then(() => {76 console.log(`created file: ${storiesFilename}`);77 })78 .catch((error_) => {79 console.error(error_);80 });81 writeFile(testFilename, testTemplate(componentName))82 .then(() => {83 console.log(`created file: ${testFilename}`);84 })85 .catch((error_) => {86 console.error(error_);87 });88};89// Read arguments from CLI90const [directoryName, componentName] = process.argv.slice(2);91// Add arguments to the create function...
addComponent.ts
Source:addComponent.ts
1import { blue, green } from 'chalk';2import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'graceful-fs';3import { compile } from 'handlebars';4import { relative, resolve } from 'path';5import { BEM_PREFIX } from '../../config/frontEndConfig';6import { generateNames } from './util';7// Find a better way to do this8const ROOT = resolve(__dirname, '..', '..');9export const addComponent = async (componentName: string) => {10 const templates = buildTemplates();11 const names = generateNames(componentName);12 const tsxFileName = `${names.pascalCase}.tsx`;13 const scssFileName = `${names.pascalCase}.scss`;14 const storiesFileName = `${names.pascalCase}.stories.tsx`;15 const config = {16 UppercaseName: names.pascalCase,17 dashCaseName: names.dashCase,18 tsxFileName,19 scssFileName,20 storiesFileName,21 BEMPrefix: BEM_PREFIX22 };23 const componentFolder = resolve(ROOT, 'src', 'components', names.pascalCase);24 if (existsSync(componentFolder)) {25 throw new Error(`Conflict: Folder '${componentFolder}' already exists`);26 }27 mkdirSync(componentFolder, { recursive: true });28 console.log(green(`Created folder: ${relative(ROOT, componentFolder)}`));29 writeFileSync(resolve(componentFolder, tsxFileName), templates.tsx(config));30 console.log(blue(`Created file: ${relative(ROOT, resolve(componentFolder, tsxFileName))}`));31 writeFileSync(resolve(componentFolder, scssFileName), templates.scss(config));32 console.log(blue(`Created file: ${relative(ROOT, resolve(componentFolder, scssFileName))}`));33 writeFileSync(resolve(componentFolder, storiesFileName), templates.stories(config));34 console.log(blue(`Created file: ${relative(ROOT, resolve(componentFolder, storiesFileName))}`));35};36const buildTemplates = () => {37 return {38 tsx: compile(readFileSync(resolve(__dirname, 'tsx.template')).toString()),39 scss: compile(readFileSync(resolve(__dirname, 'scss.template')).toString()),40 stories: compile(readFileSync(resolve(__dirname, 'stories.template')).toString())41 };42};43const componentName = (process as any).argv[2];44if (!componentName) {45 throw new Error('Please specify a component name');46}...
Using AI Code Generation
1import { storiesFilename } from 'storybook-root-config';2storiesFilename('test.js');3import { storiesFilename } from 'storybook-root-config';4storiesFilename('test2.js');5import { storiesFilename } from 'storybook-root-config';6storiesFilename('test3.js');7import { storiesFilename } from 'storybook-root-config';8storiesFilename('test4.js');9import { storiesFilename } from 'storybook-root-config';10storiesFilename('test5.js');11import { storiesFilename } from 'storybook-root-config';12storiesFilename('test6.js');13import { storiesFilename } from 'storybook-root-config';14storiesFilename('test7.js');15import { storiesFilename } from 'storybook-root-config';16storiesFilename('test8.js');17import { storiesFilename } from 'storybook-root-config';18storiesFilename('test9.js');19import { storiesFilename } from 'storybook-root-config';20storiesFilename('test10.js');21import { storiesFilename } from 'storybook-root-config';22storiesFilename('test11.js');23import { storiesFilename } from 'storybook-root-config';24storiesFilename('test12.js');25import { storiesFilename } from 'storybook-root-config';26storiesFilename('test13.js');27import { storiesFilename }
Using AI Code Generation
1import { storiesFilename } from 'storybook-root';2storiesFilename('test');3import { storiesFilename } from 'storybook-root';4storiesFilename('test2');5import { storiesFilename } from 'storybook-root';6storiesFilename('test');7import { storiesFilename } from 'storybook-root';8storiesFilename('test2');9import { storiesFilename } from 'storybook-root';10storiesFilename('test');11import { storiesFilename } from 'storybook-root';12storiesFilename('test2');
Using AI Code Generation
1import { storiesFilename } from 'storybook-root'2console.log(storiesFilename)3import { storiesFilename } from 'storybook-root'4console.log(storiesFilename)5import { storiesFilename } from 'storybook-root'6console.log(storiesFilename)7import { storiesFilename } from 'storybook-root'8console.log(storiesFilename)9import { storiesFilename } from 'storybook-root'10console.log(storiesFilename)11import { storiesFilename } from 'storybook-root'12console.log(storiesFilename)13import { storiesFilename } from 'storybook-root'14console.log(storiesFilename)15import { storiesFilename } from 'storybook-root'16console.log(storiesFilename)17import { storiesFilename } from 'storybook-root'18console.log(storiesFilename)19import { storiesFilename } from 'storybook-root'20console.log(storiesFilename)21import { storiesFilename } from 'storybook-root'22console.log(storiesFilename)23import { storiesFilename } from 'storybook-root'24console.log(storiesFilename)25import { storiesFilename } from 'storybook-root'26console.log(storiesFilename)
Using AI Code Generation
1const { storiesFilename } = require('@storybook/react/dist/server/config/defaults/webpack.config.js');2const path = require('path');3module.exports = {4 webpackFinal: async (config) => {5 config.resolve.alias = {6 'storybook-root-config': path.resolve(__dirname, '.storybook'),7 };8 return config;9 },10};
Using AI Code Generation
1const { storiesFilename } = require('@storybook/react/dist/server/config/utils');2console.log(storiesFilename);3const { storiesFilename } = require('@storybook/react/dist/server/config/utils');4console.log(storiesFilename);5const { storiesFilename } = require('@storybook/react/dist/server/config/utils');6console.log(storiesFilename);7const { storiesFilename } = require('@storybook/react/dist/server/config/utils');8console.log(storiesFilename);9const { storiesFilename } = require('@storybook/react/dist/server/config/utils');10console.log(storiesFilename);11const { storiesFilename } = require('@storybook/react/dist/server/config/utils');12console.log(storiesFilename);13const { storiesFilename } = require('@storybook/react/dist/server/config/utils');14console.log(storiesFilename);15const { storiesFilename } = require('@storybook/react/dist/server/config/utils');16console.log(storiesFilename);17const { storiesFilename } = require('@storybook/react/dist/server/config/utils');18console.log(storiesFilename);19const { storiesFilename } = require('@storybook/react/dist/server/config/utils');20console.log(storiesFilename);21const { storiesFilename } = require('@storybook/react/dist/server/config/utils');22console.log(storiesFilename);23const { storiesFilename } = require('@storybook/react/dist/server/config/utils');24console.log(storiesFilename);25const { storiesFilename } = require('@storybook/react/dist/server/config/utils');26console.log(storiesFilename);
Using AI Code Generation
1import { storiesFilename } from 'storybook-root-config';2console.log('storiesFilename', storiesFilename);3import { storiesFilename } from 'storybook-root-config';4console.log('storiesFilename', storiesFilename);5const { storiesFilename } = require('storybook-root-config');6console.log('storiesFilename', storiesFilename);7const { storiesFilename } = require('storybook-root-config');8console.log('storiesFilename', storiesFilename);9const { storiesFilename } = require('storybook-root-config');10console.log('storiesFilename', storiesFilename);11const { storiesFilename } = require('storybook-root-config');12console.log('storiesFilename', storiesFilename);13const { storiesFilename } = require('storybook-root-config');14console.log('storiesFilename', storiesFilename);15const { storiesFilename } = require('storybook-root-config');16console.log('storiesFilename', storiesFilename);17import { storiesFilename } from 'storybook-root-config';18console.log('storiesFilename', storiesFilename);19import { storiesFilename } from 'storybook-root-config';20console.log('storiesFilename', storiesFilename);21import { storiesFilename } from 'storybook-root-config';22console.log('storiesFilename', storiesFilename);23const { storiesFilename } = require('storybook-root-config');24console.log('storiesFilename', storiesFilename);
Using AI Code Generation
1const stories = require('storybook-root').storiesFilename;2const stories = require('storybook-root').storiesFilename;3const stories = require('storybook-root').storiesFilename;4const stories = require('storybook-root').storiesFilename;5const stories = require('storybook-root').storiesFilename;6const stories = require('storybook-root').storiesFilename;7const stories = require('storybook-root').storiesFilename;8const stories = require('storybook-root').storiesFilename;9const stories = require('storybook-root').storiesFilename;10const stories = require('storybook-root').storiesFilename;11const stories = require('storybook-root').storiesFilename;12const stories = require('storybook-root').storiesFilename;13const stories = require('storybook-root').storiesFilename;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!