wendt88
wendt88

Reputation: 644

aspnet core how to webpack build on deploy

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'
}

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();
...
}

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

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

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

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

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!

Upvotes: 9

Views: 5336

Answers (1)

wendt88
wendt88

Reputation: 644

Resolved using: in .csproj:

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

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"
}

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

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

Upvotes: 13

Related Questions