copy-webpack-plugin icon indicating copy to clipboard operation
copy-webpack-plugin copied to clipboard

[Feature] allow copy files before and after webpack build

Open factoidforrest opened this issue 7 years ago • 8 comments

My entry point is inside the directory that has been copied. This plugin seems to use the webpack file emit at the end to output the files. Is there a way to do this sooner? Or to somehow target an entrypoint inside the copied directory?

factoidforrest avatar Dec 18 '17 14:12 factoidforrest

cp src/* dist/* && webpack

Sry, this plugin is an integration of cp for webpack during runs, there is no way to start/use a webpack plugin without actually starting webpack

michael-ciniawsky avatar Dec 18 '17 15:12 michael-ciniawsky

But you could hook into the compilation process before entrypoints are resolved. I ended up doing it like this:

const EventHooksPlugin = require('event-hooks-webpack-plugin');
const fs = require('fs-extra')

//in webpack plugins
      new EventHooksPlugin({
        'before-run': (compilation, done) => {
          console.log('Copying source files to compiled')
          fs.copy('src', 'compiled', done);
        }
      }),

factoidforrest avatar Dec 18 '17 15:12 factoidforrest

Sry, I overread entrypoints :), (maybe) possible, but not supported atm

michael-ciniawsky avatar Dec 18 '17 15:12 michael-ciniawsky

@light24bulbs Thank you, your comment helped out a lot!

stereokai avatar Mar 22 '18 11:03 stereokai

@evilebottnawi

It would be nice to have an option like event (either in options or pattern config) which will decide when the copy is executed.

appsparkler avatar Mar 26 '19 00:03 appsparkler

PR welcome

alexander-akait avatar Mar 26 '19 08:03 alexander-akait

I would love to see this as a simple option to copy-webpack-plugin as well rather than a more complicated hook.

Our scenario is as follows. We have a stand alone web project that acts as a host for a playground to design components. For the webpack dev server to work the dist folder needs to be relative. But for the production build (or actually after it) i want to copy the output to another folder where a different web server will serve the files.

The structure looks like.

\build -- Production web server serves from here \components-web\ -- stand alone webpack dev server project. \components-web\src\ --main source \components-web\dist -- webpack output directory

Basically i just need to copy \components-web\dist to \build after the build is done.

Setting the output path to this works for the prod build but will also break the webpack dev server. I believe because it changes the relative path. Really the better approach is to copy after the build though.

 config.output = {
    path: __dirname + '/../build/dist',
}

rburnham52 avatar May 03 '19 04:05 rburnham52

possible workaround is to use WebpackShellPlugin https://stackoverflow.com/questions/36773619/webpack-execute-function-before-build-starts

Qvatra avatar Jun 28 '21 15:06 Qvatra