問(wèn)題描述
我有一個(gè)帶有 vue.js 部分的 aspnet 核心應(yīng)用程序,我使用 webpack 構(gòu)建它并且一切正常.現(xiàn)在我搜索一個(gè)解決方案來(lái)創(chuàng)建一個(gè)生產(chǎn)構(gòu)建,創(chuàng)建我的 vue 包的縮小版本并在生產(chǎn)模式下運(yùn)行 vue(沒(méi)有 console.logs),這是我在我的 webpack.config
中設(shè)置的喜歡:
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
當(dāng)我通過(guò) gulp 對(duì)其進(jìn)行 webpack 時(shí)總是未定義.比我嘗試在 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
屬性設(shè)置為 'development'
This works also fine like my gulp configuration and process.env.NODE_ENV
property is set to 'development'
現(xiàn)在我嘗試在 VS 2017 中創(chuàng)建生產(chǎn)版本(Build -> Publish Projectname),但沒(méi)有運(yùn)行 webpack 任務(wù).
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>
這會(huì)引發(fā)錯(cuò)誤:命令npm run build"以代碼 1 退出.
this throws the error: The command "npm run build" exited with code 1.
現(xiàn)在我沒(méi)有其他想法來(lái)解決它,希望任何人都可以幫助我.謝了!
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 期間使用的所有其他包)已安裝 &一個(gè)工作的 webpack.config.js
this needs webpack
& cross-env
packages (and all other used packages during webpack) installed & a working webpack.config.js
詢(xún)問(wèn)是否有人對(duì)我的 webpack.config.js
或其他代碼感興趣
Ask if someone is interrested to my webpack.config.js
or some other code
這篇關(guān)于aspnet core如何在deploy上構(gòu)建webpack的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!