aspnet core如何在deploy上构建webpack

我有一个带有 vue.js 部分的 aspnet 核心应用程序,我使用 webpack 构建它并且一切正常.现在我搜索一个解决方案来创建一个生产构建,创建我的 vue 包的缩小版本并在生产模式下运行 vue(没有 console.logs),这是我在我的 webpack.config 中设置的喜欢:

I have a aspnet core application with a vue.js part, which I build with webpack and everything works fine. Now I search a solution to create a productionbuild with it, to create a minified version of my vue bundle and to run vue in production mode (without console.logs), which I set in my webpack.config like:

if (process.env.NODE_ENV === 'production') {
    module.exports.plugins = [
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        })
    ]
} else {
    module.exports.devtool = '#source-map'
}

但是 process.env.NODE_ENV 当我通过 gulp 对其进行 webpack 时总是未定义.比我尝试在 startup.cs 中将 Microsoft.AspNetCore.SpaServices 与这一行一起使用:

but process.env.NODE_ENV is always undefined when I webpack it via gulp. Than I tried to use Microsoft.AspNetCore.SpaServices with this line in startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
    if(env.IsDevelopment())
        app.UseWebpackDevMiddleware();
...
}

这也很好,就像我的 gulp 配置和 process.env.NODE_ENV 属性设置为 'development'

This works also fine like my gulp configuration and process.env.NODE_ENV property is set to 'development'

现在我尝试在 VS 2017 中创建生产版本(Build -> Publish Projectname),但没有运行 webpack 任务.

Now I try to create a production build in VS 2017 (Build -> Publish Projectname), but no webpack task runs.

所以我尝试在我的 *.csproj 中添加它:

So i tried to add this in my *.csproj:

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <Exec Command="npm run build" />
</Target>

这会引发错误:命令npm run build"以代码 1 退出.

this throws the error: The command "npm run build" exited with code 1.

现在我没有其他想法来解决它,希望任何人都可以帮助我.谢了!

Now I'm out of other ideas to resolve it and hope that anyone can help me. THX!

推荐答案

解决方法:在 .csproj:

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <Exec Command="npm install" />
    <Exec Command="npm run production" />
</Target>

并在我的 package.json 中添加脚本:

and adding scripts in my package.json:

"scripts": {
    "dev": "cross-env NODE_ENV=development webpack --hide-modules",
    "production": "cross-env NODE_ENV=production webpack --hide-modules"
}

这需要 webpack &cross-env 包(以及 webpack 期间使用的所有其他包)已安装 &一个工作的 webpack.config.js

this needs webpack & cross-env packages (and all other used packages during webpack) installed & a working webpack.config.js

询问是否有人对我的 webpack.config.js 或其他代码感兴趣

Ask if someone is interrested to my webpack.config.js or some other code

相关文章