• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Webpack+TypeScript---环境搭建

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、环境配置

1、新建项目(即新建文件夹:HelloWorld)

 

2、项目初始化:npm  init  -y

 

3、安装typescript

    npm  i   typescript  -g

    tsc  --init

4、目录结构如下:

     

 

5、配置构建工具(webpack5)

    npm  install  webpack  -D

    npm  install  webpack-cli -D

    npm  install  [email protected]  -D

    npm  install  clean-webpack-plugin -D

    npm  install  html-webpack-plugin -D

    npm  install ts-loader

因为typescript是全局安装,所以要在项目运行之前进行链接

    npm  link typescript

  

5.1webpack.config.js

const { merge } = require("webpack-merge");

const baseConfig = require("./webpack.base.config");

const devConfig = require("./webpack.dev.config");

const proConfig = require("./webpack.pro.config");

 

module.exports = (env,argv)=> {

    let config = (argv.mode === "development")?devConfig : proConfig;

    //合并文件

    return merge(baseConfig,config);

};

5.2webpack.base.config.js

const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {

    entry: "./src/index.ts",

    output: {

        filename: "app.js",

    },

    resolve: {

        extensions: [".js",".ts",".tsx"],

    },

    module: {

        rules: [

            {

                test: /\.tsx?$/i,

                use: [

                    {

                        loader: "ts-loader",

                    },

                ],

                exclude: /node_modules/,

            },

        ],

    },

    plugins: [

        //通过一个模板帮助生成网站首页,并将输出文件嵌入

        new HtmlWebpackPlugin({

            template: "./src/tpl/index.html",

        }),

    ],

};

 5.3webpack.dev.config.js

const webpack = require("webpack");

module.exports = {

    plugins: [

        new webpack.LoaderOptionsPlugin({

            options: {

                devtools: "cheap-module-eval-source-map",

            },

        }),

    ],

};

 5.4webpack.pro.config.js

const { CleanWebpackPlugin } = require("clean-webpack-plugin");

module.exports = {

    plugins: [

        new CleanWebpackPlugin(),

    ],

};

 

6、配置文件:package.json

 

 

二、页面文件和ts文件

1、index.ts

let hello:string = "Hello TypeScript";

document.querySelectorAll(".app")[0].innerHTML = hello;

2、index.html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <div class="app"></div>

</body>

</html>

三、启动项目:

      npm  start

访问 http://localhost:8080/


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript-类型兼容性发布时间:2022-07-18
下一篇:
[vue+typescript]Property or method &quot;xxx&quot; is not defined on the instanc ...发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap