首页 > 其他分享 >ts09_使用webpack打包ts文件2

ts09_使用webpack打包ts文件2

时间:2022-12-24 13:34:42浏览次数:45  
标签:文件 plugin ts09 ts webpack html 打包

1.为了对html文件进行自动生成和一些资源的自动按需引入还需要对webpack继续进行一些配置,首先需要安装html-webpack-plugin插件帮助我们自动生成html文件

运行npm i -D html-webpack-plugin

2.引入html-webpack-plugin

const HTMLWEBPACKPLUGIN = require('html-webpack-plugin')

3.在webpack.config.js文件中配置plugins选项来引入要使用的插件

 

 

 4.为了能让代码修改后自动打包页面自动刷新还需要进行一些配置,下载webpack-dev-server

运行npm i -D webpcak-dev-server

安装好之后在packge.json文件中配置脚本

 

 

 然后运行nmp run start 如果出现以下 错误

 

 

 这个错误的意思是项目需要一个运行环境的模式,为“development” 或者“production”;

需要在webpack.config.js中配置mode选项来确定运行环境

 

 

 

 5.为了确保每次打包的dist文件都是最新的,需要在打包之前清除dist目录,还需要安装一个插件 clean-webpack-plugin

运行命令 npm i -D  clean-webpack-plugin 安装插件

引入插件

const {CleanWebpackPlugin} = require("clean-webpack-plugin") 在plugins里面配置  

 

 6.最后还需要配置一下resolve选项告诉webpack哪些文件可以被当做模块使用

 

 到这里webpack打包JS文件的一些最基本的配置就完成了

webpack.config.js

//引入path模块,管理路径
// import { Configuration } from 'webpack';//运行webpack时注释掉
/**
 * @type {Configuration}
 */
const path = require('path');
//引入html-webpack-plugin
const HTMLWEBPACKPLUGIN = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require("clean-webpack-plugin")
//webpack中的所有配置信息都应写在module.exports中
module.exports = {
    //指定入口文件
    entry: './src/index.ts',
    //指定打包文件所在目录
    output: {
        //指定打包文件的目录
        // path:'./dist'
        path: path.resolve(__dirname, 'dist'),
        //打包后文件的文件名
        filename: "bundle.js"
    },
    //指定webpack打包时要使用的模块
    module: {
        //指定加载的规则
        rules: [
            {
                //指定规则生效的文件
                test: /\.ts$/,
                //要是用的loader
                use: "ts-loader",
                //指定要排除的文件
                exclude: /node_modules/,

            }
        ]
    },
    //配置webpack的插件
    plugins: [
        new CleanWebpackPlugin(),
        new HTMLWEBPACKPLUGIN({
            // title:'这是一个自定义的title'//用来配置自动生成的html文件的title
            template:"./src/index.html"//用来配置自动生成的html文件的模板
        })
    ],
    mode:'development',
    resolve:{
        //设置引用模块
        extensions:['.ts','js']//告诉webpack哪些后缀的文件可以被当做模块引用
    }

}

  package.json文件

{
  "name": "part3",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode production",
    "dev": "webpack --mode development",
    "start": "webpack serve --open "
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-webpack-plugin": "^4.0.0",
    "html-webpack-plugin": "^5.5.0",
    "ts-loader": "^9.4.2",
    "typescript": "^4.9.4",
    "webpack": "^5.75.0",
    "webpack-cli": "^5.0.1",
    "webpack-dev-server": "^4.11.1"
  }
}

  

标签:文件,plugin,ts09,ts,webpack,html,打包
From: https://www.cnblogs.com/SadicZhou/p/17002792.html

相关文章

  • HDLBits--Verilog习题记录1
    本文档是Verilog编程题的解题记录 1.VerilogLanguage--moreVerilogfeatures---Generatefor-loop:100-bitbinaryadder2题目说明:Createa100-bitbinaryripple-c......
  • Pu’er Tea: 10 Benefits You’t Don’t Know About
    Pu'ertea.WhataretheeffectsandeffectsofdrinkingPu'ertea?FirstBenefitofDrinkingPu'erteaLipidreduction,weightloss,bloodpressure,anti-arterios......
  • lightdb extra_float_digits--控制浮点数精度
    建一张表,两个字段类型分别是float和numeric,然后插入数据,如下:zjh@postgres=>createtablet1(c1float,c2numeric);CREATETABLEzjh@postgres=>insertintot1values(0.555......
  • FreeSWITCH学习笔记:EventSocket
    本文更新于2022-12-20,使用FreeSWITCH1.10.7。目录apiauthbgapiconnectdivert_eventseventexitfilterfilterdeletelingerlogmyeventsnixeventnoeventnolingernologsendev......
  • ts08_使用webpack打包ts文件
    1.新建项目使用npminit-y在根目录生成packge.json文件,管理包2.使用npm安装webpack相关工具npmi-Dwebpackwebpack-clitypescriptts-loader,ts-loader起到一个整合......
  • requests访问https页面
     requests访问http很简单,但访问https页面时,需要设置是否验证服务器等情况。当验证服务器时需要指定根证书路径进行访问,不验证证书访问时会返回证书验证警告。 访问......
  • 安装node.js与webpack创建vue2项目
    本文为博主原创,转载请注明出处:1.安装node.js下载地址:http://nodejs.cn/download/(可查看历史版本)node.js中文网:http://nodejs.cn/api-v16/建议下......
  • ts07_ts的编译选项2
    compileOptions编译器配置选项中一些关于语法检查的配置选项  "allowJs":false,//是否对JS文件进行编译,默认false  "checkJs":false,//是否对JS文件进行检查,默......
  • 用Echarts实现SpreadJS引用从属关系可视化
    在金融行业,我们经常会有审计审查的需求,对某个计算结果进行审查,但是这个计算结果可能依赖多个单元格,而且会有会有多级依赖的情况,如果让我们的从业人员靠眼睛找,工作量巨大,而......
  • ts06_ts的编译选项1
    tsconfig.json是ts编译器的配置文件,ts编译器可以根据它的信息来对代码进行编译。可以执行tsc--init命令自动生成常用的配置选项:include:定义希望被编译文件所在的目......