From 238cbb34de8ab1c596648c33ac158672d05eb0a9 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sat, 28 Dec 2024 18:56:30 +0800 Subject: [PATCH 1/2] Load presets using jiti --- code/core/package.json | 2 +- code/core/src/common/presets.ts | 16 +++++++++------- code/core/src/common/utils/validate-config.ts | 7 ------- code/yarn.lock | 13 +++++++++++-- 4 files changed, 21 insertions(+), 17 deletions(-) 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..0fbb408815de 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; + 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); + return await jiti.import(src, { default: true }) } 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 13e798756fd5..bf3c286fc6a0 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -6258,7 +6258,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" @@ -18808,7 +18808,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: @@ -18817,6 +18817,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" From e3fddc20dd6364d57b90978fcb0a137848a36e9f Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sat, 28 Dec 2024 21:41:47 +0800 Subject: [PATCH 2/2] improve import --- code/core/src/common/presets.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/code/core/src/common/presets.ts b/code/core/src/common/presets.ts index 0fbb408815de..508bd9a8fd85 100644 --- a/code/core/src/common/presets.ts +++ b/code/core/src/common/presets.ts @@ -218,12 +218,12 @@ const map = async function getContent(input: PresetConfig) { if (input.type === 'virtual') { - const { _type, _name, ...rest } = input; + const { type, name, ...rest } = input; return rest; } const src = input.name ? input.name : input; - const jiti = createJiti(import.meta.url); - return await jiti.import(src, { default: true }) + const jiti = createJiti(import.meta.url || pathToFileURL(__filename).href); + return await jiti.import(src); } export async function loadPreset(