Ambari 编译打前端包如何提速
# Ambari 编译打前端包如何提速 🚀
在国内开发环境下,使用 npm
安装依赖包的速度经常会受到网络波动影响。通过切换到国内的 npm
镜像源,可以显著加速依赖的安装速度。本文将介绍如何配置 npm
使用国内镜像源加速,并探讨如何将这些加速配置应用于 Ambari 项目,提升构建效率。
# 一、npm 使用国内镜像源加速 🚀
# 1.1 配置 npm 使用国内镜像源 🛠️
为了显著提升安装速度,我们可以将 npm
的下载源更改为国内的镜像源。以下是将 npm
配置为淘宝镜像源的命令:
npm config set registry https://registry.npmmirror.com
这条命令会将 npm
默认的下载源切换为淘宝镜像源,它与官方源同步,并且在国内访问速度更快。💨
# 1.2 验证配置是否生效 ✅
配置完成后,可以使用以下命令确认配置是否成功:
npm config get registry
理想的输出应该是:
https://registry.npmmirror.com/
⚡ 如果看到这个输出,说明你已成功切换到国内镜像源,接下来就可以享受极速的 npm
安装体验了。
# 1.3 临时使用国内镜像源 ⏳
有时你可能只想在特定的项目中使用国内镜像源,而不是全局设置。在这种情况下,可以通过命令行参数指定 --registry
:
npm install package-name --registry=https://registry.npmmirror.com
这种方式适用于不希望修改全局配置,只希望在单次安装时加速速度的情况。💡
# 二、在 Ambari 项目中应用 npm 加速配置 🛠️
# 2.1 在 pom.xml
中配置国内镜像源
我们使用 frontend-maven-plugin
来管理 Node.js 和 npm 的安装与依赖管理。以下是 pom.xml
中的配置:
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.11.3</version>
<executions>
<execution>
<id>install node and npm</id>
<phase>generate-sources</phase>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<npmDownloadRoot>https://registry.npmmirror.com/npm/-/</npmDownloadRoot>
<nodeDownloadRoot>https://cdn.npmmirror.com/binaries/node/</nodeDownloadRoot>
</configuration>
</execution>
<execution>
<id>npm install</id>
<phase>generate-sources</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install --unsafe-perm --progress --save --verbose</arguments>
<npmRegistryURL>https://registry.npmmirror.com/</npmRegistryURL>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 2.2 关键配置解读 📖
在 pom.xml
配置中,我们使用了几个重要的配置项:
npmDownloadRoot 和 nodeDownloadRoot:这两个配置项用于指定
npm
和Node.js
的下载源,确保从国内镜像获取资源,提升安装速度。npmDownloadRoot:用于配置 npm 包下载的根地址。通过配置国内源,npm 安装时的包下载会从国内镜像获取,避免网络延迟。
nodeDownloadRoot:用于配置 Node.js 的下载源,确保 Node.js 在安装时使用国内镜像源,提高下载速度并保证稳定性。
npmRegistryURL:指定
npm install
命令使用的镜像源。通过将npmRegistryURL
配置为国内源,可以加速 npm 包的安装过程。
# 2.3 执行日志示例 📄
配置完成后,在执行 Maven 构建时,你会看到如下日志输出:
[INFO] --- frontend-maven-plugin:1.11.3:install-node-and-npm (install node and npm) @ my-project ---
[INFO] Installing node version v16.0.0
[INFO] Downloading Node.js from https://cdn.npmmirror.com/binaries/node/v16.0.0/node-v16.0.0-linux-x64.tar.gz
[INFO] Installed node version v16.0.0
[INFO] Installing npm version 7.10.0
[INFO] Downloading npm from https://registry.npmmirror.com/npm/-/npm-7.10.0.tgz
[INFO] Installed npm version 7.10.0
2
3
4
5
6
7
可以看到,Node.js 和 npm 都是从国内镜像下载的,极大加快了构建速度。⚡