Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy files with copyWebpackPlugin

I'm working with copy files from /src/libs/** to /dist directory using webpack,My config of copyWebpackPlugin is

new copyWebpackPlugin([{
    from: __dirname + '/src/libs/**',
    to: __dirname + '/dist'
}])

but It will generate an extra src folder in dist.I expect my target folder will be /dist/copy-files but it's /dist/src/copy-files.Is there any solutions?

like image 669
zero Avatar asked Apr 16 '18 07:04

zero


2 Answers

You have to differentiate between context and from.

  • context is the root path for the source files, and will not be added to the target paths.
  • from: The paths determined by the from glob, however, will be added to the target paths.

So in your case, try

new copyWebpackPlugin([{
    context: __dirname + '/src',
    from: 'libs/**',
    to: __dirname + '/dist'
}])

instead.

like image 197
Thomas Jacob Avatar answered Oct 04 '22 22:10

Thomas Jacob


If you use an absolute path in to, copy-webpack-plugin will add the resolved from to it.
To get what you want, just name the directory.

new copyWebpackPlugin([{
    from: __dirname + '/src/libs/**',
    to: 'dist'
}])

The from path also doesn't need to be absolute. It won't change the result, but still maybe a bit cleaner.

new copyWebpackPlugin([{
    from: 'src/libs/**',
    to: 'dist'
}])
like image 40
lukas-reineke Avatar answered Oct 04 '22 23:10

lukas-reineke