Use serverRequire to load config file

This commit is contained in:
Ian VanSchooten 2022-02-18 17:55:39 -05:00
parent d74511418d
commit 59ebee29f7
5 changed files with 9 additions and 19 deletions

View File

@ -1,4 +1,4 @@
import { checkAddonOrder } from '@storybook/core-common';
import { checkAddonOrder, serverRequire } from '@storybook/core-common';
import path from 'path';
export const checkDocsLoaded = (configDir: string) => {
@ -14,6 +14,6 @@ export const checkDocsLoaded = (configDir: string) => {
configFile: path.isAbsolute(configDir)
? path.join(configDir, 'main')
: path.join(process.cwd(), configDir, 'main'),
getConfig: (configFile) => import(configFile),
getConfig: (configFile) => serverRequire(configFile),
});
};

View File

@ -49,6 +49,7 @@
"@storybook/addon-viewport": "6.5.0-alpha.41",
"@storybook/addons": "6.5.0-alpha.41",
"@storybook/api": "6.5.0-alpha.41",
"@storybook/core-common": "6.5.0-alpha.41",
"@storybook/node-logger": "6.5.0-alpha.41",
"core-js": "^3.8.2",
"regenerator-runtime": "^0.13.7",

View File

@ -1,5 +1,6 @@
import path, { join } from 'path';
import { logger } from '@storybook/node-logger';
import { serverRequire } from '@storybook/core-common';
interface PresetOptions {
configDir?: string;
@ -9,25 +10,12 @@ interface PresetOptions {
}
const requireMain = (configDir: string) => {
let main = {};
const absoluteConfigDir = path.isAbsolute(configDir)
? configDir
: path.join(process.cwd(), configDir);
const mainFile = path.join(absoluteConfigDir, 'main');
try {
// eslint-disable-next-line global-require,import/no-dynamic-require
main = require(mainFile);
} catch (err) {
try {
// Try finding a .cjs version of the main file
const mainFileCjs = path.join(absoluteConfigDir, 'main.cjs');
// eslint-disable-next-line global-require,import/no-dynamic-require
main = require(mainFileCjs);
} catch (cjsErr) {
logger.warn(`Unable to find main.js or main.cjs: ${mainFile}`);
}
}
return main;
return serverRequire(mainFile) ?? {};
};
export function addons(options: PresetOptions = {}) {

View File

@ -1,4 +1,4 @@
import { checkAddonOrder } from '@storybook/core-common';
import { checkAddonOrder, serverRequire } from '@storybook/core-common';
import path from 'path';
export const checkActionsLoaded = (configDir: string) => {
@ -14,6 +14,6 @@ export const checkActionsLoaded = (configDir: string) => {
configFile: path.isAbsolute(configDir)
? path.join(configDir, 'main')
: path.join(process.cwd(), configDir, 'main'),
getConfig: (configFile) => import(configFile),
getConfig: (configFile) => serverRequire(configFile),
});
};

View File

@ -6527,6 +6527,7 @@ __metadata:
"@storybook/addon-viewport": 6.5.0-alpha.41
"@storybook/addons": 6.5.0-alpha.41
"@storybook/api": 6.5.0-alpha.41
"@storybook/core-common": 6.5.0-alpha.41
"@storybook/node-logger": 6.5.0-alpha.41
"@storybook/vue": 6.5.0-alpha.41
"@types/jest": ^26.0.16