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

Default sb init in ESM projects to use cjs #16184

Merged
merged 2 commits into from
Sep 30, 2021
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
12 changes: 10 additions & 2 deletions lib/cli/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type CommandOptions = {
yes?: boolean;
builder?: Builder;
linkable?: boolean;
commonJs?: boolean;
};

const installStorybook = (projectType: ProjectType, options: CommandOptions): Promise<void> => {
Expand All @@ -72,6 +73,7 @@ const installStorybook = (projectType: ProjectType, options: CommandOptions): Pr
language,
builder: options.builder || CoreBuilder.Webpack4,
linkable: !!options.linkable,
commonJs: options.commonJs,
};

const end = () => {
Expand Down Expand Up @@ -315,10 +317,13 @@ export function initiate(options: CommandOptions, pkg: Package): Promise<void> {
: 'Detecting project type';
const done = commandLog(infoText);

const packageJson = readPackageJson();
const isEsm = packageJson && packageJson.type === 'module';

try {
if (projectTypeProvided) {
if (installableProjectTypes.includes(options.type)) {
const storybookInstalled = isStorybookInstalled(readPackageJson(), options.force);
const storybookInstalled = isStorybookInstalled(packageJson, options.force);
projectType = storybookInstalled
? ProjectType.ALREADY_HAS_STORYBOOK
: options.type.toUpperCase();
Expand Down Expand Up @@ -346,5 +351,8 @@ export function initiate(options: CommandOptions, pkg: Package): Promise<void> {
cleanOptions.storyFormat = undefined;
}

return installStorybook(projectType, cleanOptions);
return installStorybook(projectType, {
...cleanOptions,
...(isEsm ? { commonJs: true } : undefined),
});
}
1 change: 1 addition & 0 deletions lib/cli/src/js-package-manager/PackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type PackageJson = {
peerDependencies?: Record<string, string>;
scripts?: Record<string, string>;
eslintConfig?: any;
type?: 'module';
};

export type PackageJsonWithDepsAndDevDeps = PackageJson &
Expand Down