Best JavaScript code snippet using storybook-root
runner.js
Source:runner.js
1const { runLoaders } = require('loader-runner');2const path = require('path');3const fs = require('fs');4//å
¥å£æ件5const entryFile = path.resolve(__dirname, 'src', 'title.js');6//loaderç转æ¢è§åé
ç½®7let rules = [8 {9 test: /title\.js$/,10 use: ['normal1-loader.js', 'normal2-loader.js']11 },12 {13 test: /title\.js$/,14 enforce: 'post',15 use: ['post1-loader.js', 'post2-loader.js']16 },17 {18 test: /title\.js$/,19 enforce: 'pre',20 use: ['pre1-loader.js', 'pre2-loader.js']21 }22]23//æåstyle-loaderçæ¶å使ç¨å°24let request = `inline1-loader!inline2-loader!${entryFile}`;25let parts = request.replace(/^-?!+/, '').split('!');//['inline1-loader','inline2-loader',entryFile]26let resource = parts.pop();//entryFile27const inlineLoaders = parts;//['inline1-loader','inline2-loader']28const preLoaders = [], postLoaders = [], normalLoaders = [];29rules.forEach(rule => {30 //if (rule.test.test(resource)) {31 if (resource.match(rule.test)) {32 if (rule.enforce === 'pre') {33 preLoaders.push(...rule.use);34 } else if (rule.enforce === 'post') {35 postLoaders.push(...rule.use);36 } else {37 normalLoaders.push(...rule.use);38 }39 }40})41/**42 * -! noPreAutoLoaders ä¸è¦åç½®åæ®éloader43 * ! noAutoLoaders ä¸è¦æ®éloader44 * !! noPrePostAutoLoaders ä¸è¦åç½®ãåç½®ãæ®éloader,åªè¦å
è45 */46let loaders = [];47if (request.startsWith('!!')) {48 loaders = inlineLoaders;49} else if (request.startsWith('-!')) {50 loaders = [...postLoaders, ...inlineLoaders];51} else if (request.startsWith('!')) {52 loaders = [...postLoaders, ...inlineLoaders, ...preLoaders];53} else {54 loaders = [...postLoaders, ...inlineLoaders, ...normalLoaders, ...preLoaders];55}56//ç¨äºæloaderçå称转åæä¸ä¸ªç»å¯¹è·¯å¾57const resolveLoader = loader => path.resolve(__dirname, 'runner', loader);58loaders = loaders.map(resolveLoader);59runLoaders({60 resource,//è¦å è½½å转æ¢ç模å61 loaders,//æ¯ä¸ä¸ªç»å¯¹è·¯å¾çloaderæ°ç»62 context: { name: 'zhufeng' },//loaderçä¸ä¸æ对象63 readResource: fs.readFile.bind(fs)//读å硬çä¸èµæºçæ¹æ³64}, (err, result) => {65 console.log(err);//è¿è¡é误66 console.log(result);//转æ¢åçç»æ67 //resourceBuffer æ¯bufferæ ¼å¼çæºä»£ç çå
容ï¼å¦ææ¯pitchè¿åçï¼æ²¡æ读åæºæ件ï¼é£ä¹å®å°±æ¯null68 if (result.resourceBuffer) {69 console.log(result.resourceBuffer.toString('utf8'));//æåå§ç转æ¢åçæºæ件å
容70 }...
cssLoader.js
Source:cssLoader.js
1/*2 * @Descripttion: sass/less/stylus/css/postcss æµè§å¨é
ç½®3 * @Author: all4 * @Date: 2020-07-16 09:18:205 * @LastEditors: all6 * @LastEditTime: 2020-07-16 17:58:157 */8// csså缩æåæ件9const MiniCssExtractPlugin = require('mini-css-extract-plugin');10const path = require('path');11exports.cssLoadersOptions = function(options) {12 options = options || {};13 const cssloader = {14 loader: 'css-loader',15 options: {16 importLoaders: 2,17 sourceMap: options.cssSourceMap18 }19 };20 const postcssLoader = {21 loader: 'postcss-loader',22 options: {23 sourceMap: options.cssSourceMap24 }25 };26 function generateLoaders(loader, loaderOptions, otherOPtions) {27 const loaders = options.usePostCSS28 ? [cssloader, postcssLoader]29 : [cssloader];30 if (loader) {31 loaders.push({32 loader: loader + '-loader',33 options: Object.assign({}, loaderOptions, {34 sourceMap: options.cssSourceMap35 })36 });37 }38 if (options.extract) {39 loaders.unshift(MiniCssExtractPlugin.loader);40 } else {41 loaders.unshift({42 loader: 'style-loader',43 options: Object.assign(44 {},45 {46 insert: 'head', // æ ·å¼æå
¥å° <head>47 injectType: 'singletonStyleTag' // å°ææçstyleæ ç¾å并æä¸ä¸ª48 }49 )50 });51 }52 if (otherOPtions && otherOPtions.sassCommon && loader === 'sass') {53 loaders.push({54 loader: 'sass-resources-loader',55 options: {56 resources: path.resolve(__dirname, '../../src/asstes/scss/index.scss')57 }58 });59 }60 return loaders;61 }62 return {63 css: generateLoaders(),64 less: generateLoaders('less'),65 sass: generateLoaders('sass', {}, { sassCommon: true }),66 scss: generateLoaders('sass', {}, { sassCommon: true }),67 stylus: generateLoaders('stylus'),68 styl: generateLoaders('stylus')69 };70};71// Generate loaders for standalone style files (outside of .vue)72exports.styleLoaders = function(options) {73 const output = [];74 const loaders = exports.cssLoadersOptions(options);75 for (const extension in loaders) {76 const loader = loaders[extension];77 output.push({78 test: new RegExp('\\.' + extension + '$'),79 use: loader80 });81 }82 return output;...
index.js
Source:index.js
1// 2'use strict';3const os = require('os');4const createExplorer = require('./createExplorer');5const loaders = require('./loaders');6module.exports = cosmiconfig;7function cosmiconfig(8 moduleName ,9 options 10 11 12 13 14 15 16 17 18) {19 options = options || {};20 const defaults = {21 packageProp: moduleName,22 searchPlaces: [23 'package.json',24 `.${moduleName}rc`,25 `.${moduleName}rc.json`,26 `.${moduleName}rc.yaml`,27 `.${moduleName}rc.yml`,28 `.${moduleName}rc.js`,29 `${moduleName}.config.js`,30 ],31 ignoreEmptySearchPlaces: true,32 stopDir: os.homedir(),33 cache: true,34 transform: identity,35 };36 const normalizedOptions = Object.assign(37 {},38 defaults,39 options,40 {41 loaders: normalizeLoaders(options.loaders),42 }43 );44 return createExplorer(normalizedOptions);45}46cosmiconfig.loadJs = loaders.loadJs;47cosmiconfig.loadJson = loaders.loadJson;48cosmiconfig.loadYaml = loaders.loadYaml;49function normalizeLoaders(rawLoaders ) {50 const defaults = {51 '.js': { sync: loaders.loadJs, async: loaders.loadJs },52 '.json': { sync: loaders.loadJson, async: loaders.loadJson },53 '.yaml': { sync: loaders.loadYaml, async: loaders.loadYaml },54 '.yml': { sync: loaders.loadYaml, async: loaders.loadYaml },55 noExt: { sync: loaders.loadYaml, async: loaders.loadYaml },56 };57 if (!rawLoaders) {58 return defaults;59 }60 return Object.keys(rawLoaders).reduce((result, ext) => {61 const entry = rawLoaders && rawLoaders[ext];62 if (typeof entry === 'function') {63 result[ext] = { sync: entry, async: entry };64 } else {65 result[ext] = entry;66 }67 return result;68 }, defaults);69}70function identity(x) {71 return x;...
Using AI Code Generation
1import { configure } from '@storybook/react';2const req = require.context('../src', true, /\.stories\.js$/);3function loadStories() {4 req.keys().forEach(filename => req(filename));5}6configure(loadStories, module);7const path = require('path');8module.exports = {9 module: {10 {11 {12 loader: require.resolve('@storybook/source-loader'),13 options: {14 prettierConfig: {15 },16 },17 },18 },19 },20};21import { configure } from '@storybook/react';22const req = require.context('../src', true, /\.stories\.js$/);23function loadStories() {24 req.keys().forEach(filename => req(filename));25}26configure(loadStories, module);27const path = require('path');28module.exports = {29 module: {30 {31 {32 loader: require.resolve('@storybook/source-loader'),33 options: {34 prettierConfig: {35 },36 },37 },38 },39 },40};41module.exports = {42};43import { addDecorator } from '@storybook/react';44import { withKnobs } from '@storybook/addon-knobs';45addDecorator(withKnobs);46import { addons } from '@storybook/addons';47addons.setConfig({48 theme: {49 },50});
Using AI Code Generation
1import { configure, addDecorator } from '@storybook/react';2import { withInfo } from '@storybook/addon-info';3import { withKnobs } from '@storybook/addon-knobs';4addDecorator(withKnobs);5addDecorator(withInfo);6const req = require.context('../src', true, /.stories.js$/);7function loadStories() {8 req.keys().forEach(filename => req(filename));9}10configure(loadStories, module);11import { configure, addDecorator } from '@storybook/react';12import { withInfo } from '@storybook/addon-info';13import { withKnobs } from '@storybook/addon-knobs';14addDecorator(withKnobs);15addDecorator(withInfo);16const req = require.context('../src', true, /.stories.js$/);17function loadStories() {18 req.keys().forEach(filename => req(filename));19}20configure(loadStories, module);21{22 "scripts": {23 },24 "dependencies": {
Using AI Code Generation
1import { configure, addDecorator } from '@storybook/react';2import { withKnobs } from '@storybook/addon-knobs';3import { withInfo } from '@storybook/addon-info';4import { setOptions } from '@storybook/addon-options';5import { setDefaults } from 'storybook-addon-jsx';6import { withA11y } from '@storybook/addon-a11y';7import { withOptions } from '@storybook/addon-options';8import { withConsole } from '@storybook/addon-console';9import { withViewport } from '@storybook/addon-viewport';10import { withTests } from '@storybook/addon-jest';11const req = require.context('../stories', true, /\.stories\.js$/);12function loadStories() {13 req.keys().forEach(filename => req(filename));14}15configure(loadStories, module);16addDecorator(withKnobs);17addDecorator(withInfo);18addDecorator(withA11y);19addDecorator(20 withOptions({21 })22);23addDecorator((storyFn, context) => withConsole()(storyFn)(context));24addDecorator(withViewport());25addDecorator(26 withTests({27 results: {28 }29 })30);31setOptions({32});33setDefaults({34});35addDecorator(story => <div style={{ padding: '3rem' }}>{story()}</div>);36addDecorator(story => <div style={{ padding: '3rem' }}>{story()}</div>);37addDecorator(story => <div style={{ padding: '3rem' }}>{story()}</div>);38addDecorator(story => <
Using AI Code Generation
1import { configure } from '@storybook/react';2import { setOptions } from '@storybook/addon-options';3import { setDefaults } from '@storybook/addon-info';4import { setDefaults as setInfoDefaults } from '@storybook/addon-info';5import { setDefaults as setKnobsDefaults } from '@storybook/addon-knobs';6import { setDefaults as setOptionsDefaults } from '@storybook/addon-options';7import { setDefaults as setViewportDefaults } from '@storybook/addon-viewport';8import { setDefaults as setLinksDefaults } from '@storybook/addon-links';9import { setDefaults as setNotesDefaults } from '@storybook/addon-notes';10import { setDefaults as setActionsDefaults } from '@storybook/addon-actions';11setOptions({12});13setInfoDefaults({14});15setKnobsDefaults({16});17setOptionsDefaults({18});19setViewportDefaults({20});21setLinksDefaults({22});23setNotesDefaults({24});25setActionsDefaults({26});27const req = require.context('../src', true, /.stories.js$/);28function loadStories() {29 req.keys().forEach(filename => req(filename));30}31configure(loadStories, module);32import { configure } from '@storybook/react';33import { setOptions } from '@storybook/addon-options';34import { setDefaults } from '@storybook/addon-info';35import { setDefaults as setInfoDefaults } from '@storybook/addon-info';36import { setDefaults as setKnobsDefaults } from '@storybook/addon-knobs';37import { setDefaults as setOptionsDefaults } from '@storybook/addon-options';38import { setDefaults as setViewportDefaults } from '@storybook/addon-viewport';39import { setDefaults as setLinksDefaults } from '@storybook/addon-links';40import { setDefaults as setNotesDefaults } from '@storybook/addon
Using AI Code Generation
1const loadCustomDecorators = require('@storybook/addon-storysource/loader');2module.exports = loadCustomDecorators;3module.exports = {4 webpackFinal: async (config, { configType }) => {5 config.module.rules.push({6 loaders: [require.resolve('@storybook/addon-storysource/loader')],7 });8 return config;9 },10};
Using AI Code Generation
1const path = require('path');2const { loadEnv } = require('@storybook/core/server');3const rootConfigDir = path.resolve(__dirname, '../');4module.exports = async ({ config }) => {5 loadEnv(rootConfigDir);6 return config;7};8const path = require('path');9const rootConfigDir = path.resolve(__dirname, '../');10module.exports = {11 stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],12 core: {13 },14 webpackFinal: async (config) => {15 loadEnv(rootConfigDir);16 return config;17 },18};19import { withNextRouter } from 'storybook-addon-next-router';20import { addDecorator } from '@storybook/react';21addDecorator(withNextRouter());22import { addons } from '@storybook/addons';23import { themes } from '@storybook/theming';24import { create } from '@storybook/theming/create';25addons.setConfig({26 theme: create({27 }),28});
Using AI Code Generation
1const { loadEnv } = require('@storybook/core/server');2module.exports = async ({ config }) => {3 loadEnv();4 loadEnv('development');5 loadEnv('test');6 loadEnv('production');7 return config;8};9module.exports = {10 stories: ['../src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],11 core: {12 },13 webpackFinal: (config) => {14 config.module.rules.push({15 });16 return config;17 },18};19import { addDecorator } from '@storybook/react';20import { withTests } from '@storybook/addon-jest';21import results from '../.jest-test-results.json';22addDecorator(23 withTests({24 })25);26const path = require('path');27module.exports = async ({ config, mode }) => {28 config.resolve.modules.push(path.resolve(__dirname, '../'));29 return config;30};31{32 "compilerOptions": {33 "paths": {34 }35 },
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!!