bundlers: e.g. webpack, parcel, rollup
transpiler: example for JavaScript: babel:
bundlers act as starting points for builds:
import
)example bundler:
transform code
example: processing of JavaScript:
simple bundler that supports:
start with a package.json file that contains an empty object: {}
install dependencies:
npm install parcel
in src/index.js:
import { add } from './mymath.js';
console.log(add(1, 1));
in src/mymath.js:
export const add = (a, b) => a + b;
Konfiguration in package.json:
{
"source": "src/index.html",
"scripts": {
"build": "parcel build",
"start": "parcel"
}
}
HTML entry point in src/index.html:
<!DOCTYPE html>
<html>
<head>
<title>React starter app</title>
</head>
<body>
<div id="app"></div>
<script src="index.js"></script>
</body>
</html>
build via:
npm run build
development server on localhost:8080 via:
npm run start
bundler that uses JavaScript as its central language - relies on import
statements
start with a package.json file that contains an empty object: {}
install dependencies:
npm install webpack webpack-cli webpack-dev-server
in src/index.js:
import { add } from './mymath.js';
console.log(add(1, 1));
in src/mymath.js:
export const add = (a, b) => a + b;
configuration via webpack.config.js:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};
use src/index.js as an entrypoint
write the result to dist/, using dist/bundle.js as an entrypoint
npm scripts in package.json:
"scripts": {
"build": "webpack",
"start": "webpack serve --mode development"
}
build via:
npm run build
development server on localhost:8080 via:
npm run start
examples:
creates an HTML page that runs the JavaScript
npm install html-webpack-plugin
in webpack.config.js:
const HtmlWebpackPlugin = require('html-webpack-plugin');
plugins: [
new HtmlWebpackPlugin({title: "page title"})
],
npm install babel-loader @babel/core
activating babel in webpack.config.js:
module: {
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
],
},
note: in this configuration Babel will not modify the code
npm packages:
possible configuration - e.g. in package.json:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
},