html文件如何打包 ?html文件打包的方法介绍

本篇文章给大家带来的内容是关于html文件如何打包 ?html文件打包的方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

根据上篇文章讲的js压缩,npm run server运行后会报错

要弄明白这个问题,我们先要弄清楚什么是开发环境,什么是生产环境。开发环境中是基本不会对js进行压缩的,在开发预览时我们需要明确的报错行数和错误信息,所以完全没有必要压缩JavasScript代码。而生产环境中才会压缩JS代码,用于加快程序的工作效率。devServer用于开发环境,而压缩JS用于生产环境,在开发环境中作生产环境的事情所以Webpack设置了冲突报错。

生产环境中压缩js会无法调试(找不到第几行)

在实际开发中,webpack配置文件是分开的,开发环境一个文件,生产环境一个文件。

1、html文件的打包

把dist目录下面的index.html剪切复制到src目录下,index.html里面引入的js代码可以删除掉(会自动引入js代码),这就是模板文件

立即学习“前端免费学习笔记(深入)”;

在webpack-config.js里面引入插件

  1.  plugins:[       new htmlPlugin({          minify:{              removeAttributeQuotes:true       //对html进行压缩,去掉属性的双引号          },          hash:true,      //为了开发中js有缓存效果,加入hash可以有效避免缓存JS          template:'./src/index.html'      //是要打包的html模板路径和文件名称      })    ]

登录后复制

  1. wds」: Project is running at http://10.212.109.18:8087/ i 「wds」: webpack output is served from / i 「wds」: Content not from webpack is served from F:\webLearn\webpackLearn\dist ‼ 「wdm」: Hash: 027dd749b565ba3b200d Version: webpack 4.15.1 Time: 3008ms Built at: 2018-07-11 08:49:13  Asset       Size  Chunks             Chunk Names entry2.js    139 KiB       0  [emitted]  entry2 entry.js    145 KiB       1  [emitted]  entry index.html  427 bytes          [emitted] Entrypoint entry = entry.js Entrypoint entry2 = entry2.js

登录后复制

出现上述代码即成功

如下所示即打包成功

  1. Hash: 40112e2b8d4dc81b512bVersion: webpack 4.15.1Time: 5299msBuilt at: 2018-07-11 08:47:26Asset       Size  Chunks             Chunk Namesentry2.js  962 bytes       0  [emitted]  entry2entry.js   6.75 KiB       1  [emitted]  entryindex.html  427 bytes          [emitted][0] ./src/entry2.js 33 bytes {0} [built][4] ./node_modules/css-loader!./src/css/index.css 227 bytes {1} [built][5] ./src/css/index.css 1.06 KiB {1} [built][6] ./src/entry.js 94 bytes {1} [built]+ 3 hidden modulesWARNING in configurationThe 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/Child html-webpack-plugin for "index.html": 1 asset[0] (webpack)/buildin/module.js 497 bytes {0} [built][1] (webpack)/buildin/global.js 489 bytes {0} [built][3] ./node_modules/html-webpack-plugin/lib/loader.js!./src/index.html 511 bytes {0} [built]+ 1 hidden module

登录后复制

把dist目录删掉   ,在终端输入webpack进行打包

在终端输入 npm run dev 在浏览器中运行

plugins里面配置如下:

const htmlPlugin=require(‘html-webpack-plugin’);

在终端里面进行安装:npm install –save-dev html-webpack-plugin

配置html

相关推荐:

上篇文章

上篇文章

以上就是html文件如何打包 ?html文件打包的方法介绍的详细内容,更多请关注【创想鸟】其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
编程技术

html5中的重要元素实现的代码实例

2025-4-4 17:24:22

编程技术

html中如何使用js实现长按功能(代码)

2025-4-4 17:25:20

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索