Setup for Storybook in NX monorepo library #495
-
Hey there! Basically what the title says, is there any guide for that? I tried following this example but when I add the For my Nx lib I followed this tutorial and I was able to successfully make twin.macro work when importing my components in other apps inside the monorepo, but not inside stories. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Heya, I recently setup emotion with // .storybook/main.js
const path = require('path');
const fs = require('fs');
function getPackageDir(filepath) {
let currDir = path.dirname(require.resolve(filepath));
while (true) {
if (fs.existsSync(path.join(currDir, 'package.json'))) {
return currDir;
}
const { dir, root } = path.parse(currDir);
if (dir === root) {
throw new Error(
`Could not find package.json in the parent directories starting from ${filepath}.`
);
}
currDir = dir;
}
}
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: ['@storybook/addon-essentials'],
babel: async (options) => {
options.presets.push(require.resolve('@emotion/babel-preset-css-prop'));
return options;
},
webpackFinal: async (config) => {
config.resolve.alias = {
'@emotion/core': getPackageDir('@emotion/react'),
'@emotion/styled': getPackageDir('@emotion/styled'),
'emotion-theming': getPackageDir('@emotion/react'),
};
return config;
},
}; Depending on your project, you may also need to install these packages or to avoid some strange console errors: Let me know if this helps at all. |
Beta Was this translation helpful? Give feedback.
-
Go it working! This is how my files ended // .storybook/main.js
const rootMain = require('../../../.storybook/main');
const path = require('path');
const fs = require('fs');
function getPackageDir(filepath) {
let currDir = path.dirname(require.resolve(filepath));
// eslint-disable-next-line no-constant-condition
while (true) {
if (fs.existsSync(path.join(currDir, 'package.json'))) {
return currDir;
}
const { dir, root } = path.parse(currDir);
if (dir === root) {
throw new Error(
`Could not find package.json in the parent directories starting from ${filepath}.`
);
}
currDir = dir;
}
}
rootMain.stories.push(
...['../src/lib/**/*.stories.mdx', '../src/lib/**/*.stories.@(js|jsx|ts|tsx)']
);
rootMain.webpackFinal = async (config) => {
config.resolve.alias = {
'@emotion/core': getPackageDir('@emotion/react'),
'@emotion/styled': getPackageDir('@emotion/styled'),
'emotion-theming': getPackageDir('@emotion/react'),
};
return config;
};
module.exports = rootMain My The thing that made it work was the Module not found: Error: Can’t resolve '@emotion/styled/base' in '...' Thanks a lot @ben-rogerson for the quick response! |
Beta Was this translation helpful? Give feedback.
Go it working!
This is how my files ended