copy-webpack-plugin不复制文件

时间:2018-09-24 11:53:26

标签: javascript reactjs webpack

我尝试仅复制文件以检查简单的webpack配置。因此,我坚持尝试使copy-webpack-plugin正常工作-什么也没发生:没有复制的文件,没有错误,没什么

常用配置(webpack.common.js):

const path = require('path');

const CopyWebpackPlugin = require('copy-webpack-plugin');

const postCssPlugin = [
  require('postcss-import'),
  require('postcss-nested'),
  require('postcss-simple-vars'),
  require('autoprefixer')({
    browsers: [
      'last 3 versions',
      'android 4.2'
    ]
  })
];

module.exports = {
  context: path.resolve(__dirname, '../src'),
  entry: [
    'babel-polyfill',
    path.resolve(__dirname, '../src/index.js')
  ],
  output: {
    path: path.resolve(__dirname, '../dist/js'),
    publicPath: '',
    filename: 'app.js'
  },
  resolve: {
    extensions: ['.jsx', '.js', '.json']
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.p?css$/,
        use: [
          'style-loader',
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              ident: 'postcss',
              plugins: postCssPlugin
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../src/assets/**/*'),
        to: path.resolve(__dirname, '../dist/assets'),
        flatten: true
      }
    ])
  ],
  stats: {
    modules: true,
    warnings: false,
    version: true,
    timings: true,
    performance: false,
    hash: true,
    errors: true,
    errorDetails: true,
    colors: true,
    builtAt: true
  }
};

webpack.prod.js:

const commonWebpackConfig = require('./webpack.common.js');

const UglifyJsWebpackPlugin = require('uglifyjs-webpack-plugin');

module.exports = Object.assign(commonWebpackConfig, {
  mode: 'production',
  plugins: [
    new UglifyJsWebpackPlugin({
      sourceMap: true
    })
  ]
});

并构建开始文件build.js:

const webpack = require('webpack');

const webpackProdConfig = require('./webpack.config/webpack.prod.js');

webpack(webpackProdConfig, (err, stats) => {
  if (err || stats.hasErrors()) {
    console.error(err.stack || err);
  }

  console.log('Successfully compiled!');
});

那么谁能弄清楚为什么它不起作用,我在哪里错了? 复制webpack-plugin:4.5.2 节点:9.1.0 npm:6.3.0 Windows:10

添加-文件夹结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试从dist文件夹复制。对我来说很有效 es。

new CopywebpackPlugin([{
    from: path.resolve(__dirname, 'node_modules/mettrr-component-library/dist/img'),
    to: path.resolve(__dirname, 'src/assets/img')
}]),