I have the following error when I try to start my react-styleguidist documentation:
./node_modules/react-native-gesture-handler/touchables/TouchableHighlight.js 10:22
Module parse failed: Unexpected token (10:22)
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
| */
| export default class TouchableHighlight extends Component {
> static defaultProps = {
| ...GenericTouchable.defaultProps,
| activeOpacity: 0.85,
# ./node_modules/react-native-gesture-handler/touchables/index.js 6:0-69 6:0-69
# ./node_modules/react-native-gesture-handler/index.js
My webpack.config.js look like this:
const createExpoWebpackConfigAsync = require('#expo/webpack-config');
module.exports = async function (env, argv) {
const config = await createExpoWebpackConfigAsync(env, argv);
return config;
};
My babel.config.js look like this:
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
env: {
development: {
plugins: [
'#babel/plugin-transform-react-jsx-source',
],
},
},
};
};
Reproduction:
git clone https://github.com/kopax/expo-bug-reports.git
cd expo-bug-reports
git checkout react-styleguidist
npm i
npm run styleguide
What is the appropriate Webpack loader to use the static keyword?
If you use webpack for build your project, then you need to add the babel-loader to webpack.
module: {
rules: [{
test: /\.(j|t)sx?$/,
exclude: [/node_modules/],
use: [{
loader: "babel-loader",
options: {
presets: ['babel-preset-expo'],
env: {
development: {
plugins: [
'#babel/plugin-transform-react-jsx-source',
],
},
},
}
}]
},
}
Related
I have noticed that Storybook can't process Typescript files if they are from another project (monorepo), but it is doing okay with TS files that are within its project.
How do I configure Storybook to handle TS files outside the project?
Structure of the monorepo:
package1
.storybook
main.ts
preview.ts
component.tsx (this component imports file.ts)
component.stories.tsx
package2
file.ts <- (Storybook can't process this file: ModuleParseError: Module parse failed: Unexpected token)
Here is the main.ts config file:
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const path = require('path');
const toPath = (filePath) => path.join(process.cwd(), filePath);
module.exports = {
"stories": [
"../src/**/*.stories.#(mdx|js|jsx|ts|tsx)"
],
"addons": [
"#storybook/addon-links",
"#storybook/addon-essentials",
"#storybook/addon-interactions",
"#storybook/addon-knobs/register",
"#storybook/preset-create-react-app",
"#storybook/addon-a11y",
'storybook-addon-styled-component-theme/dist/preset',
'storybook-addon-themes',
"storybook-dark-mode",
],
"framework": "#storybook/react",
"core": {
"builder": "#storybook/builder-webpack5",
"disableTelemetry": true,
},
features: {
emotionAlias: false,
},
typescript: { reactDocgen: false },
webpackFinal: async (config, { configType }) => {
return {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
'#emotion/core': toPath('node_modules/#emotion/react'),
'emotion-theming': toPath('node_modules/#emotion/react'),
},
plugins: [new TsconfigPathsPlugin()]
},
}
}
}
Here is the error:
ModuleParseError: Module parse failed: Unexpected token (7:11)
File was processed with these loaders:
* ../../node_modules/#pmmmwh/react-refresh-webpack-plugin/loader/index.js
You may need an additional loader to handle the result of these loaders.
| import { Palette as palette } from '../Palette';
|
> const Paper: any = [
| {
If I import the same TS file, but manually precompiled into regular JS - Storybook works.
I have no idea what to try to solve this :(
Add this to .storybook/main.js
webpackFinal: async (config, { configType }) => {
// `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.
// Storybook uses its own webpack config, so we need to merge our config with it
// See https://storybook.js.org/docs/configurations/custom-webpack-config/
// Add typescript loader to process TS-files from other packages
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve("ts-loader"),
options: {
reportFiles: [
"../**/src/**/*.{ts,tsx}"
]
}
},
]
});
config.resolve.extensions.push(".ts", ".tsx");
return config;
}
in documentation nx suggest to use merge option to rewrite the default webpack config
https://nx.dev/guides/customize-webpack
so, i put my custom webpack.config.js file in a project.json file
webpack.config.js file:
const { merge } = require('webpack-merge');
module.exports = (config, context) => {
return merge(config, {
module: {
rules: [
{
test: /\.sass$/i,
use: [
'style-loader',
'css-loader',
'sass-loader'
],
},
],
}
});
};
all i am trying to do is just make to read my .sass files as .module.sass
but as result i have many errors
I try to make aliases for the storybook, but I have looked on the site to find a solution. However, my problem persists, and it doesn't work. I already have some aliases in my original webpack.config.js, but I don't understand how it cannot be cached. So, when I do yarn storybook, I have an error.
Below is my JS file from the .storybook folder; I used the documentation.
main.js
const path = require('path');
const custom = require('../webpack.config.js');
module.exports = {
webpackFinal: async (config) => {
return {
...config,
module: {
...config.module,
rules: [
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
include: path.resolve(__dirname, '../'),
}
],
},
resolve: { ...config.resolve, alias: custom.resolve.alias}
};
},
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.#(js|jsx|ts|tsx)",
"../src/**/**/*.stories.#(js|jsx|ts|tsx)",
],
"addons": [
"#storybook/addon-links",
"#storybook/addon-essentials"
],
"core": {
"builder": "webpack5"
}
}
Error
ModuleParseError: Module parse failed: Unexpected token (10:27) 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 | }; |
const Template = (args) => <Header {...args} />; | | export const Primary = Template.bind({});
at handleParseError (/path/project/node_modules/#storybook/builder-webpack5/node_modules/webpack/lib/NormalModule.js:933:19)
at /path/project/node_modules/#storybook/builder-webpack5/node_modules/webpack/lib/NormalModule.js:1035:5
at processResult (/path/project/node_modules/#storybook/builder-webpack5/node_modules/webpack/lib/NormalModule.js:755:11)
at /path/project/node_modules/#storybook/builder-webpack5/node_modules/webpack/lib/NormalModule.js:819:5
at /path/project/node_modules/loader-runner/lib/LoaderRunner.js:406:3
at iterateNormalLoaders (/mnt/d/Sites/chillwatch/node_modules/loader-runner/lib/LoaderRunner.js:232:10)
at Array. (/path/project/node_modules/loader-runner/lib/LoaderRunner.js:223:4)
at runCallbacks (/path/project/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:27:15)
at /path/project/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:200:4
at /path/project/node_modules/graceful-fs/graceful-fs.js:123:16
at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:75:3)
What am I doing wrong?
GitHub Issue
As mentioned on the above link you can make aliases with the storybook as following:
const path = require("path")
module.exports = {
"webpackFinal": async (config) => {
config.resolve.alias['#'] = path.resolve(__dirname, '../src')
config.resolve.alias['#components'] = path.resolve(__dirname, '../src/components')
return config
}
}
Trying to setup a react-app with all latest versions.
Github Repo Link
Trying to run storybook with sass file imported will result in below error. Trying to run without importing the styles, storybook works.
The same code works correctly when its run as npm start run with no warnings and errors.
I have configured css modules using #dr.pogodin/babel-plugin-react-css-modules with sass, webpack 5, react 17 and with latest packages.
ERROR in ./src/assets/stylesheets/app.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
> #import "./base.scss";
| #import "./generics/font.scss";
| #import "./generics/spacing.scss";
# ./stories/index.js 5:0-44 8:2-10:4 8:58-10:3 9:4-49
# ./src/components/atoms/button/stories.js
babel.config.js
module.exports = {
presets: ["#babel/preset-env", "#babel/preset-react"],
plugins: [
[
"#dr.pogodin/babel-plugin-react-css-modules",
{
webpackHotModuleReloading: true,
autoResolveMultipleImports: true,
filetypes: {
".scss": {
syntax: "postcss-scss",
},
},
generateScopedName: "[name]__[local]___[hash:base64:5]",
},
],
],
};
webpack.config.js for css (partial code inlcuded)
{
test: /\.(css|sass|scss)$/,
exclude: /node_modules/,
use: [
isDev ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: {
auto: (resourcePath) =>
resourcePath.indexOf("assets/stylesheets") === -1,
localIdentName:"[name]__[local]___[hash:base64:5]",
},
sourceMap: true,
},
},
"sass-loader",
],
}
storybook/webpack.config.js file
const custom = require('../webpack.config.js');
module.exports = {
// stories: ['../src/components/**/*.stories.js'],
webpackFinal: (config) => {
return {
...config,
module: {
rules: custom.module.rules,
},
resolve: {
...config.resolve,
...custom.resolve,
}
};
},
};
I don't know what you have done with your configuration but you would define the config things inside .storybook/main.js. And for global style css is supposed to be included in preview.js file.
In short, you have to do the few things:
Remove your .storybook/config.js and add .storybook/main.js with following content:
const custom = require('../webpack.config.js');
module.exports = {
stories: [
'../src/**/stories.js', // The name should have a prefix for component name like `button.stories.js` instead of `stories.js` like you've done. As you renamed, you can remove this pattern
"../src/**/*.stories.#(js|jsx|ts|tsx)"
],
webpackFinal: (config) => {
return {
...config,
module: {
rules: custom.module.rules,
},
resolve: {
...config.resolve,
...custom.resolve,
}
};
},
};
Create the .storybook/preview.js to import your global style:
import "../src/assets/stylesheets/app.scss";
Some people have been running into problems a some scss preset when using Storybook 6.2.0 with Webpack 5. Instead of using a preset, I recommend configuring the Webpack config in main.js as mentioned above. Here's the relevant portion of a working Storybook Webpack config for Sass:
module: {
...config.module,
rules: [
...config.module.rules,
{
test: /\.(scss)$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: function () {
return [require('precss'), require('autoprefixer')];
},
},
},
},
{
loader: require.resolve('sass-loader'),
options: {
implementation: require('sass'),
},
},
],
},
],
},
I've written more about getting Storybook off the ground with Webpack 5 (and modifying the Storybook Webpack config) over here.
Another reason this might happen: if you are adding new components to your app and the path defined for your sass-loader does not match anymore.
E.g. if you have this in your .storybook/main.js:
webpackFinal: async config => {
// Add SASS support
// https://storybook.js.org/docs/configurations/custom-webpack-config/#examples
config.module.rules.push({
test: /\.scss$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: {
compileType: "icss",
},
},
},
"sass-loader",
],
include: path.resolve(__dirname, "../"),
})
Update or completely remove the include path.
I am not able to see the SVG icons in Storybook. I am new to front-end development so not sure what I am doing wrong. I referred to this article https://medium.com/#derek_19900/config-storybook-4-to-use-svgr-webpack-plugin-22cb1152f004. Here is file webpack.config.js
const path = require('path');
const rootDir = path.resolve(__dirname, '../src');
const pathToInlineSvg = path.resolve(__dirname, '../src/assets/images/icons');
module.exports = ({ config, mode }) => {
const fileLoaderRule = config.module.rules.find(rule => rule.test.test('.svg'));
fileLoaderRule.exclude = pathToInlineSvg;
config.module.rules = config.module.rules.map(rule => {
if (rule.exclude && rule.test.test('styles.scss')) {
rule.use = rule.use.map(use => {
if (use && use.loader && use.loader.indexOf('sass-loader') !== -1) {
use.options.data = `
#import "#/assets/styles/theme.scss";
#import "#/assets/styles/fonts.scss";
`;
}
return use;
});
}
return rule;
});
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [['react-app', { flow: false, typescript: true }]],
},
});
config.module.rules.push({
test: /\.(jsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: ["#babel/preset-env", "#salesforce/babel-preset-design-system-react"],
},
});
config.module.rules.push({
test: /\.svg$/,
include: pathToInlineSvg,
use: [{loader: '#svgr/webpack',
options: {
icon: true,
},
}
],
});
config.resolve.extensions.push('.ts', '.tsx', '.svg');
config.resolve.alias['#'] = rootDir;
return config;
};
SVGs are not loading into Storybook in your setup because Storybookâs default webpack config already has a .svg loader, and is using file-loader.
See Line 65 - Line 68 of Storybook base-webpack.config.js
If you want to use #svgr/webpack loader, you need to write a custom webpack config so that the #svgr/webpack loader rule is before the file-loader.
This webpack config rule adds the #svgr/webpack loader before any other asset loaders:
// Add SVGR Loader
// ========================================================
// Remove svg rules from existing webpack rule
const assetRule = config.module.rules.find(({ test }) => test.test('.svg'));
const assetLoader = {
loader: assetRule.loader,
options: assetRule.options || assetRule.query,
};
config.module.rules.unshift({
test: /\.svg$/,
use: ['#svgr/webpack', assetLoader],
});
You can see a complete webpack.config.js in this Gatsby starter (Gatsby + TypeScript + Emotion + Storybook)
Use storybook-preset-inline-svg.
Storybook has loaders configured that conflict with what you define in your custom Webpack configuration, so if you do not override those then your custom rules will not work.
Here's a basic way to override some of the default SVG loaders (adjust to your needs):
config.module.rules = [
...config.module.rules.map(rule => {
if (/svg/.test(rule.test)) {
// Silence the Storybook loaders for SVG files
return { ...rule, exclude: /\.svg$/i }
}
return rule
}),
// Add your custom SVG loader
{
test: /\.svg$/i,
use: ['whatever-loader-you-want']
}
]
Depending on your setup you may want to look into the Storybook preset API for Webpack to override loaders for the manager entries too.
In the newest storybook here's the solution:
Set your script on package.json to
"scripts": {
"storybook": "start-storybook -p 6006 --no-dll -s ./public",
}