如何确定已安装的webpack版本

时间:2022-03-18 07:00:28

Especially during the transition from webpack v1 to v2, it would be important to programmatically determine what webpack version is installed, but I cannot seem to find the appropriate API.

特别是在从webpack v1过渡到v2期间,以编程方式确定安装了哪个webpack版本非常重要,但我似乎找不到合适的API。

2 个解决方案

#1


51  

Version Installed:

Using webpack CLI: (--version, -v Show version number [boolean])

使用webpack CLI:( - version,-v显示版本号[boolean])

webpack --version

or:

要么:

webpack -v

Using npm list command:

使用npm list命令:

npm list webpack

Results in name@version-range:

结果名称@ version-range:

<projectName>@<projectVersion> /path/to/project
└── webpack@<version-range>

Using yarn list command:

使用纱线列表命令:

yarn list webpack

How to do it programmatically?

Webpack 2 introduced Configuration Types.

Webpack 2引入了配置类型。

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.

您可以返回一个接受环境作为参数的函数,而不是导出配置对象。运行webpack时,您可以通过--env指定构建环境密钥,例如--env.production或--env.platform = web。

We will use a build environment key called --env.version.

我们将使用名为--env.version的构建环境密钥。

webpack --env.version $(webpack --version)

or:

要么:

webpack --env.version $(webpack -v)

For this to work we will need to do two things:

为此,我们需要做两件事:

Change our webpack.config.js file and use DefinePlugin.

更改我们的webpack.config.js文件并使用DefinePlugin。

The DefinePlugin allows you to create global constants which can be configured at compile time.

DefinePlugin允许您创建可在编译时配置的全局常量。

-module.exports = {
+module.exports = function(env) {
+  return {
    plugins: [
      new webpack.DefinePlugin({
+        WEBPACK_VERSION: JSON.stringify(env.version) //<version-range>
      })
    ]
+  };
};

Now we can access the global constant like so:

现在我们可以像这样访问全局常量:

console.log(WEBPACK_VERSION);

Latest version available:

Using npm view command will return the latest version available on the registry:

使用npm view命令将返回注册表中可用的最新版本:

npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]

npm view [<@scope> /] [@ ] [ [。 ] ...]


For webpack use:

对于webpack使用:

npm view webpack version

#2


10  

For those who are using yarn

对于那些使用纱线的人

yarn list webpack will do the trick

纱线列表webpack将成功

$ yarn list webpack
yarn list v0.27.5
└─ webpack@2.6.1
Done in 1.24s.

#1


51  

Version Installed:

Using webpack CLI: (--version, -v Show version number [boolean])

使用webpack CLI:( - version,-v显示版本号[boolean])

webpack --version

or:

要么:

webpack -v

Using npm list command:

使用npm list命令:

npm list webpack

Results in name@version-range:

结果名称@ version-range:

<projectName>@<projectVersion> /path/to/project
└── webpack@<version-range>

Using yarn list command:

使用纱线列表命令:

yarn list webpack

How to do it programmatically?

Webpack 2 introduced Configuration Types.

Webpack 2引入了配置类型。

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.

您可以返回一个接受环境作为参数的函数,而不是导出配置对象。运行webpack时,您可以通过--env指定构建环境密钥,例如--env.production或--env.platform = web。

We will use a build environment key called --env.version.

我们将使用名为--env.version的构建环境密钥。

webpack --env.version $(webpack --version)

or:

要么:

webpack --env.version $(webpack -v)

For this to work we will need to do two things:

为此,我们需要做两件事:

Change our webpack.config.js file and use DefinePlugin.

更改我们的webpack.config.js文件并使用DefinePlugin。

The DefinePlugin allows you to create global constants which can be configured at compile time.

DefinePlugin允许您创建可在编译时配置的全局常量。

-module.exports = {
+module.exports = function(env) {
+  return {
    plugins: [
      new webpack.DefinePlugin({
+        WEBPACK_VERSION: JSON.stringify(env.version) //<version-range>
      })
    ]
+  };
};

Now we can access the global constant like so:

现在我们可以像这样访问全局常量:

console.log(WEBPACK_VERSION);

Latest version available:

Using npm view command will return the latest version available on the registry:

使用npm view命令将返回注册表中可用的最新版本:

npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]

npm view [<@scope> /] [@ ] [ [。 ] ...]


For webpack use:

对于webpack使用:

npm view webpack version

#2


10  

For those who are using yarn

对于那些使用纱线的人

yarn list webpack will do the trick

纱线列表webpack将成功

$ yarn list webpack
yarn list v0.27.5
└─ webpack@2.6.1
Done in 1.24s.