使用MSBuild任务将整个文件夹复制到wwwroot

时间:2019-07-06 13:12:48

标签: asp.net-core asp.net-core-2.2

我有以下内容:

<Target Name="OnBuild" BeforeTargets="Build" Condition="'$(Configuration)' == 'Debug'">
  <Exec WorkingDirectory="app" Command="npm install" />
  <Exec WorkingDirectory="app" Command="npm run build" />
</Target>

命令“ npm run build”创建文件夹“ client / dist”。

如何使用MSBuild任务将文件夹及其内容复制到“ wwwroot”?

我正在使用ASP.NET Core 2.2 ...

Webpack.config

const path = require('path');

const environment = process.env.NODE_ENV;

const CopyPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {

  plugins: [
    new MiniCssExtractPlugin({
      filename: 'main.min.css',
    }),
    new CopyPlugin([
      { from: './assets', to: './assets' }
    ])
  ],

  resolve: {
    extensions: ['.js'],
  },

  entry: path.resolve(__dirname, 'src/app/index.js'),  

  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.min.js'
  },

  optimization: {
    minimizer: [
      new MiniCssExtractPlugin({}),
      new OptimizeCSSAssetsPlugin({}),      
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: false
      })
    ]
  },

  module: {
    rules: [
      {
        test: /\.less$/,
        use: [ MiniCssExtractPlugin.loader, 'css-loader', 'less-loader' ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [ 'file-loader' ]
      },
      {
        test: /\.(otf|eot|ttf|woff|woff2)$/,
        use: [ 'file-loader' ]
      }
    ]
  },

  watch: true

};

2 个答案:

答案 0 :(得分:1)

您可以使用它并为所需的相对路径修改RelativePath:

<!-- Include the newly-built files in the publish output -->
<ItemGroup>
  <DistFiles Include="$(SpaRoot)build\**" />
  <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
    <RelativePath>wwwroot/%(RecursiveDir)%(Filename)%(Extension)</RelativePath>
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
  </ResolvedFileToPublish>
</ItemGroup>

答案 1 :(得分:0)

YourProject.csproj标签内的Target内添加以下行:

<!-- Include the newly-built files in the publish output -->

<ItemGroup>
  <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
  <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
    <RelativePath>%(DistFiles.Identity)</RelativePath>
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  </ResolvedFileToPublish>
</ItemGroup>

PS。当我们在SPA

中启动新的ASP.Net Core项目时,这段代码自动生成。