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

Load presets using jiti #30151

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
"browser-assert": "^1.2.1",
"esbuild": "^0.18.0 || ^0.19.0 || ^0.20.0 || ^0.21.0 || ^0.22.0 || ^0.23.0 || ^0.24.0",
"esbuild-register": "^3.5.0",
"jiti": "^2.4.2",
"jsdoc-type-pratt-parser": "^4.0.0",
"process": "^0.11.10",
"recast": "^0.23.5",
Expand Down Expand Up @@ -364,7 +365,6 @@
"get-npm-tarball-url": "^2.0.3",
"glob": "^10.0.0",
"globby": "^14.0.1",
"jiti": "^1.21.6",
"js-yaml": "^4.1.0",
"lazy-universal-dotenv": "^4.0.0",
"leven": "^4.0.0",
Expand Down
14 changes: 8 additions & 6 deletions code/core/src/common/presets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { join, parse } from 'node:path';
import { isAbsolute, join, parse, resolve } from 'pathe'

import { pathToFileURL } from 'node:url'
import { createJiti } from 'jiti'

import type {
BuilderOptions,
Expand All @@ -17,7 +20,6 @@ import { CriticalPresetLoadError } from '@storybook/core/server-errors';

import { dedent } from 'ts-dedent';

import { interopRequireDefault } from './utils/interpret-require';
import { loadCustomPresets } from './utils/load-custom-presets';
import { safeResolve, safeResolveFrom } from './utils/safeResolve';
import { stripAbsNodeModulesPath } from './utils/strip-abs-node-modules-path';
Expand Down Expand Up @@ -214,14 +216,14 @@ const map =
};
};

async function getContent(input: any) {
async function getContent(input: PresetConfig) {
if (input.type === 'virtual') {
const { type, name, ...rest } = input;
return rest;
}
const name = input.name ? input.name : input;

return interopRequireDefault(name);
const src = input.name ? input.name : input;
const jiti = createJiti(import.meta.url || pathToFileURL(__filename).href);
return await jiti.import(src);
}

export async function loadPreset(
Expand Down
7 changes: 0 additions & 7 deletions code/core/src/common/utils/validate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,4 @@ export function validateFrameworkName(
if (Object.keys(frameworkPackages).includes(frameworkName)) {
return;
}

// If it's not a known framework, we need to validate that it's a valid package at least
try {
require.resolve(join(frameworkName, 'preset'));
} catch (err) {
throw new CouldNotEvaluateFrameworkError({ frameworkName });
}
}
13 changes: 11 additions & 2 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6064,7 +6064,7 @@ __metadata:
get-npm-tarball-url: "npm:^2.0.3"
glob: "npm:^10.0.0"
globby: "npm:^14.0.1"
jiti: "npm:^1.21.6"
jiti: "npm:^2.4.2"
js-yaml: "npm:^4.1.0"
jsdoc-type-pratt-parser: "npm:^4.0.0"
lazy-universal-dotenv: "npm:^4.0.0"
Expand Down Expand Up @@ -18546,7 +18546,7 @@ __metadata:
languageName: node
linkType: hard

"jiti@npm:^1.20.0, jiti@npm:^1.21.6":
"jiti@npm:^1.20.0":
version: 1.21.6
resolution: "jiti@npm:1.21.6"
bin:
Expand All @@ -18555,6 +18555,15 @@ __metadata:
languageName: node
linkType: hard

"jiti@npm:^2.4.2":
version: 2.4.2
resolution: "jiti@npm:2.4.2"
bin:
jiti: lib/jiti-cli.mjs
checksum: 10c0/4ceac133a08c8faff7eac84aabb917e85e8257f5ad659e843004ce76e981c457c390a220881748ac67ba1b940b9b729b30fb85cbaf6e7989f04b6002c94da331
languageName: node
linkType: hard

"jju@npm:^1.4.0":
version: 1.4.0
resolution: "jju@npm:1.4.0"
Expand Down
Loading