問題描述
我有一個帶有 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
這篇關于aspnet core如何在deploy上構建webpack的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!