Set up backbone.js environment with webpack - backbone.js

I've started learning Backbone.js and I'm a little confused with setting up the project. How shoul I set up the Backbone.js environment with Webpack and npm? First, I did
$ npm init
$ npm i webpack --save-dev
$ npm i webpack-dev-server --save-dev
$ npm i backbone --save-dev
$ npm i jquery --save-dev
$ npm i babel-core
$ npm i babel-loader
$ npm i babel-preset-es2015
I had the following in my webpack.config.js
var config = {
entry: './main.js',
output: {
path:'./',
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
module: {
loaders: [
{
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015']
}
}
]
}
}
module.exports = config;
and in my main.js I had
import Backbone from 'backbone';
import $ from 'jquery';
but nothing worked. How should I make an initial setup for Backbone with ES6? Also, does Backbone support ES6, or it is rather outdated and deprecated for modern time?

I'd suggest you start here: https://github.com/jerrysu/backbone-webpack-example
There's nothing preventing you using Backbone with ES6, though you should read this: http://benmccormick.org/2015/04/07/es6-classes-and-backbone-js/
The first problem I see with your webpack configuration is that your loader is missing a test expression (https://webpack.js.org/configuration/)

Related

webpack configuration error

I tried to configure web pack for reacts web app, it kept telling me to use
npm install -D webpack-cli :
The CLI moved into a separate package: webpack-cli.
Please install 'webpack-cli' in addition to webpack itself to use the CLI.
-> When using npm: npm install webpack-cli -D
-> When using yarn: yarn add webpack-cli -D
module.js:557
throw err;
^
when I did what it was asking me it gave me this error :
C:\Users\Desktop\lazabre1>webpack-dev-server
C:\Users\Desktop\lazabre1\webpack.config.js:59
new webpack.NoErrorsPlugin()
^
this is my webpack.config.js file :
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool: 'inline-source-map',
entry: [
'webpack-dev-server/client?http://127.0.0.1:8080/',
'webpack/hot/only-dev-server',
'./src'
],
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
resolve: {
modulesDirectories: ['node_modules', 'src'],
extensions: ['', '.js']
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loaders: ['react-hot', 'babel?presets[]=react,presets[]=es2015']
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
};
I have 2 questions: first - how to solve this problem //
second - what is the difference between npm install webpack and npm install webpack-cli.
You are using a configuration file written for Webpack 1 (I guess, by the use of the module.loader key and the NoErrorsPlugin) and using Webpack 4 (if you did not specify otherwise, and by the request to use the CLI which is not included by default anymore).
Upgrade your configuration by following the documentation and update the NoErrorsPlugin.
The webpack-cli is a package to use webpack from the command-line tool (what you are trying to do).

How to add sass-loader with Webpack 1?

I recently ejected my create-react-app because I need to load sass files. Unfortunately, create-react-app uses webpack 1. But, the current version of sass-loader includes a dependency on webpack 2. The support docs don't indicate the last sass-loader version that supports webpack 1. Does anyone know where I can find this info?
You'll need to use the webpack 1 branch of the loader:
https://github.com/webpack-contrib/sass-loader/tree/archive/webpack-1
As shown in the documentation:
module.exports = {
...
module: {
loaders: [
{
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "sass-loader"]
}
]
}
};
And then in your react app:
require("./file.scss")
In your npm install you'd do:
npm i -S npm install sass-loader#4.1.1

Error with Typescript / whatwg-fetch / webpack

I'm trying to have a simple demo app working using Typescript and React, with webpack. When I try to add whatgw-fetch to retrieve data from a server, I get this error when running webpack :
error TS2307: Cannot find module 'whatwg-fetch'
The error is on the import line :
import * as Fetch from 'whatwg-fetch'
I did install the npm dependency and the typing for typescript
npm install --save whatwg-fetch
typings install --global --save dt~whatwg-fetch
My webpack configuration is :
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});
var path = require('path');
module.exports = {
entry: [
path.resolve(__dirname, 'app/index.tsx')
],
output: {
path: __dirname + '/dist',
filename: "index_bundle.js"
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
loaders: [
{test: /\.tsx$/, exclude: /node_modules/, loader: "ts-loader"}
]
},
plugins: [HTMLWebpackPluginConfig]
};
I don't see any error in my IDE (IntelliJ IDEA) and if I change the import to some module that actually is not there, I get a different error (Module not found: Error: Cannot resolve module 'whatwg-fetcha' in C:\dev\[...]), and my IDE tells me the import is not valid.
The import for basic React works fine with an equivalent setting :
npm install --save react
typings install --global --save dt~react
import * as React from 'react'
Am I missing something ?
Thanks for your help.
After some more research (and actually writing the question down), it seems that the typing for whatwg-fetch is in this use case : Import a module for side-effects only (as described on the site : Though not recommended practice, some modules set up some global state that can be used by other modules. These modules may not have any exports, or the consumer is not interested in any of their exports.)
So instead of
import * as Fetch from 'whatwg-fetch'
I used
import 'whatwg-fetch'
And I don't get any more errors and I can use the fetch function in my component. Hope this helps someone else.
Aside from #Antoine solution, I also needed to add #types/whatwg-fetch to my project to make it work:
npm install --save #types/whatwg-fetch

get SyntaxError in jsx with webpack and eslint [duplicate]

I'm starting to learn react with a tutorial. But webpack is not working as expected.
So here is my simple webpack.conf.js file.
module.exports = {
entry: "./app-client.js",
output: {
filename: "public/bundle.js"
},
module: {
loaders: [
{
exclude: /(node_modules|app-server.js)/,
loader: 'babel'
}
]
}
};
Also I installed all the modules:
npm install -g webpack
npm install webpack react babel-loader babel-core
But when running webpack, I got the following error message:
ERROR in ./app-client.js
Module build failed: SyntaxError: app-client.js: Unexpected token (4:13)
2 | var APP = require('./components/APP');
3 |
> 4 | React.render(<APP />, document.getElementById('react-container'));
| ^
In my understanding, babel-loader is supposed to take care of that. But it looks like it's not making the effort.
What am I missing?
Babel 6 doesn't do anything by itself. In order to properly process JSX, you need to have the following in your .babelrc file:
{
"presets": ["react"]
}
Also, you need to make sure you install that preset using NPM:
$ npm install --save-dev babel-core react react-dom babel-preset-react
A good place to start is the official React getting started page

Webpack / Babel / React build error: "Unknown option: foo/node_modules/react/react.js.Children"

I'm trying to build a project with webpack and react with this webpack config:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'babel-polyfill',
'./app/less/main.less',
'./app/main.js',
'webpack-dev-server/client?http://localhost:8080'
],
output: {
publicPath: '/',
filename: 'dist/main.js'
},
debug: true,
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, 'app'),
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
}
},
{
test: /\.less$/,
loader: "style!css!autoprefixer!less"
},
]
}
};
I am sure I have the needed pm modules too and have webpack installed, however when running webpack I get:
Module build failed: ReferenceError: [BABEL] /Users/me/foo/app/main.js: Unknown option: foo/node_modules/react/react.js.Children
Any ideas?
Sorry I forgot to install babel-preset-react:
$ npm install babel-preset-react --save-dev
I just wanted to add that I got the error after I uninstalled an old npm module I wasn't using in my project anymore. Which was weird because I wasn't using it anywhere - how could uninstalling something that isn't used anywhere cause an error?
Turns out that one of that modules sub-dependencies had babel-preset-react, which I had missed installing to my own project when I started it. Thus, uninstalling that package also uninstalled the critical babel-preset-react!
For over a year, my react app could compile thanks to another package's sub-dependency...
So yes, installing babel-preset-react solved the issue for me.

Resources