copyPlugin.js 868 Bytes
Newer Older
邓晓峰's avatar
邓晓峰 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/* eslint-disable */
const { existsSync } = require('fs');
const { join } = require('path');

module.exports = async function addCopyPlugin(opts) {
  const { config, userConfig, cwd } = opts;
  const copyPatterns = [
    existsSync(join(cwd, 'public')) && {
      from: join(cwd, 'public'),
    },
    ...(userConfig.copy
      ? userConfig.copy.map(item => {
        if (typeof item === 'string') {
            return {
              from: join(cwd, item),
              to: item,
            };
          }
          return {
            from: join(cwd, item.from),
            to: item.to,
          };
        })
      : []),
  ].filter(Boolean);

  if (copyPatterns.length) {
    // eslint-disable-next-line global-require
    config.plugin('copy').use(require('../compiled/copy-webpack-plugin'), [
      {
        patterns: copyPatterns,
      },
    ]);
  }
};