Files copied to '/static' using CopyWebpackPlugin are successfully created, but then are deleted right after the console messages 'Building page ...'.
Files copied by CopyWebpackPlugin to '/static' don't subsequently get removed by the build process.
Files copied by CopyWebpackPlugin to '/static' get copied, then deleted by the build process.
Run npm run dev with the following next.config.js (note that I'm copying a file from the workbox-sw npm package, but it can be any file):
const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = {
webpack: (config, { dev }) => {
config.plugins.push(
new CopyWebpackPlugin([
{
from: path.join(__dirname, 'node_modules/workbox-sw/build/importScripts/workbox-sw.dev.v2.1.2.js'),
to: path.join(__dirname, 'static/scripts/workbox.js')
}
]))
return config
}
}
Can't copy files at build time.
| Tech | Version |
|---------|---------|
| next | 4.1.4 |
| node | 9.2.0 |
| OS | OS X 10.11.6 |
| browser | Chrome 62 |
| etc | |
Wasn't able to reproduce in my project - static files were copied and were present after build.
But I linked static files in _document.js as per comment in this issue.
---update
Indeed, when using npm run dev it deletes files, while next build && next start doesn't.
I have the same problem. it deletes files when using npm run dev.
while next build && next start doesn't <-- but no hot reloading..
webpack is ran twice, once for the server, once for the client, the solution might be as simple as:
const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = {
webpack: (config, { dev, isServer }) => {
if(isServer) {
return config
}
config.plugins.push(
new CopyWebpackPlugin([
{
from: path.join(__dirname, 'node_modules/workbox-sw/build/importScripts/workbox-sw.dev.v2.1.2.js'),
to: path.join(__dirname, 'static/scripts/workbox.js')
}
]))
return config
}
}
If you want to implement workbox try https://github.com/hanford/next-offline
There's not much that we can change in Next.js to fix this issue, so I'm going to close it.
@timneutkens I also use the plugin CopyWebpackPlugin and the files are still deleted when I run npm run dev, even if I add the condition.
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
config.plugins.push(
new CopyWebpackPlugin([
{
context: 'node_modules/widget-editor/dist/images/',
from: '**/*',
to: 'static/images/widget-editor/'
}
])
);
}
return config;
}
};
@clementprdhomme it's fixed in Next 5.1.
It still reproduce
Hey @Organizzzm,
I'm using Next 7.0.2 and it works for me with this code:
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
webpack: (config, { isServer }) => {
config.plugins.push(
new CopyWebpackPlugin([
{
from: path.join(__dirname, 'node_modules/widget-editor/dist/images'),
to: path.join(__dirname, 'static/images/widget-editor/')
}
])
);
return config;
}
};
Make sure you use path.join.
Most helpful comment
Hey @Organizzzm,
I'm using Next 7.0.2 and it works for me with this code:
Make sure you use
path.join.