The web frontend starter project with webpack and typescript
- Init project folder
npm init
- Install Webpack
npm install webpack webpack-cli --save-dev
- Add webpack.config.js and add minimal settings
const webpack = require('webpack'); const path = require('path'); const config = { entry: './src/app.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'app.bundle.js' } }; module.exports = config;
- Add src/app.js
window.onload = function(){ document.write("hello world."); }
- Add dist/index.html
<!DOCTYPE html> <html> <head> <title>Frontend empty project</title> <meta charset="utf-8"> </head> <body> <div id="app"></div> <script src="app.bundle.js"></script> </body> </html>
- Add webpack build command to the scripts section in package.json
"scripts": { "build": "webpack", "test": "echo \"Error: no test specified\" && exit 1" },
- Test this simplest webpack project
then open the dist\index.html in any browser to see result, you should see the "hello world" in the page
npm run build
- Move the content of the STEP-1 webpack.config.js a new created webpack.common.js, this file will contain common configuration in all environment
- Change webpack.config.js to load different environment configuration by exporting a configuration function as below:
const common = require("./webpack.common"); const { merge } = require("webpack-merge"); /* *env: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true } *argv, all cli arguments: { mode: 'development', env: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true } } */ module.exports = function(env, argv) { if(typeof argv.mode === 'undefined') { console.log('please specify the \'mode\' argument like \'--mode=production\''); return null; } return merge(common, require(`./webpack.${argv.mode}`)); };
- Create a new webpack.development.js for development specific configuration
console.log("development environment..."); module.exports = { }
- Create a new webpack.production.js for production specific configuration
console.log("production environment..."); module.exports = { }
- Change the build commands in scripts section in package.json
"scripts": { "build": "webpack --mode=production", "debug": "webpack --mode=development", "test": "echo \"Error: no test specified\" && exit 1" },
- Add webpack-dev-server dependency
npm install webpack-dev-server --save-dev
- Add dev server configuration in .webpack/webpack.development.js
const path = require('path'); module.exports = { //... devServer: { contentBase: path.join(__dirname, 'dist'), hot: true, open: true, compress: true, port: 8888, }, };
- Change the build commands in scripts section in package.json
it use the 'webpack serve' to start webpack-dev-server
"scripts": { "build": "webpack --mode=production", "debug": "webpack serve --mode=development", "test": "echo \"Error: no test specified\" && exit 1" },
- Run 'npm run debug' to start, it will open localhost:8888 automatically in browser
-
Install html-webpack-plugin plugin
npm install html-webpack-plugin --save-dev
-
Add below configuration section to webpack.common.js
const HtmlWebpackPlugin = require('html-webpack-plugin'); const config = { ... plugins: [ new HtmlWebpackPlugin({ template: './src/index.html', filename: 'index.html', title: 'Code starter for web frontend', inject: 'body', }), ... ]
-
Put the index.html file from ./dist folder to the ./src folder
-
Delete below line from index.html, this will be automatically injected by the plugin "html-webpack-plugin"
<script src="app.bundle.js"></script>
-
Run 'npm build' commond will generate both of the app.bundle.js and index.html as output files
-
Add typescript development tools
npm install --save-dev typescript babel-loader @babel/core @babel/cli @babel/plugin-proposal-class-properties @babel/preset-env @babel/preset-typescript
Here we use typescript for type checking, and use babel to compile typescritp to javascript
-
Create initial configuration for typescript
tsc --init --declaration --allowSyntheticDefaultImports --noEmit --sourceMap --target esnext --outDir "dist" -
Add babel-loader configuration to webpack.common.js to compile .ts file, also change the entry configuration
const config = { entry: './src/app.ts', ... resolve: { extensions: ['.ts', '.tsx', '.js', '.json'] }, module: { rules: [ { // Include ts, tsx, js, and jsx files. test: /\.(ts|js)x?$/, exclude: /node_modules/, loader: 'babel-loader', } ] } }
-
Rename ./src/app.js to ./src/app.ts to start typescript coding
-
How to use third-party javascript libraries? Typescript needs to know the types of all objects in third party libraries, so we need to install their type definition, available type definitions can be seen here: https://www.npmjs.com/~types
For example, to use JQuery:
npm install @types/jquery
Use this web site to get such configuration: https://createapp.dev/webpack
Examples:
-
SCSS
Install tools
npm install --save-dev node-sass sass-loader css-loader style-loaderAdd below configuration to webpack.common.js
const config = { ... module: { rules: [ ... { test: /\.scss$/, use: [ 'style-loader', 'css-loader', 'sass-loader' ] } ] } };
Add a file named styles.scss in ./src as below:
$primary-color: white; $bg: black; h1 { color: $primary-color; background-color: $bg; }
Change ./src/index.html as below:
<!DOCTYPE html> <html> <head> <title>Empty project</title> <meta charset="utf-8"> </head> <body> <div id="app"> <h1>Hello Vue!</h1> </div> <!--script src="app.bundle.js"></script--> </body> </html>
Import styles.scss in app.ts
import styles from './styles.scss'
that's it!
-
VUE
Install vue and its dev tools:
npm install vue --save npm install vue-loader vue-template-compiler --save-dev
Add a file named App.vue to ./src as below:
<template> <div> <h1> {{name}} </h1> </div> </template> <script lang="ts"> import Vue from "vue"; export default Vue.extend({ data: function() { return { name: 'Hello World!', } }, }); </script>
Add below content to app.ts:
import Vue from 'vue'; import App from './App'; new Vue({ el: '#app', render: h => h(App), });
Add a new webpack configuration file named webpack.vue.js, copy and paste contents from webpack.common.js into it, now we will modify this file for vue
Add or modify configurations as below:
const VueLoaderPlugin = require('vue-loader/lib/plugin'); const config = { ... module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', }, { test: /\.(ts|js)x?$/, loader: 'babel-loader', options: { presets: [ '@babel/preset-env', [ '@babel/preset-typescript', { allExtensions: true, }, ], ] } } ] }, plugins: [ // make sure to include the plugin! new VueLoaderPlugin(), ... ],
Use webpack.vue.js in webpack.config.js as below
const common = require("./webpack.common"); const vue = require("./webpack.vue"); const { merge } = require("webpack-merge"); /* *env: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true } *argv, all cli arguments: { mode: 'development', env: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true } } */ module.exports = function(env, argv) { if(typeof argv.mode === 'undefined') { console.log('please specify the \'mode\' argument like \'--mode=production\''); return null; } //return merge(common, require(`./webpack.${argv.mode}`)); return merge(vue, require(`./webpack.${argv.mode}`)); };