Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Storybook Vite): Add JS project support #10900

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changesets/10900.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- feat(Storybook Vite): Add JS project support (#10900) by @arimendelow

This adds support to the SBV CLI for JS projects.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'node:path'
import type { ExecaError } from 'execa'
import execa from 'execa'

import { isTypeScriptProject, transformTSToJS } from '@redwoodjs/cli-helpers'
import { getPaths } from '@redwoodjs/project-config'
import { errorTelemetry } from '@redwoodjs/telemetry'

Expand Down Expand Up @@ -75,27 +76,43 @@ export async function handler({
execaOptions,
)

const usingTS = isTypeScriptProject()
const mainFileName = usingTS ? 'main.ts' : 'main.js'

const redwoodProjectPaths = getPaths()
const storybookConfigPath = path.dirname(
`${redwoodProjectPaths.web.storybook}/main.ts`,
`${redwoodProjectPaths.web.storybook}/${mainFileName}`,
)

const storybookMainFilePath = path.join(storybookConfigPath, 'main.ts')
const storybookMainFilePath = path.join(storybookConfigPath, mainFileName)
const storybookPreviewBodyFilePath = path.join(
storybookConfigPath,
'preview-body.html',
)

// Check if the config files exists yet. If they don't, create 'em!
// Because this path is dependent on whether the project is TS or JS, we
// check for the appropriate file extension. This means that if a user
// is using JS and switches to TS, we won't detect it and will create a new
// `main.ts` file.
if (!fs.existsSync(storybookMainFilePath)) {
console.log("Storybook's main.ts not found. Creating it now...")
const isTSProject = isTypeScriptProject()
console.log(`Storybook's ${mainFileName} not found. Creating it now...`)
const mainConfigTemplatePath = path.join(
__dirname,
'templates/main.ts.template',
'templates/main.ts.template', // The template is TS, and we'll convert it to JS if needed
)
const mainConfigContent = readFile(mainConfigTemplatePath)
writeFile(storybookMainFilePath, mainConfigContent)
console.log('main.ts created!')
const mainConfigContentTS = readFile(mainConfigTemplatePath)
if (isTSProject) {
writeFile(storybookMainFilePath, mainConfigContentTS)
} else {
const mainConfigContentJS = await transformTSToJS(
storybookMainFilePath,
mainConfigContentTS,
)
writeFile(storybookMainFilePath, mainConfigContentJS)
}
console.log(`${mainFileName} created!`)
}

if (!fs.existsSync(storybookPreviewBodyFilePath)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/storybook/src/plugins/auto-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ export const autoImports = AutoImport({
],
},
],

// We provide our mocking types elsewhere and so don't need this plugin to generate them.
dts: false,
})
Loading