@@ -2,43 +2,34 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs'
2
2
import { resolve } from 'node:path'
3
3
import type { Plugin } from 'vite'
4
4
5
+ function getPkgPath ( pkgName : string , fileName ) {
6
+ return resolve ( __dirname , `../../../node_modules/@vue-flow/${ pkgName } /dist/${ fileName } ` )
7
+ }
8
+
9
+ function getPublicPath ( fileName : string ) {
10
+ return resolve ( __dirname , `../../public/${ fileName } ` )
11
+ }
12
+
5
13
function copyFiles ( emit : any ) {
6
- ; [
7
- { path : '../../../node_modules/@vue-flow/core/dist/' , pkgName : 'vue-flow-core.mjs' } ,
8
- {
9
- path : '../../../node_modules/@vue-flow/background/dist/' ,
10
- pkgName : 'vue-flow-background.mjs' ,
11
- } ,
12
- {
13
- path : '../../../node_modules/@vue-flow/controls/dist/' ,
14
- pkgName : 'vue-flow-controls.mjs' ,
15
- } ,
16
- {
17
- path : '../../../node_modules/@vue-flow/minimap/dist/' ,
18
- pkgName : 'vue-flow-minimap.mjs' ,
19
- } ,
20
- {
21
- path : '../../../node_modules/@vue-flow/node-resizer/dist/' ,
22
- pkgName : 'vue-flow-node-resizer.mjs' ,
23
- } ,
24
- {
25
- path : '../../../node_modules/@vue-flow/node-toolbar/dist/' ,
26
- pkgName : 'vue-flow-node-toolbar.mjs' ,
27
- } ,
28
- ] . forEach ( ( { path, pkgName } ) => {
29
- const filePath = resolve ( __dirname , `${ path } /${ pkgName } ` )
14
+ ; [ 'core' , 'background' , 'controls' , 'minimap' , 'node-resizer' , 'node-toolbar' ] . forEach ( ( name ) => {
15
+ const fileName = `vue-flow-${ name } .mjs`
16
+
17
+ const filePath = resolve ( __dirname , getPkgPath ( name , fileName ) )
18
+
19
+ console . log ( filePath )
20
+
30
21
if ( ! existsSync ( filePath ) ) {
31
- throw new Error ( `${ pkgName } not built. ` + `Run "pnpm -w build" first.` )
22
+ throw new Error ( `${ name } not built. ` + `Run "pnpm -w build" first.` )
32
23
}
33
24
34
25
emit ( {
35
26
type : 'asset' ,
36
- fileName : pkgName ,
27
+ fileName,
37
28
filePath,
38
29
source : readFileSync ( filePath , 'utf-8' ) ,
39
30
} )
40
31
41
- console . log ( `Copied ${ filePath } to ${ resolve ( __dirname , `../../public/ ${ pkgName } ` ) } ` )
32
+ console . log ( `Copied ${ filePath } to ${ getPublicPath ( fileName ) } ` )
42
33
} )
43
34
44
35
console . log ( 'Copied vue-flow files' )
@@ -49,13 +40,23 @@ export function copyVueFlowPlugin(): Plugin {
49
40
buildStart ( ) {
50
41
// use fs to copy files
51
42
copyFiles ( ( file : any ) => {
52
- writeFileSync ( resolve ( __dirname , `../../public/${ file . fileName } ` ) , file . source )
43
+ // remove existing files
44
+ if ( existsSync ( getPublicPath ( file . fileName ) ) ) {
45
+ writeFileSync ( getPublicPath ( file . fileName ) , '' )
46
+ }
47
+
48
+ writeFileSync ( getPublicPath ( file . fileName ) , file . source )
53
49
} )
54
50
} ,
55
51
watchChange ( ) {
56
52
// use fs to copy files
57
53
copyFiles ( ( file : any ) => {
58
- writeFileSync ( resolve ( __dirname , `../../public/${ file . fileName } ` ) , file . source )
54
+ // remove existing files
55
+ if ( existsSync ( getPublicPath ( file . fileName ) ) ) {
56
+ writeFileSync ( getPublicPath ( file . fileName ) , '' )
57
+ }
58
+
59
+ writeFileSync ( getPublicPath ( file . fileName ) , file . source )
59
60
} )
60
61
} ,
61
62
generateBundle ( ) {
0 commit comments