Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a directory with Webpack?

Tags:

webpack

I've been playing around with Webpack to try and understand how it works but no matter what I do it never seems to do what I expect it to considering what the documentation and numerous videos seem to say.

As a first step to accomplishing my end goal, I simply want to copy all the files in the ./src directory to the ./dist directory, maintaining the full folder structure. After a bit of digging it seems what I need is something like this in the rules for my webpack.config.js (I currently only have html, css and js files in the source directory for testing purposes):

test: /\.(html|js|css)$/,
use: [
    {
        loader: 'file-loader',
        options: {
            name: '[path][name].[ext]'
        }
    }
]

Now in my entry.js file which is in the root directory I have this:

function requireAll (r) { r.keys().forEach(r); }
requireAll(require.context('./src/', true, /\.(html|js|css)$/));

...because from my understanding I need to import all the files I want to process into my entry file so they can then be placed into the new directory using file-loader.

With this setup, running webpack creates a dist folder like this:

dist/
    chunk.js     <-- (the output filename set in my webpack config)
    entry.js     <-- (an exact duplicate of the entry file)

This is obviously not what I want. I don't know whether I just need to tweak my setup or if I'm doing this completely wrong.

like image 264
Aaron Beaudoin Avatar asked Nov 26 '17 00:11

Aaron Beaudoin


People also ask

What does copy webpack plugin do?

With no import/require statements for our image resources in javascript, webpack would not process the images as part of bundling as it won't see those as dependencies. In such cases, we can configure webpack to copy all our image resources to the dist folder using 'CopyWebpackPlugin'.

What is write file webpack plugin?

Forces webpack-dev-server program to write bundle files to the file system. This plugin has no effect when webpack program is used instead of webpack-dev-server .

What is a webpack plugin?

A webpack plugin is a JavaScript object that has an apply method. This apply method is called by the webpack compiler, giving access to the entire compilation lifecycle.

How do I copy a static file in Webpack?

However, if you need greater flexibility or want a cleaner interface, you can also copy static files directly using my copy-webpack-plugin ( npm, Github ). For your static to build example: Compatibility note: If you're using an old version of webpack like [email protected], use [email protected]. Otherwise use latest.

How do I use copy-Webpack-plugin to copy files from the build process?

To begin, you'll need to install copy-webpack-plugin: Then add the plugin to your webpack config. For example: ℹ️ copy-webpack-plugin is not designed to copy files generated from the build process; rather, it is to copy files that already exist in the source tree, as part of the build process.

How to write files to the output directory during development in Webpack?

ℹ️ If you want webpack-dev-server to write files to the output directory during development, you can force it with the writeToDisk option or the write-file-webpack-plugin. ℹ️ You can get the original source filename from Asset Objects. Glob or path from where we copy files. Output path.

How to include references in Webpack files?

Webpack is a module bundler and everything you reference in your files will be included. You just need to specify a loader for that. Webpack will first try to parse the referenced file as JavaScript (because that's the default). Of course, that will fail.


1 Answers

You can use the copy webpack plugin.

npm install --save-dev copy-webpack-plugin

Edit your webpack.config.js file:

//Add a plugin declaration at the begining of file
const CopyWebpackPlugin = require('copy-webpack-plugin');

//...

//search (or create) the plugin array. 
plugins: [
    // ...
    // Add your plugin
    new CopyWebpackPlugin([

        // Copy directory contents to {output}/to/directory/
        //{ from: 'from/directory', to: 'to/directory' },
        { from: 'src', to: 'dist' },
    ]
  ],

There is a lot of simple examples in documentation

like image 93
Alexandre Tranchant Avatar answered Oct 03 '22 14:10

Alexandre Tranchant