webpack入门(二)what is webpack

本文详细介绍了Webpack作为模块打包工具如何解决JavaScript应用中的各种加载和转换问题,包括依赖拆分、优化加载时间、整合第三方库等特性。重点阐述了其在代码拆分、加载器使用、解析复杂依赖以及插件系统的强大功能,同时提供了从安装到项目集成的完整指南。

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

webpack is a module bundler.webpack是一个模块打包工具,为了解决上篇一提到的各种模块加载或者转换的问题。

webpack takes modules with dependencies and generates static assets representing those modules.

webpack以依赖模块和生成 静态资源代表这些模块。

Goals

  • Split the dependency tree into chunks loaded on demand 把依赖树拆分成chunks,按需加载
  • Keep initial loading time low    保证初始加载时间少
  • Every static asset should be able to be a module   每个静态资源都应该是一个模块
  • Ability to  integrate 3rd-party libraries as modules    致力于整合第三方库作为模块
  • Ability to customize nearly every part of the module bundler 致力于自定义接近每一部分的模块打包器
  • Suited for big projects  适合大工程项目

How is webpack different?

这里说了webpack的特性

Code Splitting

webpack has two types of dependencies in its dependency tree: sync and async. Async dependencies act as split points and form a new chunk. After the chunk tree is optimized(优化), a file is emitted for each chunk.

Read more about Code Splitting.

Loaders

webpack can only process JavaScript natively, but loaders are used to transform other resources into JavaScript. By doing so, every resource forms a module.

Read more about Using loaders and Loaders. webpack只能处理原生js,但是loaders就是用来把其他的资源转换为js。这样做,每个资源都形成一个模块

Clever parsing

webpack has a clever parser that can process nearly every 3rd party library. It even allows expressions in dependencies like so require("./templates/" + name + ".jade"). It handles the most common module styles: CommonJs and AMD. webpack的巧妙地解析能处理几乎每一个第三方库。

Read more about expressions in dependenciesCommonJs and AMD.

Plugin system

webpack features a rich plugin system. Most internal features are based on this plugin system. This allows you to customize webpack for your needs and distribute common plugins as open source. webpack有功能丰富的插件系统。,很多内部特性都基于插件系统,它允许你按需自定义或以开源的形式分发公共插件。

Read more about Plugins.

Installation

node.js

Install node.js.

node.js comes with a package manager called npm.

webpack

webpack can be installed through npm:

$ npm install webpack -g

webpack is now installed globally and the webpack command is available.

先安装nodejs,用里面的包控制工具npm,然后用npm安装webpack,通过这个命令将webpack安装到全局,webpack这命令就可以用了。

Use webpack in a project

It’s the best to have webpack also as dependency in your project. Through this you can choose a local webpack version and will not be forced to use the single global one.最好把webpack写进dependency,你可以选一个webpack版本,并不强制用哪个。

Add a package.json configuration file for npm with: 通过这个命令加一个package.json文件

$ npm init

The answers to the questions are not so important if you don’t want to publish your project to npm.

Install and add webpack to the package.json with:     把webpack加入到package.json中,加入到dev里面。

$ npm install webpack --save-dev

Versions

There are two versions of webpack available. The stable one and a beta version. The beta version is marked with a -beta in the version string. The beta version may contain fragile changes or experimental features and is less tested. See changelog for differences. For serious stuff you should use the stable version:    webpack有俩版本可用,测试和稳定版。安装测试版要加 -beta的mark。测试版加入了一些测试的新特性。

$ npm install webpack@1.2.x --save-dev  

Dev Tools

If you want to use dev tools you should install it:  webpack的dev tools,下面是安装命令

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

 

转载于:https://www.cnblogs.com/dh-dh/p/5165202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值