Follow along at https://pselle.github.io/gdi-build-tools.
Developers write software to make their lives easier
We use scaffolding tools (such as "rails new" or the create-react-app project) to quickly get a project started, or to see how a project works with all the pieces together
Although useful, scaffolding can constrain you to someone else's way of thinking. Critically analyze any scaffolding system you use for suitability!We're going to try out Yeoman a scaffolding tool for webapps
$ npm install -g yo
Now, run yo
from your terminal. What happens? Now, try running one of the many generator options!
$ mkdir [directory name]
$ cd [directory name]
$ yo [choose a generator]
all: clean build
clean:
rm -rf foo
build:
mkdir foo && cd foo && touch index.html
Try making a Makefile using the sample code. You can also download from github.
all: clean build
clean:
rm -rf foo
build:
mkdir foo && cd foo && touch index.html
"npm is the package manager for JavaScript and the world’s largest software registry" - npmjs.com
Yarn is an alternative to npm, and many of the commands map. It's fast and has some strong benefits.
Create a new directory and run "npm init"
{
"name": "intro-to-build-tools",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "GDI Philly",
"license": "MIT"
}
Use npm to install a package AND save it to our dependencies!
$ npm install --save dinosaur-fetcher
Tip! You can also use -S instead of --save!
const dino = require('dinosaur-fetcher');
console.log(dino.random());
BONUS activities: Be creative with how you use dinosaur fetcher! Can you prompt the user? Do something with multiple dinosaur values?
The "scripts" section of package.json can be any word, and you can run with "npm run [word]"
Some words have default behaviors, such as "start" and "install", and npm has lots of details on that.
Edit package.json to look like:
"scripts": {
"test": "echo \"No tests yet!\"",
"start": "node index.js"
},
And run "npm start"
For a challenge, use a new word (ex. "build") and have it run any bash command! Don't forget "npm run"!We'll talk about two today:
var gulp = require('gulp');
gulp.task('default', function() {
gulp.src('./*.js')
.pipe(gulp.dest('dist'));
});
var gulp = require('gulp');
gulp.task('default', function() {
gulp.src('./*.js')
.pipe(gulp.dest('dist'));
});
Webpack is a module bundler for web applications.
Webpack is smart. It understands lots of module definitions (CommonJS, AMD, ES 6 modules, etc.) and puts things together.
// webpack.config.js
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js'
}
}
// webpack.config.js
module.exports = {
entry: './index.js', // Entry!
output: { // Output!
filename: 'bundle.js'
}
}
Note: the Webpack 2 documentation has a great overview of these!
Loaders are how webpack understands files - how you can 'require()' a CSS file, or an image file, even!
Plugins perform actions on "compilations" or "chunks" of bundled code, vs. on a per file basis like a loader
We can use a CSS Loader to require CSS in our JS!
This is useful in, ex. modular components
const path = require('path');
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css/,
loaders: ['style-loader', 'css-loader'],
include: __dirname + '/css'
}
]
}
};
import { coolStuff } from './coolStuff';
import styles from './css/coolStyles.css';
import mainStyles from './css/styles.css';
document.write(`MOAR COOL STUFF ${coolStuff}!
`);
const path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css/,
loader: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
}
]
},
plugins: [
new ExtractTextPlugin("styles.css")
]
};
Try making a basic webpack configuration on your own!