aspnet核心如何在部署时进行webpack构建 [英] aspnet core how to webpack build on deploy
问题描述
我有一个带vue.js部分的aspnet核心应用程序,我使用webpack进行了构建,并且一切正常.
现在,我搜索一个解决方案以使用它创建一个productionbuild,创建我的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'
}
但是当我通过gulp将其打包时,process.env.NODE_ENV
始终是未定义的.
比起我尝试在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();
...
}
这也可以像我的口水配置一样正常工作,并且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>
这将引发错误:使用代码1退出了命令"npm run build".
this throws the error: The command "npm run build" exited with code 1.
现在我没有其他想法可以解决它了,希望任何人都可以帮助我. THX!
Now I'm out of other ideas to resolve it and hope that anyone can help me. THX!
推荐答案
使用以下方法解决:
在.csproj
:
Resolved using:
in .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
这篇关于aspnet核心如何在部署时进行webpack构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!