Last active
July 9, 2022 11:33
-
-
Save ammein/d087952d13912a0f12266d9aa7063f8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ReactRefreshPlugin = require("@pmmmwh/react-refresh-webpack-plugin"); | |
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); | |
module.exports = { | |
webpack: { | |
plugins: { | |
remove: ['ReactRefreshPlugin'], | |
add: [ | |
new ReactRefreshPlugin({ | |
exclude: [/^http.*/, /node_modules/], | |
overlay: false | |
}) | |
] | |
}, | |
configure: (webpackConfig, { | |
env, | |
paths | |
}) => { | |
const shouldUseReactRefresh = process.env.FAST_REFRESH !== 'false'; | |
// Source maps are resource heavy and can cause out of memory issue for large source files. | |
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false'; | |
const isDev = env === "development"; | |
const cssLoaders = [] | |
if(isDev) { | |
cssLoaders.push( | |
{ | |
test: /\.(css|scss)$/, | |
use: ["style-loader", "css-loader", "sass-loader"], | |
} | |
) | |
} else { | |
cssLoaders.push( | |
{ | |
test: /\.css$/, | |
use: [ | |
MiniCssExtractPlugin.loader, | |
{ | |
loader: 'css-loader', | |
options: { | |
url: false, | |
sourceMap: shouldUseSourceMap | |
} | |
} | |
] | |
}, | |
{ | |
test: /\.scss$/, | |
use: [ | |
'style-loader', | |
MiniCssExtractPlugin.loader, | |
{ | |
loader: 'css-loader', | |
options: { | |
url: false, | |
sourceMap: shouldUseSourceMap | |
} | |
}, | |
{ | |
loader: 'sass-loader', | |
options: { | |
sourceMap: shouldUseSourceMap | |
} | |
} | |
] | |
} | |
) | |
} | |
webpackConfig = { | |
...webpackConfig, | |
module: { | |
rules: [ | |
{ | |
test: /\.(woff2?|jpe?g|png|gif|ico)$/, | |
use: 'file-loader?name=' + webpackConfig.output.assetModuleFilename | |
}, | |
{ | |
test: /\.svg$/, | |
use: [{ | |
loader: require.resolve('@svgr/webpack'), | |
options: { | |
prettier: false, | |
svgo: false, | |
svgoConfig: { | |
plugins: [{ | |
removeViewBox: false | |
}], | |
}, | |
titleProp: true, | |
ref: true, | |
}, | |
}, | |
{ | |
loader: require.resolve('file-loader'), | |
options: { | |
name: 'static/media/[name].[hash].[ext]', | |
}, | |
}, | |
], | |
issuer: { | |
and: [/\.(js|jsx|ts|tsx|md|mdx)$/], | |
}, | |
}, | |
{ | |
test: /\.(js|jsx)$/, | |
exclude: [/^http.*/, /node_modules/], | |
loader: require.resolve("babel-loader"), | |
options: { | |
customize: require.resolve( | |
'babel-preset-react-app/webpack-overrides' | |
), | |
plugins: [ | |
isDev && shouldUseReactRefresh && require.resolve('react-refresh/babel'), | |
].filter(Boolean), | |
presets: [ | |
[ | |
require.resolve('babel-preset-react-app'), | |
{ | |
runtime: process.env.DISABLE_NEW_JSX_TRANSFORM !== "true" ? 'automatic' : 'classic', | |
}, | |
] | |
], | |
} | |
}, | |
...cssLoaders | |
] | |
}, | |
experiments: { | |
buildHttp: { | |
allowedUris: [ | |
"https://framer.com/m/", | |
"https://framerusercontent.com/", | |
"https://fonts.gstatic.com/", | |
"https://ga.jspm.io/", | |
"https://jspm.dev/", | |
"https://cdn.skypack.dev/", | |
] | |
} | |
}, | |
} | |
return webpackConfig; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Craco Config setup
craco.config.js
in your root foldercraco.config.js
fileRequirements
Install these npm packages in your react app using npm:
Once installed, change
scripts
value inpackage.json
intocraco
as example below:Then run the scripts:
npm start
oryarn start
Errors
If you encounter this type of error:
Install this npm package to fix the above error in your app: