user12102894
user12102894

Reputation:

How can I copy files from one location to another as one of the steps in a Webpack build?

After webpack detects a file change and creates a bundle.js I also want webpack to copy the file as such:

cp dir1/bundle.js dir2/bundle.js

I have tested the cp command manually but I wanted webpack to do this for me.

Currently my configuration file looks like this and it works fine as well.

const path = require('path');
const SRC_DIR = path.join(__dirname, '/source');
const DIST_DIR = path.join(__dirname, '/dist');
const webpack = require('webpack');
const exportFunc = ( env ) => {
  return {
    entry: `${SRC_DIR}/index.jsx`,
    output: {
      filename: 'bundle.js',
      path: DIST_DIR
    },
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [ 'style-loader', 'css-loader' ]
        },
        {
          test: /\.jsx?/,
          include: SRC_DIR,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/preset-env', '@babel/preset-react'],
              plugins: ['@babel/plugin-proposal-object-rest-spread', '@babel/plugin-proposal-class-properties'],
            }
          }
        }
      ]
    }
  };
};

module.exports = exportFunc;

Upvotes: 0

Views: 38

Answers (1)

givanse
givanse

Reputation: 14953

Seems like the copy plugin is all that you need. Docs: https://webpack.js.org/plugins/copy-webpack-plugin/

In your config you would add:

return {
  plugins: [
    new CopyPlugin({
      {from: 'dir1/bundle.js', to: 'dir2'}
    });
  ]
};

Upvotes: 1

Related Questions