首页 文章

在VSTS中使用powershell运行webpack

提问于
浏览
1

使用this post作为起点我遵循了那里概述的基本步骤 .

  • 在代理上安装webpack

  • 获取webpack的安装路径

  • 从确切的安装路径运行webpack

我在本地测试了它,脚本工作正常 . 一旦我把它作为构建的一部分放在VSTS上(只是一个代理上的PowerShell任务),它就可以运行,直到我执行webpack命令,它什么都不返回 . 没有任何输出 .

Write-Host `nInstalling webpack and webpack-command
npm install --no-save --no-package-lock -g webpack webpack-command

Write-Host `nGetting the environment variables for webpack
(Get-Item -Path ".\" -Verbose).FullName
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
$webpackOptions = Get-Command -CommandType Application -ErrorAction SilentlyContinue -Name webpack | Select-Object -ExpandProperty Definition

Write-Host `nThe options:
$webpackOptions | Write-Host

Write-Host `nSelected:
$webpack = $webpackOptions -Match 'cmd' | Select-Object -First 1
Write-Host $webpack

# On the build agent it is at this point that I get blank results
# On my local machine I get the paths such as:
#    C:\Users\Admin\AppData\Roaming\npm\webpack.cmd --config webpack.css.config.js
# The results of webpack running
Write-Host `nPacking webpack.config.cs
Write-Host $webpack --config webpack.config.js
& $webpack --config webpack.config.js

# Same as above
Write-Host `nPacking webpack.css.config.cs
Write-Host $webpack --config webpack.css.config.js
& $webpack --config webpack.css.config.js

有没有人有任何想法?这对我来说特别奇怪,因为当我打印webpack的 selected 路径时,它似乎找到了我期望的值,就是它打印 C:\npm\prefix\webpack.cmd

1 回答

  • 0

    我有一个工作,我正在使用 npmpackage.json 来运行脚本并安装依赖项 .

    • npm install webpack webpack-cli --save-dev - 这些是你运行webpack所需的依赖项,你可以用 webpack-cli 交换 webpack-cli ,如果你想要的话,它显然更轻

    • 这是在本地完成的,而不是构建步骤 . 更新package.json时检查它 .

    • package.json 中创建构建步骤 . 我创建了两个: buildbuild-prod

    • 在vsts构建中创建两个npm步骤 .

    • 第一个只是 npm install 来安装你的所有包

    • 第二个是运行package.json脚本的自定义步骤 . 就我而言,文字是 run build-prod

    而已!


    带有两个脚本的 package.json 我必须调用webpack:

    {
      ...
      "scripts": {
        "build": "webpack --config webpack.config.js && webpack --config webpack.2.config.js",
        "build-prod": "webpack -p --config webpack.config.js && webpack -p --config webpack.2.config.js"
      }
      ...
    }
    

    并且,最后是任务的图片 .

    enter image description here

相关问题