diff --git a/code/core/package.json b/code/core/package.json index db2394d2401b..ec068e77596d 100644 --- a/code/core/package.json +++ b/code/core/package.json @@ -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", @@ -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", diff --git a/code/core/src/common/presets.ts b/code/core/src/common/presets.ts index b85b35d62b77..508bd9a8fd85 100644 --- a/code/core/src/common/presets.ts +++ b/code/core/src/common/presets.ts @@ -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, @@ -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'; @@ -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( diff --git a/code/core/src/common/utils/validate-config.ts b/code/core/src/common/utils/validate-config.ts index 221ea9235522..ea70d1f844fa 100644 --- a/code/core/src/common/utils/validate-config.ts +++ b/code/core/src/common/utils/validate-config.ts @@ -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 }); - } } diff --git a/code/yarn.lock b/code/yarn.lock index a36028c2bb19..d12b502295cd 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -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" @@ -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: @@ -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"