so I'm having a problem with webpack codesplitting. It creates bundles correctly but then everything is getting messed up when it sends xhr request for the bundle. Only the index route works correctly 'cause it's just a / but it actually uses client side route as path to bundle. So using the example below it goes with the path localhost:3000/dir/something/0.bundle.js instead of localhost:3000/0.bundle.js. I'm new to webpack, is there a way to make it use the / route every time?
pseudo markup
<Link to="/dir/something/else"/>
<!-- <App/> -->
<BrowserRouter>
<Switch>
<Route path="/dir/:param1/:param2">
<Bar/>
</Route>
<Route path="/">
<Foo/>
</Route>
</Switch>
</BrowserRouter>
I'm using #loadable/component so each component is imported with a loadable(()=>import('path/to/component'))
webpack.config.js
{
mode,
entry: './src/index.tsx',
target: 'web',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.bundle.js'
},
module: {
rules: [
{
test: /\.ts(x)?$/,
use: 'ts-loader',
exclude: /node_modules/,
include: path.join(__dirname, '/src'),
},
{
test: /\.svg$/,
use: {
loader: 'url-loader',
options: {
limit: 5000
}
}
}
],
},
devServer: {
hot: true
},
plugins: [new HTMLWebpackPlugin({
template: 'src/index.html'
})],
resolve: {
modules: ['node_modules'],
extensions: ['.ts', '.tsx', '.js', '.json']
}
}
Related
I'm new to WebPack, and it's hard to debug it and I can't seem to find any right answer on google.
I'm trying to use react router, but it seems like it needs a lot of configuration to make it work exactly like create-react-app's.
React router sub-routes seems to work only when you redirect it with the Link component, example:
// App.jsx
<Route path="/profile/me/" element={<PrivateRoute component={Profile} />}>
<Route path="create" element={<PrivateRoute component={CreateProfile} />} />
</Route>;
// Sub-Component - Profile.jsx
<Link to="create">
<Button color="primary" variant="contained">
Set It Up
</Button>
</Link>;
But when I try to enter the URL in the browser search bar, I get an error:
GET
http://localhost:3000/profile/dist/bundle.js [HTTP/1.1 404 Not Found 4ms]
The resource from “http://localhost:3000/profile/dist/bundle.js” was blocked due to MIME type mismatch (X-Content-Type-Options: nosniff).
create
Here's my Webpack config:
const path = require("path");
const webpack = require("webpack");
const Dotenv = require('dotenv-webpack');
module.exports = {
entry: "./src/index.js",
mode: "development",
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules)/,
loader: "babel-loader",
options: {
presets: ["#babel/env"],
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.js$/,
include: /node_modules\/react-dom/,
use: ["react-hot-loader/webpack"],
},
],
},
resolve: {
extensions: ["*", ".js", ".jsx"],
},
output: {
path: path.resolve(__dirname, "dist/"),
publicPath: "/dist/",
publicPath: "/profile/dist/",
filename: "bundle.js",
},
devServer: {
// contentBase
static: {
directory: path.join(__dirname, "public/"),
},
port: 3000,
// publicPath
devMiddleware: {
publicPath: "https://localhost:3000/dist/",
},
// hotOnly
hot: "only",
// To use router
historyApiFallback: true,
// Proxy
/*proxy: {
"/api/v1": "http://localhost:8000",
},*/
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new Dotenv({
systemvars: true,
}),
],
};
Hello i am stuck with this one and couldnt figure it out. So i have React router v6 with basename - "/ui/" , and i want that when i open localhost:8080 that it automatically turns into "localhost:8080/ui/" because the real view is actually rendered in /ui/ route.
Router setup:
<BrowserRouter basename="/ui/">
...routes here
</BrowserRouter>
Webpack dev:
module.exports = {
mode: 'development',
devtool: 'cheap-module-source-map',
devServer: {
hot: true,
historyApiFallback: true,
open: true,
port: 6969
}
}
And common webpack:
module.exports = {
entry: path.resolve(__dirname, '..', './src/index.tsx'),
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
},
],
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader"
]
},
{
test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
type: 'asset/resource',
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
type: 'asset/inline',
},
],
},
output: {
path: path.resolve(__dirname, '..', './build'),
filename: 'bundle.js',
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, '..', './src/public/ui/index.html'),
}),
],
stats: 'errors-only',
}
I have tried to add to output publicPath: '/ui/' but it didnt work.
Or even something like this:
static: [
{
publicPath: '/',
directory: path.join(__dirname, "../public/ui/"),
}]
How i can achieve this withouth "redirecting" :\
Try this solution
<Route exact path="/">
<Redirect to="/home" />
</Route>
Check your main.js or similar in your src or sources.
Mine had:
const Root = () => (
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
{appRoute}
</Route>
</Router>
</Provider>
);
and so just had to move it to Route path="/ui/" component={App}
I am serving my static files from express and i can see that it loads index.html
But the problem is when on a rout for example localhost:8080/users and i refresh the page, the .css and main.js does not seam to be loaded so it returns blank page.. Any idea on what night be the problem?
My app.js
function App (){
return (
<div className="wrapper">
<Routes>
{routes.map(({ path, name, Component }, key) => (
<Route exact path={path} key={key} element={Component} />
))}
</Routes>
</div>
);
}
export default App
My index.js:
ReactDOM.render(
<Provider store={store}>
<React.StrictMode>
<BrowserRouter>
<Routes>
<Route path="*" element={<App />} />
</Routes>
</BrowserRouter>
</React.StrictMode>
</Provider>,
document.getElementById('root')
);
This is my webpack:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const Dotenv = require('dotenv-webpack');
const webpack = require('webpack');
require('dotenv').config({path:__dirname+'/../.env'})
module.exports = {
entry: {
app: path.join(__dirname, "/src/Index.js"),
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "main.js",
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
},
{
test: /\.css$/,
use: 'style-loader!css-loader'
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: "babel-loader",
},
{
test: /\.html$/,
exclude: /node_modules/,
use: "html-loader",
},
],
},
resolve: {
extensions: [".js", ".jsx"],
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
APP_API_URL : JSON.stringify(process.env.APP_API_URL)
},
}),
new Dotenv(),
new HtmlWebpackPlugin({
filename: "index.html",
template: "./src/index.html",
}),
new MiniCssExtractPlugin({
filename: "app.css",
chunkFilename: "[id].css",
}),
],
devServer: {
historyApiFallback: true,
contentBase: path.join(__dirname, "dist"),
compress: true,
port: process.env.PORT_CLIENT,
open: true,
stats: "errors-only",
}
};
Check out this finished example using React Router and compare it with your App.
https://stackblitz.com/edit/github-agqlf5?file=src%2Fmain.jsx
code repository url
below is my file structure
and webpack.config.js looks like something like below
const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const entryPoint = path.join(__dirname, "src", "index.js");
module.exports = {
entry: entryPoint,
output: {
path: path.join(__dirname, "public"),
filename: "bundle.js",
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
plugins: [
new HtmlWebPackPlugin({
template: path.join(__dirname, "public", "index.html"),
filename: "./index.html",
}),
],
devServer: {
contentBase: path.join(__dirname, "public"),
historyApiFallback: true,
// publicPath: "/dist/",
},
devtool: "source-map",
stats: {
errorDetails: true,
},
};
i don't know why is it still not working eventhough i have give historyApiFallback
below is my app.js
when i click on the /home route the content is not changing.
could any one please explain me what more i have to add
and my index.js
You need to add exact to your / route otherwise it will match anything starting with / and since it is in a Switch it will be the only one to be matched.
<Switch>
<Route exact path="/" component={() => <div>i am in home</div>} />
<Route exact path="/home" component={() => <div> i am user</div>} />
</Switch>
If the url is not parameterized, i would add exact to /home as well
See: https://reactrouter.com/web/api/Route/exact-bool
Hello i have a problem with react-router, my code
ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={StartPage}/>
<Route path="/matches" component={MatchesPage} />
<Route path="/sector/:idparam" component={SectorsPage} />
</Route>
</Router>
</Provider>,
app);
When I call /matches everything is OK, but when i try GET /sector/15 app failed try to load http://localhost:8080/sector/client.min.js but normally will load from default path (/)
Webpack:
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
module.exports = {
context: path.join(__dirname, "src"),
devtool: debug ? "inline-sourcemap" : null,
entry: "./js/client.js",
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-0'],
plugins: ['react-html-attrs', 'transform-decorators-legacy', 'transform-class-properties'],
}
}
]
},
output: {
path: __dirname + "/src/",
filename: "client.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({mangle: false, sourcemap: false}),
],
devServer: {
historyApiFallback: true,
contentBase: './',
hot: true
},
};
I don't think the issue is with react router, or even webpack. I think your issue is related to how you are requiring your client js file, although since you didn't include that section of code I cannot confirm.
It looks like you are including the script relative to your path
(<script src="client.min.js"></script> no leading slash) instead of an absolute path (<script src="/client.min.js"></script>).