Setting up Webpack
Webpack
Use Gulp to:
gulp.task('test:unit', () => { /* Run unit tests */});
gulp.task('test:e2e', () => { /* Run selenium tests */});
gulp.task('test', ['test:unit', 'test:e2e']);
gulp.task('build:saas', () => { /* Compile Saas */ });
Webpack takes modules with dependencies and generates static assets representing those modules.
There are 3 (main) things Webpack needs to know:
This is done in a file typically named webpack.config.js
npm install -g webpack
// webpack.config.js
// 1. Define the entry point in the application
const config = {
entry: ['./src/app.ts'],
// 2. Add a loader (processor for a file type)
module: {
rules:
[ {test: /\.ts?$/, use: ['ts-loader'] } ]
},
// 3. Specify the output
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
}
// 4. export the configuration
module.exports = config;
A basic configuration:
Webpack modules
// mathFunctions.js
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
// app.js
import {add} from './mathFunctions.js';
console.log(add(2,3));
Webpack modules
Loaders
Plugins
To sum up:
- loaders: tell webpack how to load a file of a certain type
- plugins: everything else
Code splitting
Code splitting
Tree shaking
Limitations
Running webpack - NPM Scripts
// package.json
{
...
"scripts": {
"start": "webpack -w",
"build": "webpack -p"
}
...
}
// usage
npm start
npm run build
Running webpack - NPM Scripts
NPM + Webpack -> best build tool