Webpack builds scaffolding to package TypeScript code

Webpack builds scaffolding to package TypeScript code

Create a folder

Directory structure: dabaots

Initialize npm init -y to generate package.json file

Directory structure:
dabaots
dabaots/package.json

Then install the following tools in the development environment

npm i -D

webpack························(The core tool for packaging code
webpack-cli····················(command line tool for webpack)
typescript ······················(the core package required to write ts)
ts-loader ·························(webpack and ts can be integrated through ts-loader)
html-webpack-plugin … (a webpack plugin that automatically generates HTML)
webpack-dev-server … (no need to refresh the update page)
clean-webpack-plugin … (How to regenerate the latest files in dist will be automatically cleared before each package is launched)
"@babel/core" "@babel/preset-env" babel-loader core-js (install babel conversion, it will convert to the code according to your environment)

Small problems that may be encountered: The problem that may occur here is that if the version of the dependency package webpack-dev-server you downloaded is incompatible with Google's, it is recommended to change the plug-in version to a lower level or upgrade Google Chrome to the latest version, otherwise an error "Cannot GET /chrome.exe" will occur

Next, create webpack.config.js for configuration

Directory structure:
dabaots
dabaots/package.json
dabaots/webpack.config.js

// Import a package const path = require("path")

//Introduce the package for automatically generating HTML const HtmlWebpackPlugin = require("html-webpack-plugin")

//Introduce the plugin to update the dist file const {CleanWebpackPlugin} = require("clean-webpack-plugin")

// All configuration information in webpack should be written into moudle.exportsmodule.exports={
    //Specify the entry file entry: "./src/index.ts",
    //Specify the directory where the packaged file is located output:{
        //Specify the directory path of the packaged file:path.resolve(__dirname,"dist"),
        //Specify the file name of the packaged file: "bundle.js",
        //Do not use arrow functions when compiling environment: {
            arrowFunction: false
        }
    },
    
    //Webpack needs to use module when packaging.module:{
        //Specify the rules to load rules:[{
            // test specifies the file test:/\.ts$/ where the rule takes effect.
            // use is the loader to use
            //Configure babel
            use:[
                {//Specify loader loader: "babel-loader",
                    options:{
                        //Set the predefined environment presets:[
                            [
                                //Specify the environment plugin "@babel/preset-env",
                                //Configuration information {
                                    //Browser targets to be compatible:{
                                        "chrome":"88"
                                    },
                                    //Specify the corejs version "corejs":"3",
                                    // means loading on demand "useBuiltIns": "usage"
                                }
                            ]
                        ]
                    }
                },
                'ts-loader'
            ],
            //Set files that are not packaged and uploaded exclude: ["/node_modules/"]
        }]
    },
    
    // Configure webpack plugins:[
        new HtmlWebpackPlugin({
            // Custom html template address template: "./src/index.html"
        }),
        //Each time before packaging and launching, the files in dist will be automatically cleared. How to regenerate the latest files new CleanWebpackPlugin()
    ],
    
    resolve:{
        //Solve the problem of error when introducing other ts packages separately in ts files extensions:['.ts','.js']
    }
}

Finally, write the packaging and running scripts in package.json

In the terminal, npm run build package and run

After successful packaging, a dist file will be automatically generated

npm run start automatically opens Google Chrome and the content is hot updated

This is the end of this article about webpack scaffolding and TypeScript code packaging. For more relevant webpack TypeScript code packaging content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • TypeScript enumeration basics and examples
  • In-depth reading and practice records of conditional types in TypeScript
  • Typescript+react to achieve simple drag and drop effects on mobile and PC
  • Implementation of TypeScript in React project
  • Practical record of Vue3 combined with TypeScript project development
  • Vue3 + TypeScript Development Summary
  • Vue3+TypeScript implements a complete example of a recursive menu component
  • How to use TypeScript in Vue
  • A brief discussion on the understanding of TypeScript index signatures

<<:  Analyzing the MySql CURRENT_TIMESTAMP function by example

>>:  Summary of some common writing methods that cause MySQL index failure

Recommend

After docker run, the status is always Exited

add -it docker run -it -name test -d nginx:latest...

How to write HTML head in mobile device web development

Copy code The code is as follows: <head> &l...

How to start multiple MySQL instances in CentOS 7.0 (mysql-5.7.21)

Configuration Instructions Linux system: CentOS-7...

How to handle images in Vue forms

question: I have a form in Vue for uploading blog...

Detailed explanation of Redis master-slave replication practice using Docker

Table of contents 1. Background 2. Operation step...

Detailed summary of web form submission methods

Let's first look at several ways to submit a ...

Nginx server adds Systemd custom service process analysis

1. Take nginx as an example Nginx installed using...

Differences between ES6 inheritance and ES5 inheritance in js

Table of contents Inheritance ES5 prototype inher...

How to completely uninstall Docker Toolbox

Docker Toolbox is a solution for installing Docke...

Summary of CSS front-end knowledge points (must read)

1. The concept of css: (Cascading Style Sheet) Ad...

How to build mysql master-slave server on centos7 (graphic tutorial)

This article mainly introduces how to build a MyS...

About the problem of no virtual network card after VMware installation

1 Problem description: 1.1 When VMware is install...

Solve the problem of running hello-world after docker installation

Installed Docker V1.13.1 on centos7.3 using yum B...

Form submission page refresh does not jump

1. Design source code Copy code The code is as fol...