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

feat(storybook): require config #2

Draft
wants to merge 1 commit into
base: feat/storybook-nodes
Choose a base branch
from
Draft
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
61 changes: 59 additions & 2 deletions packages/storybook/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import {
workspaceRoot,
writeJsonFile,
} from '@nx/devkit';
import { dirname, isAbsolute, join, relative } from 'path';
import { dirname, isAbsolute, extname, join, relative } from 'path';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readFileSync, readdirSync } from 'fs';
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
import { getLockFileName } from '@nx/js';
import { getLockFileName, getRootTsConfigPath } from '@nx/js';
import { registerTsProject } from '@nx/js/src/internal';
import { tsquery } from '@phenomnomnominal/tsquery';

export interface StorybookPluginOptions {
Expand Down Expand Up @@ -123,6 +124,13 @@ function buildStorybookTargets(

const storybookFramework = getStorybookConfig(configFilePath, context);

const storybookConfigRequire = getStorybookConfigRequire(
configFilePath,
context
);

console.log('storybookConfigRequire', storybookConfigRequire);

const frameworkIsAngular = storybookFramework === "'@storybook/angular'";

const targets: Record<string, TargetConfiguration> = {};
Expand Down Expand Up @@ -373,3 +381,52 @@ function buildProjectName(projectRoot: string, workspaceRoot: string): string {
}
return name ?? projectRoot;
}

function getStorybookConfigRequire(
configFilePath: string,
context: CreateNodesContext
): any {
const resolvedPath = join(context.workspaceRoot, configFilePath);

let module: any;
if (['.ts', '.mts', '.cts'].includes(extname(configFilePath))) {
const tsConfigPath = getRootTsConfigPath();

if (tsConfigPath) {
const unregisterTsProject = registerTsProject(tsConfigPath);
try {
module = load(resolvedPath);
} finally {
unregisterTsProject();
}
} else {
module = load(resolvedPath);
}
} else {
module = load(resolvedPath);
}
return module.default ?? module;
}

/**
* Load the module after ensuring that the require cache is cleared.
*/
const packageInstallationDirectories = ['node_modules', '.yarn'];

function load(path: string): any {
// Clear cache if the path is in the cache
if (require.cache[path]) {
for (const k of Object.keys(require.cache)) {
// We don't want to clear the require cache of installed packages.
// Clearing them can cause some issues when running Nx without the daemon
// and may cause issues for other packages that use the module state
// in some to store cached information.
if (!packageInstallationDirectories.some((dir) => k.includes(dir))) {
delete require.cache[k];
}
}
}

// Then require
return require(path);
}