How to copy static files to build directory with Webpack?
You don't need to copy things around, webpack works different than gulp. Webpack is a module bundler and everything you reference in your files will be included. You just need to specify a loader for that.
So if you write:
var myImage = require("./static/myImage.jpg");
Webpack will first try to parse the referenced file as JavaScript (because that's the default). Of course, that will fail. That's why you need to specify a loader for that file type. The file- or url-loader for instance take the referenced file, put it into webpack's output folder (which should be build
in your case) and return the hashed url for that file.
var myImage = require("./static/myImage.jpg");
console.log(myImage); // '/build/12as7f9asfasgasg.jpg'
Usually loaders are applied via the webpack config:
// webpack.config.js
module.exports = {
...
module: {
loaders: [
{ test: /\.(jpe?g|gif|png|svg|woff|ttf|wav|mp3)$/, loader: "file" }
]
}
};
Of course you need to install the file-loader first to make this work.
How to copy files from public into dist?
You can just setup the plugin copy-webpack-plugin to copy those files, adding this to your webpack.config.js:
plugins: [
new CopyWebpackPlugin([{ from: 'public' }])
]
and the following require: const CopyWebpackPlugin = require('copy-webpack-plugin')
Another solution, if you don't want to do it using webpack, is to use a package to copy those files to the dist folder after the build, adding the following script:
"postprod": "cpx \"public/*\" dist"
and add the package cpx
to your list of devDependencies, running npm install cpx --save-dev
.
Because you added the post
prefix to postprod
, everytime you run the prod
script, npm will automatically run postprod
after it, and therefore, will copy all files inside the public folder to the dist folder. You can read more about npm scripts here
How do I copy a static folder to both dev and build in Vite?
The following changes should be an equivalent to your previous CopyWebpackPlugin
settings:
vite-plugin-static-copy
does not support an explicitignore
option, but you can set thesrc
glob pattern to exclude dotfiles.The
dest
should be'./'
to copy the files into the root of the output directory (default isdist
).
import { defineConfig } from 'vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import path from 'path'
export default defineConfig({
plugins: [
viteStaticCopy({
targets: [
{
src: path.resolve(__dirname, './static') + '/[!.]*', // 1️⃣
dest: './', // 2️⃣
},
],
}),
]
})
demo
Webpack 5: how to disable copying static files to build folder?
UPDATE:
I have some progress. Code disabling touching urls in css files, but fonts still copying:
{
test: /\.css$/,
use: ['vue-style-loader']
},
{
test: /\.css$/,
loader: 'css-loader',
options: {
url: false,
},
},
UPDATE2: Done. Adding:
{
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
type: "asset",
}
finally disabling generate other files. At now generating only /build/build.js
file
Related Topics
Activexobject in Firefox or Chrome (Not Ie!)
Cross-Origin Read Blocking (Corb)
Variable Scope in D3 JavaScript
Prototype Based VS. Class Based Inheritance
Most Efficient Way to Concatenate Strings in JavaScript
How to Create a Jquery Plugin with Methods
JavaScript Calculate the Day of the Year (1 - 366)
Send Message to Specific Client with Socket.Io and Node.Js
How to Select Option in Drop Down Protractorjs E2E Tests
Get All Attributes of an Element Using Jquery
My Very Simple Greasemonkey Script Is Not Running
Load Jquery with JavaScript and Use Jquery
How to Do Associative Array/Hashing in JavaScript
When and Why to 'Return False' in JavaScript