I have looked at all the documentation of deploy a react app to GitHub pages and none of it has worked or is applicable. This is driving me up the wall, when Webpack is involved, but I am trying to master Webpack here.
So, I did not originally have a dist folder for my project.
Then I learned you have to put your files in a dist folder and push to gh-pages. Well, in my GitHub repo, I do not have that option the way the GitHub docs say I do. My only options are master and /docs.
This is a React 15 app with Redux and Webpack 2.
I am getting a blank browser here:
https://ldco2016.github.io/JSFaddle/
It failed to load resource of bundle.js and style.css and I do not know why. I have yet to have any luck deploying to GitHub pages.
I tweaked my webpack to include a dist folder, but nothing is being built into it, the folder is empty.
webpack.config.js:
const path = require('path');
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel'
}]
},
resolve: {
root: path.resolve(__dirname, 'src'),
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
I believe this is the root of my problem, but webpack is still a bit of a mystery to me because of the myriad of configurations you can do.
This continues to be a good working app in my local environment, but there is something I am not understanding in the deployment process.
To be clear, I did not previously have a dist directory when developing locally and after I created a dist directory, none of my static assets were being dumped there.
For hosting on GitHub pages you will have to server index.html of built react-app.
So your gh-pages/master should have index.html at the root and not within some folder named as dist.
Your bundle.js is not getting generated because webpack config is incorrect. You are not specifying resource type for babel-loader to process.
Please use the below config which I have corrected and tested on my local.
const path = require('path');
module.exports = {
entry: [
'./src/index.js'
],
output: {
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
}]
},
resolve: {
root: path.resolve(__dirname, 'src'),
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
Related
I'm using Xampp for my PHP developing.
Beside, I write codes in javascript in MVC architecture. When I run "dev": "webpack --mode development" in NPM, that's all ok and I see changes in bundle.js . But when I run "start": "webpack-dev-server" and I go to localhost:8080/js/bundle.js, I see changes, but in real, it is not saved. Because I go to my virtual host and there is no change!
How could I save changes exactly? Why is this happening?
This is my webpack config:
module.exports = {
entry: ["babel-polyfill","./resources/assets/js/hadi/index.js"],
output: {
path: path.join( __dirname, 'public/js'),
publicPath: '/js/',
filename: 'bundle.js'
},
devServer: {
contentBase: path.join(__dirname, 'public'),
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use:{
loader: "babel-loader"
}
}
]
}
};
The dev server of webpack is not meant to save anything, it is just a development server with live reloading, hot-modules and many other features.
When you run webpack command (like a build) it actually creates the output files, your javascript bundle and your static assets are copied to your dist folder, depending on your configuration.
The production bundle is much lighter in size so it can be deployed, and the dev server just stores a temporal bundle on memory while it is running.
I have a very simple tutorial project that I built which consists of no more than 100-200 lines of code.
When I build this project with webpack I end up with a bundle.js file which is being flagged as being above the recommended size of a bundle.js file. I find this unsettling because I know that my code is very small. How is it that with only using a few things like vuex, vue.js and a few node modules ending up with such an oversized bundle.js?
I understand that it packages everything up for us, but I find it hard to believe that with such a small project webpack would be unable to get it down to a much smaller size. I am concerned that this might have something to do with the sheer number of node modules I have in that project root directory.
So my question is this: does the webpack build depend at all on what node-modules are in my directory under the /node_modules/ folder? If not, then how have I already exceeded the recommended size for a bundle.js with my first ever vue project?
This brings me to another question which I have been very unsure of: Is it normal for vue to copy over almost my entire node_modules directory from my root user directory? When I watch tutorials, the "vue create My_App" command seems to finish executing in no more than 10-20 seconds, but when I run the command it can take minutes. When I was wondering what it could be I saw that it copied hundreds and hundreds of node_modules over... is that entirely necessary? Is there a configuration or setting I should have set or changed that I missed?
Thank you all for any insight you might be willing to offer, big or small.
// webpack.config.js
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
entry: './src/main.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/'
},
mode: 'development',
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
// this will apply to both plain `.js` files
// AND `<script>` blocks in `.vue` files
{
test: /\.js$/,
loader: 'babel-loader'
},
// this will apply to both plain `.css` files
// AND `<style>` blocks in `.vue` files
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.common.js'
}
},
plugins: [
// make sure to include the plugin for the magic
new VueLoaderPlugin(),
],
optimization: {
minimizer: [new UglifyJsPlugin()],
},
};
Use tools like https://nx.dev/
You can find video here https://youtu.be/mVKMse-gFBI
I am using:
react for page rendering.
typescript directly(without babel) transpiling tsx to ES5 for browsers
webpack to generate codes separately
babel-polyfill to be packaged in the vendor.js but not referenced by typescript codes
I want to package javascript libraries and my common codes into one vendor.js and generate separately [name].js files with [name].html (with template) referencing both vendor.js and [name].js for each [name].tsx (which conatins only a class definition [name] that extends React.Component<P,S>) with HtmlWebpackPlugin.
[name].tsx may import modules. Modules included in vendor.js cannot be included in [name].js, and the others must be normally included in [name].js
I have read the document form webpack official site and still have no idea how to do it.
I did find some guides that realize a multiple page website but having all libraries included in the separate [name].js files, which means the javascript libraries are redundantly included in every single [name].js and it is surely not expected.
And I found some guides about externals, but it can only be used on existing js files, not generated bundle files.
Here is my current webpack.config.js which supports only single page:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
module.exports = {
entry: {
"index.js": ['babel-polyfill', './src/index.tsx']
},
output: { filename: '[name]' },
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html"
})
],
module: {
rules: [{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
exclude: '/node_modules/'
}, {
enforce: 'pre',
test: /\.js$/,
loader: 'source-map-loader'
},]
},
resolve: {
extensions: ['.webpack.js', '.web.js', '.ts', '.js', '.tsx']
},
devtool: "source-map"
}
Is there any way to have it support multiple pages as described?
I'm still fairly new to webpack 2 but I've got most of my configurations working so far. The only thing I'm having some difficulty understanding is that when I run "npm run build" to bundle my files into my "dist" folder I noticed that only 1 of my images are being bundled. I'm using 'file-loader'. FYI all my images still show on my dev-server when I run it and appear under the public paths I assigned. It's only my local output that's not displaying all the images. Anyone know what's going on?
My Folder Structure
webpack.config.js
module.exports = {
mode: "development",
entry: {
app: "app"
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js",
publicPath: "/"
},
devServer: {
publicPath: '/',
port: 3000
},
module: {
rules: [
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images/',
publicPath: 'images/'
}
}
]
}
]
}
}
As you can see in my folder structure, it always builds with only one of my images being outputted. It's not a major issue (I don't think) since all the images still work when I run the app, but I would appreciate it if anyone could help me understand why only one image is outputting to my local 'dist'. Thank you.
Webpack only writes images to disk that you require. This is one of the benefits of Webpack, it only includes assets that your application needs, so Webpack will guarantee those images exist when you deploy.
To add more images to your output, require them either from your Javascript or CSS with url()s
Note that if you're using the dev server, Webpack doesn't write anything to disk, and keeps all compiled assets in memory.
I am creating a reactjs app and everything works fine on localhost. Things gets really problematic when I deployed the app to the hosting service.
I Have a ./reducers folder in my project which contains all of my reducers.
the project structure is this:
App
---reducers
---store.js
now the problem is that webpack is unable to solve the import reducers from './reducers' on my store.js
here is my full store.code:
import {applyMiddleware, createStore} from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import promise from 'redux-promise-middleware';
import reducer from './reducers';
const middleware = applyMiddleware(promise(), thunk, logger())
export default createStore(reducer, middleware)
and here is my webpack config:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './main.js',
output:{
path: __dirname,
filename: 'index.js',
publicPath: '/'
},
devServer:{
inline: true,
port: 1515,
historyApiFallback: true
},
module:{
loaders:[
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query:{
presets: ['react', 'es2015'],
plugins: ['react-html-attrs', 'transform-object-rest-spread']
}
}
]
}
}
and here's the error from my hosting server:
I'm really sorry for the ultra long post, I've been googling around and haven't found an answer that i can understand..deploying reactjs app and webpack is something new for me
Okay so after trying to digest every information that I got regarding this matter, I finally understand on how to deploy webpack project..
As mentioned by #JoeClay, I Should just deploy the final build from Webpack which is contrary to what I did. What I did is that I uploaded the whole project file into the hosting server and tried to run the webpack from there. I treated the production phase just like the local development phase which is not right.
After looking for so many information on the net on how to build a Webpack project I finally realized that this whole time I've got the build file with me.
If we look back into my Webpack.config.js
module.exports = {
entry: './main.js',
output:{
path: __dirname,
filename: 'index.js',
publicPath: '/'
},
devServer:{
inline: true,
port: 1515,
historyApiFallback: true
},
module:{
loaders:[
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query:{
presets: ['react', 'es2015'],
plugins: ['react-html-attrs', 'transform-object-rest-spread']
}
}
]
}
}
We can see that in the output I have a filename of 'index.js' and every time I did things like npm start, It produces the 'index.js' on the directory. So it's always been there, I've just never paid much attention to it.
What I did next is easy, I go to my hosting server and upload my Index.html along with my CSS, JS and 'index.js' file and voila! It Works!
But there is one problem that I haven't solve..Somehow after I Uploaded the file, inside my index.html file the <script src="index.js"></script> got commented out..I'd be so grateful if someone could give me an insight on this.
Hopefully this thread can help newbie webpack users like me and can help them deploy to hosting other than heroku or digitalocean. And thanks everyone who has spend their time answering my question
The problem is your reducers import is not found. You need to check your path relative to where your store is located.