Webpack for Beginners

Microsoft Web-Dev-For-Beginners 项目解析 是微软(Microsoft)开源的面向初学者的 Web 开发学习资源库,旨在帮助零基础开发者系统掌握现代 Web 开发技能。该项目在 GitHub 上拥有极高的关注度,采用循序渐进的教学模式,结合实践案例和代码示例,覆盖前端开发核心技术栈。 阅读详情

Your Step-by-Step Guide to Learning Webpack 4 by Mohamed Bouzid


源代码:
https://gitee.com/liyangzhong/webpack_beginners.git
https://github.com/Li-YangZhong/webpack_beginners.git


Once you are in the webpack_beginners folder, use the following command to initiate a basic JSON file:
$ npm init -y
This will create a file called package.json that will save references to our installed modules.
Via npm, the -y option is to answer yes to all prompted questions; it’s not that important for us at this stage, so we will just say yes to everything.


install webpack using our command line:

$ npm install webpack webpack-cli --save-dev

The --save-dev option is to tell NPM that we need this just for our development purposes, meaning that these packages will be installed on our local machine only.


cnpm不支持package-lock.json
1. 使用cnpm install时候,并不会生成package-lock.json文件,网上搜,cnpm维护者似乎并不打算支持该功能,具体大家可以去搜下。
2. cnpm install的时候,就算你项目中有package-lock.json文件,cnpm也不会识别,仍会根据package.json来安装。所以这就是为什么之前你用npm 安装产生了package-lock.json,后面的人用cnpm来安装,可能会跟你安装的依赖包不一致,这是因为cnpm 不受package-lock.json影响,只会根据package.json进行下载。

链接:https://www.jianshu.com/p/2e459040a29f


The package-lock.json is the full representation of the dependency tree of your project, including the indirect dependencies. For example, when you install webpack, other packages that it depends on will get installed as well. Package-lock.json will also contain the specific version of each module, so that if someone took your project later and ran “npm install” on their machine, they will get the exact same versions you installed. That way there will be no breakdown on the application (i.e., due to a package that got updated and has some break changes).


Out of the box, webpack won’t require you to use a configuration file.
However, it will assume the entry point of your project is src/index and will
output the result in dist/main.js minified and optimized for production.
Source: https://webpack.js.org/configuration/

What this means is that webpack expects you to create an entry file called index.js inside an src folder, and then it will create and output the result in dist/main.js for you, without the need to create the configuration file yourself or do anything else.


If you wonder what a node_modules/.bin/webpack is, it’s just the path to our webpack command, which is basically a JavaScript file itself.


As you can see from the output, webpack specifies what was built for us as a result (a file called main.js); also notice a warning at the bottom that says the following:

WARNING in configuration.
The ‘mode’ option has not been set, so webpack will fall back 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 at https://webpack.js.org/configuration/mode/

This simply means that we haven’t told webpack what mode we are working on (development or production) yet, as result, the fallback is set automatically to production so it decides that our JavaScript should be minified. In order to get rid of that warning above, we will add a flag --mode=production when calling the webpack command like this:
node_modules/.bin/webpack --mode=production


We used before “node_modules/.bin/webpack” as it might seem
that it’s an ugly way to do it, and for sure there is a better way. What you can do is to open your package.json file and look under scripts, which appears like this:

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}

Remove the line:
"test": "echo \"Error: no test specified\" && exit 1"
And replace it with this one:
"build": "webpack --mode=production"
So it will look like this:

"scripts": {
"build": "webpack --mode=production"
} 

In the “script” line that we added above to our package.json, there is no need to specify the exact path to webpack file like we did in our terminal because it will know where to find it. Now instead of calling:
$ node_modules/.bin/webpack
We will use our custom command:
$ npm run build
This will look for a webpack command in the node_modules folder and call it for us. Note that you can name your script what you like. It doesn’t necessarily have to be “build.” You can, for example, name it “dev” and then call it from your terminal as the following:
$ npm run dev


Webpack understands CommonJS and ES6 modules.


require () is not part of the standard JavaScript API. But in Node.js, it’s a built-in function with a special purpose: to load modules. Modules are a way to split an application into separate files instead of having all of your application in one file.


import { sayHello } from './greeting.js'

What if we change ‘./greeting.js’ to ‘greeting.js’ ? What will happen? Well, if you do so, webpack will assume you are looking for a node module, and it will go and search that name in node_modules folder, not in your source code folder, so make sure to always add a relative path to where your file is located when using the “import” statement.


So far, we learned that webpack requires us to have an src/index.js file to produce a dist/main.js compiled file. That’s the default webpack uses (also called “zero config”) as a ready-to-go configuration


With webpack we have the ability to customize a lot of things (including files name and their destination) by using a file called webpack.config.js (that’s the name webpack will look for by default). In order to configure itself, you can surely name it something else other than webpack.config.js (for example, “my-config.js” or any other naming you like), but if you do so, just make sure to specify this in the package.json file:

scripts: {
	"build": "webpack --config my-config.js"
} 

module.exports = {
	entry: "./src/index.js",
	output: {
		filename: "../build/application.js"
	}
}

All we need to do is to replace “main.js” by “…/build/application.js”. Because the default output folder is “dist,” we tell webpack to go back by one step and have our output file “application.js” placed in a new folder called “build.”


As you might be guessing, the other value that we can use for “mode” option is “development,” so let’s set “mode” to “development,” but this time we prefer to add it to “webpack.config.js” file instead of adding it directly to webpack command. This way we can have all our options in one place.


To use Babel, webpack provides us with a loader called “babel-loader,” which is easy and straightforward to use, so let’s install it first using our terminal:

$ npm install --save-dev babel-loader @babel/core @babel/preset-env

If you are wondering how to know what to install exactly in order to get Babel working, the answer is documentation! In order to install any loader or plugin, all you have to do is to search for “Webpack + the name of loader/plugin,” then most likely the first link will direct you to the documentation page of that loader or plugin (either webpack website or a GitHub repository) where more details are explained.


CommonJS
JavaScript is a powerful object oriented language with some of the fastest dynamic language interpreters around. The official JavaScript specification defines APIs for some objects that are useful for building browser-based applications. However, the spec does not define a standard library that is useful for building a broader range of applications.
The CommonJS API will fill that gap by defining APIs that handle many common application needs, ultimately providing a standard library as rich as those of Python, Ruby and Java. The intention is that an application developer will be able to write an application using the CommonJS APIs and then run that application across different JavaScript interpreters and host environments. With CommonJS-compliant systems, you can use JavaScript to write:
• Server-side JavaScript applications
• Command line tools
• Desktop GUI-based applications
• Hybrid applications (Titanium, Adobe AIR)
Read an additional introduction by Kris Kowal at Ars Technica.

From http://www.commonjs.org/


你可以使用我们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

From https://developer.aliyun.com/mirror/NPM?from=tnpm

如果以下安装不成功,可以改用cnpm进行安装

$ npm install image-webpack-loader --save-dev

解决raw.githubusercontent.com无法连接问题
解决方法如下:
点击进入https://site.ip138.com/raw.githubusercontent.com/
输入域名查询自己的IP地址
管理员身份运行命令行:
1. 首先ping命令选取延迟低的IP地址
2.在命令行执行以下命令:

在打开的hosts文件中,添加
199.232.68.133 raw.githubusercontent.com
备注:此IP需改成上面ping命令返回的IP地址中的一个)

保存重新运行vscode,问题解决
From https://blog.youkuaiyun.com/weixin_37636605/article/details/107077773

如果上述方法无法解决,可以删除node_modules文件夹后重新运行npm install


webpack-dev-server loads and serves any bundled file from memory instead of the local filesystem. If any resource (HTML, JS, CSS,
etc.) is not found in memory, webpack-dev-server will look at the “contentBase” path and try to find the missed file there. When no “contentBase” option is set, it will look at the root of the working directory.


In case the third-party library you installed doesn’t include a package.json
file, you can use a relative path to import it from node_modules (i.e., import lib from “../node_modules/lib_folder/lib.js”).

Docker Tutorial for Beginners 很好的docker 入门教程 This tutoriαl is meαnt for those who αre interested in leαrning Docker αs α contαiner service. This product hαs spreαd like wildfire αcross the industry αnd is reαlly mαking αn impαct on the development of new generαtion αpplicαtions 立即下载

相关推荐

Microsoft-Web-Dev-For-Beginners-PATH

面向初学者的Web开发-课程 Microsoft的Azure云倡导者很高兴提供有关JavaScript,CSS和HTML基础知识的为期12周,为期24天的课程。 每节课均包括课前和课后测验,完成课程的书面说明,解决方案,作业等。 我们基于项目的教学法使您可以在构建时学习,这是新技能“坚持”的可靠途径。 衷心感谢我们的作者Jen Looper,Chris Noring,Christopher Harrison,Jasmine Greenaway,Yohan Lasorsa,Floor Drees和写生艺术家Tomomi Imura! 老师,我们提供了有关如何使用本课程的。 如果您想创建自己的课程,我们还提供了一个 学生,要自己使用此课程,请分叉整个软件库并自行完成练习,首先要进行讲座前的测验,然后阅读讲座并完成其余的活动。 通过理解课程而不是复制解决方案代码来尝试创建项目。 但是,在每

掌握 Lit Query 缓存核心:Query Keys 查询键设计全面指南

**导读**:TanStack Query 家族(Lit Query 是其在 Web Components 生态的适配层)完全以查询键(Query Key)作为缓存的寻址依据——同一把键指向同一份数据缓存,键变了就视为另一份数据。本文以 [query-keys.md](https://link.gitcode.com/i/673c444c5020087ad1af341c73d85062) 为骨架,

gitblog_00264的博客 304

webDevForBeginner:面向初学者的Web开发实践和数据存储

面向初学者的Web开发 用Django创建博客 该过程从7月4日到8月22日的每个星期六运行,并。

Microsoft Web-Dev-For-Beginners 项目教程

Microsoft Web-Dev-For-Beginners 项目教程 【免费下载链接】Web-Dev-For-Beginners 24 Lessons, 12 Weeks, Get Started as a Web Developer ...

gitblog_00780的博客 441

learn-webpack:Example code for learning Webpack.(用于学习 Webpack 的实例代码)

什么是 webpack    webpack 是德国开发者 Tobias Koppers 开发的模块加载器。   在 webpack 中所有的文件都将被当做模块使用。当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(dependency graph),其中包含应用程序需要的每个模块,然后将所有的这些模块打包成一个或多个 bundle。如图所示: 与 Gulp/Grunt 对比   webpack 与 Gulp/Grunt 是没有对比性的,因为 Gulp/Grunt 是一种能够优化前端的开发流程的工具,而 webpack 是一种模块化的解决方案。不过 Webpack 的优点使得 Webpack 在很多场景下可以替代 Gulp/Grunt 类的工具。   Grunt 和 Gulp 的工作方式是:在一个配置文件中,指明对某些文件进行类似编译,组合,压缩等任务的具体步骤,工具之后可

MUSIC算法翻车?Toeplitz矩阵重构如何解决DOA估计中的相干信源难题

本文深入探讨了MUSIC算法在相干信源DOA估计中失效的根本原因,并详细介绍了基于Toeplitz矩阵重构的协方差矩阵修复技术。该方法通过凸优化恢复协方差矩阵的理想结构,有效解决了信号子空间秩亏损问题,从而显著提升了MUSIC算法在相干和多径场景下的分辨精度与稳健性。

a2b3c4d5e的博客 250

微软开源项目Web-Dev-For-Beginners常见问题解决方案

微软开源项目Web-Dev-For-Beginners常见问题解决方案 【免费下载链接】Web-Dev-For-Beginners 24 Lessons, 12 Weeks, Get Started as a Web Developer ...

gitblog_00834的博客 699

Flink SQL数据血缘实战:如何用Calcite追踪字段级数据流向(附源码示例)

本文深入探讨了如何利用Apache Calcite解析Flink SQL执行计划,实现字段级数据血缘的追踪。通过扩展Calcite的元数据处理器,特别是针对Flink特有的Snapshot等节点,文章提供了从获取优化后的逻辑计划到提取并可视化血缘关系的完整实战方案,并附有核心源码示例,帮助开发者构建实时数仓的数据治理能力。

weixin_29032337的博客 143

Web-Dev-For-Beginners 浏览器扩展第 5 课:用 webpack + CO2 Signal API 构建 Carbon Trigger 浏览器扩展(starter 代码实战指南)

本文基于仓库中 [Carbon Trigger 浏览器扩展的 starter 代码说明](https://link.gitcode.com/i/a4fd392f87ba87092cc942eaa8db405a)展开,讲解如何从零构建一个调用 CO2 Signal API、实时反映本地区电网碳排放强度的 Edge/Chrome 扩展:包括 npm 依赖安装、webpack 构建打包、以“Load U

gitblog_01061的博客 603

ROS+Gazebo仿真入门:用TurtleBot3模拟扫地机器人全流程

本文详细介绍了如何利用ROS和Gazebo仿真平台,以TurtleBot3 Waffle Pi模型为核心,从零搭建并运行一个虚拟扫地机器人的全流程。内容涵盖仿真环境搭建、世界与机器人模型解析,并重点演示了SLAM建图、自适应蒙特卡罗定位以及全局与局部路径规划等核心算法实战,为机器人算法开发与验证提供了高效、低成本的解决方案。

sre5engineer的博客 1089

STM32学习笔记(江协科技)-----OLED显示屏

SSD1306简介:•SSD1306是一款OLED/PLED点阵显示屏的控制器,可以嵌入在屏幕中,用于执行接收数据、显示存储、扫描刷新等任务••驱动接口:128个SEG引脚和64个COM引脚,对应128*64像素点阵显示屏••内置显示存储器(GDDRAM):128*64 bit128*8 ByteSRAM••供电:IC逻辑),VCC=7~15V(面板驱动)••通信接口:8位6800/8080并行接口,3/4线SPI。

lu322f的博客 1064

Web-Dev-For-Beginners 探索现代 Web 开发工具实战指南:从作业任务到仓库源码佐证

本指南以 [1-getting-started-lessons/1-intro-to-programming-languages/assignment.md](https://link.gitcode.com/i/90c87028371d794c17111ae2b4e68731) 的作业任务为核心,系统讲解如何调研、选择并介绍三个本课程未覆盖的现代 Web 开发工具。你将掌握作业的完整要求、九大工

gitblog_00030的博客 901

【学习笔记-QGIS】 QGIS从零快速上手

安装文章参考:https://zhuanlan.zhihu.com/p/370633306。

Xcong_Zhu的博客 1万+

Web-Dev-For-Beginners 课后任务全解:如何系统调研现代 Web 开发工具生态

> 导读:本文以 [1-intro-to-programming-languages 课程作业](https://link.gitcode.com/i/ba0857721f9867f44b3e4640948ccf43)("Exploring Modern Web Development Tools")为核心,完整拆解这项入门级调研任务的目标、选题规则、九大工具分类、交付格式、质量标准与评分量规,并

gitblog_01194的博客 406

配置xen上VM混杂模式,使其能捕获物理网络流量

本文目的:配置VM和xen相应的物理网卡(pif)和虚拟网卡(vi

LeoDuo的专栏 3721

电力系统分析 | Matlab实现源荷场景生成---蒙特卡洛法场景生成启发式同步回带削减

电力系统分析 | Matlab实现源荷场景生成---蒙特卡洛法场景生成启发式同步回带削减

前程算法屋的博客 813

每日GitHub trending热点项目盘点

趋势:前3名中有2个项目直接服务于AI开发(DeepSeek-Reasonix为AI编程助手,generative-ai-for-beginners为AI学习资源,superpowers为AI代理框架),反映出生成式AI技术正在深度融入开发者工作流。这是一个为AI编程代理设计的"超能力"框架,通过预置的技能库(如测试驱动开发、系统化调试、子代理协作等)规范AI代理的开发流程。新一代前端测试工具,区别于传统Selenium方案,Cypress直接在浏览器中运行测试代码,提供更快的执行速度和更友好的调试体验。

XuJL89的博客 300

【网络与爬虫 49】突破WAF防线:Web应用防火墙绕过实战攻略

本文深入探讨Web应用防火墙(WAF)的工作原理与绕过技术,从基础的编码变换到高级的协议层攻击,通过实战案例和代码演示,帮助安全研究者和渗透测试人员理解现代WAF防护机制并掌握合规的绕过方法。内容涵盖SQL注入、XSS、文件上传等攻击向量的WAF绕过技术,适合网络安全从业者和爬虫开发者学习参考。

熵数实验室 1849

每日GitHub trending精选

趋势:前3名中有2个项目直接服务于AI开发(DeepSeek-Reasonix为AI编程助手,generative-ai-for-beginners为AI学习资源,superpowers为AI代理框架),反映出生成式AI技术正在深度融入开发者工作流。这是一个为AI编程代理设计的"超能力"框架,通过预置的技能库(如测试驱动开发、系统化调试、子代理协作等)规范AI代理的开发流程。新一代前端测试工具,区别于传统Selenium方案,Cypress直接在浏览器中运行测试代码,提供更快的执行速度和更友好的调试体验。

XuJL89的博客 267

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Web Dev for Beginners:从入门代码到 Edge 安装——构建 Carbon Trigger 浏览器扩展完整指南

本文基于 Web-Dev-For-Beginners 仓库第 5 模块的浏览器扩展入门代码(Carbon Trigger 扩展)展开,覆盖从环境准备、webpack 构建,到在 Edge 中加载未打包扩展、配置 CO2 Signal API 密钥与区域代码的完整流程,并结合仓库中的参考实现源码剖析"碳强度彩色圆点"的实现原理。读完后你将能够独立完成一个可运行、可加载、可配置的浏览器扩展,并理解其背

gitblog_00185的博客 862

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

ML-For-Beginners 配套 Quizzer 测验应用:本地开发与 Azure Static Web Apps 部署指南

本文以 ML-For-Beginners 课程仓库中的 Quizzer 测验应用(对应文档 [translations/da/quiz-app/README.md](https://link.gitcode.com/i/4547540e74bfce80e8a7a63813c52e72))为对象,完整讲解这套"课前/课后测验"前端项目从环境搭建、本地开发、生产构建,到一键部署到 Azure Stat

gitblog_00393的博客 580

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Web-Dev-For-Beginners 浏览器扩展项目(一):从浏览器原理到构建你的第一个碳足迹扩展 My Carbon Trigger

本篇指南是 Web-Dev-For-Beginners 课程中「浏览器扩展项目」的第一课(法语版课程文档),以"理解浏览器工作原理"为切入点,带你搭建一个能在 Chrome、Firefox 与 Edge 上运行的浏览器扩展:它通过 CO2 Signal 的公开 API 显示你所在地区的电力碳排放强度与化石燃料占比。读完本文,你既能掌握浏览器与扩展的关系、扩展的安装与开发模式流程,也能完整跑通该课程

gitblog_00270的博客 302

Web Dev for Beginners 编程入门课作业实战:用“工具调研三件套“吃透现代 Web 开发工具链

`Web-Dev-For-Beginners` 是一门面向零基础学习者的 Web 开发课程(24 课时、12 周),而本篇文章讲解的是其"编程语言与开发者工具"入门课后配套的研究型作业:从编辑器、浏览器与命令行工具之外的广阔生态中,挑选 **3 个未被课程覆盖的现代开发工具**,并按统一格式完成调研报告。读完本文,你将完整掌握该作业的任务边界、九大建议调研类别、输出格式模板、五条质量准则与评分表,

gitblog_00615的博客 497

Carbon Trigger 浏览器扩展实战:Web-Dev-For-Beginners 仓库中基于 CO2 Signal API 的扩展入门

本文围绕 Web-Dev-For-Beginners 仓库第 5 课(Browser Extension)的"Starter Code"说明文档展开,带你完整走通 Carbon Trigger 扩展的获取、构建与安装流程:用 npm 安装依赖、用 Webpack 打包出 `dist` 目录,再通过 Edge 的 "Load Unpacked" 机制把扩展装进浏览器。读完本篇,你能独立完成一个 Ma

gitblog_00952的博客 472

Web-Dev-For-Beginners 浏览器扩展项目第一课:从浏览器原理到构建第一个碳足迹扩展

本篇技术指南基于 Web-Dev-For-Beginners 课程的浏览器扩展项目第一课,完整覆盖浏览器的工作机制、扩展的加载与安装流程,以及使用 Webpack + NPM 工具链搭建“区域碳足迹”扩展的完整实操过程。读完并动手完成后,你将能够独立构建、打包并在 Edge/Chrome 中以开发模式加载自己的第一个浏览器扩展。 [![Edge 浏览器扩展管理页面截图](https://raw.

gitblog_01144的博客 616

Web-Dev-For-Beginners 浏览器扩展实战:基于 CO2 Signal API 构建 Carbon Trigger 碳排放指示器扩展

本文基于课程仓库中第 5 模块(浏览器扩展)的完整实现文档,讲解 Carbon Trigger 浏览器扩展的搭建与配置全流程:从 `npm install` 与 webpack 构建,到以“加载已解压的扩展程序”方式安装到 Edge,再到 CO2 Signal API 密钥与电网区域代码的配置。读完本文,你将能够独立构建并部署这个实时显示地区电力碳强度的浏览器扩展,并读懂其源码中碳强度到图标颜色的

gitblog_00787的博客 1041
上一篇: 驾驶员安全停车技术全程图解 王淑君编著
下一篇: Jim Rohn - My Philosophy for Successful Living (2011, No Dream Too Big LLC)
CodingIsFun
博客等级 码龄8年 37粉丝 240原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值