I know there are a bunch of resources out there describing the setup and I have tried several approaches, but it seams that things are changing so fast that the examples I google have become obsolete - here goes
package.json
{
"name": "scoreboard",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.3.15",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babelify": "^7.2.0",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"webpack": "^1.12.9"
}
}
this is my webpack.config.js
module.exports = {
context: __dirname + "/app",
entry: "./main.js",
output: {
filename: "main.js",
path: __dirname + "/dist"
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ["babel-loader"],
query: {
presets: ["react", "es2015"]
}
}
]
}
}
directory structure
app
- greeting.js
- main.js
dist
node_module
package.json
webpack.config.js
content of greeting.js
import React from "react";
export default React.createClass({
render: function () {
return (
<div className="greeting">
Hello {this.props.name}!
</div>
);
},
});
content of main.js
import React from "react";
import Greeting from "./greeting";
React.render(
<Greeting/>,
document.body
);
but when I run
$ webpack Hash: 553345561044745134b3 Version: webpack 1.12.9 Time: 2573ms
+ 1 hidden modules
ERROR in ./main.js Module build failed: SyntaxError: /Users/kristiannissen/Documents/js/scoreboard/app/main.js: Unexpected token (5:8) 3 4 React.render(
> 5 <Greeting/>,
^ 6 document.body 7 ); 8
from what I can tell the <Greeting/> is not being rendered as expected. To install it all I ran npm install --save react react-dom babelify babel-preset-react --save-dev
Update Added the query to the webpack.config.js but when running webpack I now get this
$ webpack
.../scoreboard/node_modules/webpack-core/lib/LoadersList.js:54
if(!element.loader || element.loader.indexOf("!") >= 0) throw new Error("Cannot define 'query' and multiple loaders in loaders list");
Still not quit twerking the way I am hoping
Update After changing the webpack.config.js to
module.exports = {
context: __dirname + "/app",
entry: "./main.js",
output: {
filename: "main.js",
path: __dirname + "/dist"
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ["react", "es2015"]
}
}
]
}
}
and installing the mentioned module, it's twerking! There was a typo in my greeting file, the return function should not use {} but ()
Full package.json
{
"name": "scoreboard",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.3.15",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babelify": "^7.2.0",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"webpack": "^1.12.9"
}
}
loaderattribute instead ofloaders?