I am trying to achieve some sort of auto reloading in my project using webpack-dev-server and react-hot.
I am using babel to transform my js from a src directory to a public directory.
With my current config I can see that the .json and .js update files are making it to the public directory.
But in my error console I can see this:
app.js:4473 GET http://localhost:3000/sockjs-node/info?t=1473946358314 404 (Not Found)AbstractXHRObject._start # app.js:4473(anonymous function) # app.js:4362
app.js:795 [WDS] Disconnected!
I have a webpack.config.js (baseConfig) which looks like this:
require('dotenv').config();
var path = require('path');
var autoprefixer = require('autoprefixer');
var precss = require('precss');
var cssnano = require('cssnano');
var ROOT_PATH = path.resolve(__dirname, 'src');
var BUILD_PATH = path.resolve(ROOT_PATH, '../public');
module.exports = {
entry: '',
output: {
path: BUILD_PATH,
filename: 'app.js'
},
module: {
loaders: [
{
test: /\.jsx?$/, // test for js and jsx files only
loader: ['babel'],
exclude: /node_modules/,
query: {
"presets": ["es2015", "react"],
"ignore": ["/node_modules/"],
}
},
{
test: /\.css$/,
loaders: [
'isomorphic-style-loader',
'css-loader',
'postcss-loader'
],
exclude: /node_modules/
}
]
},
postcss: function() {
return [autoprefixer({browsers: ['last 2 versions']}), precss, cssnano];
},
plugins: []
}
Also a webpack-client-config.js, which looks like this:
require('dotenv').config();
var webpack = require('webpack');
var path = require('path');
var ROOT_PATH = path.resolve(__dirname, 'src');
var BUILD_PATH = path.resolve(ROOT_PATH, '../public');
var baseConfig = require('./webpack.config.js');
baseConfig.entry = [
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:3000',
(ROOT_PATH + '/client-render.js'),
];
baseConfig.plugins = [
new webpack.HotModuleReplacementPlugin(),
]
baseConfig.devServer = {
hot: true,
publicPath: 'http://localhost:3000'
}
module.exports = baseConfig;
I have obviously mis-understood something about webpack, anyone care to enlighten me as to why this is not working? I can visit localhost:3000/app.js and can see the script right there in the browser. I set public as static with express.
Related
I stumbled upons some errors when i tried to use loaders for semantic-ui-css/semantic.min.css and normal css files at the same time. I was able to load in respectively the semantic.min.css and the normal css files when i did it indepentently, but not at the same time.
Loader for Semantic UI (It works)
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
},
},
},
{
test: /\.css$/,
include: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: 'css-loader',
}
],
})
},
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
module.exports = config;
Loader for the normal CSS files (It works)
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
module.exports = config;
Loaders for both at the same time (Does not work)
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
},
},
},
{
test: /\.css$/,
include: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: 'css-loader',
}
],
})
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
module.exports = config;
This is because you are defining two tests for .css in your loaders. The first test is evaluating is true for all .css files that are included in the project so all CSS is resolving with your first loader rule. If you want to use two rules for .css files then you need to get more specific with your test. I'd put the node_modules rule second. And for the first rule, you can use exclude to make sure the loader skips anything in node_modules which will then go to your next loader.
I am working on a site build in react. It was working fine until I have implemented express. After implementation of express, I am not able to load some resources, especially images in the browser. The image path is coming correct but images are not showing in the browser.
webpack.config.js is:
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
devtool: 'eval-source-map',
entry: [
'webpack-hot-middleware/client?reload=true',
path.join(__dirname, 'app/index.js')
],
output: {
path: path.join(__dirname, '/dist/'),
filename: '[name].js',
publicPath: '/'
},
plugins: [
new ExtractTextPlugin('/bundle.css', { allChunks: true }),
new HtmlWebpackPlugin({
template: 'app/index.html',
inject: 'body',
filename: 'index.html'
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
],
resolve: {
extensions: ['', '.scss', '.css', '.js', '.json'],
modulesDirectories: [
'node_modules',
path.resolve(__dirname, './node_modules')
]
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
"presets": ["react", "es2015", "stage-0", "react-hmre"]
}
}, {
test: /\.json?$/,
loader: 'json'
}, {
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss!sass')
}, {
test: /\.css$/,
loader: 'style-loader!css-loader'
},{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
loader: "url-loader?limit=10000"
},{
test: /\.less$/, loader: "style-loader!css-loader!less-loader"
},{
test: /\.(ttf|eot|svg|woff|woff2)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "file-loader"
}]
}
};
server.js file content is as follows:
const path = require("path");
const express = require("express");
const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
const config = require("./webpack.config.js");
const app = express(),
DIST_DIR = path.join(__dirname, "dist"),
HTML_FILE = path.join(DIST_DIR, "index.html"),
isDevelopment = process.env.NODE_ENV !== "production",
DEFAULT_PORT = 3000,
compiler = webpack(config);
app.set("port", process.env.PORT || DEFAULT_PORT);
if (isDevelopment) {
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));
app.get("*", (req, res, next) => {
compiler.outputFileSystem.readFile(HTML_FILE, (err, result) => {
if (err) {
return next(err);
}
res.set('content-type', 'text/html');
res.send(result);
res.end();
});
});
}
else {
app.use(express.static(DIST_DIR));
app.get("*", (req, res) => res.sendFile(HTML_FILE));
}
app.listen(app.get("port"));
package.json is as follows:
"main": "server.js",
"script": {
"start": "babel-node server-es6.js",
"build:server": "babel server-es6.js --out-file server.js",
"build:client": "webpack -p --config webpack.config.js --progress"
},
Site is loading but some css are not loading. Console throw error:
GET http://bundle.css/ net::ERR_NAME_NOT_RESOLVED
Image path comes correctly as http://localhost:3000/img/img1.png but it's not showing in the browser. I think the issue is with webpack public path.
When I am using <img src={require('/images/image-name.png')} />, its working fine. But I don't want to do that because it's very heavy codebase and also I think it's not a nice solution.
I have taken help from webpack-express-boilerplate.
In chrome Network tool, image type is coming as text/html.
If your file path is static, you can import the file once and then provide it as a src
import image from '/path/to/images/image-name.png';
...
<img src={image} />
I'm trying to set up a developer environment for React but I'm getting some issues with webpack pathing. Usually, a quick google solves issues like these but I haven't seen anyone with the same problem, which probably means I'm an idiot and is messing something up.
So, I followed a tutorial and I have a dev-folder and a output-folder. In output I have output.js, where I want webpack to output all my stuff. So, according to the guide, I should run ./node_modules/.bin/webpack and it should work with the config I have, but it's showing the wrong filepath. I want it to go to react-test/output/output.js but it's throwing in a /dev, making it react-test/dev/output/output.js. My webpack.config.js looks like this:
var webpack = require("webpack");
var path = require("path");
var DEV = path.resolve(__dirname, "dev");
var OUTPUT = path.resolve(__dirname, "output");
var config = {
entry: DEV + "/index.jsx",
output: {
path: OUTPUT,
filename: "output.js"
},
module: {
loaders: [{
include: DEV,
loader: "babel-loader"
}]
}
};
module.exports = config;
I can't see where dev is getting added.
I would modify the file to the following:
var path = require('path');
module.exports = {
entry: path.join(__dirname, '/dev/index.js'),
output: {
path: path.join(__dirname, '/output'),
filename: 'output.js'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
}
}
]
}
};
You don't need to require webpack in this file, but you do need to tell the module loader what to test. The presets I included are assuming you are using ReactJS and ES6, you may need to change them as appropriate.
webpack.config
var path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: './dev/index.js',
output: {
path: path.resolve(__dirname, 'output'),
filename: 'output.js',
publicPath: '/'
},
module: {
rules: [
{ test: /\.(js)$/, use: 'babel-loader' },
{ test: /\.css$/, use: ['style-loader', 'css-loader'] }
]
},
devServer: {
historyApiFallback: true
},
plugins: [
new HtmlWebpackPlugin({
template: 'app/Index.html' // Copy Index.html with script tag refrencing output.js in Output folder
})
]
}
Try to set context option to react-test directory.
And set DEV and OUTPUT paths realtive to it:
var DEV = path.resolve(__dirname, "./dev");
var OUTPUT = path.resolve(__dirname, "./output");
var config = {
context: path.resolve(__dirname, './'),
entry: DEV + "/index.jsx",
output: {
path: OUTPUT,
filename: "output.js"
},
...
here is my problem i build a react app by using webpack, but it gives this warning
Warning: Failed prop type: The prop id is required to make n accessible for users of assistive technologies such as screen readers.
so how to solve it?
webpack code:
var webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
module.exports = {
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new HtmlWebpackPlugin({template: './src/index.html'})
],
entry:{
index: './src/index.js'
},
output:{
path: path.resolve(__dirname, 'dist'),
publicPath: "static/react_veg/dist/",
filename: 'boundle.js'
},
module:{
rules:[
{test: /\.css$/, use: ['style-loader','css-loader'] },
{test: /\.(js|jsx)$/ ,use:'babel-loader'},
{test: /\.(png|jpg)$/, use:'url-loader?limit=8192'}
]
},
resolve:{
alias:{},
extensions:[".js"]
}
}
I am new to webpack and react and am trying to set up an application. From looking at previous questions I think there must be something wrong with how I am setting up babel-loader, but I'm unable to see what my mistake is. Is anyone else able to see it?
webpack.config.js:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: [
{
test: /\.jsx?/,
include: APP_DIR,
loaders: ["babel-loader"],
query: {
presets: ['es2015', 'react']
}
}
]
};
module.exports = config;
babel.rc
{
"presets": ["es2015", "react"]
}
Index.jsx
import React from 'react';
import ReactDom from 'react-dom';
class App extends React.Component {
render() {
return <p>Hello React!</p>;
}
}
ReactDom.render(<App />, document.getElementById('app'));
Here is the documentation for the module options object: https://webpack.github.io/docs/configuration.html#module
If you have babel-preset-2015 and babel-preset-react npm modules installed and the following webpack.config.js (babel.rc file isn't needed with the query presets) should work:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?/,
include: APP_DIR,
loader: "babel-loader",
query: {
presets: ['es2015', 'react']
}
}]
}
};
module.exports = config;
Change you webpack file to include the babel-loader within quotes and included in an array of loaders as shown below. In modules you define an array of loaders to handle different types of files but a single loader for a particular type.
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.js?$/,
exclude: /(node_modules|bower_components)/,
include: APP_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015'],
}
}
]
},
};
module.exports = config;