如何判断安装的webpack版本
How to determine the installed webpack version
特别是在从 webpack v1 到 v2 的过渡期间,以编程方式确定安装的 webpack 版本很重要,但我似乎找不到合适的 API.
安装的版本:
使用webpack CLI:(--version, -v Show version number [boolean])
webpack --version
或:
webpack -v
使用npm list命令:
npm list webpack
结果 name@version-range
:
<projectName>@<projectVersion> /path/to/project
└── webpack@<version-range>
使用 yarn list 命令:
yarn list webpack
如何以编程方式进行?
引入了 Webpack 2 Configuration Types。
Instead of exporting a configuration object, you may return a function
which accepts an environment as argument. When running webpack, you
may specify build environment keys via --env
, such as --env.production
or --env.platform=web
.
我们将使用名为 --env.version
的构建环境密钥。
webpack --env.version $(webpack --version)
或:
webpack --env.version $(webpack -v)
为此,我们需要做两件事:
更改我们的 webpack.config.js
文件并使用 DefinePlugin.
The DefinePlugin allows you to create global constants which can be
configured at compile time.
-module.exports = {
+module.exports = function(env) {
+ return {
plugins: [
new webpack.DefinePlugin({
+ WEBPACK_VERSION: JSON.stringify(env.version) //<version-range>
})
]
+ };
};
现在我们可以像这样访问全局常量:
console.log(WEBPACK_VERSION);
可用的最新版本:
使用 npm view 命令将 return 注册表中可用的最新版本:
npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]
对于 webpack 使用:
npm view webpack version
对于正在使用 yarn
的用户
yarn list webpack
会成功
$ yarn list webpack
yarn list v0.27.5
└─ webpack@2.6.1
Done in 1.24s.
webpack 4现在提供了一个版本属性可以使用!
如果使用 Angular CLI v7+,webpack 版本会打印在 ng version
的输出中:
-> ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.0.6
Node: 11.0.0
OS: darwin x64
Angular: 7.1.0
... animations, cdk, common, compiler, compiler-cli, core, forms
... http, language-service, material, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.10.6
@angular-devkit/build-angular 0.10.6
@angular-devkit/build-optimizer 0.10.6
@angular-devkit/build-webpack 0.10.6
@angular-devkit/core 7.0.6
@angular-devkit/schematics 7.0.6
@angular/cli 7.0.6
@ngtools/webpack 7.0.6
@schematics/angular 7.0.6
@schematics/update 0.10.6
rxjs 6.3.3
typescript 3.1.6
webpack 4.19.1
另一种方式尚未提及:
如果您在本地将其安装到项目中,请打开 node_modules 文件夹并检查您的 webpack 模块。
< /node_modules/webpack/package.json
打开package.json文件并查看版本
将webpack -v
放入你的package.json:
{
"name": "js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack -v",
"dev": "webpack --watch"
}
}
然后在控制台输入:
npm run build
预期输出应如下所示:
> npm run build
> js@1.0.0 build /home/user/repositories/myproject/js
> webpack -v
4.42.0
在 CLI 中
$ webpack --version
webpack-cli 4.1.0
webpack 5.3.2
在代码中(节点运行时)
process.env.npm_package_devDependencies_webpack // ^5.3.2
或
process.env.npm_package_dependencies_webpack // ^5.3.2
在插件中
compiler.webpack.version // 5.3.2
特别是在从 webpack v1 到 v2 的过渡期间,以编程方式确定安装的 webpack 版本很重要,但我似乎找不到合适的 API.
安装的版本:
使用webpack CLI:(--version, -v Show version number [boolean])
webpack --version
或:
webpack -v
使用npm list命令:
npm list webpack
结果 name@version-range
:
<projectName>@<projectVersion> /path/to/project
└── webpack@<version-range>
使用 yarn list 命令:
yarn list webpack
如何以编程方式进行?
引入了 Webpack 2 Configuration Types。
Instead of exporting a configuration object, you may return a function which accepts an environment as argument. When running webpack, you may specify build environment keys via
--env
, such as--env.production
or--env.platform=web
.
我们将使用名为 --env.version
的构建环境密钥。
webpack --env.version $(webpack --version)
或:
webpack --env.version $(webpack -v)
为此,我们需要做两件事:
更改我们的 webpack.config.js
文件并使用 DefinePlugin.
The DefinePlugin allows you to create global constants which can be configured at compile time.
-module.exports = {
+module.exports = function(env) {
+ return {
plugins: [
new webpack.DefinePlugin({
+ WEBPACK_VERSION: JSON.stringify(env.version) //<version-range>
})
]
+ };
};
现在我们可以像这样访问全局常量:
console.log(WEBPACK_VERSION);
可用的最新版本:
使用 npm view 命令将 return 注册表中可用的最新版本:
npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]
对于 webpack 使用:
npm view webpack version
对于正在使用 yarn
yarn list webpack
会成功
$ yarn list webpack
yarn list v0.27.5
└─ webpack@2.6.1
Done in 1.24s.
webpack 4现在提供了一个版本属性可以使用!
如果使用 Angular CLI v7+,webpack 版本会打印在 ng version
的输出中:
-> ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.0.6
Node: 11.0.0
OS: darwin x64
Angular: 7.1.0
... animations, cdk, common, compiler, compiler-cli, core, forms
... http, language-service, material, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.10.6
@angular-devkit/build-angular 0.10.6
@angular-devkit/build-optimizer 0.10.6
@angular-devkit/build-webpack 0.10.6
@angular-devkit/core 7.0.6
@angular-devkit/schematics 7.0.6
@angular/cli 7.0.6
@ngtools/webpack 7.0.6
@schematics/angular 7.0.6
@schematics/update 0.10.6
rxjs 6.3.3
typescript 3.1.6
webpack 4.19.1
另一种方式尚未提及:
如果您在本地将其安装到项目中,请打开 node_modules 文件夹并检查您的 webpack 模块。
< /node_modules/webpack/package.json
打开package.json文件并查看版本
将webpack -v
放入你的package.json:
{
"name": "js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack -v",
"dev": "webpack --watch"
}
}
然后在控制台输入:
npm run build
预期输出应如下所示:
> npm run build
> js@1.0.0 build /home/user/repositories/myproject/js
> webpack -v
4.42.0
在 CLI 中
$ webpack --version
webpack-cli 4.1.0
webpack 5.3.2
在代码中(节点运行时)
process.env.npm_package_devDependencies_webpack // ^5.3.2
或
process.env.npm_package_dependencies_webpack // ^5.3.2
在插件中
compiler.webpack.version // 5.3.2