-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
CLI: Improve init for svelte #14161
CLI: Improve init for svelte #14161
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,47 @@ | ||
import fse from 'fs-extra'; | ||
import { logger } from '@storybook/node-logger'; | ||
|
||
import { baseGenerator, Generator } from '../baseGenerator'; | ||
|
||
const generator: Generator = async (packageManager, npmOptions, options) => { | ||
baseGenerator(packageManager, npmOptions, options, 'svelte', { | ||
await baseGenerator(packageManager, npmOptions, options, 'svelte', { | ||
extraPackages: ['svelte', 'svelte-loader'], | ||
extraAddons: ['@storybook/addon-svelte-csf'], | ||
}); | ||
|
||
let conf = fse.readFileSync('./.storybook/main.js').toString(); | ||
|
||
// add *.stories.svelte | ||
conf = conf.replace(/js\|jsx/g, 'js|jsx|svelte'); | ||
|
||
let requirePreprocessor; | ||
let preprocessStatement = 'undefined'; | ||
|
||
// svelte.config.js ? | ||
if (fse.existsSync('./svelte.config.js')) { | ||
logger.info("Configuring preprocessor from 'svelte.config.js'"); | ||
|
||
requirePreprocessor = `const preprocess = require("../svelte.config.js").preprocess;`; | ||
preprocessStatement = 'preprocess'; | ||
} else { | ||
// svelte-preprocess dependencies ? | ||
const packageJson = packageManager.retrievePackageJson(); | ||
if (packageJson.devDependencies && packageJson.devDependencies['svelte-preprocess']) { | ||
logger.info("Configuring preprocessor with 'svelte-preprocess'"); | ||
|
||
requirePreprocessor = 'const sveltePreprocess = require("svelte-preprocess");'; | ||
preprocessStatement = 'sveltePreprocess()'; | ||
} | ||
} | ||
|
||
const svelteOptions = ` "svelteOptions": {\n preprocess: ${preprocessStatement},\n },`; | ||
|
||
if (requirePreprocessor) { | ||
conf = `${requirePreprocessor}\n\n${conf}`; | ||
} | ||
|
||
conf = conf.replace(/\],/, `],\n${svelteOptions}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also not very robust. Same as the in the configure method of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's why you're the MVP @tooppaaa ❤️ Thanks so much for the thoughtful review! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, the main.js is generating from static properties (with JSON.stringify). However, in my case, I want to inject :
const sveltePreprocess = require('svelte-preprocess');
module.exports = {
...
svelteOptions: {
preprocessor: sveltePreprocess()
},
..
} That's why, for me at least, it wasn't a "small refactor" but I have to rewrite most of the logic inside configure.js 😅 I can work on that, but how do you see the implementation of |
||
fse.writeFileSync('./.storybook/main.js', conf); | ||
}; | ||
|
||
export default generator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means we can break Svelte CLI while touching
lib\cli\src\generators\configure.ts
Do you think it's best to pass an option "extension" that defaults to js, jsx, ts, txs ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My first implementation was checking if "framework is svelte" (there is already a special case for angular in this file), my 2nd implementation was doing that (a special properties for extensions), and my third is this PR :)