webpack简单运用(构建html 安装插件和loader)

本文介绍了webpack的基础知识,包括它的概念和配置步骤。详细讲解了如何安装与配置loader来处理不同类型的模块,如安装css处理的加载器。此外,还阐述了安装和使用插件的流程,特别是如何使用extract-text-webpack-plugin将css提取到单独的文件中。最后,提到了通过修改package.json的scripts进行打包操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

webpack是什么?

webpack配置步骤

安装loader

安装插件  

打包


webpack是什么?

 在webpack的世界里,每个文件都是一个模块,比如.css、.js、.html等。对于不同的模块,则需要不同的加载器(Loaders)来处理。

提示:下面的操作都默认你安装了node.js,如果没有请看 node.js安装步骤

webpack配置步骤

1.创建一个目录 比如 demo

使用 NPM初始化

$ npm init

2. 本地局部安装 webpack与webpack-dev-server

$ npm install webpack --save-dev

**: --save-dev 会作为开发依赖来安装

$ npm install webpack-dev-server --save-dev

如果你的 package.json 的devDependencies包含 webpack和server则表示安装成功。如果你的webpack版本大于4.0.0 则需要安装webpack-cli 最后目录如下:

     "devDependencies": {
        "webpack": "^4.21.0",
        "webpack-cli": "^3.1.2",
        "webpack-dev-server": "^3.1.9"
      }

3.在 demo目录里  创建 webpack.config.js
4. 打开 package.json 在"scripts" 加入一个服务脚本

 "dev":"webpack-dev-server --open --config webpack.config.js"

**: --open 表示会自动打开浏览器 --config是 则会直接读取webpack.config.js文件
5. 在demo目录下 创建 main.js,并打开 webpack.config.js进行如下配置:

    var path=require('path');

    var config={
        entry:{
            main:'./main'
        },
        output:{
            path:path.join(__dirname,'./dist'),
            publicPath:'/dist/',
            filename:'main.js'
        }
    };
    module.exports=config;

6. 在demo目录下 创建 index.html

    <div id="app">
        你好! 世界
    </div>
    <script src="/dist/main.js"></script>

7.启动webpack

   $ npm run dev

安装loader

1.安装css处理的加载器

 $ npm install css-loader style-loader --save-dev

2. 打开webpack配置文件,添加如下内容

        module:{
            rules:[
                {
                    test:/\.css$/,
                    use:["style-loader","css-loader"]
                }
            ]
        }

 **: module对象的 rules属性可以指定一系列的Loaders。每一个loader都必须包含test和use两个选项

3.在项目根目录 创建 style.css文件

#app{
	font-size: 20px;
	color:orange;
}

4.引入main.js

import './style.css';

 5. 重新启动服务

$ npm run dev

安装插件  

有时候因为css文件内容太多,都引入js文件里太占体积,而且不能做缓存,此时我们就需要用插件来将散落的css提取到一个main.css文件里,最终将该文件导入index.html中

1. 安装 extract-text-webpack.plugin
        提示: 如果webpack版本大于4.0.0则需要@next

    $ npm install extract-text-webpack-plugin@next --save-dev

2. 打开webpack配置文件,配置如下:

var path=require('path');
var ExtractTextPlugin=require('extract-text-webpack-plugin');
var config={
	entry:{
		main:'./main'
	},
	output:{
		path:path.join(__dirname,'./dist'),
		publicPath:'/dist/',
		filename:'main.js'
	},
	module:{
		rules:[
			{
				test:/\.css$/,
				use:ExtractTextPlugin.extract({
					use:'css-loader',
					fallback:'style-loader'
				})
			}
		]
	},
	plugins:[
		//重命名提取后的css文件
		new ExtractTextPlugin('main.css')
	]
};
module.exports=config;

 3. 重新启动服务

$ npm run dev

打包

   1.在 package.json中“”scripts” 添加如下内容:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --open --config webpack.config.js",
    "build": "webpack"
  },

2. 打包 

$ npm run build 

如果想了解es6语法转换点击这里:es6语法转es5

后面关于vue-cli的相关配置还会更新...

Rebuild started: Project: Project *** Using Compiler &#39;V6.22&#39;, folder: &#39;E:\Keil_v5\ARM\ARMCLANG\Bin&#39; Rebuild target &#39;Target 1&#39; assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值