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.csdn.net/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 立即下载

相关推荐

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

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

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

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

gitblog_00264的博客 305

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 的工作方式是:在一个配置文件中,指明对某些文件进行类似编译,组合,压缩等任务的具体步骤,工具之后可

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的博客 442

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! 老师,我们提供了有关如何使用本课程的。 如果您想创建自己的课程,我们还提供了一个 学生,要自己使用此课程,请分叉整个软件库并自行完成练习,首先要进行讲座前的测验,然后阅读讲座并完成其余的活动。 通过理解课程而不是复制解决方案代码来尝试创建项目。 但是,在每

GEE AI: 利用 Satellite Embedding 语义搜索快速识别研究区目标斑块

摘要 本文介绍了一种基于Google Earth Engine的遥感语义搜索技术,利用Satellite Embedding数据集快速识别目标地物。该方法通过将光谱信息转化为高维向量,在武汉市的案例中展示了如何:1)定义研究区和样本点;2)加载预处理的Embedding数据;3)计算向量相似度;4)结合面积过滤优化结果。相比传统监督分类,该技术具有响应快速(无需模型训练)、抗季节干扰和精准界定等优势,特别适用于森林碳储量监测和土地利用检测等应用场景。文中提供了完整的GEE代码实现流程,包括样本定义、相似度计

此星光明博客 632

微软开源项目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

快衰落、慢衰落,平坦衰落、频率选择性衰落

目录一、大尺度衰落和小尺度衰落二、平坦衰落和频率选择性衰落1.反射墙、固定天线模型2.相干带宽和时延扩展三、快衰落和慢衰落1.反射墙、运动天线模型2.相干时间和多普勒扩展四、联系总结 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例可供参考 一、大尺度衰落和小尺度衰落 移动无线信道的主要特征是信道强度关于时间和频率的变化,大致分两种: 大尺度衰落: .

weixin_45662974的博客 3万+

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

Zemax操作41--公差分析(一)

zemax公差

lzy750750的博客 5989

Prometheus-2.53.1安装

Prometheus+Grafana安装

qlj062127的博客 1794

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的博客 903

CE电流法测试中的dBμV负值解析

传导发射(CE)电流法测试中出现的dBμV负值现象表明被测设备干扰电平低于1μV参考基准。这源于对数计算特性(小于1μV的电压转换后为负值)、设备优良的EMC设计(如滤波效果显著)及测试环境的影响(LISN隔离、屏蔽室低噪声)。负值虽不代表实际负电压,但反映干扰水平优于标准限值,为系统集成预留电磁裕度。工程应用中需结合设备灵敏度、环境背景噪声等因素综合分析,避免误判,这类数据可用于评估EMI设计冗余度。

硬核科技 306

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

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

gitblog_01194的博客 406

(王道计算机组成原理)第二章数据的表示和运算-第四节:浮点数的表示与运算 3&4(数据的大小端和对齐存储)

本文主要介绍了计算机系统中两种重要的内存管理概念:大小端模式和内存对齐。大小端模式 涉及多字节数据在内存中的存储顺序,大端模式将最高有效字节存储在低地址,而小端模式则相反,将最低有效字节存储在低地址。现代计算机多采用小端模式,因其在处理运算时更为高效。内存对齐 则是为了提高数据访问效率,确保数据按特定边界(如2字节或4字节)存储,从而减少内存访问次数。虽然对齐可能增加内存占用,但通过空间换时间的方式显著提升了系统性能。

快乐江湖的博客 565

直捣黄龙~

这篇文章描述了一个矩阵寻路问题,要求计算从起点到终点的最短路径数量及长度。主要思路是通过BFS算法解决:首先标记哨兵警戒区域为禁区,然后从起点出发进行广度优先搜索,记录每个位置的最短步数和路径条数。当到达终点时,输出路径数和步数;若不可达则返回(0,0)。算法使用队列实现BFS,同时处理路径统计和步数记录,适用于网格寻路类问题。

面试高手的博客 231

每日GitHub trending热点项目盘点

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

XuJL89的博客 305

手把手教你实现RK3588的Secure Boot:密钥烧录与镜像签名验证

摘要:本文详细介绍了瑞芯微RK3588平台下SecureBoot的密钥烧录与镜像签名验证实现方法。首先阐述了SecureBoot的工作原理、安全优势及其在RK3588智能安防、工业控制等场景中的重要作用。接着分步骤指导了硬件准备、软件工具配置、密钥对生成等准备工作,并详细说明了进入烧录模式、工具选择配置、密钥烧录执行等关键操作流程。文章还解析了镜像签名生成与验证机制,包括启动验证流程和常见故障处理方法。最后对SecureBoot在RK3588设备中的未来应用进行了展望,强调其与TrustZone等安全技术融

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 2057

每日GitHub trending精选

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

XuJL89的博客 273

文档:亲测可用解决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的博客 873

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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的博客 498

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的博客 618

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

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

gitblog_00787的博客 1045
上一篇: 驾驶员安全停车技术全程图解 王淑君编著
下一篇: 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、付费专栏及课程。

余额充值
⌂ 换网址