L04: Eject! Babel, Webpack, ESLint
> babel script.js
class Person {
constructor(name) {
this.name = name;
}
toString() {
return `This person is called ${this.name}!`;
}
}
const martin = new Person("Martin");
console.log(martin);No transforms
> babel script.js --plugins=transform-es2015-block-scoping
class Person {
constructor(name) {
this.name = name;
}
toString() {
return `This person is called ${this.name}!`;
}
}
var martin = new Person("Martin");
console.log(martin);> cat script.js
class Person {
constructor(name) {
this.name = name;
}
toString() {
return `This person is called ${ this.name }!`;
}
}
const martin = new Person("Martin");
console.log(martin);> babel script.js --presets es2015
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Person = function () {
function Person(name) {
_classCallCheck(this, Person);
this.name = name;
}
_createClass(Person, [{
key: "toString",
value: function toString() {
return "This person is called " + this.name + "!";
}
}]);
return Person;
}();
var martin = new Person("Martin");
console.log(martin);// package.json
{
…,
"babel": {
"presets": [
"react-app"
]
},
…
}// app.js
import bar from './bar';
bar();// bar.js
export default function bar() {
// do something
}// webpack.config.js
module.exports = {
entry: './app.js',
output: {
filename: 'bundle.js'
}
}//index.html
<html>
<head>
...
</head>
<body>
...
<script src="bundle.js"></script>
</body>
</html>const path = require('path');
const config = {
entry: './path/to/my/entry/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js'
},
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' }
]
}
};
module.exports = config;// …
},
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new HtmlWebpackPlugin({template: './src/index.html'})
]
};(config from package.json)
> yarn eslint .
yarn eslint v1.0.1
$ "/Users/marlehma/Desktop/pg6300-17/f04/code/demo-ejected-app/node_modules/.bin/eslint" "."
/Users/marlehma/Desktop/pg6300-17/f04/code/demo-ejected-app/src/index.js
7:7 warning 'a' is assigned a value but never used no-unused-vars
✖ 1 problem (0 errors, 1 warning)
✨ Done in 1.30s.yarn eslint . --fix
^ helps a lot