How I run storybook with tsx and sass files - reactjs

I'm using TSDX to create a library with storybook.
My problem is when I run storybook with:
npm run start-storybook -p 6006
show me next errors:
ERROR in ./src/sidenav/functions/index.tsx Module not found: Error: Can't resolve '../../../constants/global-constants'
ERROR in ./src/alarmbar/styles/alarmbar.scss 1:0
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
It seems that cant load tsx and sass files.
In scss files I'm using this to use dark and light mode on each component.
$color-text-black-base: #343434;
$color-text-white-base: #ffffff;
#function themed($key) {
#return map-get($theme-map, $key);
}
$a-tags: "a, a:active, a:hover, a:visited";
$a-tags-hover: "a:active, a:hover";
/*
* Implementation of themes
*/
#mixin themify($themes) {
#each $theme, $map in $themes {
.#{$theme} & {
$theme-map: () !global;
#each $key, $submap in $map {
$value: map-get(map-get($themes, $theme), "#{$key}");
$theme-map: map-merge(
$theme-map,
(
$key: $value,
)
) !global;
}
#content;
$theme-map: null !global;
}
}
}
$themes: (
light: (
textColorBase: $color-text-black-base,
),
dark: (
textColorBase: $color-text-white-base,
),
);
So, with that each component change if theme is dark or light:
#import "./styles-utils.scss";
.breadcrumb-item {
font-weight: 500;
#include themify($themes) {
color: themed("textColorBase");
}
margin-right: 5px;
user-select: none;
.normal {
#include themify($themes) {
color: themed("textColorBase");
}
text-decoration: none;
}
.normal:active,
.normal:visited,
.normal:hover {
#include themify($themes) {
color: themed("textColorBase");
}
}
}
.breadcrumb-item--active {
font-weight: 700;
#include themify($themes) {
color: themed("textColorBase");
}
}
Any idea how to fix it?
SOLUTION:
Update "#storybook/react": "^6.1.11"
Add "#storybook/preset-create-react-app": "^3.1.5"
Add this on main.js inside .storybook:
const path = require("path");
module.exports = {
stories: ["../src/**/*.stories.#(ts|tsx|js|jsx|mdx)"],
addons: [
"#storybook/addon-links",
{
name: "#storybook/addon-docs",
options: {
configureJSX: true,
},
},
"#storybook/addon-essentials",
"#storybook/preset-create-react-app",
"#storybook/addon-knobs",
"#storybook/addon-actions",
],
// https://storybook.js.org/docs/react/configure/typescript#mainjs-configuration
typescript: {
check: true, // type-check stories during Storybook build
},
module: {
rules: [
{
test: /\.(scss|css)$/,
use: ["style-loader", "css-loader", "sass-loader"],
include: path.resolve(__dirname, "../"),
},
// { test: /\.css$/, loader: 'style-loader!css-loader', include: __dirname },
{
test: /\.(woff|woff2)$/,
use: {
loader: "url-loader",
options: {
name: "fonts/[hash].[ext]",
limit: 5000,
mimetype: "application/font-woff",
},
},
},
{
test: /\.(ttf|eot|svg|png)$/,
use: {
loader: "file-loader",
options: {
name: "fonts/[hash].[ext]",
},
},
},
],
},
};

Related

Problems with fonts and images when I use my library in a project

I am creating React UI library with #emotion. I have problems with fonts and images when I use my library in a project.
My lib (emotion-lib)
enter image description here
Project when I use my library
enter image description here
Web-pack modules (emotion-lib):
module: {
rules: [
{
test: /\.(ttf|otf)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: false,
name: '[name].[ext]',
outputPath: 'fonts',
},
},
],
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(ts|tsx)?$/,
use: ['babel-loader', 'ts-loader'],
exclude: /node_modules/,
},
{
test: /\.svg$/,
loader: require.resolve('#svgr/webpack'),
options: {
svgoConfig: {
plugins: [
{
name: 'removeViewBox',
active: false,
},
],
},
},
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images',
},
},
],
},
],
},
global.ts (emotion-lib)
import { css } from '#emotion/react';
import SFPro from '../../../assets/fonts/SF-Pro/SF-Pro.ttf';
export const global = css`
/* SF Pro */
#font-face {
font-family: 'SF Pro';
src: url('${SFPro}');
}
html,
body {
margin: 0;
padding: 0;
font-family: 'SF Pro', sans-serif;
font-weight: 500;
font-size: 16px;
line-height: 28px;
}
a {
text-decoration: none;
color: inherit;
}
* {
box-sizing: border-box;
font-family: 'SF Pro Text', sans-serif;
}
`;
I tried to import fonts and images in my project, but there was a mistake ('not found').

tailwind css with css modules in next.js

How to config next.js to support Simultaneous tailwind css with css modules?
I want tailwindcss wrap whole project:
// /tailwind.scss
:global {
#import "tailwindcss/base";
#import "tailwindcss/components";
#import "tailwindcss/utilities";
}
// /test-module.css
.example {
font-size: 36px;
}
// /pages/_app.jsx
import '../talwind.scss';
...
And in a sample component:
// /components/my-component.jsx
import css from '../test-module.css';
const Test = () => (
<div className={`bg-red-500` ${css.example}}>Test Tailwind with CSS</div>
);
A solution is split webpack style loader. A loader for global css another for css modules loader so webpack loader is looks like below:
{
test: /\.s?[ac]ss$/,
exclude: /\.global.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !isProduction,
reloadAll: true,
},
},
// 'css-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: {
localIdentName: '[name]__[local]___[hash:base64:5]',
},
},
},
'postcss-loader',
{ loader: 'sass-loader', options: { sourceMap: true } },
],
},
{
test: /\.global.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !isProduction,
reloadAll: true,
},
},
'css-loader',
'postcss-loader',
],
},
I had the same problem, I did the following and it worked:
const Test = () => (
<div className={["bg-red-500", css.example].join(" ")}>
Test Tailwind with CSS
</div>
);
You need to write classname like this
const App = () => {
<h1 className={[style[`bg-teal-600`], style.holi].join(' ')}>Holi</h1>;
};

Config antd with react and webpack

Here is the error I receive:
Uncaught Error: Module parse failed: Unexpected token (15:5)
You may need an appropriate loader to handle this file type.
| /* stylelint-disable at-rule-no-unknown */
| html,
> body {
| width: 100%;
| height: 100%;
at eval (antd.css:1)
at Object../node_modules/antd/dist/antd.css (index.js:228)
at __webpack_require__ (index.js:20)
at eval (App.js:10)
at Module../KQShopping/frontend/src/App.js (index.js:97)
at __webpack_require__ (index.js:20)
at eval (index.js:2)
at Module../KQShopping/frontend/src/index.js (index.js:109)
at __webpack_require__ (index.js:20)
at index.js:84
webpack config file:
const path = require('path');
const fs = require('fs');
const lessToJs = require('less-vars-to-js');
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './ant-theme-vars.less'), 'utf8'));
module.exports = {
module: {
rules: [
{
loader: 'babel-loader',
test: /\.(js|jsx)$/,
exclude: /node_modules/,
options: {
plugins: [
['import', { libraryName: "antd", style: true }]
]
},
},
{
test: /\.less$/,
use: [
{loader: "style-loader"},
{loader: "css-loader"},
{loader: "less-loader",
options: {
modifyVars: themeVariables
}
}
]
}
]
}
};
App.js:
import React from "react";
import ReactDOM from "react-dom";
import { DatePicker, message } from "antd";
import "antd/dist/antd.css";
class App extends React.Component {
state = {
date: null,
};
handleChange = date => {
message.info(`Selected Date: ${date ? date.format("YYYY-MM-DD") : "None"}`);
this.setState({ date });
};
render() {
const { date } = this.state;
return (
<div style={{ width: 400, margin: "100px auto" }}>
<DatePicker onChange={this.handleChange} />
<div style={{ marginTop: 20 }}>
Selected Date: {date ? date.format("YYYY-MM-DD") : "None"}
</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("app"));
Babel config file:
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": ["transform-class-properties", ["import", { "libraryName": "antd", "style": "true" }]]
}
I followed multi tutorials and how-tos but I end up with an error every time and I have no idea how to fix this error since I have just started learning about babel and webpack with small experince.
In this problem I followed exactly this docs https://ant.design/docs/react/getting-started and I still end up with an error
In the end, I used CSS, in the webpack config file add this:
{
use: ['style-loader', 'css-loader'],
test: /\.css$/
}
You shouldn't need to import the antd css at the top of App.js. I also think that the babel plugin should have style set to css in the babel config file (that's how our config file is set up anyway!).
[
'import',
{
'libraryName': 'antd',
'style': 'css'
}
]
Our less loader also has javascriptEnabled set to true:
test: /\.less$/,
use: [{
loader: 'style-loader' // creates style nodes from JS strings
},
{
loader: 'css-loader' // translates CSS into CommonJ
},
{
loader: 'less-loader', // compiles Less to CSS
options: {
javascriptEnabled: true
}
}]

How do you remove unused classes with Webpack 4 tree shaking with CSS modules?

Someone made a great example to remove unused CSS based on whether or not the JS Module is used, but I am trying to figure out how to remove the unused CSS classes from the bundle that are not actually used by components.
Example
// Sub.scss
.sub-container {
background-color: green;
}
.unused-junk {
color: blue;
}
// Sub.js
import React from "react";
import styles from "./Sub.scss";
export default function Sub() {
return <div className={styles.subContainer}>Hi from sub.</div>;
}
// App.scss
.app-container {
background-color: red;
}
// App.js
import React from "react";
import ReactDOM from "react-dom";
import Sub from "./Sub";
import styles from "./App.scss";
function App() {
return (
<div className={styles.appContainer}>
Hi from app.
<Sub />
</div>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
// webpack.config.js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
module.exports = {
entry: "./src/App.js",
output: {
path: __dirname + "/build",
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.js/,
loader: "babel-loader",
include: __dirname + "/src",
query: {
presets: ["react"]
}
},
{
test: /\.scss/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: true,
camelCase: true,
importLoaders: 1,
localIdentName: "[name]--[local]--[hash:base64:5]"
}
},
"sass-loader"
],
include: __dirname + "/src"
}
]
},
plugins: [
new CopyWebpackPlugin([{ from: `src/index.html`, to: "index.html" }]),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
]
};
// CSS bundle
.App--app-container--3wd6W {
background-color: red; }
.Sub--sub-container--38uqh {
background-color: green; }
.Sub--unused-junk--2-h5r {
color: blue; }
Is there a way to tree shake the .unused-junk class from the bundle?

SCSS file cannot import node_module

Tried #import "~react-icons" in an SCSS file.
I get an error: Module build failed:
#import "~react-icons";
^
File to import not found or unreadable: ~react-icons.
This was an npm module that I installed and is on package-json.
My scss code looks like this, the error is at the first line:
#import "~react-icons";
input {
width: 100%;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
padding: 12px 20px 12px 20px;
margin-bottom: 20px;
}
My webpack configuration looks like this:
const webpack = require('webpack');
const path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
let BUILD_DIR = path.resolve(__dirname, 'dist');
let APP_DIR = path.resolve(__dirname, 'src');
let config = {
entry: path.join(APP_DIR, '/index.js'),
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.jsx?/,
include: APP_DIR,
loaders: ['babel-loader']
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ['css-loader', 'sass-loader'],
publicPath: "/dist"
})
},
{
test: /\.(ttf|eot|svg|gif|woff(2)?)(\?[a-z0-9]+)?(\?v=
[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader',
}
]
},
resolve: {
extensions: ['.js', '.jsx']
},
devServer: {
contentBase: path.join(__dirname),
// serve index.html in place of 404 responses to allow HTML5
// history
historyApiFallback: true
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
title: 'Project',
minify: {
collapseWhitespace: true
},
hash: true,
template: './index.html'
}),
new ExtractTextPlugin({
filename: 'style.css',
disable: false,
allChunks: true
})
]
};
module.exports = config;
There is not an scss file to import for react-icons. You have to import the icons in the file you want to use:
import FaSearch from 'react-icons/lib/fa/search';
class Search extends React.Component {
render() {
return <FaSearch />
}
}

Resources