Best JavaScript code snippet using storybook-root
rewrite-imports.ts
Source:rewrite-imports.ts
...34 // Extracting comments from spec as they are stripped in `matchDynamicImportValue`35 webpackMagicCommentMatches = spec.match(WEBPACK_MAGIC_COMMENT_REGEX);36 spec = matchDynamicImportValue(spec) || '';37 }38 let rewrittenImport = replaceImport(spec);39 if (imp.d > -1) {40 rewrittenImport = webpackMagicCommentMatches41 ? `${webpackMagicCommentMatches.join(' ')} ${JSON.stringify(rewrittenImport)}`42 : JSON.stringify(rewrittenImport);43 }44 rewrittenCode = spliceString(rewrittenCode, rewrittenImport, imp.s, imp.e);45 }46 return rewrittenCode;47}48async function transformHtmlImports(code: string, replaceImport: (specifier: string) => string) {49 let rewrittenCode = code;50 let match;51 const jsImportRegex = new RegExp(HTML_JS_REGEX);52 while ((match = jsImportRegex.exec(rewrittenCode))) {53 const [, scriptTag, scriptCode] = match;54 // Only transform a script element if it contains inlined code / is not empty.55 if (scriptCode.trim()) {56 rewrittenCode = spliceString(57 rewrittenCode,58 await transformEsmImports(scriptCode, replaceImport),59 match.index + scriptTag.length,60 match.index + scriptTag.length + scriptCode.length,61 );62 }63 }64 const cssImportRegex = new RegExp(HTML_STYLE_REGEX);65 while ((match = cssImportRegex.exec(rewrittenCode))) {66 const [, styleTag, styleCode] = match;67 // Only transform a script element if it contains inlined code / is not empty.68 if (styleCode.trim()) {69 rewrittenCode = spliceString(70 rewrittenCode,71 await transformCssImports(styleCode, replaceImport),72 match.index + styleTag.length,73 match.index + styleTag.length + styleCode.length,74 );75 }76 }77 return rewrittenCode;78}79async function transformCssImports(code: string, replaceImport: (specifier: string) => string) {80 let rewrittenCode = code;81 let match;82 const importRegex = new RegExp(CSS_REGEX);83 while ((match = importRegex.exec(rewrittenCode))) {84 const [fullMatch, spec] = match;85 // Only transform a script element if it contains inlined code / is not empty.86 rewrittenCode = spliceString(87 rewrittenCode,88 // CSS doesn't support proxy files, so always point to the original file89 `@import "${replaceImport(spec).replace('.proxy.js', '')}";`,90 match.index,91 match.index + fullMatch.length,92 );93 }94 return rewrittenCode;95}96export async function transformFileImports(97 {baseExt, contents}: SnowpackSourceFile<string>,98 replaceImport: (specifier: string) => string,99) {100 if (baseExt === '.js') {101 return transformEsmImports(contents, replaceImport);102 }103 if (baseExt === '.html') {...
gulpfile.js
Source:gulpfile.js
...39});40gulp.task('override:bootstrap', () => {41 const SCSS = join(ROOT, '/scss/overrides/bootstrap');42 return gulp.src(join(ROOT, '/node_modules/bootstrap/scss/bootstrap.scss'))43 .pipe(gulpif(!!CUSTOM_BOOTSTRAP_SCSS, replaceImport('utilities', CUSTOM_BOOTSTRAP_SCSS)))44 .pipe(replaceImport('custom', '/custom.scss', SCSS))45 .pipe(gulpif(!!CUSTOM_BOOTSTRAP_VARIABLES, replaceImport('custom', CUSTOM_BOOTSTRAP_VARIABLES)))46 .pipe(replaceImport('reboot', '/reboot.scss', SCSS))47 .pipe(replaceImport('buttons', '/buttons.scss', SCSS))48 .pipe(replaceImport('forms', '/forms.scss', SCSS))49 .pipe(replaceImport('card', '/card.scss', SCSS))50 .pipe(replaceImport('modal', '/modal.scss', SCSS))51 .pipe(replace(/\\/g, '\\\\'))52 .pipe(sass().on('error', sass.logError))53 .pipe(rename('bootstrap-override.css'))54 .pipe(gulp.dest(join(ROOT, '/css')));55});56const replaceImport = (existentFileName, newFile, path) => {57 const newFilePath = path ? join(path, newFile) : newFile;58 return replace(new RegExp(`@import "${existentFileName}";`), `@import "${existentFileName}";\n@import "${newFilePath}";`);59};60gulp.task('override:min', () => {61 return gulp.src([62 join(ROOT, `/css/bootstrap-override.css`),63 join(ROOT, `/css/material-design-override.css`)64 ])...
snowball-loader.js
Source:snowball-loader.js
1var loaderUtils = require('loader-utils');2function replaceImport(source, _package, replaceWith) {3 const getPackageName = typeof replaceWith === 'function'4 ? replaceWith5 : function (packageName) {6 return replaceWith;7 };8 const getImportExp = function (varName, packageName, semicolon = ';') {9 if (!varName) return '';10 return "var " + varName.replace(/([$\w]+)\s+as\s+([$\w]+)/g, "$1:$2") + "=" + getPackageName(packageName) + semicolon;11 };12 // const STRING_RE = "'(?:(?:\\\\{2})+|\\\\'|[^'])*'|\"(?:(?:\\\\{2})+|\\\\\"|[^\"])*\"|`(?:(?:\\\\{2})+|\\\\`|[^`])*`";13 _package = '(' + _package + ')';14 source = source15 .replace(new RegExp("\\bimport\\s*\\*\\sas\\s+([\\w$]+)\\s+from\\s*(\"|')" + _package + "\\2\\s*;?", 'mg'), function (match, name, q, packageName) {16 return getImportExp(name, packageName);17 })18 .replace(new RegExp("\\bimport\\s*([\\w$]+)(?:\\s*,\\s*(\\{[^}]+\\}))?\\s*from\\s*(\"|')" + _package + "\\3\\s*;?", "mg"), function (match, name, names, q, packageName) {19 return getImportExp(name, packageName)20 + getImportExp(names, packageName);21 })22 .replace(new RegExp("\\bimport\\s*(\\{[^}]+\\})(?:\\s*,\\s*([\\w$]+))?\\s*from\\s*(\"|')" + _package + "\\3\\s*;?", "mg"), function (match, names, name, q, packageName) {23 return getImportExp(names, packageName)24 + getImportExp(name, packageName);25 });26 source = source27 .replace(new RegExp("\\b(?:var|const|let|,)\\s+([\\w$]+)\\s*=\\s*require\\(\\s*(\"|')" + _package + "\\2\\s*\\)\\s*(;)?", 'mg'), function (match, name, q, packageName, semicolon) {28 return getImportExp(name, packageName, semicolon || '');29 });30 return source;31}32module.exports = function (source) {33 const options = loaderUtils.getOptions(this);34 var content = replaceImport(source, "snowball", "window.Snowball");35 content = replaceImport(content, "snowball/app", "window.Snowball._app");36 content = replaceImport(content, "snowball/components", "window.Snowball._components");37 content = replaceImport(content, "snowball/graphics", "window.Snowball._graphics");38 content = replaceImport(content, "snowball/utils", "window.Snowball.util");39 content = replaceImport(content, "snowball/widget", "window.Snowball._widget");40 content = replaceImport(content, "snowball/(?!libs/).+", (packageName) => "{};throw new Error('unavaliable import `" + packageName + "`!!')");41 content = replaceImport(content, "react", "window.Snowball._React");42 content = replaceImport(content, "react-dom", "window.Snowball._ReactDOM");43 if (options && options.modules) {44 Object.keys(options.modules)45 .forEach((name) => {46 content = replaceImport(content, name, options.modules[name]);47 });48 }49 this.cacheable();50 this.callback(null, content);...
Using AI Code Generation
1import { replaceImport } from 'storybook-root-decorator';2replaceImport('react', 'preact/compat');3replaceImport('react-dom', 'preact/compat');4import { replaceImport } from 'storybook-root-decorator';5replaceImport('react', 'preact/compat');6replaceImport('react-dom', 'preact/compat');7import { configure } from '@storybook/react';8import { replaceImport } from 'storybook-root-decorator';9replaceImport('react', 'preact/compat');10replaceImport('react-dom', 'preact/compat');11configure(require.context('../src', true, /\.stories\.js$/), module);12import { addons } from '@storybook/addons';13import { themes } from '@storybook/theming';14import { create } from '@storybook/theming/create';15import { replaceImport } from 'storybook-root-decorator';16replaceImport('react', 'preact/compat');17replaceImport('react-dom', 'preact/compat');18addons.setConfig({19 theme: create({20 }),21});22const path = require('path');23const { replaceImport } = require('storybook-root-decorator');24replaceImport('react', 'preact/compat');25replaceImport('react-dom', 'preact/compat');26module.exports = ({ config }) => {27 config.resolve.alias = {28 '@storybook/addon-docs/react': path.resolve(29 };30 return config;31};32const path = require('path');33const { replaceImport } = require('storybook-root-decorator');34replaceImport('react', 'pre
Using AI Code Generation
1import { replaceImport } from "storybook-root-import";2replaceImport();3import { storiesOf } from "src/stories";4import { action } from "src/stories";5import { linkTo } from "src/stories";6storiesOf("Button", module)7 .add("with text", () => (8 <Button onClick={action("clicked")}>Hello Button</Button>9 .add("with some emoji", () => (10 <Button onClick={action("clicked")}>11 .add("with some emoji and action link", () => (12 <Button onClick={linkTo("Button", "with some emoji")}>13 ));
Using AI Code Generation
1import replaceImport from 'storybook-root-import';2replaceImport('src', '/src');3import './test';4import { configure } from '@storybook/react';5import { setOptions } from '@storybook/addon-options';6import { setDefaults } from '@storybook/addon-info';7setDefaults({8 styles: stylesheet => {9 stylesheet.infoBody = {10 };11 stylesheet.infoStory = {12 };13 return stylesheet;14 },15});16setOptions({
Using AI Code Generation
1import { replaceImport } from 'storybook-root-import'2replaceImport('@storybook', 'storybook')3replaceImport('@storybook/addon-knobs', 'storybook-addon-knobs')4replaceImport('@storybook/addon-actions', 'storybook-addon-actions')5replaceImport('@storybook/addon-links', 'storybook-addon-links')6replaceImport('@storybook/addon-notes', 'storybook-addon-notes')7replaceImport('@storybook/addon-options', 'storybook-addon-options')8replaceImport('@storybook/addon-storyshots', 'storybook-addon-storyshots')9replaceImport('@storybook/addon-storysource', 'storybook-addon-storysource')10replaceImport('@storybook/addon-viewport', 'storybook-addon-viewport')11replaceImport('@storybook/addon-backgrounds', 'storybook-addon-backgrounds')12replaceImport('@storybook/addon-centered', 'storybook-addon-centered')13replaceImport('@storybook/addon-info', 'storybook-addon-info')14replaceImport('@storybook/addon-jest', 'storybook-addon-jest')15replaceImport('@storybook/addon-options', 'storybook-addon-options')16replaceImport('@storybook/addon-storyshots', 'storybook-addon-storyshots')17replaceImport('@storybook/addon-storysource', 'storybook-addon
Using AI Code Generation
1import { replaceImport } from 'storybook-root-import';2replaceImport();3import { configure } from '@storybook/react';4configure(() => {5 require('./stories');6}, module);7import { replaceImport } from 'storybook-root-import';8replaceImport();9import { configure } from '@storybook/react';10configure(() => {11 require('stories');12}, module);13import { configure } from '@storybook/react';14configure(() => {15 require('../stories');16}, module);17import { configure } from '@storybook/react';18configure(() => {19 require('../stories');20}, module);21const path = require('path');22module.exports = (baseConfig, env, defaultConfig) => {23 defaultConfig.resolve.alias = {24 '@': path.resolve(__dirname, '../src'),25 };26 return defaultConfig;27};28{29 {30 "alias": {31 }32 }33 "storybook-root-import",34 {35 }36}37{38 "scripts": {
Using AI Code Generation
1const { replaceImport } = require('storybook-root-import');2replaceImport();3import { Button } from 'components/Button';4import { configure } from '@storybook/react';5import 'test.js';6configure(require.context('../src', true, /\.stories\.js$/), module);7const path = require('path');8const rootImport = require('storybook-root-import');9module.exports = (baseConfig, env, config) => {10 config.module.rules.push({11 loaders: [require.resolve('@storybook/addon-storysource/loader')],12 });13 config.resolve.alias = {14 };15 return config;16};17import '@storybook/addon-actions/register';18import '@storybook/addon-links/register';19import '@storybook/addon-storysource/register';20const rootImport = require('storybook-root-import');21module.exports = {22 resolve: {23 alias: {24 },25 },26};27{28 "name": "storybook-root-import",29 "description": "Root import for storybook",30 "scripts": {31 },32 "dependencies": {33 "babel-plugin-root-import": "^6.4.1"34 }35}36{37 "plugins": [["root-import", {38 }]]39}40const path = require('path');41const rootImport = require('storybook-root-import');42module.exports = (baseConfig, env, config) => {43 config.module.rules.push({44 loaders: [require.resolve('@storybook/addon-storysource/loader')],45 });46 config.resolve.alias = {
Using AI Code Generation
1const replaceImport = require('storybook-root-import/replaceImport');2replaceImport();3module.exports = {4};5const path = require('path');6const rootImport = require('storybook-root-import');7module.exports = (baseConfig, env, config) => {8 config.resolve.alias = {9 ...rootImport({10 }),11 };12 return config;13};14{15 "compilerOptions": {16 "paths": {17 }18 }19}20{21 "scripts": {22 },23 "devDependencies": {24 "storybook-root-import": "^0.1.2"25 }26}
Using AI Code Generation
1import { replaceImport } from 'storybook-root-import';2replaceImport();3import { storiesOf } from '@storybook/react';4import Button from 'components/Button';5storiesOf('Button', module)6 .add('default', () => <Button>Default</Button>)7 .add('primary', () => <Button primary>Primary</Button>)8 .add('secondary', () => <Button secondary>Secondary</Button>);9{10 "name": "storybook-root-import",11 "description": "storybook-root-import",12 "scripts": {13 },14 "devDependencies": {15 "babel-plugin-root-import": "^6.4.1",16 "storybook-root-import": "^1.0.0"17 },18 "dependencies": {19 },20 "babel": {21 "babel-plugin-root-import",22 {23 }24 }25}
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!!