Skip to content

Instantly share code, notes, and snippets.

@Sudheer-Reddy
Last active April 12, 2021 14:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sudheer-Reddy/5a0d8b8752e202ae1adecf360f9cf478 to your computer and use it in GitHub Desktop.
Save Sudheer-Reddy/5a0d8b8752e202ae1adecf360f9cf478 to your computer and use it in GitHub Desktop.
Copy static assets in Webpack
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
const PATHS = {
src: path.join(__dirname, 'src'), //absolute path to RepoDir/src
dist: path.join(__dirname, 'dist') //absolute path to RepoDir/dist
}
module.exports = {
entry: {
//Webpack will automatically resolve it to RepoDir/src/js/index.js (if file name is not specified)
//main: PATHS.src + '/js/app.js' //Webpack will resolve it to RepoDir/src/js/app.js
main: PATHS.src + '/js'
},
output: {
//Will resolve to RepoDir/dist
path: PATHS.dist,
//[name] is placeholder for entry ie.. main from line `12`
//So output file name will be main<somehash>.js
filename: 'js/[name][hash].js'
},
plugins: [
new CopyWebpackPlugin([
{
//Note:- No wildcard is specified hence will copy all files and folders
from: 'src/assets', //Will resolve to RepoDir/src/assets
to: 'assets' //Copies all files from above dest to dist/assets
},
{
//Wildcard is specified hence will copy only css files
from: 'src/css/*.css', //Will resolve to RepoDir/src/css and all *.css files from this directory
to: 'css'//Copies all matched css files from above dest to dist/css
}
])
]
}
@luidgigromat
Copy link

Hi,

The plugin must be initialized with "patterns" property:

    new CopyWebpackPlugin({
      patterns: [
        {
          from: 'src/assets',
          to: 'assets' // copies all files to dist/assets
        },
        {
          from: 'src/libs',
          to: 'libs'
        }
      ]
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment