From 48fa359fae9d83dd128071f8838885e4cb2aa616 Mon Sep 17 00:00:00 2001
From: Daniel Balogh <danielferencortel@gmail.com>
Date: Fri, 1 Sep 2023 19:08:26 +0300
Subject: [PATCH] Fix missing directory errors

---
 .gitignore   | 5 +++++
 readme.md    | 4 ++++
 src/index.js | 8 ++++++++
 3 files changed, 17 insertions(+)

diff --git a/.gitignore b/.gitignore
index 9c62828..83ec3db 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,8 @@
 node_modules
 coverage
 dist
+.idea
+.DS_Store
+.vscode
+yarn-debug.log*
+yarn-error.log*
\ No newline at end of file
diff --git a/readme.md b/readme.md
index 2ba3a8d..b5ecfde 100644
--- a/readme.md
+++ b/readme.md
@@ -39,6 +39,10 @@ export default {
 }
 ```
 
+**Note**: if you're using any kind of clean/delete plugins, such as [rollup-plugin-del](https://www.npmjs.com/package/rollup-plugin-del), 
+which remove any of the destination folders, ensure that this plugin is run **after** that plugin, otherwise the copied files might
+accidentally be deleted.
+
 ### Configuration
 
 There are some useful options:
diff --git a/src/index.js b/src/index.js
index 95ecc92..f63f875 100644
--- a/src/index.js
+++ b/src/index.js
@@ -112,8 +112,16 @@ export default function copy(options = {}) {
           const { contents, dest, src, transformed } = copyTarget
 
           if (transformed) {
+            if (!fs.existsSync(path.dirname(dest))) {
+              fs.mkdirSync(path.dirname(dest), { recursive: true })
+            }
+
             await fs.outputFile(dest, contents, restPluginOptions)
           } else {
+            if (!fs.existsSync(path.dirname(dest))) {
+              fs.mkdirSync(path.dirname(dest), { recursive: true })
+            }
+
             await fs.copy(src, dest, restPluginOptions)
           }