我有一个带有 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模板网!
Browserify,Babel 6,Gulp - 传播运算符上的意外令牌Browserify, Babel 6, Gulp - Unexpected token on spread operator(Browserify,Babel 6,Gulp - 传播运算符上的意外令牌)
是否可以将标志传递给 Gulp 以使其以不同的方式Is it possible to pass a flag to Gulp to have it run tasks in different ways?(是否可以将标志传递给 Gulp 以使其以不同的方式运行任务
为什么我们需要在全局和本地安装 gulp?Why do we need to install gulp globally and locally?(为什么我们需要在全局和本地安装 gulp?)
如何一个接一个地依次运行 Gulp 任务How to run Gulp tasks sequentially one after the other(如何一个接一个地依次运行 Gulp 任务)
由于 MIME 类型而未加载样式表Stylesheet not loaded because of MIME-type(由于 MIME 类型而未加载样式表)
使用 gulp-nodemon & 时无法使用 Ctrl+C 停止 GCannot stop Gulp with Ctrl+C when using gulp-nodemon amp; gulp.watch together(使用 gulp-nodemon amp; 时无法使用 Ctrl+C 停止 Gulpgulp.watch 一起