AngularClass/angular2-webpack-starter:从用于 PROD 构建的 webpack 配置中排除模拟文件 [英] AngularClass/angular2-webpack-starter : exclude mock files from webpack config for PROD builds

查看:27
本文介绍了AngularClass/angular2-webpack-starter:从用于 PROD 构建的 webpack 配置中排除模拟文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AngularClass/angular2-webpack-starter 并且我'一直在努力使用 webpack 配置,尽管我想要实现的目标看起来很简单.

I'm using the AngularClass/angular2-webpack-starter and I've been struggling with its webpack configuration for a while, although what I want to achieve seems very simple.

基本上,我在 src\assets\mock\data 中有一些 mock 文件,我想将它们保留在 dev构建,但我想从 prod 构建中排除.这些 mock 文件应该根据它们名称上的正则表达式从 prod 构建中排除(我尝试了几种类似 exclude: [/^mock\.data\..*\.json$/],或按文件夹,或至少列出所有.

Basically, I have some mock files located in src\assets\mock\data that I'd like to keep in the dev build, but that I want to exclude from the prod build. These mock files should be excluded from the prod build according to a regexp on their name (I tried several flavours of something like exclude: [/^mock\.data\..*\.json$/], or by folder, or at the very least by listing them all.

dev 构建使用 webpack.dev.js 很好,但我一直在修改 webpack.prod.js 文件直到现在都无济于事.

The dev build using webpack.dev.js is fine, but I've been tinkering with the webpack.prod.js file to no avail until now.

有人能指出我正确的方向吗?谢谢.

Could someone point me in the right direction ? Thank you.

推荐答案

Context: angular2-webpack-starter

在构建任务期间重新复制资产文件夹由 webpack 插件处理,又名 CopyWebpackPlugin.默认情况下,它仅在 webpack.common.js 中设置.为了实现您的目标,您应该在两个环境中放置相同的任务.这样您就可以排除不需要的文件.

The assets folder recopy during the build task is handle by a webpack plugin aka CopyWebpackPlugin. By default it is only setup in the webpack.common.js. To achieve what you are trying to, you should put the same task in both of your environments. That way you would be able to exclude the unwanted files.

  1. webpack.common.js

  1. webpack.common.js

new CopyWebpackPlugin([
  { from: 'src/meta'}
])

  • webpack.dev.js

  • webpack.dev.js

    new CopyWebpackPlugin([
      { from: 'src/assets', to: 'assets' }
    ])
    

  • webpack.prod.js

  • webpack.prod.js

    new CopyWebpackPlugin([
      { from: 'src/assets', to: 'assets', ignore: ['mock/data/*'] }
    ])
    

  • 这篇关于AngularClass/angular2-webpack-starter:从用于 PROD 构建的 webpack 配置中排除模拟文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆